﻿function createXMLHttp() {
    if (typeof XMLHttpRequest != "undefined") {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      var aVersions = [ "MSXML2.XMLHttp.5.0",
        "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
        "MSXML2.XMLHttp","Microsoft.XMLHttp"
      ];

      for (var i = 0; i < aVersions.length; i++) {
        try {
            var oXmlHttp = new ActiveXObject(aVersions[i]);
            return oXmlHttp;
        } catch (oError) {/*Do nothing*/}
      }
    }
    throw new Error("XMLHttp object could be created.");
}

function GetContent(elementId, url, append)
{
    
    
    var oXmlHttp = createXMLHttp(); //Create a XMLHttp object
    
    oXmlHttp.onreadystatechange = function () //do this when the status changes
    {
        if (oXmlHttp.readyState == 4) //response is complete
        {
            if (append==false) document.getElementById(elementId).innerHTML ="";
            if (oXmlHttp.status == 200) //response status is success
            {
                document.getElementById(elementId).innerHTML +=oXmlHttp.responseText;
                alert(oXmlHttp.responseText);
            }
            else 
                document.getElementById(elementId).innerHTML += '<b>Error:</b>'+oXmlHttp.status+' ['+oXmlHttp.statusText+']<br /><b>Url:</b>'+url;
        }
    };
    
    oXmlHttp.open("GET", url, true); // open(HTTP_REQUEST_TYPE, URL, ASYNC_REQUEST)
    oXmlHttp.send(null);    
}

//Populate an element with the response from a URL (must be within same domain - due to access restrictions)
function Run(url)
{
    var oXmlHttp = createXMLHttp(); //Create a XMLHttp object
    
    oXmlHttp.onreadystatechange = function () //do this when the status changes
    {
        if (oXmlHttp.readyState == 4) //response is complete
        {
            if (oXmlHttp.status == 200) //response status is success
                eval(oXmlHttp.responseText);
            else 
                alert('<b>Error:</b>'+oXmlHttp.status+' ['+oXmlHttp.statusText+']<br /><b>Url:</b>'+url);
        }
    };
    
    oXmlHttp.open("GET", url, true); // open(HTTP_REQUEST_TYPE, URL, ASYNC_REQUEST)
    oXmlHttp.send(null);    
}




function PostContent(elementId, url, params, append)
{
    
    document.getElementById(elementId).innerHTML  = "Searching ...";
    
    try
    {

    var oXmlHttp = createXMLHttp(); //Create a XMLHttp object
      
    oXmlHttp.onreadystatechange = function () //do this when the status changes
    {
        if (oXmlHttp.readyState == 4) //response is complete
        {
            if (append==false) document.getElementById(elementId).innerHTML ="";
            if (oXmlHttp.status == 200) //response status is success
            {
                
                document.getElementById(elementId).innerHTML +=oXmlHttp.responseText;
            }   
            else 
                document.getElementById(elementId).innerHTML += '<b>Error:</b>'+oXmlHttp.status+' ['+oXmlHttp.statusText+']<br /><b>Url:</b>'+url;
        }
    };
    
    var time = Date();
    oXmlHttp.open("POST", url+'?time='+time, true); // open(HTTP_REQUEST_TYPE, URL, ASYNC_REQUEST)
    oXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    oXmlHttp.setRequestHeader("Content-length", params.length);
    oXmlHttp.setRequestHeader("Connection", "close")

    oXmlHttp.send(params);     
    }
    catch(oError){}
}
