﻿/****************************************
 * Pure Ajax Settings - JScript File    *
 * Created by: Shadi Khalid Abu Hilal   *
 ****************************************/


/*---------------------------------------------------------------------------------------*/
/* public variables - Start */
//  Vars are moved to PageHead
/* public variables - End */
/*---------------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------------*/
/* Common Functions - Start */
function ctrl(ctrlID) /* get the control by search in all frames  */
{   
    for(var i=0 ; i < window.frames.length ; i++ )
    {
        try{
            if(ctrlID == window.frames[i].document.getElementById(ctrlID).id)
              return window.frames[i].document.getElementById(ctrlID);
        }catch(er){}
    }
    return document.getElementById(ctrlID); 
}
/*-----------------------------------------*/
function ctrlTop(ctrlID) /* get the control in the main frame  */
{    return top.document.getElementById(ctrlID); }
/*-----------------------------------------*/
function IsEmptyOrNull(strValue) /* check the value if Empty Or Null */
{ 
    if( strValue == null || strValue == 'undefined' || strValue == '' || strValue == '\t' || strValue == '\n' || strValue == '\r' )
        return true;
   
    try
    {
        while(strValue.indexOf(' ') != -1)
        {    strValue = strValue.replace(' ', ''); }
    }catch(er){}    
        
    if( strValue.length == 0)
        return true;
  
    return false;
}
/*-----------------------------------------*/
/* Common Functions - End */
/*---------------------------------------------------------------------------------------*/

/** Don't Change Any Thing **/
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;
}
/*---------------------------------------------------------------------------------------*/

/** Don't Change Any Thing **/
function DoAjax_GET(strExecPageUrl, divHolderID, divLoadingID, strLoadingMsg, strExecJs)
{
    var xmlHttp;
    xmlHttp=GetXmlHttpObject(); /* Fill the xmlHttp object by XMLHttpRequest */
    
    if (xmlHttp == null) /*check if the browser support Ajax */
    { alert ("Your browser does not support AJAX!");  return; } 

    /* onreadystatechange function that checks the ajax state */
    xmlHttp.onreadystatechange = function stateChanged()
    { /* Start of stateChanged function */
          
           var strLodingTemp ="";
          
           if( ! IsEmptyOrNull(strLoadingMsg) ) /* check if there is a custom loading message */
                strLodingTemp =  strLoadingMsg; /* use custom loading message */
           else 
                strLodingTemp = strLoading; /* use the default loading message */
          
          
                switch(xmlHttp.readyState)
                {  /* Start of switch */
                   
                   case 0: /*  0 The request is not initialized */
                     ctrl(divHolderID).innerHTML = "No Connection...";
                     break;
                   
                   case 1: /* 1 The request has been set up  */ 
                    if( ! IsEmptyOrNull(divLoadingID) ) /* check the divLoadingID is needed  */
                     ctrl(divLoadingID).innerHTML =  strLodingTemp; /* "start Connecting..."; */ 
                     break;
                   
                   case 2: /* 2 The request has been sent */ 
                    if( ! IsEmptyOrNull(divLoadingID) ) /* check the divLoadingID is needed  */
                     ctrl(divLoadingID).innerHTML = strLodingTemp; /* "Connecting..."; */ 
                     break;  
                   
                   case 3: /* 3 The request is in process  */ 
                     if( ! IsEmptyOrNull(divLoadingID) ) /* check the divLoadingID is needed  */
                     ctrl(divLoadingID).innerHTML = strLodingTemp; /* "Loading..."; */ 
                     break;
                   
                   case 4: /* 4 The request is complete */ 
                     ctrl(divHolderID).innerHTML=xmlHttp.responseText; /* Fill the ajax result in holder div */
                     
                     if( ! IsEmptyOrNull(strExecJs) ) /* check if strExecJs is needed  */
                     {
                        try{
                            eval(strExecJs); /* execute the js string after the ajax result */
                        }catch(err){};
                      }
                      break; 
                                        
                }; /* End of switch */

        }; /* End of stateChanged function */
        
              /*(Type  , Url            , Asynchronous) */  
    xmlHttp.open("GET", strExecPageUrl, true);
        /* 
	    Note: Asynchronous "true" means that script processing 
	         carries on after the send() method, without waiting for a response,
	         and "false" means that the script waits for a response before
	         continuing script processing.
	    */ 
    xmlHttp.send(null);
}
/*---------------------------------------------------------------------------------------*/

/** Don't Change Any Thing **/
function DoAjax_POST(strExecPageUrl, strParameters, divHolderID, divSendingID, strSendingMsg, strExecJs)
{
	var xmlHttp;
    xmlHttp=GetXmlHttpObject(); /* Fill the xmlHttp object by XMLHttpRequest */
    if (xmlHttp==null)  /*check if the browser support Ajax */
    { alert ("Your browser does not support AJAX!");  return;   } 
  
    /* onreadystatechange function that checks the ajax state */
    xmlHttp.onreadystatechange = function stateChanged()
    { /* Start of stateChanged function */
               
          var strSendingTemp ="";
          
          if( ! IsEmptyOrNull(strSendingMsg) )  /* check if there is a custom loading message */
               strSendingTemp =  strSendingMsg; /* use custom loading message */
          else 
               strSendingTemp = strLoading;  /* use the default loading message */

                switch(xmlHttp.readyState)
                { /* Start of switch */
                   case 0: /*  0 The request is not initialized */ 
                     ctrl(divHolderID).innerHTML="No Connection..."; 
                     break;
                   case 1: /* 1 The request has been set up */ 
                     if( ! IsEmptyOrNull(divSendingID) ) /* check the divSendingID is needed  */
                     ctrl(divSendingID).innerHTML =  strSendingTemp; /*"start Connecting...";*/ 
                     break;
                   case 2: /* 2 The request has been sent*/ 
                     if( ! IsEmptyOrNull(divSendingID) ) /* check the divSendingID is needed  */
                     ctrl(divSendingID).innerHTML = strSendingTemp; /*"Connecting...";*/ 
                     break;
                   case 3: /* 3 The request is in process */ 
                     if( ! IsEmptyOrNull(divSendingID) ) /* check the divSendingID is needed  */
                     ctrl(divSendingID).innerHTML = strSendingTemp; /*"Sending...";*/ 
                     break;
                   case 4: /* 4 The request is complete */ 
                      ctrl(divHolderID).innerHTML=xmlHttp.responseText;  /* Fill the ajax result in holder div */
                      
                      if( ! IsEmptyOrNull(strExecJs) ) /* check if strExecJs is needed  */
                      {
                        try{
                            eval(strExecJs); /* execute the js string after the ajax result */
                        }catch(err){};
                      }
                      break; 

                };/* End of switch */

        };  /* End of stateChanged function */

              /*(Type  , Url            , Asynchronous) */ 
	xmlHttp.open("POST", strExecPageUrl , true);
	    /* 
	    Note: Asynchronous "true" means that script processing 
	         carries on after the send() method, without waiting for a response,
	         and "false" means that the script waits for a response before
	         continuing script processing.
	    */ 
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.setRequestHeader("Content-length", strParameters.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(strParameters);
}
/*---------------------------------------------------------------------------------------*/
