/**
 * Copyright (c) 2009
 * TechEmpower, Inc.
 * All Rights Reserved.
 *
 * httpGET.js
 *
 * Development history:
 *   Jan 16, 2009 - jdavis - Created
 */
 
/**
 * HttpGET
 *
 * An HttpGET object that allows for simple ansynchronous (or synchronous) requests 
 * to be made to a server.
 *
 * This script DEPENDS ON ajaxconnector.js
 */
function HttpGET(url)
{
  // The url to connect to
  this.url = url;
  
  // A set of parameters to send
  this.parameters = new Object();
  
  // The AJAXConnector
  this.connector = null;
  
  // The AJAXResponse after the connection finishes.
  this.response = null;
  
  //
  // Sets the value of a paramter to be sent in the request.
  // Call this for each parameter desired.
  //
  this.setParameter = function(name, value)
  {
    if (name != undefined)
      this.parameters[name] = value;
  }
  
  //
  // Removes the given parameter from the request.
  //
  this.removeParameter = function(name)
  {
    this.setParameter(name);
  }
  
  //
  // Sends the request. An optional callback and callback invoker can be 
  // specified for processing the AjaxResponse, but the default behavior will 
  // simply store the result in the response member of the HttpGET. 
  // If wait is true then this will not return until a response is received.
  //
  this.send = function(callback, invoker, wait)
  {
    if (callback == undefined)
      callback = this.receive;
    
    if (invoker == undefined)
      invoker = this;

    this.response = null;
    this.connector = AjaxFactory.getConnector();
    
    var params = "";
    for (paramName in this.parameters)
    {
      if (params.length > 0)
        params += "&";
        
      params += paramName + "=" + this.parameters[paramName];
    }
    
    this.connector.setUrl(this.url);
    this.connector.setResponseCallback(callback, invoker);
    
    return this.connector.connect(false, params, wait);
  }
  
  //
  // Sends the request and waits for a response. An optional callback and 
  // callback invoker can be specified for processing the AjaxResponse, 
  // but the default behavior will simply store the result in the response 
  // member of the HttpGET. 
  // 
  this.wait = function(callback, invoker)
  {
    return this.send(callback, invoker, true);
  }
  
  //
  // The default callback stores the response in the invoker (the HttpGET)
  //
  this.receive = function(response, invoker)
  {
    if (invoker != undefined)
      invoker.response = response;
  }
}
