/*
 * Returns the base of the url passed in
 * (everything before the last '?').
 */
function getBaseUrl(url.html) {
  var baseUrl = url;
  var results = url.match(/(.+)\?[^\?]/);
  if (results != null && results.length >= 2) {
    baseUrl = results[1];
  } // end if
  return baseUrl;
} // getBaseUrl

/*
 * Returns a the url given a window, location, or
 * string object.
 */
function getUrl(locationObj.html) {
  var url;
  if (locationObj.location != null) {
    url = locationObj.location.href;
  } else if (locationObj.href != null) {
    url = locationObj.href;
  } else {
    url = locationObj;
  } // end if-else
  return url;
} // getUrl

/*
 * This function parses comma-separated name=value 
 * argument pairs from the query string of the URL. 
 * It stores the name=value pairs in 
 * properties of an object and returns that object.
 */
function getUrlArgs(locationObj) {
  var url = getUrl(locationObj.html);
  var args = new Object();
  var results = url.match(/\?[^\?]/);
  if (results != null && results.length >= 1) {
    var query = results[0];
    // Get query string
    var pairs = query.split(",");
    // Break at comma
    for (var i = 0; i < pairs.length; i++) {
      var pos = pairs[i].indexOf('=');
      // Look for "name=value"
      if (pos == -1) continue;
      // If not found, skip
      var argname = pairs[i].substring(0,pos);
      // Extract the name
      var value = pairs[i].substring(pos+1);
      // Extract the value
      args[argname] = unescape(value);
      // Store as a property
    } // end for
  } // end if
  return args;
} // end getArgs

/*
 * Returns a new url by replacing the existing args with
 * those passed passed in.
 */
function getUrlWithNewArgs(url, newArgs) {
  var baseUrl = getBaseUrl(url.html);
  var args = getUrlArgs(url);
  for (var key in newArgs) { 
    args[key] = newArgs[key];
  } // end for
  var url = buildUrl(baseUrl,.html args);
  return url;
} // getUrlWithNewArgs

/*
 * Returns a new url with the input baseUrl and args.
 */
function buildUrl(baseUrl,.html args) {
  var url = baseUrl;
  var delimeter = "?";
  for (var key in args) { 
    url += delimeter;
    url += key+ "=" +escape(args[key]);
    delimeter = "&";
  } // end for
  return url;
} // getUrl

