/********************************************************************************************/
/* AHAH functions by Phil Ballard                                                           */
/* This code is intended for study purposes.                                                */
/* You may use these functions as you wish, for commercial or non-commercial applications,  */
/* but please note that the author offers no guarantees to their usefulness, suitability or */
/* correctness, and accepts no liability for any losses caused by their use.                */
/********************************************************************************************/

function callWau(url, pageElement, callMessage, errorMessage) {
     document.getElementById(pageElement).innerHTML = callMessage;
     try {
     req = new XMLHttpRequest(); /* e.g. Firefox */
     } catch(e) {
       try {
       req = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
       } catch (e) {
         try {
         req = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
         } catch (E) {
          req = false;
         } 
       } 
     }
     req.onreadystatechange = function() {responseWau(pageElement, errorMessage);};
     req.open("GET",url,true);
     req.send(null);
  }
function responseWau(pageElement, errorMessage) {
   var output = '';
   if(req.readyState == 4) {
      if(req.status == 200) {
         output = req.responseText;
         document.getElementById(pageElement).innerHTML = output;
         } else {
         document.getElementById(pageElement).innerHTML = errorMessage+"\n"+output;
         }
      }
  }
function callWau1(url, pageElement, callMessage, errorMessage) {
     document.getElementById(pageElement).innerHTML = callMessage;
     try {
     req = new XMLHttpRequest(); /* e.g. Firefox */
     } catch(e) {
       try {
       req = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
       } catch (e) {
         try {
         req = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
         } catch (E) {
          req = false;
         } 
       } 
     }
     req.onreadystatechange = function() {responseWau1(pageElement, errorMessage);};
     req.open("GET",url,true);
     req.send(null);
  }
function responseWau1(pageElement, errorMessage) {
   var output = '';
   if(req.readyState == 4) {
      if(req.status == 200) {
         output = req.responseText;
         document.getElementById(pageElement).innerHTML = output;
		 evalScripts(document.getElementById(pageElement));
         } else {
         document.getElementById(pageElement).innerHTML = errorMessage+"\n"+output;
		 evalScripts(document.getElementById(pageElement));
         }
      }
  }
