﻿
// utility function to retrieve an expiration date in proper

// format; pass three integer parameters for the number of days, hours,

// and minutes from now you want the cookie to expire (or negative

// values for a past date); all three parameters are required,

// so use zeros where appropriate

function getExpDate(days, hours, minutes) {

    var expDate = new Date( );

    if (typeof days == "number" && typeof hours == "number" && 

        typeof hours == "number") {

        expDate.setDate(expDate.getDate( ) + parseInt(days));

        expDate.setHours(expDate.getHours( ) + parseInt(hours));

        expDate.setMinutes(expDate.getMinutes( ) + parseInt(minutes));

        return expDate.toGMTString( );

    }

}

   

// utility function called by getCookie( )

function getCookieVal(offset) {

    var endstr = document.cookie.indexOf (";", offset);

    if (endstr == -1) {

        endstr = document.cookie.length;

    }
    return unescape(document.cookie.substring(offset, endstr));

}
   

// primary function to retrieve cookie by name

function getCookie(name) {//获取Cookie
    name=escape(name);

    var arg = name + "=";

    var alen = arg.length;

    var clen = document.cookie.length;

    var i = 0;

    while (i < clen) {

        var j = i + alen;

        if (document.cookie.substring(i, j) == arg) {

            return  unescape( getCookieVal(j));

        }

        i = document.cookie.indexOf(" ", i) + 1;

        if (i == 0) break; 

    }

    return "";

}

   

// store cookie value with optional details as needed

function setCookie(name, value, expires, path, domain, secure) {
    name=escape(name);
    value=escape(value);
    document.cookie = name + "=" + escape (value) +

        ((expires) ? "; expires=" + expires : "") +

        ((path) ? "; path=" + path : "") +

        ((domain) ? "; domain=" + domain : "") +

        ((secure) ? "; secure" : "");
//        alert(document.cookie);

}

   

// remove the cookie by setting ancient expiration date

function deleteCookie(name,path,domain) {

    if (getCookie(name)) {

        document.cookie = name + "=" +

            ((path) ? "; path=" + path : "") +

            ((domain) ? "; domain=" + domain : "") +

            "; expires=Thu, 01-Jan-70 00:00:01 GMT";

    }

}

//该函数用于返回某个Cookie的值
//cookieValue为整个Cookie文件的内容
//cookieName为要获得值的Cookie名
function returnCookieValue(cookieValue, cookieName) {
    //设置返回值var returnCookieValue = "";
    //查找Cookie名在整个Cookie文件内容中的位置
    var cookieNameIndex = cookieValue.indexOf(cookieName + "=");

    //如果在Cookie文件内容中找到Cookie名，则进一步查找该Cookie的值
    if (cookieNameIndex != -1) {

        //查找Cookie值在Cookie文件内容中的位置。
        //该位置为Cookie名的位置加上Cookie名的长度，再加上"="号的长度，即1
        var cookieValueBeginIndex = cookieNameIndex + cookieName.length + 1;

        //查找Cookie值在Cookie文件内容中的位置。
        //该位置为从Cookie名的位置开始到第一个";"为止
        var cookieVlaueEndIndex = cookieValue.indexOf("&", cookieValueBeginIndex);

        //如果从Cookie名的位置开始到整个Cookie文件内容的最后都没有找到";"，那么Cookie值的结束位置就是整个Cookie内容的最后
        if (cookieVlaueEndIndex == -1) {
            cookieVlaueEndIndex = cookieValue.length;
        }

        //通过Cookie值的开始位置和结束位置获得Cookie值
        CookieValue = cookieValue.substring(cookieValueBeginIndex, cookieVlaueEndIndex);

    } else {
        CookieValue = ""
    }

    //返回Cookie值
    return CookieValue;
}


