// JavaScript Document
//last mod : 10-09-2008
/*
Usage :
xmlHttp({url : "url.asp",response_handler : SomeFunction})
Or :
xmlHttp({
		url : "?id="+_id
		,method : "get"
		,data : null
		,response_handler : this.response_handler
		,error_handler : this.error_handler
		,xml_handler : this.response_handler
	})
*/
function xmlHttp(_OP){
this.createXMLHttp = function() {
	try {		return new XMLHttpRequest(); }catch(e){}
	try {		return new ActiveXObject('Msxml2.XMLHTTP');	}catch (e) {}
	try {	    return new ActiveXObject('Microsoft.XMLHTTP'); }catch(e){  return null	}
}
this.foo = function(_x_){	return	}
this.rnd_url = function(_url){
	if (_url.indexOf("?") > -1){		return	_url + "&rnd=" + Math.random()	}
	return _url + "?rnd=" + Math.random()
}
this.asyncronous	= true // asyncronous IMPORTENT IF false window.onload not return data MOREInfo http://www.webopedia.com/TERM/A/asynchronous.html 
this.url 			= _OP.url ? this.rnd_url(_OP.url) : "#"
this.method 		= (String(_OP.method).toLowerCase()=="post") ?  "POST" : "GET"	
this.data 			= _OP.data ?  _OP.data	:  null	
_OP.response_handler = _OP.response_handler ?	_OP.response_handler : this.foo
_OP.error_handler 	= _OP.error_handler ? _OP.error_handler : this.foo
_OP.xml_handler 	= _OP.xml_handler ? _OP.xml_handler : this.foo
this.request_header = (this.method=="GET") ? "text/html" : "application/x-www-form-urlencoded"	// ; Charset=iso-8859-1"


try {	
		var xmlHttp = this.createXMLHttp();
		if (!(xmlHttp)) return
		
		xmlHttp.open(this.method, this.url, this.asyncronous);
	
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4 ){
					 if (xmlHttp.status == 200) {
						_OP.response_handler(xmlHttp.responseText)
						_OP.xml_handler(xmlHttp.responseXML)
					 }else{
						_OP.error_handler(xmlHttp.status);
					}
			}
		}
		
		xmlHttp.setRequestHeader("Content-Type",this.request_header) //ajax POST ASP.request.form not response without header PHP work with $GLOBALS['HTTP_RAW_POST_DATA'];
		xmlHttp.send(this.data) // xmlHttp.onreadystatechange has to be before the xmlHttp.send 
	}	
	catch (e) {
		console.log("Ajax Error :"+e+ " " + e.description)
	}
}


