// fAjaxCall Function provides both IE and Firefox ajax capabilities
/*
Author :Richard Taylor-Kenny
Inputs : hsh hash or object
Outputs : String
 success - empty string or failure msg

hsh can contain in order of precedence
 fcallback - function to process the responseText
 script - indicating the responseText will be eval'd as script
 xml - xml is the elementid where the responseText will replace that element's xml
 elementid - the document's elementid innerHTML is the recipient of the responseText 
 Sample usages
  elementid - Updates the DOM element(dvSearchInfo).innerHTML is replaced with the responseText 
     var msg = fAjaxCall({url:"CitySearch.asp",elementid:"dvSearchInfo",content:"State=Oregon"});

  fcallback - The return from CitySearch.asp will call the included function  
     var msg = fAjaxCall({url:"CitySearch.asp",fcallback:function (req){alert(req.responseText);},content:"State=Oregon"});

  script - The responseText will be evaluated as javascript
     var msg = fAjaxCall({url:"CitySearch.asp",script:1,content:"State=Oregon"});
 
  alert - The responseText will be popped up in an alert box
     var msg = fAjaxCall({url:"CitySearch.asp",alert:1,content:"State=Oregon"});
 
  xml - Updates the DOM element(dvSearchInfo).xml  is replaced with the responseText 
     var msg = fAjaxCall({url:"CitySearch.asp",elementid:"dvSearchInfo",content:"State=Oregon"});
  
  (none of the above) No action is taken upon return.  
*/

function fAjaxCall(hsh)
{
 var httpDoc=null;
 var bufferStart=0;
 try{if(window.XMLHttpRequest){httpDoc=new XMLHttpRequest();}else httpDoc=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){httpDoc=new ActiveXObject("Microsoft.XMLHTTP");}
 if(!httpDoc)
  return "Processing - Failure Can't create service call. Microsoft Internet Explorer 6.0 or higher required.";
  var f = function ()
  {
   if(httpDoc.readyState==3 )
   {
    if(hsh.fcallback3 != null)
    {
     hsh.fcallback3(httpDoc,bufferStart);
     bufferStart=httpDoc.responseText.length;
    }
   }
   if(httpDoc.readyState==4 )
   {
    var req=httpDoc;
    if(hsh.script != null)
    {
     eval(httpDoc.responseText);
     return;
    }
    var script="";
    var s=0;
    var slen=0;
    if(req.responseText.indexOf("<script>")>0)
    {
     s=req.responseText.indexOf("<script>")+8;
     slen=req.responseText.substr(s).indexOf("</script>");
//      alert(s + " : " +slen+":"+req.responseText.substr(s,slen));
     if(slen < 0)
      alert(s + " : " +slen);
     else
      script=req.responseText.substr(s,slen);
    }
    if(hsh.fcallback != null)
    {
     if(script.length>bufferStart)
      bufferStart=s+slen;
     hsh.fcallback(httpDoc,bufferStart);
    }
    else if(hsh.alert != null)
    {
     alert(httpDoc.responseText.substr(s+slen));
    }
    else if(hsh.xml != null)
    {
     document.getElementById(hsh.xml).xml=httpDoc.responseText.substr(s+slen);
    }
//    else if(hsh.dom != null)
//    {
//     document.getElementById(hsh.xml).xml=httpDoc.responseText.substr(s+slen);
//    }
    else if(hsh.elementid != null)
     document.getElementById(hsh.elementid).innerHTML=httpDoc.responseText.substr(s+slen);
    if(script.length>0)
     eval(script);
    httpDoc = null;
   }
  };
 try
 {
  httpDoc.onreadystatechange = f;


  httpDoc.open(hsh.method==null?"GET":hsh.method,hsh.url,hsh.async!=null?hsh.async:true);
  if(hsh.method!="GET")
  {
   httpDoc.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   httpDoc.setRequestHeader("Content-length", hsh.content==null?0:hsh.content.length);
   httpDoc.setRequestHeader("Connection", "close");
   httpDoc.send(hsh.content);
  }
  else
   httpDoc.send();
  if(window.XMLHttpRequest)
  {
   f();
  }
 }
 catch(e)
 {
  return "Processing - Failure "+e.description;
 }
 return "";
}

