﻿// JScript 檔
//========================================================================================
// 功能說明：此為一個提供處理 Cookies 的 Liberally
// 作者：Jimmy
// 建立日期：2008/07/10
//========================================================================================

var CookieLibObj = new Object;

CookieLibObj.CookieLib = function() {
    
}

//-- 新增 Cookie
CookieLibObj.CookieLib.prototype.addCookie = function(strName, strValue, intExpires, strPath, strDomain, blnSecure){
    var strCookie = strName + "=" + strValue;
    if (intExpires) {
        //-- 計算 Cookie 的期限, 單位為天數
        try {
            var datCur = new Date();
            datCur.setTime(datCur.getTime() + (intExpires*24*60*60*1000));
            strCookie += "; expires=" + datCur.toGMTString();
        }catch(e) {
            alert(e.description);
        }
    }
    //-- Cookie 的路徑
    strCookie += (strPath) ? "; path=" + strPath : "";
    //-- Cookie 的 Domain
    strCookie += (strDomain) ? "; domain=" + strDomain : "";
    //-- 是否需要保密傳送,為一個 Boolean 值
    strCookie += (blnSecure) ? "; secure" : "";
    //-- 新增此 Cookie
    document.cookie = strCookie;
}

//-- 檢查 Cookie 是否存在
CookieLibObj.CookieLib.prototype.checkCookieExist = function(strName) {
    if (this.getCookie(strName))
        return true;
    else
        return false;
}

//-- 刪除 Cookie 
CookieLibObj.CookieLib.prototype.deleteCookie = function(strName) {
    //-- 檢查 Cookie 是否存在
    if (this.checkCookieExist(strName)) {
        //-- 設定 Cookie 的期限已經過期
        var strCookie = strName + "=";
        strCookie += "; expires=Thu, 01-Jan-70 00:00:01 GMT";
        document.cookie = strCookie;
        
    }
}

//-- 取得 Cookie
CookieLibObj.CookieLib.prototype.getCookie = function(strName) {
    var strCookies = document.cookie;   //-- 取得所有 cookie
    var strCookieName = strName + "=";  //-- Cookie 名稱
    var intValueBegin=0, intValueEnd=0;
    var strValue="";
    //-- 尋找是否有此 Cookie 名稱
    intValueBegin = strCookies.indexOf(strCookieName);
    if (intValueBegin == -1) return null;   //-- 找不到此 Cookie
    //-- 取得值的結尾位置
    intValueEnd = strCookies.indexOf(";", intValueBegin);
    if (intValueEnd == -1) intValueEnd = strCookies.length; //-- 此為最後一個 Cookie
    //-- 取得 Cookie 值
    strValue = strCookies.substring(intValueBegin + strCookieName.length, intValueEnd);
    return strValue;
}


var CookieLib = CookieLibObj.CookieLib;


//==========================================================================

//-- 刪除 Cookie
function deleteCookie(strName) {
    var cl = new CookieLib();
    cl.deleteCookie(strName);
    cl = null;
}
//-- 取得 Cookie
function getCookieValue(strName) {
    var cl = new CookieLib();
    var strValue="";
    if (cl.checkCookieExist(strName)) {
        strValue = cl.getCookie(strName);
    } else {
        strValue = "";
    }
    cl = null;
    return strValue;
}
//-- 設定 Cookie
function setCookieValue(strName, strValue, intExpires) {
    var cl = new CookieLib();
    cl.addCookie(strName, strValue, intExpires);
    cl = null;
}
