/* example on usage 
*
		var xml = new ajaxCOM();
		xml.request("test.php", handler, null);
		//to send form for the post xml.request("test.php", handler, document.forms[0]);

		function handler(respnose)
		{
			alert(response);		
		}

		**alternative
		**xml.request("test.php", function(response){alert(response);}, null);
		
		other properties
		containserror (true or false): indicates whether an error occured during the process
		lasterror: indicates what the last error was, if any
		waitcursor (true or false): indicates whether you wish to change the cursor to an hour glass while the ajax request is being processed
		returnxml (true or false): indicates whether you want to receive the responeText or responseXML from the xml object
		
		other methods
		sethandler (the state (0,1,2,3,4) and the function to call during that state)
			during each state of the ajax request a handler can be assigned to inform you and perform some action at that time!
			
		request: returns true or false (contains error)
*/



function ajaxCOM(){
	var xml = createXmlHttp();					//private object for creating requests
	var statehandler = new Array();				//private array for holding state handlers

	this.containswarning=false;					//flag for checking warnings
	this.lastwarning="";						//holds last warning message
	this.containserror=false;					//flag for checking error	
	this.lasterror="";							//holds last error message
	this.waitcursor=true;						//should we show a wait cursor during the call?
    this.method="post";                         //post or get	
    this.contenttype="application/x-www-form-urlencoded";
   	this.returnxml=false;						//return xml or text?
   	this.json=false;                            //return json object
	
	this.sethandler = function(state, handler){	//set different state handlers
		if(state>=1 && state <=4){statehandler[state]=handler;}
	}
	this.request = function(url, handler, form){
		this.lastwarning="";
		this.containswarning=false;				
		this.lasterror="";										//reset value of error message
		this.containserror=false;								//reset value of error flag

		var formdata = '';                                      //changed from null to '' due to firefox length required issue

		if(form!=null){
			formdata = getFormElements(form);
		}

		if(!xml){
			this.containserror = true;
			this.lasterror = "browser doesn't support ajax";
		}else{
			xml.open(this.method, url, true);
			xml.onreadystatechange=new xmlhandler(this, handler).processRequest;
			
			//keep this as a work around for IE.. as it caches GET and won't run subsequent requests
			xml.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT'); 
			xml.setRequestHeader("Content-Type",this.contenttype);
			//xml.setRequestHeader("Content-Type","multipart/form-data");
			xml.send(formdata);
		}
		return this.containserror;
	}
	
	function getFormElements(frm)
	{
		var aParams = new Array();
		for(var i=0;i<frm.elements.length;i++)
		{
			var sParam = ""; 
			var v = "";
			var n = "";
			if(frm.elements[i].type){
				var t = frm.elements[i].type;
				if(t=="checkbox"){
					if(frm.elements[i].checked)
					{
						n=encodeURIComponent(frm.elements[i].name);
						v=encodeURIComponent(frm.elements[i].checked);
					}
				}else if(t=="radio"){
					if(frm.elements[i].checked){
						n=encodeURIComponent(frm.elements[i].name);
						v=encodeURIComponent(frm.elements[i].value);
					}
				}else{
					n=encodeURIComponent(frm.elements[i].name);
					v=encodeURIComponent(frm.elements[i].value);
				}
			}else{
				n=encodeURIComponent(frm.elements[i].name);
				v = encodeURIComponent(frm.elements[i].value);
			}
			sParam=n;
			sParam+="=";
			sParam+=v;
			aParams.push(sParam);	
		}
		return aParams.join("&");
	}
	
	//handles the response 
	function xmlhandler(ajax, handler){
		this.processRequest = function(){
			if(statehandler[xml.readyState]){statehandler[xml.readyState]();}
			switch(xml.readyState){
				case 1:
					if(ajax.waitcursor){document.body.style.cursor="wait";}
					break;
				case 2:
					break;
				case 3:
					break;
				case 4:
					if(xml.status!=200){
						ajax.containserror=true;
						ajax.lasterror="generic error processing request";
					}
					if(ajax.json)
					    handler(eval(xml.responseText));
					else
					    if(ajax.returnxml){handler(xml.responseXML);}
					    else{handler(xml.responseText);}
	
	   				if(ajax.waitcursor){document.body.style.cursor="default";}
					break;
			}
		}
	}

	//private function for creating the xml object
	function createXmlHttp(){
		try{XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");}										//ie
		catch(e)
		{
			try{XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
			catch(e){XmlHttp = null;}
		}
		if(!XmlHttp && typeof XMLHttpRequest != "undefined"){XmlHttp = new XMLHttpRequest();}	//mozilla and safari
		return XmlHttp;
	}	
}