﻿
//Author:fengxu
//Date:200-08-18
//Summary:读写Cookies

CookiesHelper = function() {
    var d = new Date()
    d.setDate(d.getDate() + 15);
    this.expriceDate = d.toUTCString();             //设置过期时间
    this.unDesCodeValue = document.cookie;          //未解码的Cookies值
    this.cookies = new Array();  //cookies对象
    if (this.unDesCodeValue != '' && this.unDesCodeValue != null) {
        var tempArr = this.unDesCodeValue.split(';'); //将cookies对象分开
        for (var i = 0; i < tempArr.length; i++) {
            var arrCookItem;
            if (tempArr[i].split('=').length > 2)        //如果cookie项有子cookies值,  暂时不支持asp.net的写法Response.Cookies["Name"]["Key"]
            {
                //                arrCookItem = new Array();
                //                arrCookItem.push(tempArr[i].substring(0, tempArr[i].indexOf('=') - 1));
                //                arrCookItem.push(tempArr[i].substring(tempArr[i].indexOf('='), tempArr[i.length-1]).split('&'))
                //                arrCookItem[0] = decodeURIComponent(arrCookItem[0]);  //解码名称和值


            }
            else {
                arrCookItem = tempArr[i].split('=');   //将cookies名称和值分开
                arrCookItem[0] = decodeURIComponent(arrCookItem[0]);  //解码名称和值
                arrCookItem[1] = decodeURIComponent(arrCookItem[1]);
                this.cookies.push(arrCookItem);        //得到一个二维数组，对应cookies值
            }
        }
    }
}

//取cookies值
CookiesHelper.prototype.GetCookieValue = function(cookName) {
    if (null == this.cookies) return '';
    var name;
    var value;
    var arrTemp;
    for (var i = 0; i < this.cookies.length; i++) {
        arrTemp = this.cookies[i];
        name = arrTemp[0];
        value = arrTemp[1];
        if (name == cookName)
            return value;
    }
    return null;
}

CookiesHelper.prototype.SetCookiesValue = function(cookName, cookValue) {
    var name;
    var value;
    var arrTemp;
    var isAdd = true;  //判断是否是添加新Cookies值
    for (var i = 0; i < this.cookies.length; i++) {
        arrTemp = this.cookies[i];
        name = arrTemp[0];
        value = arrTemp[1];
        if (name == cookName) {
            this.cookies[i][1] = cookValue;     //如果有则修改；
            document.cookie = cookName + '=' + encodeURIComponent(cookValue) + "; expires=" + this.expriceDate + "";
            isAdd = false;
            break;
        }
    }
    if (isAdd) {
        var newCookie = new Array();            //如果没有则添加;
        newCookie.push(cookName);
        newCookie.push(cookValue);
        this.cookies.push(newCookie);
        document.cookie = cookName + '=' + encodeURIComponent(cookValue) + "; expires=" + this.expriceDate + "";
    }
}

CookiesHelper.prototype.AddCookie = function(cookName, cookValue) {
    this.SetCookiesValue(cookName, cookValue);
}

//CookiesHelper.prototype.SaveCookies = function() {
//    var realCookies = '';
//    if (this.cookies.length == 0)
//        this.unDesCodeValue = '';
//    else {
//        for (var i = 0; i < this.cookies.length; i++) {
//            realCookies += this.cookies[i][0] + '=';
//            realCookies += encodeURIComponent(this.cookies[i][1]);
//            realCookies += "; expires=" + this.expriceDate + ";";
//        }
//        this.unDesCodeValue = realCookies;
//    }
//    document.cookie = realCookies ;
//}

//删除cookie
CookiesHelper.prototype.DeleteCookie = function(cookieName) {
    if (this.cookies.length == 0)
        return;
    else {
        var d = new Date();
        d.setDate(d.getDate() - 1);
        var realCookies = '';
        var documentCookie = new Array();       //删除时要更新数组
        for (var i = 0; i < this.cookies.length; i++) {
            arrTemp = this.cookies[i];
            name = arrTemp[0];
            value = arrTemp[1];

            if (name.replace(/\s/g,'') == (cookieName)) {
                realCookies += name + '=' + encodeURIComponent(value) + "; expires=" + d.toGMTString() + "";
                break;
            }
            else {
                var tempArr = new Array();
                tempArr.push(name);
                tempArr.push(value);
                documentCookie.push(tempArr);
//                realCookies += encodeURIComponent(name) + '=';
//                realCookies += encodeURIComponent(value);
//                realCookies += "; expires=" + this.expriceDate + ";";
            }
        }
        this.cookies = documentCookie;
        alert(realCookies);
        document.cookie = realCookies;
    }
}

var cookieHelper = new CookiesHelper();

CookiesHelper.prototype.DeCodeCookieValue = function() {
//    this.desCodeValue = decodeURIComponent(this.cookies)
}


//CookiesHelper.registerClass('CookiesHelper');

//if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
