
// Copyright: Babysoft. All Right Reserved

// Global Variables //
var A_H = null;
var BROWSER_TYPE = null; // Define Browser Type, 0: IE, 1: Others
var RESPONSE = null; // Contains the value return from server
var RESPONDING = new Array(); // associated array, check is the data is currently responding to avoid data overlap
var DATA_RECEIVE_ERROR_TIMEOUT = 30000; // How long should I expect the data to received, in milliseconds


// (Ajax) This function will initial the ajax handler and return it out
// Param1: null
// Return: the ajax handler
function ajax_init(){
  var a_h=null;
  try{
    // Firefox, Opera 8.0+, Safari
    a_h=new XMLHttpRequest();
    BROWSER_TYPE = '1';
  }catch (e){
    // Internet Explorer
    try{
      a_h=new ActiveXObject("Msxml2.XMLHTTP");
      BROWSER_TYPE = '0';
    }catch (e){
      a_h=new ActiveXObject("Microsoft.XMLHTTP");
      BROWSER_TYPE = '0';
    }
  }
  return a_h;
}



// This function defined how you wish to react on each state within the connection. State reference:
// 0: Not initialized, 1: Request has been setup, 2: Request sent, 3: Request in Process, 4: Request Complete
// Note: This function is only designed for non-IE use, for IE, use inline function.
function ajax_ff_state(){
  if (A_H.readyState == 4 && A_H.status == 200){ 
      RESPONSE = A_H.responseText;
  }
}



// This function will pass request to the ajax server and return the value back
// Note: this function required to first call ajax_init() before using it
// Param1: the url you wish to submit
// Param2: the get type parameter you wish to send, without leading ?
// Return: the value get from the ajax server when status is 4 (complete)
function ajax_send(url, param){

  var exitCounter = 0;

  if (A_H == null || BROWSER_TYPE == null){
    alert("Your current browser do not support this product.\nYou may consider use Firefox (Free download online) to use this product.");
    return false;
  }
  else{
     // Now, ready to submit
     try{
       A_H.open("POST",url,true); // Asyschronize
//       A_H.open("POST",url,false); // Syschronize
       // Send the proper header information along with the request
       A_H.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       A_H.setRequestHeader("Content-length", param.length);
       A_H.setRequestHeader("Connection", "close");

       // For Asyschronize Ajax //
       A_H.onreadystatechange = function(){
         if (A_H.readyState == 4 && A_H.status == 200){ 
           RESPONSE = A_H.responseText;
         }
       }
       A_H.send(param);
       // End for Asyschronize Ajax //

       // For Syschronize Ajax //
//       A_H.send(param);
//       RESPONSE = A_H.responseText;
       // End of Syschronize Ajax //
//       while( (RESPONSE == null || RESPONSE == undefined) ){}
     }catch(err){
       return null;
     }
  }
  return RESPONSE;
}



// Fully local ajax submit request and response from server
// Param1: the url you wish to submit
// Param2: the get type parameter you wish to pass
// Param3: (Opt.) is this a asynchronous request. true: asynchronous, false(default): synchronize
// Param4: (Opt.) the ID you wish to place the asynchronious response to. This parameter is required
//                if the request method is asynchronious, without this parameter, async request cannot
//                return. The id will be automatically ignore if the request method is synchronize.
//                Note: it will start try put in value field of the id, if not exist, then try innerHTML
// Return: the value get from the server
function ajax_connect(url, param, async, id){

  var xmlhandler = null;
  var response = null;

  if (async == undefined || async == null || async === false || async == ''){
    async = false;
  }else{
    async = true;
  }

  try{
    // Firefox, Opera 8.0+, Safari
    xmlhandler = new XMLHttpRequest();
    browser_type = '1';
  }catch (e){
    // Internet Explorer
    try{
      xmlhandler = new ActiveXObject("Msxml2.XMLHTTP");
      browser_type = '0';
    }catch (e){
      xmlhandler = new ActiveXObject("Microsoft.XMLHTTP");
      browser_type = '0';
    }
  }


  try{
    // For Asyschronize Ajax //
    if (async === true){

      // Set timeout for Ajax data receiving
      var dataTimeout = setTimeout(function(){
	xmlhandler.abort();
	// alert('Signal Aborted !!');
        clearTimeout(dataTimeout); // Data aborted, clear time out
      }, DATA_RECEIVE_ERROR_TIMEOUT);

      xmlhandler.onreadystatechange = function(){
        if (xmlhandler.readyState == 4 && xmlhandler.status == 200){ 
	  // alert ("Server Ready State: " + xmlhandler.readyState + " Status: " + xmlhandler.status);
          clearTimeout(dataTimeout); // Data received, clear time out
          response = xmlhandler.responseText;
          if (id != undefined && id != null && id != '' && document.getElementById(id) != null){
            if (document.getElementById(id).value != null){
	      document.getElementById(id).value = response;
	    }else if (document.getElementById(id).innerHTML != null){
	      document.getElementById(id).innerHTML = response;
            }
          }
        }else{
	    // alert ("Server Ready State: " + xmlhandler.readyState + " Status: " + xmlhandler.status);
	}
      };
    }
    // End for Asyschronize Ajax //

    // xmlhandler.open("POST",url,true); // Asyschronize
    // xmlhandler.open("POST",url,false); // Syschronize
    xmlhandler.open("POST",url,async); 
    // Send the proper header information along with the request
    xmlhandler.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhandler.setRequestHeader("Content-length", param.length);
    // if (browser_type == '1'){xmlhandler.setRequestHeader("Connection", "close");}
    xmlhandler.send(param);

    // For Synchronize Ajax //
    if (async === false){
      response = xmlhandler.responseText;
      return response;
    }
    // For Asynchronious Ajax //
    else{
      // Wait until the response return
    }
    // End of Syschronize Ajax //
  }catch(err){
    return null;
  }
}




// End Ajax Specialized Functions //





