//
//
//      Basic AJAX Framework
//      Andrew Crowe
//
//


// Make HTTP call, passing ParameterArray to TemplateAddress and replacing the content div AreaID
function jaxDisplayAction(AreaID, TemplateAddress, ParameterArray){

    QueryString="?"; // TODO: Investigate use of POST instead of GET
    for(Parameter in ParameterArray){
        QueryString+= escape(Parameter)+"="+escape(ParameterArray[Parameter])+"&";
    }
    QueryString+="__nocache="+GetUniqueID(); // Make sure results aren't cachable

    xmlHttp=GetXmlHttpObject();
    xmlHttp.open("GET", TemplateAddress+QueryString, false);
    xmlHttp.send(null);
    
    AreaObject=document.getElementById(AreaID);
    if(AreaObject){
        AreaObject.innerHTML=xmlHttp.responseText;
        return true;
    }
    return false;
}

// Call TemplateAddress with no parameters and replacing the content div AreaID
function jaxDisplay(AreaID, TemplateAddress){
    AreaObject=document.getElementById(AreaID);
    if(AreaObject){
    
        QueryString="?__nocache="+GetUniqueID(); // Make sure results aren't cachable
    
        xmlHttp=GetXmlHttpObject();
        xmlHttp.open("GET", TemplateAddress+QueryString, false);
        xmlHttp.send(null);
        
        AreaObject.innerHTML=xmlHttp.responseText;
        return true;
    }
    return false;
}


// Call TemplateAddress with parameters but do not display anything, instead returning the result
function jaxAction(TemplateAddress, ParameterArray, SchoolArray, CmdType){

   QueryString="?"; // TODO: Investigate use of POST instead of GET
    for(Parameter in ParameterArray){
        QueryString+= escape(Parameter)+"="+escape(ParameterArray[Parameter])+"&";
    }
    QueryString+= escape("SchoolCode")+"="+escape(SchoolArray["schoolcode"])+"&";
    QueryString+= escape("type")+"="+escape(CmdType)+"&";
    QueryString+="__nocache="+GetUniqueID(); // Make sure results aren't cachable
//alert( TemplateAddress+QueryString);

    xmlHttp=GetXmlHttpObject();
    xmlHttp.open("GET", TemplateAddress+QueryString, false);
    xmlHttp.send(null);
    
    //alert(xmlHttp.getAllResponseHeaders());

    return xmlHttp.responseText;

}

function getItems(TemplateAddress, ParameterArray, SchoolArray, ItemType){

	QueryString="?"; // TODO: Investigate use of POST instead of GET
    for(Parameter in ParameterArray){
        QueryString+= escape(Parameter)+"="+escape(ParameterArray[Parameter])+"&";
    }
    QueryString+= escape("SchoolCode")+"="+escape(SchoolArray["schoolcode"])+"&";
    QueryString+= escape("type")+"="+escape("GetItems")+"&";
    QueryString+= escape("itemtype")+"="+escape(ItemType)+"&";
    QueryString+="__nocache="+GetUniqueID(); // Make sure results aren't cachable
//alert( TemplateAddress+QueryString);
    xmlHttp=GetXmlHttpObject();
    xmlHttp.open("GET", TemplateAddress+QueryString, false);
    xmlHttp.send(null);
    
    //alert(xmlHttp.getAllResponseHeaders());

    return xmlHttp.responseText;
    
}

function getAccomItems(TemplateAddress, ParameterArray, SchoolArray, ItemType){

	QueryString="?"; // TODO: Investigate use of POST instead of GET
    for(Parameter in ParameterArray){
        QueryString+= escape(Parameter)+"="+escape(ParameterArray[Parameter])+"&";
    }
    QueryString+= escape("SchoolCode")+"="+escape(SchoolArray["schoolcode"])+"&";
    QueryString+= escape("type")+"="+escape("GetAccomItems")+"&";
    QueryString+= escape("itemtype")+"="+escape(ItemType)+"&";
    QueryString+="__nocache="+GetUniqueID(); // Make sure results aren't cachable
//alert( TemplateAddress+QueryString);
    xmlHttp=GetXmlHttpObject();
    xmlHttp.open("GET", TemplateAddress+QueryString, false);
    xmlHttp.send(null);
    
    //alert(xmlHttp.getAllResponseHeaders());

    return xmlHttp.responseText;
    
}

function getItemCodes(TemplateAddress, ParameterArray, SchoolArray){

	QueryString="?"; // TODO: Investigate use of POST instead of GET
    for(Parameter in ParameterArray){
        QueryString+= escape(Parameter)+"="+escape(ParameterArray[Parameter])+"&";
    }
    QueryString+= escape("SchoolCode")+"="+escape(SchoolArray["schoolcode"])+"&";
    QueryString+= escape("type")+"="+escape("GetItemCodes")+"&";
    QueryString+="__nocache="+GetUniqueID(); // Make sure results aren't cachable

    xmlHttp=GetXmlHttpObject();
    xmlHttp.open("GET", TemplateAddress+QueryString, false);
    xmlHttp.send(null);
    
    //alert(xmlHttp.getAllResponseHeaders());

    return xmlHttp.responseText;
    
}

function getDescription(TemplateAddress, ItemCode){

	QueryString="?"; // TODO: Investigate use of POST instead of GET
    for(Parameter in ParameterArray){
        QueryString+= escape("itemcode")+"="+escape(ItemCode)+"&";
    }    
    QueryString+="__nocache="+GetUniqueID(); // Make sure results aren't cachable
    xmlHttp=GetXmlHttpObject();
    xmlHttp.open("GET", TemplateAddress+QueryString, false);
    xmlHttp.send(null);    
    //alert(xmlHttp.getAllResponseHeaders());
    return xmlHttp.responseText;
}


// Create correct XMLHttp object for user's browser 
function GetXmlHttpObject()
{ 
    var XmlHttpObject=null
    if (window.XMLHttpRequest)
    {
        XmlHttpObject=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        XmlHttpObject=new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    if(!XmlHttpObject){
        alert("Your browser is not compatible with this website, please use either IE6 or FireFox");
    }
    
    return XmlHttpObject;
} 

// Get Unique random ID to prevent caching (time in ms + Math.random)
function GetUniqueID(){
	var datetime = new Date();
	return datetime.getTime() + "." + parseInt(Math.random()*10000);
}