/*
Ajax.js v1.0, Wed Nov 01 10:25pm CST 2006
Copyright AllenPages.com 2006 (www.allenpages.com).

 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
 "Software"), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to
 the following conditions:
 
 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 Any third party links, software, and the like are subject to the terms 
 and conditions of that party. Permission to use any third party software
 must be obtained from that party, independant of this agreement.
 
 A special thanks to Yahoo! for making a handy scripting solution.
 Refer to http://developer.yahoo.com/yui/connection/ for more information.
*/

document.write('<script type="text/javascript" src="shared/yahooLib/utilities.js"></script>');

function Ajax2()
{
  this.async = async;

  /*
  * function async(myUrlWithArgs, postData, callbackFunction, failureFunction)
  * Parameters:
  *   myUrlWithArgs: The absolute or relative URL to the document you wish to retrieve.
  *   postData: The data you wish to send via the POST method. You must format this 
  *             in the proper URL encoded scheme. If you pass NULL instead, the method
  *             is switched to GET and no data will be sent. If you wish to use the GET
  *             method, pass the arguements through the myUrlWithArgs parameter.
  *   callbackFunction: The name and input argument of a function to be called when 
  *             the data has been retrieved. The function must take an argument to 
  *             receive the xml from the responce (i.e., function myFunction(xmlResponse){}).
  *             If the callbackFunction is not specified, it is assumed we are just pushing 
  *             data to the server and don't need anything else to happen.
  *   failureFunction: The name and input argument of a function to be called if the 
  *             data can not be retrieved. If the failureFunction is not specified, 
  *             a generic function will be called.
  *
  * Return: void
  *
  *
  * Example of use:
  * Say these functions are defined by the user
  * function loadData() 
  * { new Ajax().async("getData.jsp", null, "parseResults(reqResults)", "errorOccured(errorResults)"); }
  *
  * function parseResults(reqResults)
  * { 
  *   //parse the results
  * }
  *
  * function errorOccured(errorResults)
  * { 
  *   //parse the error
  * }
  *
  * Notice that the functions being called in the async function have the full name and 
  * argument. These are called through the "new Function(...)" class.
  *
  *
  *
  *
  *
  */
  function async(myUrlWithArgs, postData, callbackFunction, failureFunction, timeoutInMillis)
  { 
    var methodType = "";
    //check arguments
    if(myUrlWithArgs == null || myUrlWithArgs == "")
    { alert("FAILURE: No url specified. Please report this problem to us."); return; }
    
    if(postData == null)
    { methodType = "GET"; }
    else
    { methodType = "POST"; }
    
    if(callbackFunction == null)
    { 
      callbackFunction = genericAjaxCallbackFunction; 
    }
    
    if(failureFunction == null)
    { 
      failureFunction = genericAjaxFailureFunction; 
    }
    
    if(timeoutInMillis == null || isNaN(timeoutInMillis))
    { timeoutInMillis = (30*60*1000); } //30 minutes
    
    //build the URL
    var urlArgBaseSeparator = "&";
    if(myUrlWithArgs.indexOf("?") == -1)
    { urlArgBaseSeparator = "?"; }
        
    var dateMillis = (new Date()).getTime(); //force url to refresh, no caching
    var url = (myUrlWithArgs + urlArgBaseSeparator + "noCaching=" + dateMillis);
    
    var callback = 
    { 
      success: callbackFunction, 
      failure: failureFunction, 
      timeout: timeoutInMillis
    } 
    //alert(callback.success);
    
    YAHOO.util.Connect.asyncRequest(methodType, url, callback, postData);
  }

}


/** 
* Serialize a form into a format which can be sent as a GET string or a POST 
* content.It correctly ignores disabled fields, maintains order of the fields 
* as in the elements[] array. The 'file' input type is not supported, as 
* its content is not available to javascript. This method is used internally 
* by the submit class method. 
*/ 
function serializeForm(theform) 
{ 
  var els = theform.elements;
  var len = els.length;
  var queryString = "";
  this.addField = function(name,value) 
    { 
      if(queryString.length>0)
      { queryString += "&"; } 
      queryString += encodeURIComponent(name) + "=" + encodeURIComponent(value);
    };
    
  for(var i=0; i<len; i++)
  {
    var el = els[i];
    if(!el.disabled)
    { 
      switch(el.type)
      {
        case 'text': 
        case 'password': 
        case 'hidden': 
        case 'textarea': 
          this.addField(el.name,el.value);
          break;
        case 'select-one':
          if(el.selectedIndex>=0)
          { this.addField(el.name,el.options[el.selectedIndex].value); } 
          break; 
        case 'select-multiple': 
          for(var j=0; j<el.options.length; j++)
          { 
            if(el.options[j].selected) 
            { this.addField(el.name,el.options[j].value); }
          }
          break; 
        case 'checkbox': 
        case 'radio': 
          if(el.checked)
          { this.addField(el.name,el.value); }
          break;
      } 
    } 
  } 
  
  return queryString; 
};

function serializeFormAlert(theform)
{
  var params = serializeForm(theform);
  params = params.replace(/&/g,"\n&");
  alert(params);
}




/*
* function genericAjaxCallbackFunction()
* Parameters: none
* Return: void
*/
function genericAjaxCallbackFunction()
{
  //do nothing
}

/*
* function genericAjaxFailureFunction(reqResults)
* Parameters:
*   reqResults: We will get the HTTP error info.
*
* Return: void
*
*/
function genericAjaxFailureFunction(reqResults)
{
  alert("FAILURE: A problem was encountered.\nHTTP Error Code: " + reqResults.status);
}