﻿/* initial xmlhttp connection object (cross-platform) */

function XMLHTTPInit()
{
   var xmlhttp = false;

   try
   {
      /* new IE */
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch(e)
   {
      try
      {
         /* old IE */
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(E)
      {
         /* reset for non-IE */
         xmlhttp = false;
      }
   }
   if (!xmlhttp && typeof XMLHttpRequest!='undefined')
   {
      /* non-IE */
      xmlhttp = new XMLHttpRequest();
   }
   
   return xmlhttp;
}


/* open a synchronized connection and get data. the 'postData' parameter
   is optional, if call want to use 'GET' method to open a http request.
   in another word, the function will use "POST" method to open a http
   request, if the 'postData' parameter is not null value.
*/

function XMLHTTPSyncResult(xmlhttp, url, postData)
{
   if(null != postData)
   {
      xmlhttp.open("POST", url, false);
      xmlhttp.setRequestHeader("Content-Type", 
                               "application/x-www-form-urlencoded; charset=UTF-8");
      xmlhttp.send(postData);
   }
   else
   {
      xmlhttp.open("GET", url, false);
      xmlhttp.send(null);
   }

   if(null == xmlhttp.status)
      return null;
   else if(200 != xmlhttp.status)
      return "";
   else
      return xmlhttp.responseText;
}

/* open a asynchronized connection and get data. A call-back function must
   be assigned to use this function. when a connection is return
   (by evet trigger), the call-back function will be executed.    
   the real function interface must be "func(result, param)"
   if the xmlhttp respond 200 OK, the 'result' will be the result data,
   else the 'result' will be null. the 'param' is the user assign data,
   it can be any variable type, and will always be assigned to the 
   call-back function, no matter xmlhttp respond 200 OK or not.  
*/
function XMLHTTPAsyncResult(xmlhttp, url, func, param, postData)
{
   /* bind event on ready status change (use a inner function object) */   
   xmlhttp.onreadystatechange = 
      function( )
      {
         if(xmlhttp.readyState != 4)
            return false;
            
         /* if the respond status is 200 OK, then call the call-back, 
            pass the result data, and the user assigned parameter 
            else, pass null instead of the resultdata */
         /* diatango, 070725, use try-cache to fix NS_ERROR_NOT_AVAILABLE for FF */
         try
         {
            if(null == xmlhttp.status)
               func(null, param);
            else if(xmlhttp.status != 200)
               func("", param);
            else
               func(xmlhttp.responseText, param);
         }
         catch(err)
         {            
            return false;
         }
         
         return true;
      }

   if(null != postData)
   {
      xmlhttp.open("POST", url, true);
      xmlhttp.setRequestHeader("Content-Type", 
                               "application/x-www-form-urlencoded; charset=UTF-8");
      xmlhttp.send(postData);
   }
   else
   {
      xmlhttp.open("GET", url, true);
      xmlhttp.send(null);
   }

   return true;
}

/* escape '%', '&', '+', '=', and '?' in the value string */
function XMLHTTPEscPostValue(value)
{
  var re = new RegExp();
  var arr = null;
  var escStr = "";
  
  re.compile("([^%&\+=\?]*)([%&\+=\?])([^%&\+=\?]*)", "g");  
  
  while((arr = re.exec(value)) != null)
  {
     for(var i = 1; i < arr.length; i++)
     {
        switch(arr[i])
        {
            case "":
               break;
            case "%":
               escStr += "%25";
               break;
            case "&":
               escStr += "%26";
               break;
            case "+":
               escStr += "%2b";
               break;
            case "=":
               escStr += "%3d";
               break;
            case "?":
               escStr += "%3f";
               break;
            default:
               escStr += arr[i];
               break;
        }
     }
   }
   if(escStr == "")
      escStr = value;
   return escStr;
}

