// <!--

/*********************************************************************************************************
  This variable holds the XMLHttpRequest objects calls.
  DEPENDS ON: none
*********************************************************************************************************/

//var req = null;

var req = new Array( );

/*********************************************************************************************************
  This variable holds the function that will be fired on the
  onreadystatechange event in ASYNCRONOUS XMLHttpRequest calls.
  DEPENDS ON: none
*********************************************************************************************************/

var processReqChangeError = function( sMsg ) { alert( sMsg ); };

/*********************************************************************************************************
  This function set the req var with the appropriate XMLHttpRequest Object
  DEPENDS ON: none
*********************************************************************************************************/
function getRequestObject( )
{
  if ( window.XMLHttpRequest )
  {
    req.push( new XMLHttpRequest( ) );
  }
  else if ( window.ActiveXObject )
  {
    try
    {
      req.push( new ActiveXObject( "Microsoft.XMLHTTP" ) );
    }
    catch( e )
    {
      try
      {
        req.push( new ActiveXObject( "Msxm12.XMLHTTP.4.0" ) );
      }
      catch( e )
      {
        try
        {
          req.push( new ActiveXObject( "Msxm12.XMLHTTP.3.0" ) );
        }
        catch(e)
        {
          try
          {
            req.push( new ActiveXObject( "Msxm12.XMLHTTP" ) );
          }
          catch(e)
          {
            req.push( false );
          }
        }
      }
    }
  }

  return req.length - 1;
}

/*********************************************************************************************************
  This function makes ASYNCRONOUS XMLHttpRequest calls using GET method.
  Pass the URL to the page the request should be made to.  You may pass an
  additional parameter that contains the name of the function to use as the
  onreadystatechange handler.  Generally this parameter will only be used
  when the returned data will not be in our standardized XML structure.  By
  default, the onreadystatechange event handler will be the processReqChange()
  function below.
  DEPENDS ON: getRequestObject, processRequestChange, your custom function
*********************************************************************************************************/
function loadXMLDoc( url )
{
  if (  debugAjax )
  {
    alert( 'loadXMLDoc: url: '+url );
  }

  var reqId = getRequestObject( );
  var reqIdString = reqId + '';
  var thisReq = req[reqId];

  if ( arguments.length == 1 )
  {
    req[reqId].onreadystatechange = Function( "processReqChange("+reqId+");" );
  }
  else
  {
    if ( debugAjax )
    {
      alert( 'loadXMLDoc: override state change function:\n' + arguments[1] );
    }

    req[reqId].onreadystatechange = Function( arguments[1]+"("+reqId+");" );
  }

  req[reqId].open( "GET", url, true );
  req[reqId].send( null );
  return thisReq;
}

/*********************************************************************************************************
  This function makes SYNCRONOUS XMLHttpRequest calls using GET method.
  Pass the URL to the page the request should be made to.  You may pass an
  additional parameter that contains the name of the function to use as the
  onreadystatechange handler.  Generally this parameter will only be used
  when the returned data will not be in our standardized XML structure.  By
  default, the onreadystatechange event handler will be the processReqChange()
  function below.
  DEPENDS ON: getRequestObject, processRequestChange, your custom function
*********************************************************************************************************/

function loadXMLDocSync( url )
{
  if ( debugAjax )
  {
    alert( 'loadXMLDocSync: url: ' + url );
  }

  var reqId = getRequestObject( );
  req[reqId].open( "GET", url, false );
  req[reqId].send( null );

  var thisReq = req[reqId];

  if ( req[reqId].status == 200 )
  {
    if  ( arguments.length == 1 )
    {
      processReqChange( reqId );
    }
    else
    {
      if( debugAjax )
      {
        alert( 'loadXMLDocSync: override state change function:\n' + arguments[1] );
      }

      eval( arguments[1] + '( ' + reqId + ' )' );
    }
  }
  else
  {
    alert( "There was a problem retrieving the data:\n" + req.statusText );
  }

  return thisReq;
}

