﻿/*

This file contains the core ajax functionality needed to communiate with servers

*/

function AjaxRequest(inUrl, inCallback){
    
    var date = new Date();
    var timestamp = date.getTime();

    var xmlHttp = null;
    var url = inUrl + "&time=" + timestamp;
    
    if (inCallback != null){
        _callback = inCallback;
    }
    
    // Ajax request on demand 'constructor'
    function _createXMLHttpRequest(){
        if (window.ActiveXObject){
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest){
            xmlHttp = new XMLHttpRequest();
        }
    }
    
    // 
    function _startRequest(){
        //alert("Request: " + url);
        _createXMLHttpRequest();
        xmlHttp.onreadystatechange = _handleStateChange;
        xmlHttp.open("GET", url, true);

        //prevent browser caching.
        xmlHttp.setRequestHeader('Pragma', 'no-cache');
        xmlHttp.send(null);
    }
    
    // Internal callback function for server request which calls either a default or specified callback.
    function _handleStateChange(){
        if (xmlHttp.readyState == 4){
            if(xmlHttp.status == 200){
                _callback(xmlHttp.responseText);
            }
        }
    }
    
    // Default callback if client did not specify one.
    function _callback(responseString){
        alert("Response: " + responseString);
    }
    
    this.Execute = _startRequest;
}