/**
* @projectDescription  querystring  library
*
* @author   Ryan Estes  estes.ryan@gmail.com
* @version  0.3
*/

/**
* Create a new instance of querystring
*
* @classDescription        Defines a querystring
* @return {coordinate}     Returns a new querystring.
* @type {Object}
* @constructor             optional argument
*/

function querystring()
{
   this._obj = new Object();
   if(arguments.length == 1)
   {
      var str = arguments[0].replace(/\+/g, " ");
      if(str.indexOf("?") == 0)
      {
         str = str.substring(1,str.length);
      }
      var arr = str.split("&");
      for(var i = 0; i < arr.length; i++)
      {
         var p = arr[i].split("=");
         if(p.length == 2)
         {
            this._obj[unescape(p[0])] = unescape(p[1]);
         }
      }
   }

   /**
   * Adds an entry with the specified
   * name and value.
   * @return {void}
   */
   this.add = function(pKey,pValue) {
     if(pKey != null && pValue != null)
     {
        this._obj[pKey] = pValue;
     }
   }

   /**
   * Removes all entries
   * @return {void}
   */ 
   this.clear = function() {
      this._obj = new Object();
   }
   
   /**
   * Executes a function while
   * iterating through the entries
   * @return {void}
   */ 
   this.enumerate = function(f) {
      for(k in this._obj)
      {
         f(k,this._obj[k]);
      }
   }

   /**
   * Gets the value at the specified name
   * @return {string}
   */ 
   this.get = function(pKey) {
      return this._obj[pKey];
   }

   /**
   * Gets the number of key/value pairs
   * @return {int}
   */  
   this.getCount = function() {
      return this.getKeys().length;
   }
   
   /**
   * Gets all keys
   * @return {string[]}
   */ 
   this.getKeys = function() {
      var arr = new Array();
      var f = function() {
         arr.push(arguments[0]);
      }
      this.enumerate(f);
      return arr;
   }

   /**
   * Gets all values
   * @return {string[]}
   */  
   this.getValues = function() {
      var arr = new Array();
      var f = function() {
         arr.push(arguments[1]);
      }
      this.enumerate(f);
      return arr;
   }
   
   /**
   * Returns a value indicating whether
   * or not the key exists.
   * @return {bool}
   */  
   this.hasKey = function(pKey) {
      return this._obj[pKey] != null;
   }
   
   /**
   * Returns a value indicating whether
   * or not keys exist.
   * @return {bool}
   */ 
   this.hasKeys = function() {
       return this.getKeys().length > 0;
   }
   
   /**
   * Removes an entry
   * @return {void}
   */  
   this.remove = function(pKey){
      this._obj[pKey] = null;
   }
   
   /**
   * Returns a string representaton of 
   * the querystring, a series of key/value 
   * pairs separated by an ampersand (&).
   * @return {string}
   */  
   this.toString = function() {
      if(this.getCount() == 0)
      {
         return "";
      }
      var str = "?";
      var fnc = function() {
         str += (escape(arguments[0]) 
                   + "=" 
                   + escape(arguments[1]) 
                   + "&");
      }
      this.enumerate(fnc);
      str = str.replace(/" "/g,"+")
      str = str.substring(0,str.length - 1);
      return str;
   }
}
