var whir = window.whir || {};
whir.request =
{
    get: function (name) {
        if (location.href.indexOf("?") == -1 || location.href.indexOf(name + '=') == -1) {
            return '';
        }
        var queryString = location.href.substring(location.href.indexOf("?") + 1);
        var parameters = queryString.split("&");
        var pos, paraName, paraValue;
        for (var i = 0; i < parameters.length; i++) {
            pos = parameters[i].indexOf('=');
            if (pos == -1) {
                continue;
            }
            paraName = parameters[i].substring(0, pos);
            paraValue = parameters[i].substring(pos + 1);
            if (paraName == name) {
                return decodeURIComponent(paraValue.replace(/\+/g, " "));
            }
        }
        return '';
    },
    appendUrlArg: function (key, value) {
        var url = window.document.URL;
        var arrayParams = url.split("?");
        var result = arrayParams[0] + "?";

        if (url.indexOf("?") > 0) {
            var parms = arrayParams[1].split("&");
            var exsit = false;
            for (var i = 0; i < parms.length; i++) {
                var param = parms[i].split("=");
                if (param[0] == key) {
                    exsit = true;
                    result += param[0] + "=" + value + "&"; //修改参数
                } else {
                    result += param[0] + "=" + param[1] + "&"; //保持原样
                }
            }
            if (exsit == false) {
                result += key + "=" + value + "&"; //不存在时，附加参数
            }

        } else {
            result += key + "=" + value;
        }
        result = result.substring(result.length - 1, result.length) == "&" ? result.substring(0, result.length - 1) : result;
        return result;
    },
    appendUrlParm: function (url, key, value) {
        var arrayParams = url.split("?");
        var result = arrayParams[0] + "?";

        if (url.indexOf("?") > 0) {
            var parms = arrayParams[1].split("&");
            var exsit = false;
            for (var i = 0; i < parms.length; i++) {
                var param = parms[i].split("=");
                if (param[0] == key) {
                    exsit = true;
                    result += param[0] + "=" + value + "&"; //修改参数
                } else {
                    result += param[0] + "=" + param[1] + "&"; //保持原样
                }
            }
            if (exsit == false) {
                result += key + "=" + value + "&"; //不存在时，附加参数
            }

        } else {
            result += key + "=" + value;
        }
        result = result.substring(result.length - 1, result.length) == "&" ? result.substring(0, result.length - 1) : result;
        return result;
    }
};