// JavaScript Document
/**************************************************************************************************
	AJAX Processor by Shane Kretzmann @ www.WalkerSystemsSupport.com
	
	startAJAX_Get(delay,where,defaultState,string1,string2,string3,...)
	startAJAX_Post(delay,str,where,defaultState)

  delay = number of milliseconds to delay sending the request (saving server requests) (default 500 milliseconds)
	where = Name of the Div Tag - must match the switch($id) in phpProcessor
	defaultState = What to display in div container when the field being processed is blank
	string{X} = What to evaluate (ie: what is in the current form field).  You can use this to send
	             other variables to the phpProcessor also.  They are sent as q, q2, q3, etc.

**************************************************************************************************/
// Configure variables as needed to match environment




var phpProcessor = 'include/ajax/processor.php' // location of backend php script used to process requests
var imageLocation = 'include/ajax/eclipse_loader.gif' // location of ajax preloader graphic




/******************************** DO NOT EDIT BELOW THIS LINE ************************************/
var http // Ajax constructor
function GetXmlHttpObject() { // Determine what type of XMLHttp request is required and start it up
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
} 




function startAJAX_Post() {
  a = arguments;
  delay = a[0];
  if (delay < 1 || !IsNumeric(delay)) { delay = 500; } 
    
  if(window.myPosttimeout) window.clearTimeout(window.myPosttimeout);
  window.myPosttimeout = window.setTimeout("postAJAX(a)", delay);
  return true;
}

// Passes the AJAX request via POST to phpProcessor
function postAJAX() {
  // we'll ignore [0][0] as that is the delay time already handled in startAJAX_Get()
	var where = arguments[0][1] // sent along as id=
	var defaultState = arguments[0][2] // sent along as defaultState=
	var str = arguments[0][3] // sent along as q=
	// any additional arguments will be sent along as q2= q3= etc

	str = stripTags(str);

	if (str.length==0) { 
		document.getElementById(where).innerHTML=defaultState
		return
	}
	http=GetXmlHttpObject()
	if (http==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	
	var url=phpProcessor+"?sid="+Math.random()
	var params= "id="+where+"&defaultState="+defaultState+"&q="+str
	total = arguments.length;
	for (x=3;x<=total;x++) {
		num = x-1;
		params+="&q"+num+"="+arguments[x]
	}
	http.open("POST",url,true)
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	http.onreadystatechange= function() {
		if (http.readyState==4 || http.readyState=="complete") { 
			document.getElementById(where).innerHTML=http.responseText 
		} 
	}
	http.send(params)
} 


// The following code allows for AJAX requests via GET method to the phpProcessor 

function startAJAX_Get() {
  a = arguments;
  delay = a[0];
  if (delay < 1 || !IsNumeric(delay)) { delay = 500; } 
    
  if(window.myGettimeout) window.clearTimeout(window.myGettimeout);
  window.myGettimeout = window.setTimeout("getAJAX(a)", delay);
  return true;
}

// Passes the AJAX request via GET to phpProcessor
function getAJAX() {

    //alert("DEBUG: Get request fired");
   // we'll ignore [0][0] as that is the delay time already handled in startAJAX_Get()
    
  	var where = arguments[0][1] // sent along as id=
  	var defaultState = arguments[0][2] // sent along as defaultState=
  	var str = arguments[0][3] // sent along as q=

      loaderImage = defaultState+' <img border="0" src="'+imageLocation+'" />';
      document.getElementById(where).innerHTML=loaderImage;

  	// any additional arguments will be sent along as q2= q3= etc
  
  	str = stripTags(str);
  	if (str.length==0) { 
  		document.getElementById(where).innerHTML=defaultState
  		return
  	}
  	http=GetXmlHttpObject()
  	if (http==null)	{
  		alert ("Browser does not support HTTP AJAX Requests.  Please upgrade your browser to fully experience this website.")
  		return
  	} 
  	var url=phpProcessor+"?id="+where
  	url=url+"&defaultState="+defaultState
  	url=url+"&q="+str
  	total = arguments.length;
  	for (x=4;x<=total;x++) {
  		num = x-1;
  		url+="&q"+num+"="+arguments[0][x]
  	}
  	
  	url=url+"&sid="+Math.random()
  	http.open("GET",url,true)
  	http.onreadystatechange= function() {
  		if (http.readyState==4 || http.readyState=="complete") { 
  			document.getElementById(where).innerHTML=http.responseText 
  		} 
  	}
  	http.send(null)
} 
// This will protect the AJAX from script injections removing anything found between < and >
function stripTags(oldString) {
  return oldString.replace(/<&#91;^>&#93;*>/g, "");
}

// Check if data is numeric
function IsNumeric(sText) {
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
