﻿function redirectControlsLocation() {
  
    var l = location.search.substring(1).replace(/\+/g, ' ');
    /* parse the query */
    /* semicolons are nonstandard but we accept them */
    var x = l.replace(/;/g, '&').split('&'), i, name, t;
    /* q changes from string version of query to object */
    for (data = {}, i = 0; i < x.length; i++) {
        t = x[i].split('=', 2);
        name = unescape(t[0]);
        if (name.length > 0) {
            if (!data[name])
                data[name] = [];
            if (t.length > 1) {
                data[name][data[name].length] = unescape(t[1]);
            }
            /* next two lines are nonstandard */
            else
                data[name][data[name].length] = true;
        }
    }  

    this.getValue = function(key) {
        return data[key];
    }
    this.setValue = function(key, value) {
        if (value == null)
            delete data[key];
        else
            data[key] = value;
    }
    
    this.toString = function() {
        var queryString = new String("");

        for (var key in data) {
            if (queryString != "")
                queryString += "&"
            if (data[key])
                queryString += key + "=" + data[key];
        }
        if (queryString.length > 0)
            return "?" + queryString;
        else
            return queryString;
    }
    this.clear = function() {
        delete data;
        data = [];
    }

    this.getLocation = function()
    {
        return location.pathname + this.toString();
    }
}


function redirectionDropDownList(ddl, parameterName) {

    var newLocation = new redirectControlsLocation();
    newLocation.setValue(parameterName, ddl.selectedIndex);
    location.href = newLocation.getLocation();
}



function onClickRedirectButton(controls) {

    var newLocation = new redirectControlsLocation();

    for (i = 0; i < controls.length; i++) {
        var clientID = controls[i][0];
        var parameterName = controls[i][1];
        var element = document.getElementById(clientID);
        if (element) {
            var value;
            if (element.nodeName.toLowerCase() == "select") {
                value = element.selectedIndex;
            }
            else {
                value = (element.value || "").replace(/\r/g, "");
            }
            newLocation.setValue(parameterName, value);
                    
        }
    }
    location.href = newLocation.getLocation(); 
    return false;
    


}