// JavaScript Document

/*
*	ajaxMethod is the class method called on the PHP side.
* 	data is the values parsed by the class->method();
*	callback is either string or function.  If string, function returnGeneric() is called.  And sring is treated as element ID
*	option allows for additional information to be parsed by the class->method();
*/
function ajaxInterface(u,r){
	var URL = u + "/ajaxInterface.php?";
	var URL_indiv = "/ajaxInterfaceIndiv.php?";
	var report = r;
	
	
	this.populateData = function(ajaxMethod,data,callback,option){
			xmlHttp = GetXmlHttpObject();
			callbackFunction = callback;
			if (xmlHttp==null){
				alert ("Browser does not support HTTP Request")
				return
			} 
			var url = URL;
			url=url+"rand="+Math.random()+"&method="+ajaxMethod+"&data="+data+"&report="+report+"&option="+option;
			xmlHttp.onreadystatechange = stateChanged;
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
		}
	
	/**
	 * Using individual ajax file classes
	 */
	this.populateDataIndiv = function(ajaxMethod,data,callback,option){
			xmlHttp = GetXmlHttpObject();
			callbackFunction = callback;
			if (xmlHttp==null){
				alert ("Browser does not support HTTP Request")
				return
			} 
			var url = URL_indiv;
			url=url+"rand="+Math.random()+"&method="+ajaxMethod+"&data="+data+"&report="+report+"&option="+option;
			xmlHttp.onreadystatechange = stateChanged;
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
		} 		
		
	function stateChanged() {
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
				if(typeof(callbackFunction) == "string"){
					returnGeneric(xmlHttp.responseText,callbackFunction )
				}else{
					callbackFunction(xmlHttp);
				}
				
			} 
	} 
	function GetXmlHttpObject(){ 
			var objXMLHttp = null;
			if (window.XMLHttpRequest){
				objXMLHttp = new XMLHttpRequest();
			}else if (window.ActiveXObject){
				objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		return objXMLHttp;
	}
	
	function returnGeneric(response,id){
		document.getElementById(id).innerHTML = response;
	}
}
	