﻿var xhReq = null;

var bustcachevar=1; //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects="";
var rootdomain="http://"+window.location.hostname;
var bustcacheparameter="";

function ajaxLoadPage(url, containerid)
{
    xhReq = createXMLHttpRequest();
    
    if (xhReq == null)
    {
        document.getElementById(containerid).innerHTML = "Cannot load page.";
        return;
    }

    xhReq.onreadystatechange=function(){
     if (xhReq.readyState != 4) { return; }
     if (xhReq.status != 200)  {
       OnRequestStateChange("FAILURE", "An error occured , please try again.");
       return;
     }

        loadPageData(xhReq, containerid);
        OnRequestStateChange("SUCCESS", xhReq.responseText);
    }
    
    if (bustcachevar) //if bust caching of external page
    
    bustcacheparameter=(url.indexOf("?")!=-1)? "&" + new Date().getTime() : "?" + new Date().getTime();
    
    xhReq.open('GET', url + bustcacheparameter, true);
    xhReq.send(null);
}

function loadPageData(xhReq, containerid)
{
    if (xhReq.readyState == 4 && (xhReq.status==200 || window.location.href.indexOf("http")==-1))
        document.getElementById(containerid).innerHTML=xhReq.responseText
}

function loadobjs()
{
    if (!document.getElementById)
        return
    for (i=0; i<arguments.length; i++)
    {
        var file=arguments[i];
        var fileref="";
        if (loadedobjects.indexOf(file)==-1)
        { //Check to see if this object has not already been added to page before proceeding
            if (file.indexOf(".js")!=-1)
            { //If object is a js file
                fileref=document.createElement('script');
                fileref.setAttribute("type","text/javascript");
                fileref.setAttribute("src", file);
            }
            else if (file.indexOf(".css")!=-1)
            { //If object is a css file
                fileref=document.createElement("link");
                fileref.setAttribute("rel", "stylesheet");
                fileref.setAttribute("type", "text/css");
                fileref.setAttribute("href", file);
            }
        }
        if (fileref!="")
        {
            document.getElementsByTagName("head").item(0).appendChild(fileref);
            loadedobjects+=file+" "; //Remember this object as being already added to page
        }
    }
}

function createXMLHttpRequest() 
{
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
    {
        try { return new XMLHttpRequest(); } catch(e) {}
    }
    else if (window.ActiveXObject)
    { // if IE
       var progids = ["Msxml2.XMLHTTP.5.0", 
                       "Msxml2.XMLHTTP.4.0", 
                       "MSXML2.XMLHTTP.3.0", 
                       "MSXML2.XMLHTTP", 
                       "Microsoft.XMLHTTP"];
        var o;
        for(var i = 0; i < progids.length; i++)
        {
            try
            {
                o = new ActiveXObject(progids[i]);
                return o;
            }
            catch (ex) {};
        }
    }
    
    return null;
 }

// To use this the calling page must implement a function OnRequestStateChange(Status, ResponseText); Status will be FAILURE or SUCCESS; ResponseText is either an error message or the actual text received from the URL;
function sendGetRequest(sURL)
{
 
    xhReq = createXMLHttpRequest();
    
    if (xhReq == null)
    {
        window.location.href = sURL;
        return;
    }
    
    sURL+=(sURL.indexOf("?")!=-1)? "&ajax=1" : "?ajax=1";
    xhReq.open("get", sURL, true);              // Server stuck in a loop.
    
//    var requestTimer = setTimeout(function() 
//    {
//       xhReq.abort();
//       // Handle timeout situation, e.g. Retry or inform user.
//     }, MAXIMUM_WAITING_TIME); 
   
   xhReq.onreadystatechange = function() {
     if (xhReq.readyState != 4)  { return; }
     //clearTimeout(requestTimer);
     if (xhReq.status != 200)  {
       OnRequestStateChange("FAILURE", "An error occured , please try again.");
       return;
     }
     OnRequestStateChange("SUCCESS", xhReq.responseText);
   };
}

// Post Data is sPostData = "field1=1&field2=2"
function sendPostRequest(sURL, sPostData)
{
    xhReq = createXMLHttpRequest();
    
    if (xhReq == null)
    {
        window.location.href = sURL + (sPostData != "" ? "?" + sPostData : "");
        return;
    }
   xhReq.open("post", sURL, true);
   xhReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');     //set that the request is a post
   
   xhReq.onreadystatechange = function() {
     if (xhReq.readyState != 4) { return; }
     if (xhReq.status != 200)  {
       OnRequestStateChange("FAILURE", "An error occured , please try again.");
       return;
     }
     OnRequestStateChange("SUCCESS", xhReq.responseText);
   };
   xhReq.send(sPostData + "&ajax=1");
}