function makeactive(tab) { 
	document.getElementById("tab_PM").className = "inactive"; 
	document.getElementById("tab_"+tab).className = "active";
	callWau('panel_main.php?page='+tab, 'main_content', '<div id="sub_loading"></div>', 'Error'); 
} 
function folderactive(tab, tol) { 
	document.getElementById("tab_Compose").className = "inactive"; 
	document.getElementById("tab_Incoming").className = "inactive"; 
	document.getElementById("tab_Sent").className = "inactive"; 
	document.getElementById("tab_Trash").className = "inactive";
	for ( i=0; i < tol; i++ ) {	document.getElementsByName('folder_In[]')[i].className = "inactive";}
	document.getElementById("folder_In"+tab).className = "active";
	callWau('inbox-msg.php?folder_id='+tab, 'sub_content', '<div id="sub_loading"></div>', 'Error'); 
} 
function inboxactive(tab, tol) { 
	document.getElementById("tab_Compose").className = "inactive"; 
	document.getElementById("tab_Incoming").className = "inactive"; 
	document.getElementById("tab_Sent").className = "inactive"; 
	document.getElementById("tab_Trash").className = "inactive"; 
	for ( j=0; j < tol; j++ ) {	document.getElementsByName('folder_In[]')[j].className = "inactive";}
	document.getElementById("tab_"+tab).className = "active";
	callWau('panel_main.php?page='+tab, 'sub_content', '<div id="sub_loading"></div>', 'Error'); 
} 
function allinactive(tab, tol) { 
	document.getElementById("tab_Compose").className = "inactive"; 
	document.getElementById("tab_Incoming").className = "inactive"; 
	document.getElementById("tab_Sent").className = "inactive"; 
	document.getElementById("tab_Trash").className = "inactive"; 
	for ( j=0; j < tol; j++ ) {	document.getElementsByName('folder_In[]')[j].className = "inactive";}
	callWau('panel_main.php?page='+tab, 'sub_content', '<div id="sub_loading"></div>', 'Error'); 
} 
function inboxview(message_id, style, folder_id) { 
	callWau('inbox-view.php?message_id='+message_id+'&style='+style+'&folder_id='+folder_id, 'sub_content', '<div id="sub_loading"></div>', 'Error');
} 
function sendreply(msg_senderid, msg_title) { 
	callWau('inbox-compose.php?msg_senderid='+msg_senderid+'&msg_title='+msg_title, 'sub_content', '<div id="sub_loading"></div>', 'Error'); 
} 
function sendmail(msg_senderid) { 
	callWau('mail-compose.php?msg_senderid='+msg_senderid, 'sub_content', '<div id="sub_loading"></div>', 'Error'); 
} 
function confirmDelete(sbt_value, folder_id) { 
	var yesto= confirm('Are you sure you want to delete this folder and the messages within it?');
	if (yesto== true) {
	makePOSTRequest('inbox-folder.php', 'sbt_value='+sbt_value+'&folder_id='+folder_id, 'sub_content'); 
	callWau1('inbox-right-menu-ajax.php', 'right_side_menu_panel', '<div id="sub_loading"></div>', 'Error'); 
	}
	else { return false;}
} 
function getRefresh(url, sub_url) { 
	callWau(url, 'main_content', '<div id="sub_loading"></div>', 'Error');
	callWau1(sub_url, 'right_side_menu_panel', '<div id="sub_loading"></div>', 'Error'); 
} 
function validate_username(check_id, check_value, url, update_element) { 
	callWau(url+'?check_id='+check_id+'&check_value='+check_value, update_element , '', 'Error');
} 
var http_request = false;
   function makePOSTRequest(url, parameters, update_page) {
      document.getElementById(update_page).innerHTML = 'Loading...';
     http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
	  http_request.onreadystatechange = function() {alertContents(update_page);};
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents(update_page) {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
			if(result.indexOf("Opps") == 1)
 				{
					document.getElementById("txtSubject").value='';
 					document.getElementById("txtMessage").value='';
					document.getElementById("userid").value='';
				}
			document.getElementById(update_page).innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
    
	function evalScripts(el) {
		var scripts = el.getElementsByTagName("script");
		var i = 0;
		var end = scripts.length;
		
		for (i; i < end; i++) {
			eval( scripts[i].innerHTML );
		}
		
		scripts = null;
	}

   function getPost(subvl, url) {
	
	var getstr = "";
	getstr += "folder=" + encodeURI( document.getElementById("folder").value ) + "&";

	for (i=0; i<document.getElementsByTagName("input").length; i++) {
       if (document.getElementsByTagName("input")[i].type == "checkbox") {
          if (document.getElementsByTagName("input")[i].checked) {
              getstr += document.getElementsByTagName("input")[i].name + "=" + 
                 document.getElementsByTagName("input")[i].value + "&";
           }
        }
       if (document.getElementsByTagName("input")[i].type == "button") {
          if (document.getElementsByTagName("input")[i].id == subvl) {
              getstr += "sbt_value=" + 
                 document.getElementsByTagName("input")[i].id + "&";
           }
        }
       if (document.getElementsByTagName("input")[i].type == "hidden") {
              getstr += document.getElementsByTagName("input")[i].id + "=" + 
                 document.getElementsByTagName("input")[i].value + "&";
        }
  }
	  
      makePOSTRequest(url, getstr, 'sub_content');
	  callWau('panel_left.php?page=inbox', 'right_panel', '<div id="sub_loading"></div>', 'Error'); 

   }

   function getSent(subvl, url, update_element) {
	
	var getstr = "";

	for (i=0; i<document.getElementsByTagName("input").length; i++) {
        if (document.getElementsByTagName("input")[i].type == "text") {
           getstr += document.getElementsByTagName("input")[i].name + "=" + 
                   document.getElementsByTagName("input")[i].value + "&";
        }
       if (document.getElementsByTagName("input")[i].type == "checkbox") {
          if (document.getElementsByTagName("input")[i].checked) {
              getstr += document.getElementsByTagName("input")[i].name + "=" + 
                 document.getElementsByTagName("input")[i].value + "&";
           }
        }
       if (document.getElementsByTagName("input")[i].type == "button") {
          if (document.getElementsByTagName("input")[i].id == subvl) {
              getstr += "sbt_value=" + 
                 document.getElementsByTagName("input")[i].id + "&";
           }
        }
       if (document.getElementsByTagName("input")[i].type == "hidden") {
              getstr += document.getElementsByTagName("input")[i].id + "=" + 
                 document.getElementsByTagName("input")[i].value + "&";
        }
       if (document.getElementsByTagName("input")[i].type == "password") {
              getstr += document.getElementsByTagName("input")[i].id + "=" + 
                 document.getElementsByTagName("input")[i].value + "&";
        }
  }
	  
      makePOSTRequest(url, getstr, update_element);
   }

   function getCompose(obj) {
	
	var getstr = "";

	for (i=0; i<obj.getElementsByTagName("input").length; i++) {
        if (obj.getElementsByTagName("input")[i].type == "text") {
           getstr += obj.getElementsByTagName("input")[i].name + "=" + 
                   obj.getElementsByTagName("input")[i].value + "&";
        }
       if (obj.getElementsByTagName("input")[i].type == "checkbox") {
          if (obj.getElementsByTagName("input")[i].checked) {
              getstr += obj.getElementsByTagName("input")[i].name + "=" + 
                 obj.getElementsByTagName("input")[i].value + "&";
           }
        }
       if (obj.getElementsByTagName("input")[i].type == "hidden") {
              getstr += obj.getElementsByTagName("input")[i].name + "=" + 
                 obj.getElementsByTagName("input")[i].value + "&";
        }
  	}
	  
	  getstr += "userid=" + encodeURI( document.getElementById("userid").value ) + "&";
	  
	  getstr += "txtMessage=" + document.getElementById("txtMessage").value + "&";
      makePOSTRequest('inbox-compose-sent.php', getstr, 'comp_msg');
   }
   function getFolder(subvl, url) {
	
	var getstr = "";

	for (i=0; i<document.getElementsByTagName("input").length; i++) {
        if (document.getElementsByTagName("input")[i].type == "text") {
           getstr += document.getElementsByTagName("input")[i].name + "=" + 
                   document.getElementsByTagName("input")[i].value + "&";
        }
       if (document.getElementsByTagName("input")[i].type == "checkbox") {
          if (document.getElementsByTagName("input")[i].checked) {
              getstr += document.getElementsByTagName("input")[i].name + "=" + 
                 document.getElementsByTagName("input")[i].value + "&";
           }
        }
       if (document.getElementsByTagName("input")[i].type == "button") {
          if (document.getElementsByTagName("input")[i].id == subvl) {
              getstr += "sbt_value=" + 
                 document.getElementsByTagName("input")[i].id + "&";
           }
        }
       if (document.getElementsByTagName("input")[i].type == "hidden") {
              getstr += document.getElementsByTagName("input")[i].id + "=" + 
                 document.getElementsByTagName("input")[i].value + "&";
        }
  }
	  
      makePOSTRequest(url, getstr, 'sub_content');
	  callWau('inbox-right-menu-ajax.php', 'right_side_menu_panel', '<div id="sub_loading"></div>', 'Error'); 
   }
	function getInvitation(obj, sbt_value, page) { 
	var getstr = "";
 	for (i=0; i<obj.getElementsByTagName("input").length; i++) {
        if (obj.getElementsByTagName("input")[i].type == "hidden") {
           getstr += "&" + obj.getElementsByTagName("input")[i].name + "=" + 
                   obj.getElementsByTagName("input")[i].value;
        }
	}
	makePOSTRequest('submit-alert.php', 'sbt_value='+sbt_value+getstr, page);
	callWau1('panel_left.php?page=inbox', 'right_panel', '<div id="sub_loading"></div>', 'Error'); 
} 
