// *********************
// DXMLHttpRequest Class
// *********************
/*
 * Error codes
 * 1 - Cannot initialize XMLHTTP/ActiveX object
 * 2 - Load URL does not exist
 * 3 - Error in load URL (http error - 400 - 500)
 * 4 - Error in GET request
 * 5 - Error in POST request
 * 6 - Error in loading data
 * 7 - timeout
 */

function DXMLHttpRequestShell() {
    this.init();
}
DXMLHttpRequestShell.prototype.XMLHttp = null;
//Default encoding
DXMLHttpRequestShell.prototype.encoding = "utf-8";
//Default load timeout
DXMLHttpRequestShell.prototype.timeout = 18000;
DXMLHttpRequestShell.prototype.timeoutHandler = null;
DXMLHttpRequestShell.prototype.DLength = "DLength";
DXMLHttpRequestShell.prototype.loadProgress = 0;
DXMLHttpRequestShell.prototype.isError = false;
DXMLHttpRequestShell.prototype.inProgress = false;

DXMLHttpRequestShell.prototype.init = function() {
    if (window.ActiveXObject) {
        try {
            this.XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(ex) {
        	this.isError = true;
            this.onError(1, "Microsoft.XMLHTTP could not be inited");
        }
    } else if (window.XMLHttpRequest){
        this.XMLHttp = new XMLHttpRequest();
    } else {
    	this.isError = true;
    	this.onError(1, "XMLHttp object could not be found!");
    }
    var obj = this;
    this.XMLHttp.onreadystatechange = function() {
        obj._onLoadHandler();
    };
}
DXMLHttpRequestShell.prototype.timeoutError = function() {
	this.isError = true;
	this.XMLHttp.abort();
	this.onError("7", "Loading timeout");
}
DXMLHttpRequestShell.prototype.getReadyState = function() {
    return this.XMLHttp.readyState;
}
DXMLHttpRequestShell.prototype.getServerStatusId = function() {
	try {
		return this.XMLHttp.status;
	} catch(asd) {
		//
	}
}
DXMLHttpRequestShell.prototype.getServerStatusText = function() {
    return this.XMLHttp.statusText;
}
DXMLHttpRequestShell.prototype.getResponseXML = function() {
    if (this.getReadyState() == 4) {
        return this.XMLHttp.responseXML;
    }
}
DXMLHttpRequestShell.prototype.getResponseText = function() {
    if (this.getReadyState() == 4) {
        return this.XMLHttp.responseText;
    }
}
DXMLHttpRequestShell.prototype.onProgress = function(currentProgress) {
	this.loadProgress = currentProgress;
}
DXMLHttpRequestShell.prototype._onLoadHandler = function() {
	if (this.getServerStatusId() >= 400) {
		if(this.isError) {
			return;
		}
		if (this.timeoutHandler) {
			clearTimeout(this.timeoutHandler);
		}
		this.isError = true;
		this.XMLHttp.abort();
		this.onError(3, "HTTP status code: " + this.getServerStatusId());
	} else if (this.getReadyState() == 3) {
		try {
			this.onProgress(this.XMLHttp.responseText.length / this.XMLHttp.getResponseHeader(this.DLength) * 100);
		} catch (asd) {
			//
		}
	} else if (this.getReadyState() == 4) {
        if (this.getServerStatusId() == 200 || this.getServerStatusId() == 304) {
            try {
            	clearTimeout(this.timeoutHandler);
                this.onLoad();
            } catch (ex) {
            	this.isError = true;
                this.onError(6, "Error in loading data");
            }
        }
    }
}
DXMLHttpRequestShell.prototype.fastGetUrl = function(url) {
    try {
        this.XMLHttp.open("GET", url, true);
        this.XMLHttp.send(null);
        var self = this;
        this.timeoutHandler = setTimeout("self.timeoutError()", this.timeout);
    } catch(ex) {
    	this.isError = true;
        this.onError(4, "Error in GET request");
    }
}

DXMLHttpRequestShell.prototype.fastPostUrl = function(url, parameters) {
//	this.checkRequestURL(url);
    try {
        this.XMLHttp.open("POST", url, true);
        this.XMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	this.XMLHttp.setRequestHeader("Content-length", parameters.length);
      	this.XMLHttp.setRequestHeader("Connection", "close");
      	this.XMLHttp.setRequestHeader("Charset", this.encoding);
        this.XMLHttp.send(parameters);
        var self = this;
//        this.isError = true;
        this.timeoutHandler = setTimeout("self.timeoutError()", this.timeout);        
    } catch(ex) {
        this.onError(5, "Error in POST request");
    }
}

DXMLHttpRequestShell.prototype.onLoad = function() {}
DXMLHttpRequestShell.prototype.onError = function(code, message) {
	alert(message);
}

DXMLHttpRequestShell.prototype.getAttributeValue = function(attributeName, XMLNodeObject) {
    for (var i = 0; i < XMLNodeObject.attributes.length; i++) {
        with (XMLNodeObject.attributes.item(i)) {
            if (nodeName == attributeName) {
                return nodeValue;
            }
        }
    }
    return null;
}
DXMLHttpRequestShell.prototype.getAllData = function(XMLNodeObject) {
    var collection = {
        name : "",
        attributes : {},
        value : "",
        childNodes : []
    };

    with (XMLNodeObject) {
        collection.name = nodeName;
        if (hasChildNodes()) {
            collection.value = firstChild.nodeValue;
        }
    }
    
    if (XMLNodeObject.attributes.length > 0) {
        for (var i = 0; i < XMLNodeObject.attributes.length; i++) {
            with (XMLNodeObject.attributes.item(i)) {
                collection.attributes[nodeName] = nodeValue;
            }
        }
    }
    
    if (XMLNodeObject.childNodes.length > 0) {
        for (var i = 0; i < XMLNodeObject.childNodes.length; i++) {
            var currentNode = XMLNodeObject.childNodes.item(i);
            if (currentNode.nodeName.indexOf("#") != 0) {
                collection.childNodes.push(
                    this.getAllData(currentNode
                ));
            }
        }
    }
    
    return collection;
}
// *************************
// DXMLHttpRequest Class end
// *************************
