/*
This class provides client side access to the QueryString
*/

/*
** Start of instance members
*/

//Instance Constructor
//function QueryString() {
//  this.Keys = new Array();
//  this.values = new Array();
//  this.Url = "";
//  this.Parameters = "";
//  this.Location = "";
//}

function QueryString(key) {
  if (key != null && key != "undefined" && key != "") {
    //used for static value retrieval
    return __defaultQS.ValueByKey(key);
  }

  this.Keys = new Array();
  this.values = new Array();
  this.Url = "";
  this.Parameters = "";
  this.Location = "";
  return null;
}

//Parses the QueryString and stores the values
QueryString.prototype.Parse = function(url) {
  if (url == null || url == "undefined" || url == "") {
    return;
  }

  this.Url = url;
  this.Location = this.Url.replace("?", "");

  var init = this.Url.split("?");

  if (init.length > 1) {
    var pairs = init[1].split("&");
    for (var i = 0; i < pairs.length; i++) {
      var pos = pairs[i].indexOf('=');
      if (pos >= 0) {
        var argname = pairs[i].substring(0, pos);
        var value = pairs[i].substring(pos + 1);
        this.Keys[this.Keys.length] = argname;
        this.values[this.values.length] = value;
      }
    }
  }

  this.Parameters = this.GetParameters();

  if (this.Url.indexOf("?") > -1) {
    //there are params
    this.Location = this.Url.replace("?" + this.Parameters, "")
  }
}

//Returns the parameters contained in the QueryString
QueryString.prototype.GetParameters = function() {
  var params = "";

  for (var i = 0; i < this.values.length; i++) {
    if (i == 0) {
      params += this.Keys[i] + "=" + this.values[i];
    }
    else {
      params += "&" + this.Keys[i] + "=" + this.values[i];
    }
  }
  return params;
}

//Returns the Value for a given index
QueryString.prototype.ValueByIndex = function(index) {
  for (var i = 0; i < this.values.length; i++) {
    if (i == index) {
      return this.values[i];
    }
  }
  return null;
}

//Returns the Value for a given Key
QueryString.prototype.ValueByKey = function(key) {
  for (var i = 0; i < this.Keys.length; i++) {
    if (this.Keys[i] == key) {
      return this.values[i];
    }
  }
  return null;
}

//Sets the Value for a given Key
QueryString.prototype.SetValue = function(key, value) {
  for (var i = 0; i < this.Keys.length; i++) {
    if (this.Keys[i] == key) {
      this.values[i] = value;
      this.Parameters = this.GetParameters();
      return;
    }
  }
}

//Adds a new Value to the QueryString
QueryString.prototype.AddValue = function(key, value) {
  this.Keys[this.Keys.length] = key;
  this.values[this.values.length] = value;
  this.Parameters = this.GetParameters();
}

//Returns the Key for a given index
QueryString.prototype.KeyByIndex = function(index) {
  for (var i = 0; i < this.Keys.length; i++) {
    if (i == index) {
      return this.Keys[i];
    }
  }
  return null;
}

//Returns the Index for a given key
QueryString.prototype.KeyIndex = function(key) {
  for (var i = 0; i < this.Keys.length; i++) {
    if (this.Keys[i] == key) {
      return i;
    }
  }
  return -1;
}

//Removes the key for a given index
QueryString.prototype.RemoveByIndex = function(index) {
  this.Keys.splice(index, 1); //(index, #elements to remove)
  this.values.splice(index, 1);

  this.Parameters = this.GetParameters();
}

//Removes the Index for a given key
QueryString.prototype.RemoveByKey = function(key) {
  for (var i = 0; i < this.Keys.length; i++) {
    if (this.Keys[i] == key) {
      return this.RemoveByIndex(i);
    }
  }
  return null;
}

//Returns the full url
QueryString.prototype.ToString = function() {
  return this.Location + "?" + this.Parameters;
}

/*
** End of instance members
*/

/*
** Start of static members
*/
/* The 'static' constructor for this class */
var __defaultQS = new QueryString();
__defaultQS.Parse(window.location.href);


QueryString.URL = function() {
  return __defaultQS.Url;
}

QueryString.Location = function() {
  return __defaultQS.Location;
}

QueryString.Parameters = function() {
  return __defaultQS.Parameters;
}

QueryString.SetValue = function(p_Key, p_Value) {
  __defaultQS.SetValue(p_Key, p_Value);
}

QueryString.AddValue = function(p_Key, p_Value) {
  __defaultQS.AddValue(p_Key, p_Value);
}

QueryString.RemoveByKey = function(key) {
  __defaultQS.RemoveByKey(key);
}

QueryString.RemoveByIndex = function(index) {
  __defaultQS.RemoveByIndex(index);
}

QueryString.KeyIndex = function(key) {
  return __defaultQS.KeyIndex(key);
}

QueryString.Keys = function() {
  return __defaultQS.keys;
}

QueryString.ToString = function() {
  return __defaultQS.ToString();
}



/*
** End of static members
*/