/*********************************************************************************************************
  This function makes ASYNCRONOUS XMLHttpRequest calls using POST method.
  Pass the URL to the page the submittal should be made to.  Pass the id of
  the form whos data you will be submitting.  This id should be compatable
  with the getObjectFormat function above.  The data from the form will automatically
  be gathered and formatted for the data submittal.  You may pass an
  additional parameter that contains the name of the function to use as the
  onreadystatechange handler.  Generally this parameter will only be used
  when the returned data will not be in our standardized XML structure.  By
  default, the onreadystatechange event handler will be the processReqChange()
  function below.
  DEPENDS ON: getRequestObject,processRequestChange, your custom function
*********************************************************************************************************/
function postXMLDoc( url, formId )
{
  if ( debugAjax )
  {
    alert( 'postXMLDoc: url: ' + url );
  }

  var reqId = getRequestObject( );
  var thisReq = req[reqId];

  if ( req[reqId] && typeof req[reqId] != 'undefined' )
  {
    if ( debugAjax )
    {
      alert( 'Protocol = ' + window.location.protocol );
      alert( 'Host Name = ' + window.location.hostname );
      alert( 'Path Name = ' + window.location.pathname );
    }

    var refererURL = window.location.protocol + '//' + window.location.hostname;

    if ( window.location.port != '' && window.location.port != '80' )
    {
      refererURL = refererURL + ':' + window.location.port;
    }

    if ( debugAjax )
    {
      alert( 'postXMLDoc: refererURL:\n' + refererURL );
    }

    if ( arguments.length == 2 )
    {
      req[reqId].onreadystatechange = Function( "processReqChange("+reqId+");" );
    }
    else
    {
      if ( debugAjax )
      {
        alert( 'postXMLDoc: override state change function:\n' + arguments[1] );
      }

      req[reqId].onreadystatechange = Function( arguments[2]+"("+reqId+");" );
    }

    req[reqId].open( "POST", url, true );
    req[reqId].setRequestHeader( 'Referer', refererURL );
    req[reqId].setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    req[reqId].send( getFormFields( formId ) );
  }

  return thisReq;
}

/*********************************************************************************************************
  This function makes SYNCRONOUS XMLHttpRequest calls using POST method.
  Pass the URL to the page the submittal should be made to.  Pass the id of
  the form whos data you will be submitting.  This id should be compatable
  with the getObjectFormat function above.  The data from the form will automatically
  be gathered and formatted for the data submittal.  You may pass an
  additional parameter that contains the name of the function to use as the
  onreadystatechange handler.  Generally this parameter will only be used
  when the returned data will not be in our standardized XML structure.  By
  default, the onreadystatechange event handler will be the processReqChange()
  function below.
  DEPENDS ON: getRequestObject, processRequestChange, your custom function
*********************************************************************************************************/
function postXMLDocSync( url, formId )
{
  if ( debugAjax )
  {
    alert( 'postXMLDocSync: url: ' + url );
  }

  var reqId = getRequestObject( );
  var thisReq = req[reqId];

  if ( req[reqId] && typeof req[reqId] != 'undefined' )
  {
    var refererURL = window.location.protocol + '//' + window.location.hostname;

    if ( window.location.port != '' && window.location.port != '80' )
    {
      refererURL = refererURL + ':'  +window.location.port;
    }

    if ( debugAjax )
    {
      alert( 'postXMLDocSync: refererURL:\n' + refererURL );
    }

    req[reqId].open( "POST", url, false );
    req[reqId].setRequestHeader( 'Referer', refererURL );
    req[reqId].setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
    req[reqId].send( getFormFields( formId ) );

   if( arguments.length == 2 )
   {
     processReqChange( reqId );
   }
   else
   {
     if( debugAjax )
     {
       alert( 'postXMLDocSync: override state change function:\n' + arguments[2] );
     }

     eval( arguments[2] + '( ' + reqId + ' )' );
   }
  }

  return thisReq;

}

