// JavaScript Document
function AjaxMix ()
{
//Init variable
	var _xmlHttp 		= null;
	
	var _SendUrl 		= "";
	var _SendMethod 	= "GET";
	var _SendParam 		= "";
	var _SendASync 		= true;
	var _SendType 		= 0; 
	// 0 : Send Only
	// 1 : DHTML
	// 2 : Eval after return (javascript / function name [call function with 1 array params])
	var _WriteType 		= 0; 
	//	0 : Replace
	//	1 : Append
	
	var _onSuccessFunction = "";
	var _onFailureFunction = "";

	var ReturnText 		= "";
	var	ReturnObject 	= "";
	var ReturnFunction	= "";
	
	this.SendOnly	=	_SendOnly;
	this.SendDHTML  =   _SendDHTML;
	this.SendEval	=	_SendEval;
	this.SendFunction = _SendFunction;
	
	this.SetASync	=	function(async){_SendASync = async;} //true / false
	this.SetMethod	=	function(method){_SendMethod = method;} //GET / POST
	this.SetParam	=	function(param){_SendParam = param;} //param for POST
	this.SetWriteType	=	function(writeType){_WriteType = writeType;} //param for POST
	
//Private Function
	function GetXmlHttpObject()
	{
		var xmlHttp	=	null;
		try
		{
			// Firefox, Opera 8.0+, Safari
			xmlHttp	=	new XMLHttpRequest();
		}
		catch (e)
		{
			// Internet Explorer
			try
			{
				xmlHttp	=	new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				xmlHttp	=	new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		return xmlHttp;
	}
	
	function ProcessResponse()
	{
		
		//alert(ReturnObject);
		if (_SendType == 0) {};
		if (_SendType == 1) 
		{
			if (_WriteType == 0)
			{
				//alert(ReturnObject);
				document.getElementById(ReturnObject).innerHTML = ReturnText;
			}
			else if (_WriteType == 1)
			{	
				document.getElementById(ReturnObject).innerHTML += ReturnText;				
			}
		};
		if (_SendType == 2)
		{
			if (ReturnFunction == "")
			{
				eval(ReturnText);
			}
			else
			{
				eval(ReturnFunction + "(" + ReturnText + ")");
			}
		};
	}
	
	function _StateChanged()
	{ 
		if ( _xmlHttp.readyState ==  4)
		{
			//alert(_xmlHttp.responseText);
			if (_xmlHttp.responseText.length > 0) // KHONG BIET SAO NO VAN TRA VE STRING SPACE VOI LENGTH = 4
			{
				ReturnText = _xmlHttp.responseText;
				ProcessResponse();
				if (_onSuccessFunction != "")
					eval(_onSuccessFunction + "();");
			}
			else
			{
				if (_onFailureFunction != "")
					eval(_onFailureFunction + "();");
			}
			
			//cancel all  global variable
			_onSuccessFunction = "";
			_onFailureFunction = "";
		
		}
		else
		{
			//if (_onFailureFunction != "")
			//	eval(_onFailureFunction + "();");
			//alert(_xmlHttp.readyState);
		}
	}

	function Send()
	{
		_xmlHttp.onreadystatechange	=	_StateChanged;

		if (_SendMethod == "POST")
		{
			_xmlHttp.open(_SendMethod, _SendUrl,_SendASync);
			_xmlHttp.setRequestHeader ("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
			_xmlHttp.setRequestHeader ("Content-length", _SendParam.length);
			_xmlHttp.setRequestHeader ("Connection", "close");
			_xmlHttp.setRequestHeader ("Accept-Charset","UTF-8");
			_xmlHttp.send(_SendParam);
		}
		else
		{
			//alert(_xmlHttp.responseText);
			//_xmlHttp.open(_SendMethod, _SendUrl + "?" + _SendParam,_SendASync);
			//alert(_SendUrl +  _SendParam);
			_xmlHttp.open(_SendMethod, _SendUrl +  _SendParam,_SendASync);
			_xmlHttp.send(null);
			
			//alert(_SendUrl + "?" + _SendParam);
		}
		if (!_SendASync) // Ajax synchronous
		{	
			//alert(_xmlHttp.responseText);
			ReturnText = _xmlHttp.responseText;
			ProcessResponse();
		}
	}

//init Http Request
	_xmlHttp = GetXmlHttpObject();
	
//Public Function
	function _SendOnly(url,param)
	{
		_SendUrl = url;
		_SendType = 0;
		_SendParam = param;
		Send();
	}

	function _SendDHTML(url, param, ObjDHTML, writeType, onSuccessFunction, onFailureFunction) /*innerHTML*/
	{
		_SendUrl = url;
		_SendType = 1;
		_SendParam = param;
		if (writeType){
			_WriteType = writeType;}

		ReturnObject = ObjDHTML;
		if (onSuccessFunction)
			_onSuccessFunction = onSuccessFunction;
		if (onFailureFunction)
			_onFailureFunction = onFailureFunction;
		Send();
	}
	
	
	function _SendEval(url,param) /*eval*/
	{
		_SendUrl = url;
		_SendType = 2;
		_SendParam = param;
		ReturnFunction = "";
		Send();
	}
	

	function _SendFunction(url,param,funcName) /*funcName ( text )*/
	{
		_SendUrl = url;
		_SendType = 2;
		_SendParam = param;
		ReturnFunction = funcName;
		Send();
	}
}

