///////////////////////////////////////////////////////////////////////////////
// RegisterOnLoadFunction()
// if a page, control, etc wants to run any initialization at run time, they
// should write an initialization function, then register it using this method.
// the reason for this is so that one control's onload override doesn't
// overwrite anothers.
///////////////////////////////////////////////////////////////////////////////
function RegisterOnLoadFunction(AppendedOnLoadFn) {
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", AppendedOnLoadFn, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", AppendedOnLoadFn );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
          oldOnload( e );
          AppendedOnLoadFn();
      };
    }
    else {
      window.onload = AppendedOnLoadFn;
    }
  }
}
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// PostData object
///////////////////////////////////////////////////////////////////////////////
function PostXmlDataCtrl(strDestination)
{
  this.Destination = strDestination;
  this.AddHeader = _AddHeader;
  this.Post = _Post;
  this.OnStateChange = _OnStateChange;
  this.ProcessResponsePost = _ProcessResponsePost;
  this.ProcessAsyncResponsePost = _ProcessAsyncResponsePost;
  
  var maHeaders = new Array();
  var objHTTP = null;
  var asyncPostProcessing = null;
  
  /////////////////////////////////////////////////////////////////////////////
  function _AddHeader(strName, strValue)
  {
    var objHeader = new _Header(strName, strValue)
    maHeaders[maHeaders.length] = objHeader;
  }
  /////////////////////////////////////////////////////////////////////////////
  
  /////////////////////////////////////////////////////////////////////////////
  function _Header(strName, strValue)
  {
    this.Name   = strName;
    this.Value  = strValue;
  }
  /////////////////////////////////////////////////////////////////////////////
  
  /////////////////////////////////////////////////////////////////////////////
  function _Post(strData, bAsync, strDestination)
  {
    if (arguments.length < 1) {
      strData = "";
    }

    if (arguments.length < 2)
      bAsync = false;

    if (arguments.length < 3)
    {
      if (typeof(this.Destination) == "undefined")
        throw new Error(-1, "No destination supplied.")
      else
        strDestination = this.Destination;
    }

    try
    {
      // open a connection to the page
      // var objHTTP = null;
      if ( window.XMLHttpRequest ) {
        // firefox, etc.
        objHTTP = new XMLHttpRequest();
      }
      else {
        // ie
        objHTTP = new ActiveXObject("Msxml2.XMLHTTP.3.0");
      }
      
      if ( bAsync ) {
        objHTTP.onreadystatechange = this.OnStateChange
      }

      objHTTP.open("POST", strDestination, bAsync);
      objHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
      // add any request headers
      if (maHeaders != null)
      {
        for (var i=0; i < maHeaders.length; i++)
        {
          objHTTP.setRequestHeader(maHeaders[i].name, maHeaders[i].value);
        }
      }

      // send the xml
      objHTTP.send(strData);

      if ( !bAsync ) {
        // get the response back
        return objHTTP.responseText;
      }
    }
    catch (e)
    {
      throw e;
    }
  }
  /////////////////////////////////////////////////////////////////////////////
  
  /////////////////////////////////////////////////////////////////////////////
  function _OnStateChange()
  {
    // if xmlhttp shows "loaded"
    if (objHTTP.readyState == 4)
    {
      // if "OK"
      if (objHTTP.status==200)
      {
        var arrResp = objHTTP.responseText.split("|DELIM|");
        if ( arrResp[0] != null && document.getElementById(arrResp[0]) != null ) {
          document.getElementById(arrResp[0]).innerHTML = arrResp[1];
        }
        
        if (asyncPostProcessing != null ) {
          eval(asyncPostProcessing);
        }
      }
      else
      {
        throw "Problem retrieving XML data";
      }
    }
  }
  /////////////////////////////////////////////////////////////////////////////
  
  /////////////////////////////////////////////////////////////////////////////
  function _ProcessResponsePost(strPostProcessing)
  {
    var strResp = this.Post();
    var arrResp = strResp.split("|DELIM|");
    if ( arrResp[0] != null && document.getElementById(arrResp[0]) != null ) {
      document.getElementById(arrResp[0]).innerHTML = arrResp[1];
    }
    
    if ( arguments.length > 0 && strPostProcessing != null ) {
      eval(strPostProcessing);
    }
  }
  /////////////////////////////////////////////////////////////////////////////

  /////////////////////////////////////////////////////////////////////////////
  function _ProcessAsyncResponsePost(strPostProcessing)
  {
    asyncPostProcessing = strPostProcessing;
    this.Post("", true);
  }
  /////////////////////////////////////////////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////
