// xhttp CLASS / CREATED ON FEB 20TH 2007 / BY SIMON-PIERRE ALEPIN / ©2007 BAMSTRATEGY.COM
//	VERSION 1.5 (Coming soon: xml/json)
//	LAST MODIFICATION : 2007-04-27
// 	FIXED IN V1.5:
// 		-French characters bugs (need to use utf8_decode function in php before dump in db)
// 		-Cache problen in IE
//		-setRequestHeader timing problem
//
/**************************************************************************************/
// This class sends and receives data to a php file using XML HTTP OBJECT 
//  HOW TO USE:
//	Set up three variables: e.g. 
//	# var url = 'yourfile.php?var1=something&var2=something';
//	# var action = Function_To_Be_Called_When_Data_Loaded;
//	# var method = 'POST'; //or GET
//	# xhttp.load(url, method, action);
//	# function Function_To_Be_Called_When_Data_Loaded(data){
//	# 	 var response = data;
//	#	 something else here...	
//	# } 
/**************************************************************************************/

//Creating a new object
var xhttp = {
	oXMLHttp : false, //Will hold XML HTTP OBJECT
	action : function(data){}, //Will be used to call a function
	getXMLHttpObject : function(){ 
		//CREATE A XML HTTP OBJECT FOR AJAX
		var oXMLHttp = false; //Making sure the object is empty
		if (window.XMLHttpRequest){
			oXMLHttp = new XMLHttpRequest(); //For mozilla
			 if (oXMLHttp.overrideMimeType){
				oXMLHttp.overrideMimeType('text/xml');
			 }			
		}
	  	else if (window.ActiveXObject){ //For IE
			try{oXMLHttp = new ActiveXObject('Msxml2.XMLHTTP');}catch (e){
	   			try {oXMLHttp = new ActiveXObject('Microsoft.XMLHTTP');} catch (e) {}
		 }
	  }				
		return oXMLHttp; // Returns the object to the root of the class
	},
	load : function(url, parameters, method, action){
		this.init(); //Calls the init function to create the XMLHTTP object
		if(this.oXMLHttp.overrideMimeType){this.oXMLHttp.overrideMimeType('text/xml');}
		var ths = this; //Storing this into ths to access later the root of the class
		this.action = action; //Assigning the var action from the function to the class var
		if (method == 'post'){var method = 'POST';}
		if (method == 'get'){var method = 'GET';}
		if (!method){var method = 'POST';}			
		var uid = '&uid=' + new Date().getTime();//Fix problem in IE with cache
		parameters += uid;
		parameters = parameters.replace(/[\n\r]/g,'');
		this.oXMLHttp.open(method, url, true); //Sets up connection type POST/GET
		if (method == 'POST'){
			this.oXMLHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); //Encoding as plain text POST ONLY
			this.oXMLHttp.setRequestHeader('Content-length', parameters.length); //Sets up content length POST ONLY
			this.oXMLHttp.setRequestHeader('Connection', 'close');
			this.oXMLHttp.send(parameters);	 //POST ONLY / GET METHOD shoud be	this.oXMLHttp.send(null);	
		}else
		if (method == 'GET'){
			this.oXMLHttp.send(null);
		}		
		this.oXMLHttp.onreadystatechange = function(){ //When the state of the object changes it creates that function
			var oXMLHttp = ths.oXMLHttp; 
			if (oXMLHttp.readyState == 4){ //State changes to 4 when info from php has been loaded
				if(oXMLHttp.status == 200){
					var result = '';
					if(oXMLHttp.responseText){ //It checks if php returned an empty string
						result = oXMLHttp.responseText;
					}
					if(ths.action){ //If the action parameter has been passed, it calls the function
						ths.action(result);	
					}
				}
			}
		}		
	},	
	init : function(){
		//Init XML HTTP OBJECT
		this.oXMLHttp = this.getXMLHttpObject();
	}	
}//END CLASS