/*
 This set of AJAX functions tries to help shorten code.
  instead of always retyping the code in the AJAX() function when using AJAX, this set simplifies things by just defining the AJAX() function.

 You simply would define a tUrl & call AJAX(tUrl);
  or AJAX(tUrl,myFunction); 
  
  (c) 2007 (MMVII) Bill Frederickson, nerdz4.net
  Released under GPL Licence.
  IE is either ActiveXObject("Microsoft.XMLHTTP"); or ActiveXObject("Msxml2.XMLHTTP");
*/
var xmlHttp = null;
function GetXmlHttpObject(handler){ // Main AJAX Handler - get's Request.
 var objXMLHttp = null;
 objXMLHttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
 return objXMLHttp; 
}
//================================================================================================
function AJAX(url,funct){
 var outp = "";
 if ((window.XMLHttpRequest || window.ActiveXObject) && url) {
  xmlHttp = GetXmlHttpObject();  //Ret.indexOf("check18=") >=0
  if (funct)  xmlHttp.onreadystatechange = eval(funct); // calls sub function if there is one.  
  // xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // for "Post" ??
  xmlHttp.open("POST",url,true);
  xmlHttp.send(null);
  /*
   -- other methods:
   abort()
   getAllResponseHeaders()
   getResponseHeader("headerLabel")
   open("method", "URL"[, asyncFlag[, "userName"[, "password"]]])
   
   -- Properties
   responseText
   readyState
   responseText
   responseXML
   status
   statusText   
  */
 }	
}
//================================================================================================
 function rAJAX(){
  var ajXpr = /(4)|(complete)/ig;
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
   return (xmlHttp.responseText) ? xmlHttp.responseText : xmlHttp.responseXML;
  }
 }
 //================================================================================================
 function xAJAX(){
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
   return xmlHttp.readyState;
  }
 }
//================================================================================================


function getElement(iVal){
 return (document.all) ? document.all[iVal] : document.getElementById(iVal);
} // end function

function ShowError(iVal){ //Error
  var nObj = (mode == "pword") ? "pwError" : "Error";
  var tObj = (document.all) ? document.all[nObj].style : document.getElementById(nObj).style;
  tObj.display = ( tObj.display == "none") ? "block" : "none"; // Alert/confirm dosen't lose focus.
  var fcs = "signupBtn"+nObj;
  if (tObj.display == "block") document.forms[0].elements[fcs].focus();
  if (iVal == "y" && tryCnt <=3) {
   document.forms[0].elements['username'].value = "";
   document.forms[0].elements['username'].focus();
   tryCnt++;   
  }
  if (tryCnt >= 4) {
    document.forms[0].elements['username'].disabled = true;
	document.forms[0].elements['password'].disabled = true;
	document.forms[0].elements['Submit'].disabled = true;
  }
}

function showHint(str,inObj){  
  var tUrl = "";
  if ((window.XMLHttpRequest || window.ActiveXObject)){     
	 if (str) {
      var tObj = document.getElementById("CompanyList").style;
      tObj.display = "block";
	  tUrl = (str) ? "companies.php?q="+str+"&sid="+Math.random() : "";
      xmlHttp = GetXmlHttpObject();	
	  iObj = inObj;
      xmlHttp.onreadystatechange=stateChanged;
      xmlHttp.open("GET",tUrl,true);
      xmlHttp.send(null);
	 } // end if str
  } // end if
} // end function

function Lookup(str,lType){   
  if (!lType) lType = "em";
  mode = (lType == "pw") ? "pword" : "email";
  var tUrl = "";  
  if ((window.XMLHttpRequest || window.ActiveXObject)){     
	 if (str) {
	  tUrl = (str) ? "userlookup.php?"+lType+"="+str+"&sid="+Math.random() : "";
      xmlHttp = GetXmlHttpObject();	
      xmlHttp.onreadystatechange = stateChanged;
      xmlHttp.open("GET",tUrl,true);
      xmlHttp.send(null);
	 } // end if str
  } // end if
} // end function

function AddToOptionList(OptionList, OptionValue, OptionText) {
   // Add option to the bottom of the list
   var Xpr =/\[disabled\]/i;
   var newOpt =  "";
   var OText = OptionText.replace(Xpr,"");
   var OValue = OptionText.replace(Xpr,"");
   var oList = document.forms[0].elements[OptionList];
   oList[oList.length] = new Option(OText, OValue);
   
   if (Xpr.test(OptionValue) ) {
     var num = oList.length-1;	 
	 oList[num].disabled = true;
	 oList[num].style.color = "#808080";	  
	 oList[num].style.backgroundColor = "#d3d3d3";	  
   } // end if    
} // end function

function ClearOptions(OptionList) {
   // Always clear an option list from the last entry to the first
   var oList = document.forms[0].elements[OptionList];
   for (x = oList.length; x >= 1; x--) oList[x] = null;
}

function addoption(){
 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
  ClearOptions('endusercounty');
  var output = xmlHttp.responseText;
  var cList = output.split(";");
  var tStr="";
  for (i=0;i<=cList.length;i++){
	if (cList[i]) AddToOptionList('endusercounty', cList[i], cList[i]);
  }
 }
} // end function

function showCounties(iVal,uid,company){
  var Xpr=/object/i;
  if ((window.XMLHttpRequest || window.ActiveXObject)){
    var state = (Xpr.test(iVal)) ? iVal.options[iVal.selectedIndex].value : iVal;
	if (state){
     tUrl = (state) ? "counties.php?state="+state+"&sid="+Math.random()+"&uid="+uid+"&company="+company : "";
     xmlHttp = GetXmlHttpObject();	
     xmlHttp.onreadystatechange = addoption;//stateChanged;
     xmlHttp.open("GET",tUrl,true);
     xmlHttp.send(null);
	 getElement("EndUserCounty").style.display = "block";
	}
	else {
	 ClearOptions('endusercounty');
	 getElement("EndUserCounty").style.display = "none";
	}
  }
} // end function

/*

//Sample usage:
function somefunction(){
 tUrl = VIRTUALBASE+"/cgi-bin/getfiles.cgi?data="+data+"&docType="+docType+"&mode=list&user="+user+"&sid="+Math.random();
 AJAX(tUrl,NewFunction);
}

function NewFunction(){
 somevar = rAJAX();
 // do things..
} 
	
*/