AddNamespace("DSS.Web");

DSS.Web.URLTool = function()
    {
    this.URL = null;
    this.Paras = [];
    
    this.Init = function()
        {
        if (location.href.indexOf("?") != -1)
            {
            urlquery = location.href.split("?");
            this.URL = urlquery[0];
            
            var hadParameters = false;
            var urlquery = "";
            var rUrlTerms = [];
            var pairs = [];
            if (location.href.indexOf("?") != -1)
                {
                urlquery = location.href.split("?");
                hadParameters = true;
                rUrlTerms = urlquery[1].split("&");
                }
                
            for (var i = 0; i < rUrlTerms.length; i++)
                {
                var ind = this.Paras.length;
                this.Paras[ind] = new Array();
                this.Paras[ind][0] = rUrlTerms[i].split("=")[0];
                this.Paras[ind][1] = rUrlTerms[i].split("=")[1];
                }
            }
        else
            {
            this.URL = location.href;
            }
        };
    this.Init();
    
    this.SetParameter = function(key, val)
        {
        if (this.Paras.length > 0)
            {
            var found = false;
            for (var i = 0; i < this.Paras.length && !found; i++)
                {
                var itemName = this.Paras[i][0];
                var itemvalue = this.Paras[i][1];
                if (itemName == key)
                    {
                    found = true;
                    this.Paras[i][1] = val;
                    }
                }
            if (!found)
                {
                var ind = this.Paras.length;
                this.Paras[ind] = new Array();
                this.Paras[ind][0] = key;
                this.Paras[ind][1] = val;
                }
            }
        else
            {
            this.Paras[0] = new Array();
            this.Paras[0][0] = key;
            this.Paras[0][1] = val;
            }
        };
        
    this.GetParameter = function(key)
        {
        for (var i = 0; i < this.Paras.length; i++)
            {
            var itemName = this.Paras[i][0];
            var itemValue = this.Paras[i][1];
            if (itemName.toLowerCase() == key.toLowerCase())
                {
                return this.Paras[i][1];
                }
            }
        return null;
        };
        
    this.RemoveParameter = function(key)
        {
        var foundIndex = -1;
        for (var i = 0; i < this.Paras.length && foundIndex == -1; i++)
            {
            var itemName = this.Paras[i][0];
            var itemValue = this.Paras[i][1];
            if (itemName.toLowerCase() == key.toLowerCase())
                {
                foundIndex = i;
                }
            }
            
        var newParas = [];
        for (var i = 0; i < foundIndex; i++)
            {
            newParas[i] = this.Paras[i];
            }
        for (var i = (foundIndex + 1); i < this.Paras.length; i++)
            {
            newParas[i - 1] = this.Params[i];
            }
        this.Paras = newParas;
        };
        
    this.ToString = function()
        {
        var query = "";
        for (var j = 0; j < this.Paras.length; j++)
            {
            query += this.Paras[j][0] + "=" + this.Paras[j][1];
            if (j != this.Paras.length - 1)
                {
                query += "&";
                }
            }
        return this.URL + "?" + query;
        }
    };