// ============================================================================
// Module      : Ajax
//
// Author      : Andi Irwan 
//
// Application : Allianz FootPrints
//
// Date+Time of change   By    Description
// --------------------- ----- ------------------------------------------------
// january-2009 WIB Andi Irwan Creation of the program
//
// ============================================================================


function XHConn()
{
  var xmlhttp, bComplete = false;
  xmlhttp = XHRFactory.getInstance();
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          if(fnDone != null) fnDone(xmlhttp);
          XHRFactory.release(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}


var XHRFactory = (function(){
 // static private member
 var stack = new Array();
 var poolSize = 10;
 
 var nullFunction = function() {};
 
 
 function createXHR() {
  if (window.XMLHttpRequest) {
       return new XMLHttpRequest();
     } else if (window.ActiveXObject) {
       return new ActiveXObject('Microsoft.XMLHTTP')
     }
    }


 for (var i = 0; i < poolSize; i++) {
  stack.push(createXHR());
 }
 

 return ({
  release:function(xhr){
   xhr.onreadystatechange = nullFunction;
   stack.push(xhr);
  },
  getInstance:function(){
   if (stack.length < 1) {
    return createXHR();
   } else {
    return stack.pop();
   }
  },
  toString:function(){
   return "stack size = " + stack.length;
  }
 });
})();



var xmlHttp;

// START Ajax Request

function GetXmlHttpObject()
{
   var xmlHttp = null;
   
   try {
   xmlHttp = new XMLHttpRequest();  // firefox, opera, safari   
   }
   
   catch (e) {
      try { 
       xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP");   // internet explorer  6 
      }
	  catch (e) {
  	  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); // internet explorer other
	  }
   }
   
   return xmlHttp;
}

// OPEN AJAX

function OpenAjaxUrl( method, url, single,statechange )
{      
   xmlHttp = GetXmlHttpObject();

   if ( xmlHttp == null ) {
       alert ( "Your browser does not support ajax technology" )
	   return
   }
   
   if ( single != 1 )
   get_url = url + "&rand_id=" + Math.random();
   else
   get_url = url + "?rand_id=" + Math.random();
   
   xmlHttp.onreadystatechange = statechange;
   
      if (method == "GET")
      {
        xmlHttp.open(method, get_url ,true);
        get_url = "";
      }
      else
      {
        xmlHttp.open(method, get_url ,true);
        xmlHttp.setRequestHeader("Method", "POST "+get_url+" HTTP/1.1");
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      }
   
   
   
   xmlHttp.send(get_url);
   
}