﻿function Ajax() {};
Ajax.sendRequest=function(url, callback, data, async, method) {
// method: specifies structure of response data: xml, html, JSON
    
    if (typeof async =="undefined") async = true;
	if (typeof method =="undefined") method="xml";
	method = method.toLowerCase();
	switch (method) {
		case "html":
			method = "Html";
			break;
		case "json":
			method = "JSON";
			break;
		default:
			method = "Xml";
			break;
	}
	
    var httpReq = Ajax.createHttpRequest();
    httpReq.onreadystatechange = function() {
        if (httpReq.readyState==4) {
            var responseContent = httpReq.responseText;
            var xmlObj = httpReq.responseXML;
            if (httpReq.status!=200 && httpReq.status!=304) {
               if (httpReq.status>=600) {
					// Custom error handling on server-side
                   alert("SERVER ERROR:\n" + xmlObj.documentElement.getAttribute("message"));
               } else {
                   Ajax.displayServerError(responseContent);
               }
            } else {
                httpReq = null;
				if (method=="JSON") {
					callback(Ajax.parseJSON(responseContent));
				} else if (method=="Html") {
					callback(responseContent);
				} else {
					callback(xmlObj);
				}
            }
         } else {
            // Still Loading
         }
    };
    if (typeof(data)=="string") {
        // name value pairs in a string
        httpReq.open("POST",url,async);
        httpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        httpReq.setRequestHeader("Ajax-Response",method);
    } else if (typeof(data)=="undefined") {
        // Data in url. Send as GET
        httpReq.open("GET",url,async);
        httpReq.setRequestHeader("Ajax-Response",method);
    } else {
        // Xml Data
        httpReq.open("POST",url,async);
        httpReq.setRequestHeader("Content-Type","text/xml");
        httpReq.setRequestHeader("Ajax-Response",method);
    }
    httpReq.setRequestHeader("Cache-Control","no-cache");
    httpReq.send(data);
};
Ajax.createHttpRequest = function() {
    var o;
    var xmlHttpFactory = [
        function() {return new XMLHttpRequest()},
        function() {return new ActiveXObject("MSXML2.XMLHTTP")},
        function() {return new ActiveXObject("MSXML3.XMLHTTP")},
        function() {return new ActiveXObject("Microsoft.XMLHTTP")}
    ];
    for (var i=0; i<xmlHttpFactory.length; i++) {
        try {
            o = xmlHttpFactory[i]();
        } catch(e) {
            continue;
        }
        break;
    }
    
    delete(xmlHttpFactory);
    return o;
};
Ajax.encodeAsUrl = function(data) {
    var values = new Array();
    for (var i=0; i<data.length; i++) {
        values.push((data[i][0]+"="+escape(data[i][1])));
    }
    return values.join("&");
};  
Ajax.encodeXml = function(xmlString) {
	xmlString = xmlString.replace("&","&amp;");
	xmlString = xmlString.replace("<","&lt;");
	xmlString = xmlString.replace(">","&gt;");
	return xmlString;
}
Ajax.createXmlObject= function(rootNodeName) {
    var o;
    var xmlObjFactory = [
        function() {return document.implementation.createDocument("",rootNodeName,null);},
        function() {return new ActiveXObject("MSXML2.DOMDocument.3.0");},
        function() {return new ActiveXObject("MSXML2.DOMDocument.4.0");},
        function() {return new ActiveXObject("MSXML2.DOMDocument");}
    ];
    for (var i=0; i<xmlObjFactory.length; i++) {
        try {
            o = xmlObjFactory[i]();
        } catch(e) {
            continue;
        }
        break;
    }
    
    delete(xmlHttpFactory);
    
    if (!o.hasChildNodes()) 
        o.appendChild(o.createElement(rootNodeName));
        
    return o; 
};
Ajax.displayServerError = function(html) {
    var errorWind = window.open("","errorWind","menubar=no,resizable=yes,scrollbars=yes");
    errorWind.document.open();
    errorWind.document.write(html);
    errorWind.document.close();
    errorWind.focus();        
};
Ajax.parseJSON = function(json) {
	if ((typeof json =="undefined") || (json.length==0)) return;
	
	var o = typeof json === "string" ? eval("(" + json + ")") : json;
	
	// determine if there are date objects specified in the properties of the object
	for (p in o) {
		if (typeof o[p] === "string") {
			if (/^@([\w\,\s]+\d+:\d+:\d+)@$/i.test(o[p])) {
				o[p] = new Date(o[p].substr(1,o[p].length-2));
			}
		} else if (typeof o[p] === "object") {
			o[p] = Ajax.parseJSON(o[p]);
		}
	}
	return o;
}
