﻿function GetCookieVal(offset)
//获得Cookie解码后的值
{
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function SetCookie(name, value)
//设定Cookie值
{
    var exp = new Date();
    exp.setTime(exp.getTime() + 3600000000);
    document.cookie = name + "=" + value + "; expires=" + exp.toGMTString() + "; path=/";
    //    var expdate = new Date();
    //    var argv = SetCookie.arguments;
    //    var argc = SetCookie.arguments.length;
    //    var expires = (argc > 2) ? argv[2] : null;
    //    var path = (argc > 3) ? argv[3] : null;
    //    var domain = (argc > 4) ? argv[4] : null;
    //    var secure = (argc > 5) ? argv[5] : false;
    //    if (expires != null) expdate.setTime(expdate.getTime() + (expires * 1000));
    //    document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expdate.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
}
function DelCookie(name)
//删除Cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 3600000000);
    var cval = GetCookie(name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString() + "; path=/";
}
function GetCookie(name)
//获得Cookie的原始值
{
    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 GetCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

function addpv(objf) {
    var obj = document.getElementById("hn_uname").value;
    if (GetCookie("addnums" + obj) != null) {
        return;
    }
    else {
        SetCookie("addnums" + obj, "1");
        NfsAjaxPro.pv(obj).value;
    }
}

function addtopicpv() {
    var topicid = document.getElementById("topic_id").value;
    var userid = document.getElementById("userip").value;
    var obj = document.getElementById("hn_uname").value;
    if (GetCookie("topic" + obj + topicid) != null) {
        return;
    }
    else {
        SetCookie("topic" + obj + topicid, "1")
        NfsAjaxPro.topicpv(obj, topicid, userid).value;
    }
}

function addnphotopv() {

    var imgid = document.getElementById("imgid").value;
    var userid = document.getElementById("userip").value;
    var obj = document.getElementById("hn_uname").value;
    if (GetCookie("photo" + obj + imgid) != null) {
        return;
    }
    else {
        SetCookie("photo" + obj + imgid, "1")
        NfsAjaxPro.photopv(obj, imgid, userid).value;
    }
}

function addnproductpv() {
    var pdid = document.getElementById("pdid").value;
    var userid = document.getElementById("userip").value;
    var obj = document.getElementById("hn_uname").value;
    if (GetCookie("product" + obj + pdid) != null) {
        return;
    }
    else {
        SetCookie("product" + obj + pdid, "1")
        NfsAjaxPro.productpv(obj, pdid, userid).value;
    }
}
/*
$.cookie('the_cookie'); // 获得cookie
$.cookie('the_cookie', 'the_value'); // 设置cookie
$.cookie('the_cookie', 'the_value', { expires: 7 }); //设置带时间的cookie
$.cookie('the_cookie', '', { expires: -1 }); // 删除
$.cookie('the_cookie', null); // 删除 cookie
$.cookie(’the_cookie’, ‘the_value’, {expires: 7, path: ‘/’, domain: ‘jquery.com’, secure: true});//新建一个cookie 包括有效期 路径 域名等
*/
jQuery.cookie = function (name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;                
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        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]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
