/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
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";
    }
}

/* *
 * 获得URL某个参数 
 *
 */

function getURLParam(strParamName) {
    var strReturn = "";
    var strHref = window.location.href;
    if ( strHref.indexOf("?") > -1 ){
        var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
        var aQueryString = strQueryString.split("&");
        for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
            if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }   
        }
    }
    return strReturn;
}

/**
 * 判断改文章时候属于当前用户
 *
 */
var _preCache = null;

function isMyNovel() {
    var novelId = getURLParam("novelid");
    var myCookie = getCookie("myNovel");
    var isMyNovel = false;
    
    if (novelId.length > 0 && myCookie != null) {
        var tmp = new Array();
        tmp = myCookie.split(',');
        for (id in tmp )
         if (tmp[id] == novelId) {
             isMyNovel = "true";
         }
    }
    return isMyNovel;
}

/**
 * 判断用户是否作者,生成管理链接
 *
 */
function genUserPannel(novelid,commentid,state) {
    if (_preCache == null) {
        _preCache = isMyNovel();
    }
    
    if (_preCache == "true") {
       stateText = (state == 0) ? '[作者加精]' : '[取消精华]';
       stateParm = (state == 0) ? '' : '&likeit=1';
       document.write("<a href=\"likeit.php?novelid="+ novelid + "&commentid="+ commentid + stateParm +"\" target=\"_blank\">"+ stateText +"<\/a>"); 
    }
}

/**
 * 判断用户是否有管理权限,生成管理链接
 *
 */
function genAdminPannel(ip,novelid,commentid,iswonderful) {
    var myCookie = getCookie("isAdmin");
    if (myCookie != null && myCookie == 'true') {
        document.write("<span class=\"toleft\">IP：<span class=\"redtext\">"+ ip +"<\/span><\/span><a href=\"guanli\/delete.php?novelid="+ novelid +"&commentid="+ commentid +"\" target=\"_blank\">[删除]<\/a>　　<a href=\"guanli\/zero.php?novelid="+ novelid + "&commentid="+ commentid + "\" target=\"_blank\">[清零]<\/a>");
        if (iswonderful == 1) {
            document.write("<a href=\"guanli\/notwonderful.php?commentid=" + commentid + "&novelid=" + novelid + "\" target=\"_blank\">[取消精华]<\/a>");
        }
    }
}


/**
 * 判断用户是否有管理权限,生成导出链接
 *
 */
function genExportPannel(novelid) {
    var myCookie = getCookie("isAdmin");
    if (myCookie != null && myCookie == 'true') {
        document.write("<a href=\"guanli\/outputhtml.php?novelid="+ novelid +"\">[导出html]</a> <a href=\"comment.php?novelid="+ novelid + "\">[查看全部书评]<\/a>");
    }
}


/**
 * 判断用户是否有文章管理员权限,生成管理
 *
 */
function genWarningPannel(novelid) {
    var myCookie = getCookie("moderatorName");
    if (myCookie != null) {        
        document.write("<form method=\"post\" action=\"\/guanli\/insertedit.php\"  onsubmit=\"return makesure()\">");
        document.write("   <div class=\"warning\">");
        document.write("    <div class=\"warningtitle\">");
        document.write("    <input name=\"novelid\" type=\"hidden\" id=\"novelid\" value=\""+ novelid + "\">&nbsp;&nbsp;"+ myCookie + "：");
        document.write("    <\/div>");
        document.write("    <div class=\"warningbody\">");
        document.write("    <div class=\"textcenter\">");
        document.write("    <span class=\"toright\">");
        document.write("    <div class=\"fanbuttondiv\">");
        document.write("    <input name=\"submit\" type=\"submit\" class=\"fanbutton\" id=\"submit\" value=\"确定\">");
        document.write("    <\/div>");
        document.write("   <\/span>");
        document.write("    <textarea name=\"editbody\" cols=\"100\" rows=\"7\" class=\"input_textarea_short\" id=\"editbody\"><\/textarea>");
        document.write("    <\/div>");
        document.write("    <\/div>");
        document.write("   <\/div>") ;
        document.write("<\/form>");
    }
}
