/*
My first ever AJAX class... Thank you MATTHEW EERNISSE
Ok !... ouff lets try to write this ajax class...
Pain in the butt but what the hell...

This is V 1.0 and it may need a lot lot of fixing

Joe Abi Raad 07.04.07
*/
function Ajax(){  // My constructor
	this.req=null;
	this.url=null;
    this.method='GET';
	this.async=true; // No shit !!!!
	this.status=null;
	this.statusText='';
	this.postData=null;
	this.readyState=null;
	this.responseText=null;
	this.responseXML=null;
	this.handleResp=null;
	this.responseFormat='text'; // could be XML or an OBJECT
	this.mimeType=null; // ASHOU ? ? ?
};

this.init = function() {
    var i = 0;
    var reqTry = [ 
      function() { return new XMLHttpRequest(); },
      function() { return new ActiveXObject('Msxml2.XMLHTTP') },
      function() { return new ActiveXObject('Microsoft.XMLHTTP' )} ];
      
    while (!this.req && (i < reqTry.length)) {
      try { 
        this.req = reqTry[i++]();
      } 
      catch(e) {}
    }
    return true;
};

this.doReq = function() { // My main request function....

	if ( !this.init() ){ // Go init !???
		alert('Could not create XMLHttpRequest object\nAre you using a very old browser?');
		return;
	}
	try{
		this.req.open(this.method,this.url,this.async);
	}
	catch(e){
		alert(e);
	}
	//not finnished
	if ( this.method == "POST" ){
		this.req.setRequestHeader('Content-Type',
								  'application/x-www-form-urlencoded');
	}
	////////////////
	var self = this; // F*cken loss-of-scope... i lost a lot of time for this ugly error
	this.req.onreadystatechange = function(){
		if ( self.req.readyState == 4 ){ // Yalla start to handle stuff here.... Wouh
			switch (self.responseFormat){
				case 'text':
					resp = self.req.responseText;
				break;
				case 'xml':
					resp = self.req.responseXML;
				break;
				case 'object':
					resp = req;
				break;
			}
			if ( self.req.status >= 200 && self.req.status <=299 ){ // Ok response is good
				self.handleResp(resp);
			} else{
				self.handleErr(resp);
			}
		}
	};
	this.req.send(this.postData);
};
this.handleErr = function(){
	var errorW;
	try{
		errorW=window.open('','errorW');
		errorW.document.body.innerHTML=this.responseText;
	}
	catch(e){
		alert('Please disable your popup blocker! ');
	}
}
this.abort = function () {
	if ( this.req ){
		this.req.onreadystatechange = function() {};
		this.req.abort();
		this.req=null;
	}
};
this.doGet = function(url,hand,format){
	this.url=url;
	this.handleResp=hand;
	this.method='GET';
	this.async=true;
	this.responseFormat = format || 'test';
	this.doReq();
};
///////////////////////////////////////////////
this.doPost = function(url,postData,hand,format){
	this.url=url;
	this.handleResp=hand;
	this.method='POST';
	this.async=true;
	this.postData=postData;
	this.responseFormat = format || 'test';
	this.doReq();
}
/* OUFF lets try it now ////// *///////////////
