/*
INSTRUCTIONS FOR QUERY STRING FUNCTIONS

1. It's recommended that you place this script in a separate
   file named "queryString.js". Then link to it with
   a the below code.
   <script language="javascript" type="text/javascript" src="queryString.js">
   </script>

2. Next, start a separate <script></script> block that will
   call the actual queryString functions.

3. Prototypes of the queryString functions.
      stripQuery([URLString|LocationObject]) //returns value of the search
                                       //part of the location
      queryString(key, [URLString|LocationObject])
      formatQuery(keyArray,valueArray,[URLString])

Author: TUCOWS
Version: 1.7.1
*/

// Globals //////////////////////////////////////////////////////////////////

var __gbl_qstr = stripQuery(this.location);

// End of Globals ///////////////////////////////////////////////////////////


/* Given a key name in the query string, returns the
 * value it is set to.  Returns null if the variable is
 * undefined.
 *
 * `src' is optional, uses the address of the current frame
 * by default.  Accepts src as a URL in string format or a
 * Location object.
 *
 * This script does recognize the '+' as a space.
 */
function queryString(key, src) {
  var __qstr = stripQuery(src);
  var strIndex = __qstr.indexOf(key+'=');
  if(strIndex == -1) return null;
  var strReturn = '', ch = '';

  for(var i = strIndex + key.length; i < __qstr.length; i++) {
    ch = __qstr.charAt(i);
    if(ch == '&' || ch == ';') break;
    if(ch == '+') strReturn += ' ';
    else if(ch != '=') strReturn += ch;
  }
  return unescape(strReturn);
}


/* Given the source address (optional), returns the query string portion
 * of the address.  If the source address is not given, returns
 * the query string portion for current window/frame.
 *
 * Accepts src as a URL in string format or a Location object.
 */
function stripQuery(src) {
  if(src == null) return __gbl_qstr;

  if(typeof src == 'string') {
    var __qstr   = new String();
    var __tmpNum = src.indexOf('?');
    
    __qstr = (__tmpNum != -1)
             ? src.substr(
                 __tmpNum + 1, src.length
               )
             : null;
    
    delete __tmpNum;
    return __qstr;
  }
  else if(typeof src == 'object') { // assumes the object is of type location
    return location.search.substr(1, location.search.length);
  }
  else return __gbl_qstr;
}


/* Given an array of keys, an array of values, and a destination
 * address (optional), builds the query string on the end of
 * the destination address.  Any elements in the `valueRay'
 * beyond the length of the `keyRay' will be ignored.
 *
 * If the destination address is not passed, just builds the
 * query string.
 *
 * Elements in the `keyRay' should only be letters and numbers.
 *
 * Returns the new address.
 */
function formatQuery(keyRay, valueRay, destAddr) {
  var newAddr = ((destAddr == null) ? '' : destAddr) + '?';
  for(var i = 0; i < keyRay.length; i++) {
    newAddr += keyRay[i] + "=";
    if(valueRay[i] != null) newAddr += escape(valueRay[i]);
    if(i+1 < keyRay.length) newAddr += '&';
  }
  return newAddr;
}
