/***************************************************************
*
* Name         : Ajax class
* Desciption   :
* Author       : Canh Chan
* Created on   : 
* 
* ************************************************************/

/*
* Function: 
* Description: 
* Author: Canh Chan
*/
 var _objAjaxActionTimeOut;
 var _objExceptTimeOut=false; // except timer for specify function
 var _timeToRemindSessionAlive = 1200000; // 20 minutes
 var _handleRemindSessionAlive = null;
function Ajax(method)
{
    var _strAjaxFunctionCallBack 			= "";
    var _method                            = method?method:"POST";
    var _objAjaxTimeOutID = null;
    var _objScollPoll = null;
    var _intMaxSecondWaitingServerResponse	= 10;
    var _objTimeOutLoadingID               = null;
    var _strDivLoadingID                    = "divLoading";
    var _strDivLoadingInsideID              = "divLoading_inside";
    var _strDefaultMsg                      = "Loading... Please wait";
    var _strCustomeMsg                      = "";
    var _strFuncTimeout=null;
    var _xmlHTTPRequest;

    // TODO: TinhDoan edited [20100708]
    var _flagQueue = false;
    var _queueRequest = new Array();
/*
* Function: 
* Description: 
* Author: Canh Chan
*/
    this.GetXMLHTTPRequest = function()
    {
	     var request = false;
	     try
	      {
		     request = new XMLHttpRequest(); /* e.g. Firefox */
	       }
	     catch(err1)
	       {
	       try
		     {
		     request = new ActiveXObject("Msxml2.XMLHTTP");/* some versions IE */
		     }
	       catch(err2)
		     {
		     try
		      {
		       request = new ActiveXObject("Microsoft.XMLHTTP");/* some versions IE */
		       }
		       catch(err3)
			     {
			     request = false;
			     }
		     }
	       }
	     return request;
    }
 
/*
* Function: 
* Description: 
* Author: Canh Chan
*/
    this.SendRequestToServer = function(strURL, strSendValues, strFuncCallBack, blnAsync) {

        if (strURL.length == 0) {
            return;
        }

        //set function callback
        _strAjaxFunctionCallBack = strFuncCallBack;
         

        //get xmlHTTPRequest
        xmlHTTPRequest = this.GetXMLHTTPRequest();

         
         
        //fill parameters then send to server
        xmlHTTPRequest.open(_method, strURL, blnAsync);
         
        // Set timeout function
         
        
        // Set call back function
        xmlHTTPRequest.onreadystatechange = this.CallBackFromServer;
        _objAjaxActionTimeOut = window.setInterval(this.timeoutFired,REQUEST_TIMEOUT);
            
        // Set header
        xmlHTTPRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        // Send
        xmlHTTPRequest.send(strSendValues);     
        // Set default msg
    //    if (_strCustomeMsg.length == 0) {
    //        _strCustomeMsg = _strDefaultMsg;
    //    }
         
        // Show div loading
        // Cho nay khong duoc thay doi vi no quyet dinh cho hanh dong KEEP_SESSION_ALIVE duoc chay ngam (khong xuat hien thong bao "Dang xu ly...")
        if (_strCustomeMsg != null && _strCustomeMsg != "")
        {          
            showHideLoading(true);
        }

        // Remind to send request to server to keep SESSION alive
        if (typeof(_keepSessionAlive)!="undefined" && _keepSessionAlive)
        { 
            window.clearInterval(_handleRemindSessionAlive);
            _handleRemindSessionAlive = window.setInterval(keepSessionAlive,_timeToRemindSessionAlive);
        }
         
        return xmlHTTPRequest;
    }	
 
/*
* Function: 
* Description: 
* Author: Thanh Dao
*/
    this.timeoutFired = function()
    {
        if (_objExceptTimeOut==true)
        {
            _objExceptTimeOut = false;
            window.clearInterval(_objAjaxActionTimeOut);
            return;
        }
        window.clearInterval(_objAjaxActionTimeOut);
        try { xmlHTTPRequest.abort(); }
        catch(e) {}
        _strCustomeMsg="";
        showHideLoading(false);
        if ( _strFuncTimeout!= null ){
            _strFuncTimeout(xmlHTTPRequest);
        }
        else
        {
		    dlgBox.showDialog(SYSTEM_TITLE,MSG_TIMEOUT,null,null,2);
        }
    }
 
    this.SendRequestToServerWithCustomMsg = function(strURL, strSendValues, strFuncCallBack, blnAsync, strCustomMsg, strFuncTimeout, objExceptTimeOut, noCheck)
    {
        // thuc hien kiem tra va dua vao queue
        if (!noCheck && _flagQueue)
        {
            // thuc hien luu thong tin request lai
            var rq = new Array();
            _queueRequest.push({
                strURL: strURL, strSendValues: strSendValues, strFuncCallBack: strFuncCallBack, blnAsync: blnAsync,
                strCustomMsg: strCustomMsg, strFuncTimeout: strFuncTimeout, objExceptTimeOut: objExceptTimeOut
            });
            return;
        }
        _flagQueue = true;
        
        window.clearInterval(_objAjaxActionTimeOut);
        if (objExceptTimeOut==null)
        {
            _objExceptTimeOut = false;
        }
        else
        {
            _objExceptTimeOut = objExceptTimeOut;
        }    
        _strCustomeMsg = strCustomMsg;    
        _strFuncTimeout = strFuncTimeout;    
        this.SendRequestToServer(strURL, strSendValues, strFuncCallBack, blnAsync);
    }

/*
* Function: 
* Description: 
* Author: Canh Chan
*/
     this.CallBackFromServer = function()
     {
	    if(xmlHTTPRequest.readyState == 4) 
	    {
		    if(xmlHTTPRequest.status == 200) 
		    {		    
			    //clear timeout
			    window.clearTimeout(_objAjaxActionTimeOut);

			    // Hide div loading
	            showHideLoading(false);
    	        
	            //call call back function
			    if(_strAjaxFunctionCallBack != null)		
			    { 			
				    _strAjaxFunctionCallBack(xmlHTTPRequest);
			    }
    			
			    // kiem tra va goi thuc hien neu con
		        if (_queueRequest.length!=0)
		        {
		            var rq = _queueRequest.shift();
		            ajax.SendRequestToServerWithCustomMsg(rq.strURL, rq.strSendValues, rq.strFuncCallBack, rq.blnAsync, 
		                                                rq.strCustomMsg, rq.strFuncTimeout, rq.objExceptTimeOut, true);
		        }
		        else
		        {
		            // bat co tro lai binh thuong
		            _flagQueue = false;
		            // Clear custom msg
	                _strCustomeMsg = "";
		        }
		    }
	    }
     }

    showHideLoading = function(isVisible) {
        var divLoading = null;

        //show over page and enable or disable all page component
        if (isVisible) {
            var oBody = document.getElementsByTagName("BODY").item(0);

            divLoading = core.getObject(_strDivLoadingID);

            //remove divLoading if existed
            if (divLoading != null) {
                core.getObject(_strDivLoadingInsideID).innerHTML = _strCustomeMsg;
            }else{
		        var newDivLoading = document.createElement('div');
    		    
		        newDivLoading.id = _strDivLoadingID;
		        newDivLoading.style.position = "absolute";
		        newDivLoading.style.display = "block";
		        newDivLoading.style.zIndex = 100;
		        newDivLoading.backgroundColor = "#FFEEAA";
		        newDivLoading.innerHTML = "<img src='../images/loading.gif'/>&nbsp;"+_strCustomeMsg;
		        // add div de parent 
		        oBody.appendChild(newDivLoading);
            }
            // Move to current position 
            moveLoadingDiv();        
        }else {
            // Clear all time out id        
            window.clearTimeout(_objTimeOutLoadingID);
            window.clearTimeout(_objScollPoll);
        }
     
        // Show, hide divLoading
        core.showHide(_strDivLoadingID, isVisible);
    }

    moveLoadingDiv = function() {
        var scrollPos = core.getScroll();
	    //move divLoading
	    core.moveTo(_strDivLoadingID, core.getInsideWindowWidth()/2 + scrollPos.x - 100, 10 + scrollPos.y);

	    // Set time out
	    _objScollPoll = window.setTimeout("moveLoadingDiv()", 300);
    }
}
//khoi tao doi tuong ajax
var ajax = new Ajax();

// Keep session alive 
function keepSessionAlive()
{
    var ajax = new Ajax(METHOD_GET);
    ajax.SendRequestToServerWithCustomMsg("index.php?act=" + ACT_REFRESH_SESSION, null , null, true, null,null,true);    
}