// fAjaxCall Function provides both IE and Firefox ajax capabilities
/**
* common.js - Common functions that are used by numerous pages.
* 
* @author: Richard Taylor-Kenny
* @date: 5/15/2007 
* 
* 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;
 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.";
 try
 {
  var f = function ()
  {
   if(httpDoc.readyState==4)
   {
    if(hsh.fcallback != null)
    {
     hsh.fcallback(httpDoc);
    }
    else if(hsh.script != null)
    {
     try
     {
      eval(httpDoc.responseText);
     }
     catch(e){alert(e.description+"\r\n"+hsh.url+"\r\n"+httpDoc.responseText);}
    }
    else if(hsh.alert != null)
    {
     alert(httpDoc.responseText);
    }
    else if(hsh.xml != null)
    {
     document.getElementById(hsh.xml).xml=httpDoc.responseText;
    }
//    else if(hsh.dom != null)
//    {
//     document.getElementById(hsh.xml).xml=httpDoc.responseText;
//    }
    else if(hsh.elementid != null)
     document.getElementById(hsh.elementid).innerHTML=httpDoc.responseText;
    httpDoc = null;
   }
  };
  httpDoc.onreadystatechange = f;


  if(hsh.loginid)
   httpDoc.open((hsh.loginid || hsh.method==null)?"POST":hsh.method,hsh.url,false,hsh.loginid,'G0c@rdina1');
  else
   httpDoc.open(hsh.method==null?"POST":hsh.method,hsh.url,false);
  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.send(hsh.content);
  }
  else
   httpDoc.send();
  if(window.XMLHttpRequest)
  {
   f();
  }
 }
 catch(e)
 {
  return "Processing - Failure "+e.description;
 }
 return "";
}
