
function Url(uri) {

    this.uri_array = new Array();

    //if no URI is passed, use the current URI.
    if (typeof(uri) == 'undefined') {
        uri = window.location.href;
    }

    // Change to support HTTPS
    if( ! uri.match(/http(s)*:\/\//g)) {
        this.page_parts = new Array('', uri);
    } else {
        if(uri.indexOf('?') > 0) {
            this.page_parts = uri.split('?');
        } else {
            this.page_parts = new Array(uri, '');
        }
    }

    //split the URI into its key/var pairs
    this.uri_tmp_array = this.page_parts[1].split('&');


    //loop through the pairs splitting on equals
    for(i=0; i < this.uri_tmp_array.length; i++) {
        pair_array = this.uri_tmp_array[i].split('=');

        //automatic array splitting removed to provide ability to address simplebrowse variables
        if(typeof(pair_array[1])=='undefined'){
            pair_array[1] = '';
        }
        this.uri_array[pair_array[0]] = pair_array[1].trim('#');
    }

    delete(this.uri_array['msg']);

    this.isVar = function (key) {
        if(typeof(this.uri_array[key]) != 'undefined') {
            return true;
        } else {
            return false;
        }
    }


    this.go = function()
    {
        try {
            location.href = this.getUrl();
        } catch (e) {
        }
    }


    this.getUrl = function() {
        uri = '';
        for (key in this.uri_array) {
            if (typeof(this.uri_array[key]) == 'function') continue;
            if(key.length > 0) {
                if(uri.length > 0) {
                    uri = uri + '&';
                }
                if(typeof(this.uri_array[key]) == 'object') {
                    for (sub_key in this.uri_array[key]) {
                        if (typeof(this.uri_array[key][sub_key]) == 'function') continue;
                        if((uri.substr(uri.length-1,uri.length) != '&') && (uri.length > 1)) {
                            uri = uri + '&';
                        }
                        // this was removed about when .replace() below was added, but is still needed for when an
                        // array is passed as a val to this.setVar(). for an example, see simplebrowse.js sbNavigate().
                        // this relies on automatic conversion of an array to a comma delimited string during string
                        // concatenation. bit of a gotcha - any reason why we can't use a .join(',') for clarity?
                        if (typeof(this.uri_array[key][sub_key]) == 'object') {
                            uri += key + '[' + sub_key + ']=' + this.uri_array[key][sub_key];
                        }
                        // only replace # symbols when val is a string and therefore has a .replace() method.
                        if (typeof(this.uri_array[key][sub_key]) == 'string') {
                            uri += key + '[' + sub_key + ']=' + this.uri_array[key][sub_key].replace('#', '');
                        }
                    }
                } else {
                    uri += key + '=' + new String(this.uri_array[key]).replace('#', '');
                }
            }
        }

        if(uri.length > 0) {
            uri = '?' + uri;
        }

        return this.page_parts[0] + uri;
    }


    this.getVar = function (key) {
        if (this.isVar(key)) {
            // make sure variables don't contain the trailing hash from anchors
            return new String(this.uri_array[key]).replace('#', '');
        } else {
            return false;
        }
    }



    this.setVar = function(key, val) {
        if(typeof(key) == 'object') {
            if(typeof(this.uri_array[key[0]]) != 'object') {
                this.uri_array[key[0]] = new Array();
            }
            this.uri_array[key[0]][key[1]] = val;
        } else {
            this.uri_array[key] = val;
        }
    }


    this.delVar = function(key) {
        if(typeof(key) == 'object') {
            delete this.uri_array[key[0]];
        } else {
            delete this.uri_array[key];
        }
    }

    this.delVarsMatching = function(regex) {
        for (var key in this.uri_array) {
            if (typeof this.uri_array[key] == 'function') {
                continue;
            }
            if (key.match(regex)) {
                delete this.uri_array[key];
            }
        }
    }

    this.delVarArray = function(key) {
        if(typeof(key) == 'object') {
            this.delVarsMatching(new RegExp('^' + key[0] + '(\\[.*\\])?$'));
        } else {
            this.delVarsMatching(new RegExp('^' + key + '(\\[.*\\])?$'));
        }
    }

}


String.prototype.trim = function(c, n) {
    if (!c) {
        c = '\\s';
    }

    n = parseInt(n);

    var r1 = new RegExp('^' + c + '*');
    var r2 = new RegExp(c + '*$');

    var value = this.replace(r1, '').replace(r2, '');

    if ((n > 0) && (value.length > n)) {
        value = value.slice(0, n) + '...';
    }

    return value;
};

