/****************************************
 * 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();
   this.arraydelimiter  = ';';   
   
   // methods
   //
   this.Load               = aj_Load;
   this.setHandler         = aj_setHandler;
   this.setUrl             = aj_setUrl;
   this.setArrayDelimiter  = aj_setArrayDelimiter;
   this.handleHttpResponse = aj_handleHttpResponse;
 
   // initialise
   //
   this.setHandler(handler);
   this.setUrl(url);
}

function aj_Load(url, paramstring, method, data) {
   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');
      }
      //alert("posting "+this.url+ " " + paramstring);
      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_setArrayDelimiter(delimiter) { 
   this.arraydelimiter = delimiter;
}

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(this.arraydelimiter);
        
        // 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']);
        } 
     }
  }
}


function UpdateLB(lb,options,l)  {
   autoSelect=false;
//   alert(lb.id);
   while (lb.options.length > 1) {
      lb.options[1] = null;
   }
   if (options != "") {
      //alert(options.length);
      for (key in options) {
         parts = options[key].split("\|");
         //alert(options[key]+","+parts[0]+","+parts[1]);
         //alert ("add option: "+options[key]+"="+key);

         oOption =  document.createElement("OPTION");
         oOption.text = parts[1];
         oOption.value = parts[0];
         lb.options.add(oOption);
         //if (key == selOption)
         //   lb.options[lb.options.length-1].selected = true;
      }
   //   if (autoSelect && lb.options.length == 2)
   //      lb.options[1].selected = true;
   }
}

