var keyValues = new Array();
var urlBase = "";
var loadstatustext = "<img src='images/loading-spin.gif' /><span>Requesting content...</span>";
var loadstatustext2 = "<img src='images/loading-spin.gif' />";
var contentBox = "exDoc";
function makePOSTRequest(url, parameters, callbackfunc, destDiv) {
    var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType("text/xml");
                // See note below about this line
        }
    } else {
        if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {
                }
            }
        }
    }
    if (!http_request) {
        alert("Giving up :( Cannot create an XMLHTTP instance");
        return false;
    }
    http_request.onreadystatechange = function () {
        callbackfunc(http_request, destDiv);
    };
    var bustcacheparameter = (url.indexOf("?") != -1) ? "&rand=" + String(new Date().getTime()) : "?rand=" + String(new Date().getTime());
    url = url + bustcacheparameter;
    http_request.open("POST", url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
}
function alertContents(http_request, targetDiv) {
  //  alert(targetDiv);
    try {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
            //alert(http_request.responseText);
                var result = http_request.responseText;
                document.getElementById(targetDiv).innerHTML = result;
                document.getElementById("stsBar").innerHTML = "";
            } else {
	           
                alert("There was a problem with the request.");
                document.getElementById("stsBar").innerHTML = "";
            }
        }
    }
    catch (e) {
        alert("Caught Exception: " + e.description);
    }
}
function fillElement(http_request, destElement) {
 //   alert(destElement);
  //  alert(targetDiv);
    try {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
            //alert(http_request.responseText);
                var result = http_request.responseText;
                document.getElementById(destElement).innerHTML = result;
            } else {
//             alert(http_request.responseText);
                alert("There was a problem with the request.");
            }
        }
    }
    catch (e) {
        alert("Caught Exception: " + e.description);
    }
}
function getPostParameters(callerKey) {
    var dataStr = "key=" + encodeURI(callerKey);
    var ele = null;
    for (var i = 0; i < keyValues.length; i++) {
        ele = document.getElementById(keyValues[i]);
        if ((ele != null) && (ele.type != "radio")) {
            dataStr = dataStr + "&" + ele.id + "=" + encodeURI(ele.value);
        }
    }
    return dataStr;
}
function postData(urltarget, callerKey) {
    document.getElementById("stsBar").innerHTML = loadstatustext;
    var poststr = getPostParameters(callerKey);
    makePOSTRequest(urlBase + urltarget, poststr, alertContents, "exDoc");
}
function postDataEx(urltarget, callerKey, destDiv) {
    var poststr = getPostParameters(callerKey);
     document.getElementById(destDiv).innerHTML = loadstatustext;
    makePOSTRequest(urlBase + urltarget, poststr, alertContents, destDiv);
}
function doGet(callerKey, callerId, urltarget, destTarget) {  
    var dataStr = "key=" + encodeURI(callerKey);
    var ele = document.getElementById(callerId);
    dataStr = dataStr + "&" + ele.id + "=" + encodeURI(ele.value);
     document.getElementById(destTarget).innerHTML = loadstatustext2;
    makePOSTRequest(urlBase + urltarget, dataStr, fillElement, destTarget);
}
/*
var ajaxpack=new Object()
ajaxpack.basedomain="http://"+window.location.hostname;
ajaxpack.ajaxobj=createAjaxObj();
ajaxpack.filetype="txt";
ajaxpack.addrandomnumber=0; //Set to 1 or 0. See documentation.

ajaxpack.getAjaxRequest=function(url, parameters, callbackfunc, filetype){
ajaxpack.ajaxobj=createAjaxObj(); //recreate ajax object to defeat cache problem in IE
if (ajaxpack.addrandomnumber==1) //Further defeat caching problem in IE?
var parameters=parameters+"&ajaxcachebust="+new Date().getTime();
if (this.ajaxobj){
this.filetype=filetype;
this.ajaxobj.onreadystatechange=callbackfunc;
this.ajaxobj.open('GET', url+"?"+parameters, true);
this.ajaxobj.send(null);
}
}

ajaxpack.postAjaxRequest=function(url, parameters, callbackfunc, filetype){
ajaxpack.ajaxobj=createAjaxObj() //recreate ajax object to defeat cache problem in IE
if (this.ajaxobj){
this.filetype=filetype;
this.ajaxobj.onreadystatechange = callbackfunc;
this.ajaxobj.open('POST', url, true);
this.ajaxobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.ajaxobj.setRequestHeader("Content-length", parameters.length);
this.ajaxobj.setRequestHeader("Connection", "close");
this.ajaxobj.send(parameters);
}
}





//ACCESSIBLE VARIABLES (for use within your callback functions):
//1) ajaxpack.ajaxobj //points to the current ajax object
//2) ajaxpack.filetype //The expected file type of the external file ("txt" or "xml")
//3) ajaxpack.basedomain //The root domain executing this ajax script, taking into account the possible "www" prefix.
//4) ajaxpack.addrandomnumber //Set to 0 or 1. When set to 1, a random number will be added to the end of the query string of GET requests to bust file caching of the external file in IE. See docs for more info.

//ACCESSIBLE FUNCTIONS:
//1) ajaxpack.getAjaxRequest(url, parameters, callbackfunc, filetype)
//2) ajaxpack.postAjaxRequest(url, parameters, callbackfunc, filetype)

///////////END OF ROUTINE HERE////////////////////////


//////EXAMPLE USAGE ////////////////////////////////////////////
/* Comment begins here

//Define call back function to process returned data
function processGetPost(){
var myajax=ajaxpack.ajaxobj
var myfiletype=ajaxpack.filetype
if (myajax.readyState == 4){ //if request of file completed
if (myajax.status==200 || window.location.href.indexOf("http")==-1){ if request was successful or running script locally
if (myfiletype=="txt")
alert(myajax.responseText)
else
alert(myajax.responseXML)
}
}
}

/////1) GET Example- alert contents of any file (regular text or xml file):

ajaxpack.getAjaxRequest("example.php", "", processGetPost, "txt")
ajaxpack.getAjaxRequest("example.php", "name=George&age=27", processGetPost, "txt")
ajaxpack.getAjaxRequest("examplexml.php", "name=George&age=27", processGetPost, "xml")
ajaxpack.getAjaxRequest(ajaxpack.basedomain+"/mydir/mylist.txt", "", processGetPost, "txt")

/////2) Post Example- Post some data to a PHP script for processing, then alert posted data:

//Define function to construct the desired parameters and their values to post via Ajax


var poststr=getPostParameters()

ajaxpack.postAjaxRequest("example.php", poststr, processGetPost, "txt")
ajaxpack.postAjaxRequest("examplexml.php", poststr, processGetPost, "xml")

Comment Ends here */

