﻿var xmlHttp, elementId, responseText;

function getHTML(elementId, url)
{    
    //Check if requesting browser support AJAX call or not
    if (window.XMLHttpRequest)
    {
        //For IE7, Firefox
        xmlHttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        //For IE5 and IE6
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    //IF browser does not support AJAX call
    if (xmlHttp==null)
    {
        alert ("Your browser does not support HTTP request");
        return;
    }

    //Assign target id to the global target id
    setToElementId = elementId;
    
    //Check state of xmlHttp object
    xmlHttp.onreadystatechange = stateChanged;
    
    xmlHttp.open("GET", url, true);
    xmlHttp.setRequestHeader("Content-Type","text/html; charset=utf-8");
    xmlHttp.setRequestHeader('charset','utf-8');

    xmlHttp.send(null);    
}

function stateChanged()
{
    /*
    //State codes:
    0 Uninitialized - open() has not been called yet. 
    1 Loading - send() has not been called yet. 
    2 Loaded - send() has been called, headers and status are available. 
    3 Interactive - Downloading, responseText holds the partial data.  
    4 Completed - Finished with all operations 
    */

    if (xmlHttp.readyState == 4)
    {
        /*
        404 = file not found
        200 = file found and loaded
        304 = file found, but determined unchanged and loaded from cache
        */
        if (xmlHttp.status == 200)
        {
            //Get response from page
            responseText = xmlHttp.responseText;
            
            setHTML();        
        }
        else
        {
            alert(xmlHttp.status + ": Problem retrieving response from request data");
        }
    }
}

function setHTML()
{
    //Set response text to the element
    document.getElementById(setToElementId).innerHTML = responseText;
}
