/****************************************
 * File: AjasxObject.js
 * Purpose: Request for a webpage while keeping the current page active  
 * 
 * How to use:
 * ===========
 * Include this in your page:
      <script type="text/JavaScript"    Language="JavaScript" src="../js/AjaxObject.js"></script>
      <script type="text/JavaScript"    Language="JavaScript">
         // maak een nieuw AjaxObject aan
         //
         MyAjax = new AjaxObject('MyAjax', '../php/getCityState.php', 'MyAjaxHandler');
         function MyAjaxHandler(responsetext, responsearray) {
            alert(responsetext);
         }
      </script>
    
 *
 *  If you want to load the page, use:
     MyAjax.Load('?param1=value1&param2=value2'); // parameters are optional
 *   
 * 
 ****************************************/  


function getHTTPObject() {
   var xmlhttp;
   /*@cc_on
     @if (@_jscript_version >= 5)
     try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
       try {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (E) {
          xmlhttp = false;
       }
     }
     @else
        xmlhttp = false;
     @end @*/

   if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
      try {
         xmlhttp = new XMLHttpRequest();
      } catch (e) {
         xmlhttp = false;
      }
   }

   return xmlhttp;
}

function AjaxObject(name, url, handler) {
   // Properties
   //
   //alert ("new ajaxobj");
   this.name            = name;
   this.http            = getHTTPObject(); // We create the HTTP Object
   this.url             = null; // The server-side script
   this.responsehandler = null; // The function that handles the response
   this.isProcessing    = false;
   this.data            = '';
   this.queue           = Array();
   
   // methods
   //
   this.Load               = aj_Load;
   this.setHandler         = aj_setHandler;
   this.setUrl             = aj_setUrl;
   this.handleHttpResponse = aj_handleHttpResponse;
 
   // initialise
   //
   this.setHandler(handler);
   this.setUrl(url);
}
function aj_Load(url, paramstring, method, data) {
   var postdata;
   if (!this.isProcessing && this.http) {
      this.url = url;
      if(data) {
         this.data = data;
      }
      if(typeof method=='undefined') {
         method = 'GET';
      }
      if(method == 'POST') {
         postdata    = paramstring;
         paramstring = '';
      }else {
         postdata    = null;
      }
      if(typeof paramstring == 'undefined') {
         paramstring = '';
      }
      if(paramstring != '') {
         paramstring = '?'+paramstring;
      }
      this.http.open(method, this.url + paramstring, true);
      this.http.onreadystatechange = new Function(this.name+'.handleHttpResponse()');
      this.isProcessing = true;
      if(method == 'POST') {
         this.http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        // this.http.setRequestHeader('Content-Type', 'application/unknown');
      }
      
      // Wanneer een + wordt ontvangen door PHP denkt php dat dit een spatie is
      // urldecode vervangt deze dus door een spatie. Om dit te voorkomen 
      // maken we er __PLUS van, en aan de serverkant replacen we dit weer met +
      // (wanneer de hele string escaped wordt zijn de & en de = ook escapecharacters 
      // geworden, deze replacen we daarom ook tijdelijk...
      //
      if(postdata != null) {
         postdata = postdata.replace(/\=/ig,'__EQUALS');
         postdata = postdata.replace(/\+/ig,'__PLUS');
         postdata = postdata.replace(/€/ig,'__EURO');
         postdata = postdata.replace(/™/ig,'__TM');
         postdata = postdata.replace(/’/ig,'__QUOTE');
         postdata = escape(postdata.replace(/&/ig,'__AMP'));
         
         // na de escape terugplaatsen
         //
         postdata = postdata.replace(/__EQUALS/ig,'=');
         postdata = postdata.replace(/__AMP/ig,'&');
         postdata = 'isAjaxObject=true&'+postdata;
      }
      if((typeof(globals) != 'undefined') && (globals['ID_session'] != 'undefined')) {
         postdata = 'ID_session=' + globals['ID_session']+'&'+postdata;
      }      
      //alert("posting "+this.url+ " " + postdata);
      this.http.send(postdata);
      //alert("done posting "+this.url);
   }else {
	
     var temp = Array();
     temp['url'] = url;
     temp['paramstring'] = paramstring;
     temp['method']		 = method;
     temp['data']	     = data;
     this.queue[this.queue.length] = temp;
        // alert("queue is now "+this.queue.length+" state = "+this.http.readyState);
   }
}
function aj_setHandler(responsehandler) {
   this.responsehandler = responsehandler;
}
function aj_setUrl(url) {
   this.url = url;
}
function aj_handleHttpResponse() {
//alert("aj_handleHttpResponse "+this.http.readyState);
  if(this.http.readyState == 4) {
     if(this.http.responseText.indexOf('invalid') == -1) {
        // Split the comma delimited response into an array
        responsearray = this.http.responseText.split(",");
        
        // We'll put the scripts in an array, so the user can execute the script after loading
        //
        this.script = '';
        var a = this.http.responseText.search(/\<script /ig);
        if(a > -1) {
           var b = this.http.responseText.search(/\<\/script>/ig);
           this.script = this.http.responseText.substring(a,b+9);
        }
        eval(this.responsehandler+'(this.http.responseText, responsearray, this.data, this.script);');
        this.isProcessing = false;
        //alert("end of response handler");
        if(this.queue.length > 0) {
           var temp = this.queue.pop();
           this.http  = getHTTPObject(); // We create the HTTP Object
           this.Load(temp['url'], temp['paramstring'], temp['method'], temp['data']);
        } 
     }
  }
}

var loadedAjaxObject = true;
