jQuery.SetCookie = function (CookieName, options, CookieSetttings) {

    var path, domain, secure;

    if (typeof CookieSetttings != "undefined") {

        options = options || {};

        if (options == null)//

            CookieSetttings.expires = -1;

        var expires = '';

        if (CookieSetttings.expires && (typeof CookieSetttings.expires == 'number' || CookieSetttings.expires.toUTCString)) {

            var date;

            if (typeof CookieSetttings.expires == 'number') {

                date = new Date();

                date.setTime(date.getTime() + (CookieSetttings.expires * 24 * 60 * 60 * 1000));

            } else {

                date = CookieSetttings.expires;

            }

            expires = ';expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE

        }

        path = CookieSetttings.path ? ';path=' + CookieSetttings.path : '; path=\/';

        domain = CookieSetttings.domain ? ';domain=' + CookieSetttings.domain : '';

        secure = CookieSetttings.secure ? ';secure=' + CookieSetttings.secure : '';

    }

    else {

        path = '';

        domain = '';

        expires = '';

    }

    var cookievalue = "[{";

    if (options !== null) {

        if (typeof options == "object")//确定输入格式不是格式一

        {

            if (typeof options.length != "undefined") {//确定输入格式是格式三

                for (var i = 0; i < options.length; i++) {

                    if (i != options.length - 1) {

                        cookievalue += options[i].KeyValues + "},{";

                    }

                    else {

                        cookievalue += options[i].KeyValues + "}]";

                    }

                }

            }

            else {

                cookievalue = "[{" + options.KeyValues + "}]"; //确定输入格式是格式二

            }

        }

        else {

            //确定输入格式是格式一

            cookievalue = "[{'" + CookieName + "':'" + options + "'}]";

        }

    }

    else {

        cookievalue += "}]";

    }

    document.cookie = CookieName + '=' + encodeURIComponent(cookievalue) + expires + path + domain;

}

jQuery.GetCookie = function (CookieName, KeyName, index) {

    var cookieValue = GetCookieByName(CookieName);

    var EvalResult = eval("(" + cookieValue + ")"); //转化为JSON格式

    if (cookieValue === null)

        return "NoSuchCookie"; //不存在此cookie

    //给出了访问索引

    if (typeof index == "number") {

        if (index >= EvalResult.length) {

            return "OutOfRange"; //超出索引范围

        }

        cookieValue += "[" + index + "].";

    }

    var ReturnJson = "[";

    var temValue = "";

    if (typeof KeyName != "undefined") {

        if (typeof index != "number") {

            for (var i = 0; i < EvalResult.length; i++) {

                temValue += "{\"";

                for (var j in EvalResult[i]) {

                    if (j == KeyName) {

                        temValue += j + "\":\"" + EvalResult[i][j] + "\",";

                    }

                }

                temValue = temValue.substring(0, temValue.length - 1);

                temValue += "}";

                if (temValue.length > 2) {

                    ReturnJson += temValue + ",";

                    temValue = "";

                }

                if (i == EvalResult.length - 1) {

                    ReturnJson = ReturnJson.substring(0, ReturnJson.length - 1) + "]";

                }



            }

            return eval("(" + ReturnJson + ")");

        }

        else {

            return eval("(" + cookieValue + KeyName + ")");

        }

    }

    else {

        return EvalResult;

    }

}

//修改cookie值

//支持修改方式为

//格式一：(cookiename,KeyName,Index);

//格式二：（cookiename,{expires:6});

jQuery.UpdateCookie = function (CookieName, KeyName, KeyValue, index, options) {

    var CookieValue = GetCookieByName(CookieName);

    if (KeyName === null) {

        options = { expires: -10 };

    }

    else if (typeof KeyName == "object") {

        options = KeyName;

    }

    if (typeof KeyValue == "undefined")//此时修改的 cookie格式为：（cookiename,value）产生的

    {

        KeyValue = KeyName;

        KeyName = CookieName;

    }

    else if (typeof KeyValue == "object") {

        options = KeyValue;

        KeyValue = KeyName;

        KeyName = CookieName;

    }

    var path, domain, secure;

    var ChangeIndex = 0;

    var EvalResult;

    if (CookieValue === null)

        return "NoSuchCookie"; //不存在此cookie 

    EvalResult = eval("(" + CookieValue + ")");

    if ((typeof index == "number")) { //如果未给出了修改的索引，默认为0 

        if (index >= EvalResult.length) {

            return "OutOfRange"; //超出索引范围

        }

        ChangeIndex = index;

    }

    else if (typeof index == "object") { //如果给出的参数是1，2，3，5

        options = index;

        options = options || {};

        if (options == null)

            options.expires = -1;

        var expires = '';

        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {

            var date;

            if (typeof CookieSetttings.expires == 'number') {

                date = new Date();

                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));

            } else {

                date = options.expires;

            }

            expires = '; expires=' + date.toUTCString();

        }

        path = CookieSetttings.path ? ';path=' + CookieSetttings.path : '; path=\/';

        domain = CookieSetttings.domain ? ';domain=' + CookieSetttings.domain : '';

        secure = CookieSetttings.secure ? ';secure=' + CookieSetttings.secure : '';

    }



    //遍历修改

    var NewValue = "[{";

    if (ChangeIndex > EvalResult.length) {

        return "OutOfRange"; //超出索引范围

    }

    for (var j = 0; j < EvalResult.length; j++) {

        NewValue += "\"KeyValues\":\"";

        for (var i in EvalResult[j]) {

            if (i == KeyName && j == ChangeIndex) {

                NewValue += "'" + i + "':'" + KeyValue + "',";



            }

            else {

                NewValue += "'" + i + "':'" + EvalResult[j][i] + "',";

            }

        }

        NewValue = NewValue.substring(0, NewValue.length - 1);

        if (j != EvalResult.length - 1) {

            NewValue += "\"},{";

        }

        else {

            NewValue += "\"}]";

        }

    }

    //重新设置cookie参数

    jQuery.SetCookie(CookieName, eval("(" + NewValue + ")"), options);

}

//根据Cookie名称获取Cookie值

function GetCookieByName(CookieName) {

    var cookieValue = null;

    if (document.cookie && document.cookie != '') {

        var cookies = document.cookie.split(';');

        for (var i = 0; i < cookies.length; i++) {

            var cookie = jQuery.trim(cookies[i]);

            if (cookie.substring(0, CookieName.length + 1) == (CookieName + '=')) {

                cookieValue = decodeURIComponent(cookie.substring(CookieName.length + 1));

                break;

            }

        }

    }

    return cookieValue;

}