var req;
var elementId;
var successCallBack;
var failCallBack;
/**
*  This will load up the given url once it is loaded it will run the method processReqChange();
*/
function loadXMLDoc(url, mElementId, mSuccessCallBack, mFailCallBack, defaultURL)
{
  // branch for native XMLHttpRequest object
  elementId = mElementId;
  successCallBack = mSuccessCallBack;
  failCallBack = mFailCallBack;
  if (window.XMLHttpRequest)
  {
    req = new XMLHttpRequest();
    req.onreadystatechange = processReqChange;
    req.open("GET", url, true);
    req.send(null);
    // branch for IE/Windows ActiveX version
  }
  else if (window.ActiveXObject)
  {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req)
    {
      req.onreadystatechange = processReqChange;
      req.open("GET", url, true);
      req.send();
    }
  }
  else
  {
    //There is not XMLHttpRequest or ActiveXObject this must be a browser not supporting AJAX
    //alert("about to redirect to " + defaultURL);
    window.location = defaultURL;
  }
}

/**
*
* This is called once the response comes back from the server
*  Currently it takes the entire response and replaces the elementId.innerHtml with that response
*  Then it calls the successCallBack function if it is defined.
*
*/
function processReqChange()
{
  // alert("processReqChange start req" + req.readyState);
  // only if req shows "complete"
  if (req.readyState == 4)
  {
    // only if "OK"
    // alert("It is ok now : " + req.statusText);
    if (req.status == 200)
    {
      // alert(req.responseText);
      /*
        This will take the find a specified element and replace the innerHTML with the response text.
      */
      document.getElementById(elementId).innerHTML = req.responseText;
      if (successCallBack != undefined && successCallBack != null)
      {
        eval(successCallBack);
      }
    }
    else
    {
      alert("There was a problem retrieving the XML data:\n" + req.statusText);
      if (failCallBack != undefined && failCallBack !=null)
      {
        eval(failCallBack);
      }
    }
  }

}

 /**
   *
   * This will take in an object and create a name=value pair comma delimited and attach it to the
   * window.location.hash property.
   *
   * This can be useful to make the back button work.
   */
  function addHashToURL(hash)
  {
    var hashString = "";
    for (i in hash)
    {
      if (hashString != "")
      {
        hashString = hashString + ",";
      }
      hashString = hashString + i + "=" + hash[i];
    }
    if (hashString != "")
    {
      window.location.hash = hashString;
    }
  }
  /**
   *
   *This will return an object that contains a name value pair of the items in the
   * window.location.hash property.
   *
   * This can be useful to make the back button work.
   */
  function getURLHash()
  {
    var hash = new Object;
    var hashString = window.location.hash;
    if (hashString != undefined && hashString != "")
    {
      hashString = hashString.substring(hashString.indexOf("#") + 1);
      var hashArray = hashString.split(",");
      for (var i = 0; i < hashArray.length; i++)
      {
        var nameValue = hashArray[i].split("=");
        hash[nameValue[0]] = nameValue[1];
      }
    }
    return hash;
  }