/*  Ajax tools v1.0
Base Functions:
AjaxObject()     use:  var a=new AjaxObject(); returns a new ajax object with the following prototype:
     definable events:
      AjaxObject.complete(sc,st,rt,rxml)   function called when the AjaxObject state changes to "complete"  - use this to define a function which will parse the xml results
      AjaxObject.loading()     function called when the AjaxObject state changes to "Loading"  
      AjaxObject.loaded()     function called when the AjaxObject state changes to "Loaded"
      AjaxObject.interactive()    function called when the AjaxObject state changes to "interactive"
    methods:
      AjaxObject.abort()   stop the current request
      AjaxObject.call(url)   asynchronously request url (using GET)
      AjaxObject.form(url,form_id)  asynchronously request url (using POST) with name/value pairs form the form identified by form_id


Shortcut Function:
ajaxCallFunction(url,func)  creates a new AjaxObject (garbage collecting), sets the complete attribute to func and asynchronously requests url   
ajaxCall(url,target) creates a new AjaxObject (garbage collecting), asynchronously requests url and places the results into the document element idendtified by target.
     if the result is a valid xml document with one or more  <ajax_script> elements - the javascript within these elements will be executed (target will not be altered)
ajaxForm(form,target,url)  same as ajaxCall except posting information to the request url with name/value pairs from 'form'.  If url is not defined - the action of the form will be used
ajaxFormFunction(form,func,url)  same as ajaxCallFunction except posting information to the request url with name/value pairs from 'form'.  If url is not defined - the action of the form will be used

Other Tools:
el(id)  returns the document element with the given id
getTagValue(node,tag)  return the first child value of the first tag (identified by 'tag') within the document element node ('node')

*/
function AjaxObjectGen(){
  if(window.XMLHttpRequest){
    return new XMLHttpRequest();
  }else if(window.ActiveXObject){
    var msxmls=new Array( 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0',  'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP');
    for (var i=0; i<msxmls.length; i++){
      try{
        return new ActiveXObject(msxmls[i]);
      }catch(e){
      }
    }
  }
  throw new Error("Could not instantiate XMLHttpRequest");
}  

function AjaxObject(){
  this._xmlhttp=new AjaxObjectGen();
}

function AjaxObject_abort(){ 
  if(this._xmlhttp.readyState!=4) this._xmlhttp.abort();
}

function AjaxObject_call(url){
  var instance=this;
  this._xmlhttp.open('GET', url, true);
  this._xmlhttp.onreadystatechange=function(){
    switch(instance._xmlhttp.readyState){
      case 1:
        instance.loading();
        break;
      case 2:
        instance.loaded();
        break;
      case 3:
        instance.interactive();
        break;
      case 4:
        TARGET=instance._target;
        instance.complete(instance._xmlhttp.status, instance._xmlhttp.statusText, instance._xmlhttp.responseText, instance._xmlhttp.responseXML); 
        instance._done=1;    
        break;
    }
  }
  this._xmlhttp.send(null);
}

function AjaxObject_post(url,form){
  var instance=this;
  var parameters="";
  var x=el(form).getElementsByTagName("*");
  for(var i=0; i<x.length; i++){  
    var name=""; var val="";
    if(x[i].nodeName=='INPUT'){
      if(x[i].type=="checkbox"||x[i].type=="radio"){
        if(x[i].checked) name=x[i].name; val=x[i].value;
      }else  name=x[i].name; val=x[i].value;
    }
    if(x[i].nodeName=='SELECT'){
       name=x[i].name; val=x[i].options[x[i].selectedIndex].value;
    }
    if(x[i].nodeName=='TEXTAREA'){
       name=x[i].name; val=x[i].value;
    }
    if(name!=""){
      if(parameters=="") parameters=name+"="+escape(val);
      else parameters=parameters+"&"+name+"="+escape(val);
    }
  }
  this._xmlhttp.open('POST', url, true);
  this._xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  this._xmlhttp.setRequestHeader("Content-length", parameters.length);
  this._xmlhttp.setRequestHeader("Connection", "close");
  this._xmlhttp.onreadystatechange=function(){
    switch(instance._xmlhttp.readyState){
      case 1:
        instance.loading();
        break;
      case 2:
        instance.loaded();
        break;
      case 3:
        instance.interactive();
        break;
      case 4:
        TARGET=instance._target;
        instance.complete(instance._xmlhttp.status, instance._xmlhttp.statusText, instance._xmlhttp.responseText, instance._xmlhttp.responseXML); 
        instance._done=1;    
        break;
    }
  }
  this._xmlhttp.send(parameters);
}


function AjaxObject_loading(){ }
function AjaxObject_loaded(){ }
function AjaxObject_interactive(){ }
function AjaxObject_complete(status,statusText,repsonseText,responseHTML){ }

AjaxObject.prototype.loading = AjaxObject_loading;
AjaxObject.prototype.loaded = AjaxObject_loaded;
AjaxObject.prototype.interactive = AjaxObject_interactive;
AjaxObject.prototype.complete = AjaxObject_complete;

AjaxObject.prototype.abort = AjaxObject_abort;
AjaxObject.prototype.call = AjaxObject_call;
AjaxObject.prototype.form = AjaxObject_post;


function el(id){ return document.getElementById(id);}

function getTagValue(node,tag){
  if(node){
    var first_tag=node.getElementsByTagName(tag)[0];
    if(first_tag) if(first_tag.firstChild) return first_tag.firstChild.nodeValue;
    return "";
  }
}


function ajaxLoadXML(sc,st,rt,rxml){
  el(TARGET).onclick=null;
  el(TARGET).className="";  
  var x=rxml.getElementsByTagName('ajax_script');
  if(x.length>0){
    var XML=rxml;
    for(var i=0; i<x.length; i++){  
      var y=x[i].childNodes;
      for(var j=0; j<y.length; j++)   eval(y[j].nodeValue);
    }
  }else{
    el(TARGET).innerHTML=rt;
  }
}

// Global Functions 
var ajaxObjects = new Array();

function ajaxCallFunction(url,func){
  var j=ajaxObjects.length;
  for(var i=0; i<ajaxObjects.length; i++){
    if(ajaxObjects[i]._done==1){ j=i; break;}
  }
  ajaxObjects[j]= new AjaxObject();
  ajaxObjects[j]._target="";
  ajaxObjects[j].complete=func;
  ajaxObjects[j].call(url);  
  return ajaxObjects[j];  
}

function ajaxCall(url,target){
  el(target).onclick=Function("return false");
  el(target).className="loading";
  var j=ajaxObjects.length;
  for(var i=0; i<ajaxObjects.length; i++){
    if(ajaxObjects[i]._done==1){ j=i; break;}
  }
  ajaxObjects[j]= new AjaxObject();
  ajaxObjects[j]._target=target;
  ajaxObjects[j].complete=ajaxLoadXML;
  ajaxObjects[j].call(url);  
  return ajaxObjects[j];
}

function ajaxForm(form,target,url){
  el(target).onclick=Function(" return false");
  el(target).className="loading";  
  if(url==null) url=el(form).action;
  var j=ajaxObjects.length;  
  for(var i=0; i<ajaxObjects.length; i++){
    if(ajaxObjects[i]._done==1){ j=i; break;}
  }
  ajaxObjects[j]= new AjaxObject();
  ajaxObjects[j]._target=target;
  ajaxObjects[j].complete=ajaxLoadXML;
  ajaxObjects[j].form(url,form);  
  return false;
}

function ajaxFormFunction(form,func,url){
  if(url==null) url=el(form).action;
  var j=ajaxObjects.length;  
  for(var i=0; i<ajaxObjects.length; i++){
    if(ajaxObjects[i]._done==1){ j=i; break;}
  }
  ajaxObjects[j]= new AjaxObject();
  ajaxObjects[j].complete=func;
  ajaxObjects[j].form(url,form);  
  return false;
}
