/*
    ------------------------------------------------------------
    httpRequest routine
    ------------------------------------------------------------
    CopyRight (c) TFM Productions                     21-12-2006
    ------------------------------------------------------------
*/

function httpRequest() { }
httpRequest.prototype = {

	requestType:"POST",
	Asynchroon:true,  
	xmlhttp:false,
	callback:false,
    refresh:false,
    statusRequest:"httprequest.gif",
    statusResponse:"httpresponse.gif",

	// what is called when send is called on XMLHttpRequest
	// set your own function to onSend to have a custom loading effect
	onSend:function() {
		// document.getElementById(statusSign).style.display = 'block';
	},

	// what is called when when readyState 4 is reached, this is called before your callback
	onLoad:function() {
		// document.getElementById(statusSign).style.display = 'none';
	},

	// what is called when an http error happens
	onError:function(error) {
		alert(error.message);
	},
	
	// method to initialize an xmlhttpclient
	init:function() {
		try {
		    // Mozilla / Safari
		    this.xmlhttp = new XMLHttpRequest();
		} catch (e) {
			// IE
			var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP');
			var success = false;
			for (var i=0; i<XMLHTTP_IDS.length && !success; i++) {
				try {
					this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
					success = true;
				} catch (e) {}
			}
			if (!success) {
				throw new Error('httpreq.js:\nUnable to create XMLHttpRequest.');
			}
		}
	},

	// method to make a page request
	// @param string URL  The page to make the request too
	// @param string argumentList  What your sending if this is a POST request
	makeRequest:function(URL, argumentList) {
		if (!this.xmlhttp) {
			this.init();
		}
		this.xmlhttp.open(this.requestType, URL, this.Asynchroon);
        if(this.requestType == 'POST') {
            this.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        }

		// set onreadystatechange here since it will be reset after a completed call in Mozilla
		var self = this;
		this.xmlhttp.onreadystatechange = function() {
            self._readyStateChangeCallback();
        }

		this.xmlhttp.send((argumentList)?argumentList:"");

		if (!this.Asynchroon) {
			return this.xmlhttp.responseText;
		}
	},
	

	// internal method used to handle ready state changes
	_readyStateChangeCallback:function() {
		switch(this.xmlhttp.readyState) {
			case 2:
				this.onSend();
				break;
			case 4:
				this.onLoad();
				if (this.xmlhttp.status == 200) {
					this.callback(this.xmlhttp);
				}
				else {
					this.onError(new Error('httpreq.js:\nHTTP Error Making Request: [' + this.xmlhttp.status + '] ' + this.xmlhttp.statusText));
				}
			break;
		}
	},

    xmlResponse:function() {
        if(!this.xmlhttp) {
            throw new Error("httpreq.js:\nNo XML-request initialized");
            return false;
        }

        var xmldoc = this.xmlhttp.responseXML;
        if(!xmldoc || !xmldoc.documentElement)
            throw new Error("httpreq.js:\nInvalid XML-document:\n" + this.xmlhttp.responseText);

        try {
            var firstNode = xmldoc.documentElement.nodeName;
            if(firstNode == 'parseerror')
                throw new Error("httpreq.js:\nInvalid XML-document:\n" + this.xmlhttp.responseText);
        } catch(e) {
            alert(e.message);
        }

        return xmldoc;
    }
}

