var ELEMENT_NODE_TYPE = 1;
var	CDATA_SECTION_NODE_TYPE = 4;

function handleStateChange(ajaxRequest, postRequest ) {
    var ro = ajaxRequest.getXMLHttpRequestObject();
    if(ro.readyState == 4 && ro.status == 200) {
        ajaxRequest.setHtmlResponse("");
        ajaxRequest.setScriptResponse("");
        if(ro.responseXML && ro.responseXML.documentElement) {
            var nodes = ro.responseXML.documentElement.childNodes;
            for(var i = 0; i < nodes.length; i++) {
                var element1 = nodes[i];
                if(element1.nodeType != ELEMENT_NODE_TYPE) {
                    continue;
                }
                var tagName = element1.tagName.toLowerCase();
                var polymetaHtmlType = (tagName=="polymeta-html");
                var polymetaScriptType = (tagName=="polymeta-script");
                if(polymetaHtmlType || polymetaScriptType){
                    for(var y = 0; y < element1.childNodes.length; y++) {
                        var element2 = element1.childNodes[y];
                        if(element2.nodeType==CDATA_SECTION_NODE_TYPE) {
                            if(polymetaHtmlType) {
                                ajaxRequest.setHtmlResponse(ajaxRequest.getHtmlResponse() + element2.data );
                            } else if (polymetaScriptType) {
                                ajaxRequest.setScriptResponse(ajaxRequest.getScriptResponse() + element2.data );
                            }
                        }
                    }
                }
            }
        } else {
            ajaxRequest.setHtmlResponse( ro.responseText );
        }
        postRequest(ajaxRequest);
    }
}


function AjaxRequest(url, postRequestFunction) {

    /** @private */
    var self = this;

    /** @private */
    var xmlHttp = createXMLHttpRequest();

    /** @private */
    var queryString = "";

    /** @private */
    var requestURL = url;

    /** @private */
    var method = "POST";

	/** @private */
    var async = true;

    /** @private */
    var postRequest = postRequestFunction ? postRequestFunction : null;

    /** @private */
    var requestID = null;

    /** @private */
    var htmlResponse = null;

    /** @private */
    var scriptResponse = null;

    this.getXMLHttpRequestObject = function() {
        return xmlHttp;
    }

    this.getRequestID = function() {
        return requestID;
    }

    this.setRequestID = function(reqID) {
        requestID = reqID;
    }

    this.setPostRequest = function(func) {
        postRequest = func;
    }

    this.setUsePOST = function() {
        method = "POST";
    }

    this.setUseGET = function() {
        method = "GET";
    }

    this.getHtmlResponse = function() {
        return htmlResponse;
    }

    this.getScriptResponse = function() {
        return scriptResponse;
    }

    this.setHtmlResponse = function(newval) {
        htmlResponse = newval;
    }

    this.setScriptResponse = function(newval) {
        scriptResponse = newval;
    }

	this.getASync = function() {
        return async;
    }

    this.setASync = function(a) {
        async = a;
    }

    /**  Send the Ajax request.  */
    this.sendRequest = function() {
        xmlHttp.onreadystatechange = function () { handleStateChange(self, postRequest) };

        if(method == "GET") {
            requestURL = requestURL + "&" + this.queryString;
            xmlHttp.open(method, requestURL, async);
            xmlHttp.send(null);
        } else {
            xmlHttp.open(method, requestURL, async);
            xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlHttp.send(this.queryString);
        }
    }

}
	function createXMLHttpRequest() {
	    if (window.ActiveXObject) {
	        return new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    else if (window.XMLHttpRequest) {
	        return new XMLHttpRequest();
	    }
	}
