function $getE(obj) {
    return document.getElementById(obj);
}

//初始化评论
function commentInit() {
    var x = new AjaxRequest('XML', 'commentForm');
    x.labelname = "内容评论标签";
    x.para = ['itemId='+$getE("ItemID").value,'nodeId='+$getE("commentNodeID").value];
    x.currentpage = 1;
    x.post('updatelabel', '/ajax.aspx', function(s) {
        var xml = x.createXmlDom(s);
        $getE('commentform').innerHTML = xml.getElementsByTagName("body")[0].firstChild.data;
        changepage(1, '评论列表', '惟有爱评论分页');
        setCurrentUserInfo();
    });
}

//添加评论
function addComment() {
    if ($getE('commentTitle').value == '') {
        alert("请输入标题！");
        $getE("commentTitle").focus();
        return;
    }
    if ($getE("commentContnet").value == '') {
        alert("请输入评论内容！");
        $getE("commentContnet").focus();
        return false;
    }
    var commentValidCode = "";
    var checkValidCode = false;
    if ($getE("commentValidCode") != null) {
        if ($getE("commentValidCode").value == '') {
            alert("请输入验证码！");
            $getE("commentValidCode").focus();
            return;
        }
        commentValidCode = $getE("commentValidCode").value;
        checkValidCode = true;
    }
    var x = new AjaxRequest('XML', 'status');
    x.para = ['username=' + $getE("commentUsername").value, 'commenttitle=' + $getE("commentTitle").value, 'content=' + $getE("commentContnet").value, 'email=' + $getE("commentEmail").value, 'gid=' + $getE("ItemID").value, 'nid=' + $getE("commentEmail").value, 'private=' + $getE("commentIsPrivate").value, 'position=' + $getE("commentViewpoint").value, 'score=' + $getE("commentRanking").value, 'TxtValidCode=' + commentValidCode, 'isguest=' + $getE("commentIsAnonymous").value];
    x.paratype = ['nohtml', 'nohtml', 'nohtml']
    x.post('addcomment', '/ajax.aspx', function(s) {
        var xml = x.createXmlDom(s);
        var status = xml.getElementsByTagName("status")[0].firstChild.data;
        switch (status) {
            case "ok":
                changepage(1, '评论列表', '惟有爱评论分页');
                alert("发表评论成功！");
                window.location.href = '#commentTarget';
                $getE("commentContnet").value = '';
                break;
            case "check":
                alert("发表评论成功，请等待管理员审核。");
                break;
            case "err":
                alert("发表评论失败！");
                break;
            case "nopurview":
                alert("此栏目已禁止发表评论！");
                break;
            case "noTourists":
                alert("此栏目已禁止游客发表评论！");
                break;
            case "checkCodeError":
                $getE("commentValidCode").value = '';
                refreshValidCode($getE("commentValidCodeImg"));
                alert("您输入的验证码和系统产生的不一致，请重新输入！");
                break;
            default:
                alert("发表评论失败！");
                break;
        }
        if (checkValidCode) {
            refreshValidCode($getE("commentValidCodeImg"));
            $getE("commentValidCode").value = '';
            checkValidCode = false;
        }
    });
}

function addPkZone(commentid, position, content) {
    var x = new AjaxRequest('XML', 'status');
    x.para = ['commentid=' + commentid, 'position=' + position, 'content=' + content];
    x.post('addpkzone', '/ajax.aspx', function(s) {
        var xml = x.createXmlDom(s);
        var status = xml.getElementsByTagName("status")[0].firstChild.data;
        switch (status) {
            case "ok":
                changepage(1, '评论列表', '惟有爱评论分页');
                break;
            default:
                alert("辩论失败！");
                break;
        }
    });
}

