/**
 * XMLHttpRequest Wrapper
 * @category   HTML
 * @package    AJAX
 * @author     Jeffrey Buot <jeffreybuot@pinoyportal.com>
 * @copyright  2005 Pinoyportal.com 
 */


function HttpClient() { }

HttpClient.prototype = {
	xmlhttp: null,
	
	dataReturned: null,
	
	dataReturnedXML: null,
	
	requestUrl: null,
	
	requestMethod: "GET",
	
	isAsync: true,
	
	timeout: 10000,
	
	timeoutID: null,
	
	functionNameToCall: null,
	
	contentType: "text/xml",
	
	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('Unable to create XMLHttpRequest.');
			}
		}
	},	

	MakeRequest: function() {
		/*try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
		}
		catch(e) {}	
		if(!this.xmlhttp) {
			this.init();
		}
		else {
			this.Abort();
			this.init();		
		}*/
		this.xmlhttp = null;
		this.init();		
		var self = this;
		try {			
			this.xmlhttp.onreadystatechange = function() {
				self.RetrieveData();
			};			
			this.xmlhttp.open(this.requestMethod, this.requestUrl,this.isAsync);		
			this.xmlhttp.setRequestHeader("Content-Type",this.contentType);
			this.xmlhttp.send(null);			
			//this.timeoutID = window.setTimeout(function() { self.Abort() }, this.timeout);
		}
		catch(e) { alert(e); }
		
		
	},
	
	RetrieveData: function() {			
		switch(this.xmlhttp.readyState) {
			case 1:
				break;
			case 2:
				break;
			case 3:
				break;
			case 4: 
				if(this.xmlhttp.status==200) {
					try {
						this.dataReturned = this.xmlhttp.responseText;						
						//this.dataReturnedXML = this.xmlhttp.responseXML;
						//alert(this.xmlhttp.getAllResponseHeaders());
						if(this.functionNameToCall!=null)
							eval(this.functionNameToCall + "(this.dataReturned)");				
						//if(this.timeoutID!=null) 
						//	window.clearTimeout(this.timeoutID);
					}
					catch(e) {alert(e)}					
				}
				else {
					alert("Invalid Http Request!");
				}
		}
	},
	
    RetrievingInProgress: function() 
    {
        switch ( this.xmlhttp.readyState ) {
            case 1:
            case 2:
            case 3:
                return true;
            break;
            default:
                return false;
            break;
        }
    },
    
    Abort: function() 
    {
        if (this.RetrievingInProgress()) {
            this.xmlhttp.abort();
			//alert("HTTP Request Aborted. Server maybe down at this time.");            
        }
    }
	
}