/*********************************************************************************************************
  This function is the default handler for all of our XMLHttpRequest calls and acts
  as the onreadystatechange event.  This function expects a standardized XML document
  to be returned.  It will dynamically pass the returned data to your custom response
  processor for handling.  Your custom response processor must be named
  [calledmethod]+Response and expect the resulting data as a parameter.
  DEPENDS ON: processReqChangeError
*********************************************************************************************************/
function processReqChange( reqId )
{
  // only if req shows "complete"
  if ( req[reqId].readyState == 4 )
  {
    // only if "OK"
    if ( req[reqId].status == 200 )
    {
      if ( debugAjax )
      {
        alert( 'processReqChange:\nResponse Text:\n' + req[reqId].responseText );
        alert( 'processReqChange:\nResponse XML:\n' + req[reqId].responseXML );
        alert( 'processReqChange:\nHeaders:\n\n' + req[reqId].getAllResponseHeaders( ) );
        alert( 'processReqChange:\nResponse Header: Content-Length:\n' + req[reqId].getResponseHeader( 'content-length' ) );
        alert( 'processReqChange:\nActual Content-Length:\n' + req[reqId].responseText.length );
      }

      var response  = req[reqId].responseXML.documentElement;
      var method    = response.getElementsByTagName( 'method' );

      if ( method.length>0 )
      {
        method    = method[0].firstChild.data;
        var result    = response.getElementsByTagName( 'result' )[0];
        eval( method + 'Response( result )' );
        req[reqId] = null;
      }
      else
      {
        processReqChangeError( "There was a problem interpreting the XML data:\n" + req[reqId].responseText );
        req[reqId] = null;
      }
    }
    else
    {
      processReqChangeError( "There was a problem retrieving the XML data:\n" + req[reqId].statusText );
      req[reqId] = null;
    }
  }
}

function processReqChangeCustom( reqId, functionName )
{
  eval( functionName + '(' + reqId + ')' );
}

/*********************************************************************************************************
  This function is called from the postXMLDoc function above.  It will retrieve the data from
  the form id provided by using the getObjectFormat function above.  Then it will process that data
  into name=value pairs seperated by the & symbol.  This is suitable for use in GET or PUT opperations
  DEPENDS ON: getObjectFormat
*********************************************************************************************************/
function getFormFields( formId )
{
  var frm = getObjectFormat( formId );
  var e = frm.elements.length;
  var ret = '';
  var eo = '';
  var et = '';
  var er = '';

  for ( var i = 0; i < e; i++ )
  {
    eo = frm.elements[i];
    et = eo.type;

    switch ( et )
    {
      case 'button':
      case 'reset':
      case 'submit':
          // do nothing as we do not need to submit these input types with the form data
        break;

      case 'checkbox':
      case 'radio':
          if ( eo.checked )
          {
            if ( ret != '' )
            {
              ret += '&';
            }

            ret += encodeURIComponent( eo.name ) + '=' + encodeURIComponent( eo.value );
          }
        break;

      case 'select-one':
      case 'select-multiple':
          for ( var j=0; j<eo.options.length; j++ )
          {
            if ( eo.options[j].selected )
            {
              if ( ret != '' )
              {
                ret += '&';
              }

              ret += encodeURIComponent( eo.name ) + '=' + encodeURIComponent( eo.options[j].value ); // || eo.options[j].text );
            }
          }
        break;

      case 'file': // will only be submitted as path info
      case 'hidden':
      case 'password':
      case 'text':
      case 'textarea':

      default:
          if ( ret != '' )
          {
            ret += '&';
          }

          ret += eo.name + '=' + escape(eo.value);
        break;
    }
  }

  return ( ret == '' ) ? null : ret;
}
// -->