//更新列表
function changepage(pagenum, sourcename, spanname) {
    var xCount = new AjaxRequest('XML', 'count_comment');
    xCount.labelname = "评论数统计";
    xCount.para = ['GeneralID=' + $getE("ItemID").value];
    xCount.currentpage = 1;
    xCount.post('updatelabel', '/ajax.aspx', function(s) {
        var xmlCount = xCount.createXmlDom(s);
        var commentCount = xmlCount.getElementsByTagName("body")[0].firstChild.data;
        var x = new AjaxRequest('XML', 'pe100_' + sourcename);
        x.labelname = sourcename;
        x.currentpage = pagenum;
        x.para = ['itemId=' + $getE("ItemID").value, 'pageIndex=' + pagenum, 'commentCount=' + commentCount, 'outputQty=10', 'page=true', 'pagesize=10', 'currentpage=' + pagenum];
        x.post('updatelabel', '/ajax.aspx', function(s) {
            var xml = x.createXmlDom(s);
            var plstr = "";
            for (var i = 0; i < xml.getElementsByTagName("body")[0].childNodes.length; i++) {
                plstr += xml.getElementsByTagName("body")[0].childNodes[i].nodeValue;
            }
            $getE('pe100_' + sourcename).innerHTML = plstr;
            updatepage(spanname, sourcename, xml);
        });
    });
}
//更新分页
function updatepage(spanname, sourcename, xml) {
    if (parseInt(xml.getElementsByTagName("total")[0].firstChild.data) > 0) {
        var x = new AjaxRequest('XML', 'pe100_page_' + sourcename);
        x.labelname = spanname;
        x.sourcename = sourcename;
        x.total = xml.getElementsByTagName("total")[0].firstChild.data;
        x.currentpage = xml.getElementsByTagName("currentpage")[0].firstChild.data;
        x.pagesize = xml.getElementsByTagName("pagesize")[0].firstChild.data;
        x.post('updatepage', '/ajax.aspx', function(s) {
            var xml = x.createXmlDom(s);
            if ($getE('pe100_page_' + sourcename) != null) {
                var plstr = "";
                for (var i = 0; i < xml.getElementsByTagName("body")[0].childNodes.length; i++) {
                    plstr += xml.getElementsByTagName("body")[0].childNodes[i].nodeValue;
                }
                $getE('pe100_page_' + sourcename).innerHTML = plstr;
            }
        });
    }
}

//设置默用户信息
function setCurrentUserInfo() {
    $getE("commentTitle").value = $getE('h1').innerHTML;
    try {
        var x = new AjaxRequest('XML', '');
        x.post('logincheck', '/ajax.aspx', function(s) {
            var xml = x.createXmlDom(s);
            if (xml.getElementsByTagName("status")[0].firstChild.data == "ok") {
                if ($getE('commentUsername') != null) {
                    $getE('commentUsername').value = xml.getElementsByTagName("username")[0].firstChild.data;
                    $getE('commentEmail').value = xml.getElementsByTagName("email")[0].firstChild.data;
                    $getE('commentUsername').disabled = true;
                    $getE('commentEmail').disabled = true;
                }
            }
        });
    }
    catch (e) {
    }
}

//刷新验证码
function refreshValidCode(obj) {
    obj.src = obj.src + '?code=' + randomNumber(10);
}

function randomNumber(n) {
    var rnd = '';
    for (var i = 0; i < n; i++)
        rnd += Math.floor(Math.random() * 10);
    return rnd;
}

//设置匿名
function setAnonymous(obj) {
    if (obj.checked) {
        $getE('commentUsernameBlock').style.display = 'none';
        $getE('commentEmailBlock').style.display = 'none';
        $getE('commentIsAnonymous').value = 'true';
    }
    else {
        $getE('commentUsernameBlock').style.display = '';
        $getE('commentEmailBlock').style.display = '';
        $getE('commentIsAnonymous').value = 'false';
    }
}

//设置私有
function setPrivate(obj) {
    if (obj.checked) {
        $getE('commentIsPrivate').value = 1;
    }
    else {
        $getE('commentIsPrivate').value = 0;
    }
}

//设置观点
function setViewpoint(value) {
    for (i = -1; i < 2; i++) {
        if (i == value) {
            $getE('commentViewpointTab' + i).className = "current";
        }
        else {
            $getE('commentViewpointTab' + i).className = "";
        }
    }
    $getE('commentViewpoint').value = value;
}

//设置按钮
function changeBtnStyle(obj, className) {
    obj.className = className;
}

//设置评分
function changeRanking(index, isfixed) {
    var colStars = $getE("commentRankingBlock").getElementsByTagName("input");
    var k = isfixed ? parseInt($getE("commentRanking").value) : index;
    for (var i = 0; i < colStars.length; i++) {
        colStars[i].src = (i < k ? "/Images/fstar.gif" : "/Images/estar.gif");
    }
}

function rankingClick(index) {
    $getE("commentRanking").value = index;
}

function rankingMouseOver(index) {
    changeRanking(index, false);
}

function rankingMouseOut() {
    changeRanking(0, true);
}

function copyToClipBoard() {
    var clipBoardContent = '';
    clipBoardContent += window.location;
    window.clipboardData.setData("Text", clipBoardContent);
    alert("你已复制链接及标题，请粘贴到POPO/MSN等发给好友!");
}
