/* ========================================================== */
/* File name: ajax.js                                         */
/* Description: Ajax                                          */
/* Create data: 20 August 2008                                */
/* Programmed by Bodro (Bodrov Vasiliy)                       */
/* Contact: vbodrov@v-kernel.com                              */
/* ========================================================== */

Function.CreateDelegate = function(obj, fn){
	return function(){
		fn.apply(obj);
	}
}

function Ajax(){
	var req;
	var reqTimeout;
	var reqTimeoutTime = 5000;
	var processReqChange = null;
	var handlerXMLOK = null;
	var handlerTextOK = null;
	var handlerFAILED = null;
	var handlerAjaxNotSupport = null;
	var handlerOtherAjaxException = null;
	var handlerTimeOut = null;
	
	this.ajaxload = function(url, vars, method, content_type){
		try{
			if(window.XMLHttpRequest){
				req = new XMLHttpRequest();
			}else if(window.ActiveXObject){
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}else{
				try{
					req = new ActiveXObject('Msxml2.XMLHTTP');
				}catch(e){
					try{
						req = new ActiveXObject('Microsoft.XMLHTTP');
					}catch(e){
					}
				}
			}
			
			if(req){
				req.onreadystatechange = processReqChange;
				req.open(method, url, true);
				req.setRequestHeader('Content-Type', content_type);
				req.send(vars);
				reqTimeout = setTimeout(Function.CreateDelegate(this, this.EndTimeOut), reqTimeoutTime);
			}else{
				if(handlerAjaxNotSupport != null){
					handlerAjaxNotSupport();
				}
				else{
					alert("Ajax not support!");
				}
				
				return -1;
			}
		}catch(e){
			if(handlerOtherAjaxException != null){
				handlerOtherAjaxException(e);
			}
			else{
				alert("Ajax exception: " + e);
			}
			
			return -1;
		}
		
		return 0;
	}
	
	processReqChange = function(){
		if(req.readyState == 4){
	        clearTimeout(reqTimeout);
			
			if(req.status == 200){
				if(handlerXMLOK != null){
					handlerXMLOK(req.responseXML);
				}
				if(handlerTextOK != null){
					handlerTextOK(req.responseText);
				}
			}
			else{
				if(handlerFAILED != null){
					handlerFAILED(req.status, req.statusText);
				}
			}
		}
	}

	this.EndTimeOut = function(){
		req.abort();
		if(handlerTimeOut != null){
			handlerTimeOut();
		}
	}

	this.GetTimeOut = function(){
		return reqTimeoutTime;
	}
	
	this.SetTimeOut = function(x){
		reqTimeoutTime = x;
	}
	
	this.SetHandlerAjaxNotSupport = function(handler){
		handlerAjaxNotSupport = function(){handler();};
	}
	
	this.SetHandlerOtherAjaxException = function(handler){
		handlerOtherAjaxException = function(e){handler(e);};
	}
	
	this.SetHandlerXMLOK = function(handler){
		handlerXMLOK = function(x){handler(x);};
	}
	
	this.SetHandlerTextOK = function(handler){
		handlerTextOK = function(x){handler(x);};
	}
	
	this.SetHandlerFAILED = function(handler){
		handlerFAILED = function(stat_num, stat_txt){handler(stat_num, stat_txt);};
	}
	
	this.SetHandlerTimeOut = function(handler){
		handlerTimeOut = function(){handler();};
	}
	
	this.LoadDocPOST = function(url, vars, content_type){
		this.ajaxload(url, vars, "POST", content_type);
	}
	
	this.LoadDocGET = function(url, vars, content_type){
		this.ajaxload(url, null, "GET", content_type);
	}
	
	this.LoadXMLDocPOST = function(url, vars){
		this.ajaxload(url, vars, "POST", "text/xml");
	}
	
	this.LoadXMLDocGET = function(url){
		this.ajaxload(url, null, "GET", "text/xml");
	}
	
	this.LoadFORMDocPOST = function(url, vars){
		this.ajaxload(url, vars, "POST", "application/x-www-form-urlencoded");
	}
	
	this.LoadFORMDocGET = function(url){
		this.ajaxload(url, null, "GET", "application/x-www-form-urlencoded");
	}
}
/* ========================================================== */
/* End of file                                                */
/* ========================================================== */
