﻿//Scott Smith

//AJAX Functionality (XmlRequestObject)


//[-------Global Variables---------------]

//xml request object
var xmlhttp;

//[-------HTTPCallBack---------------]

//This function initiates a request to the server. The server uses the querystrings to act upon the commands.

function HTTPCallBack(URL) {

    xmlhttp = null;

    //Check which browser the user is using.
    if (window.XMLHttpRequest) {

        //All browsers other than IE.
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {

        //Internet Exploder
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    //Check if the xml request object has been set to an instance of the xmlHttpRequest.
    //If it hasnt then the broswer used does not support it.
    if (xmlhttp != null) {

        //Set the onreadystatechange event to the httpcallbackresponse. This will be triggered as
        //soon as the server responds to the request.            
        xmlhttp.onreadystatechange = HTTPCallBackResponse;

        //Replace any html special characters with the correct character. Querystring params are set in text as
        // &amp; so that it passes xhtml.
        URL = URL.replace(/&amp;/g, '&');

        //Open the request as a get.
        xmlhttp.open("GET", URL, true);

        //Send the request to the server.
        xmlhttp.send(null);

    }
    else {
        //XMLHttpRequest object not supported in the used browser.
        alert("XMLHttpRequest Object unavailable. Please upgrade your broswer.");
    }

}

//[-------HTTPCallBack---------------]



//[-------HTTPCallBackResponse---------------]

//This function is called once the server responds to the request.

function HTTPCallBackResponse() {

    //First a check is made on the ready state property. If the state is = 4 then we know the request object
    //has loaded ok.
    if (xmlhttp.readyState == 4) {

        //The next check is to check the status code from the server. If we recieve a status code of 200
        //we know everything is ok.
        if (xmlhttp.status == 200) {

            if (xmlhttp.responseText != '{}') {

                //If no output element was set the text from the server will contain the commands for the 
                //chat client. The response is split by !
                var info = xmlhttp.responseText.split("|");
                var price = info[0];
                var rrp = info[1];
                var leadtime = info[2];
                var image = info[3];
                changeProductInfo(price, rrp, leadtime, image);
            }

        }
        else {
            //If there is an error with the callback page the error is alerted to the user.
            alert(xmlhttp.responseText);
        }
    }
}

//[-------HTTPCallBackResponse---------------]



//[-------rndParam---------------]

//This function produces a random 5 character string. This string is used as a querystring key. Internet
//Exploder caches all responses after the first response when using an xml request object.

function rndParam() {

    //arr to hold letters used to create a random string.
    var arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];

    var ranNumber = 0;
    var strParam = "";

    //Loop 5 times creating a random char adding it to the overall string.
    for (i = 0; i <= 5; i++) {

        ranNumber = Math.floor(Math.random() * 7)
        strParam += arr[ranNumber];

    }

    //Return the random string.
    return strParam;

}

//[-------rndParam---------------]
