function sendContentComment(content_id, volume) {
    var postContent = 'content_id=' + content_id;
    if (volume != "") postContent += '&volume=' + volume;
    var text = document.getElementById('textarea_comment_text').value;
    postContent += "&text=" + text;
    
    var xmlHttp = getXmlHttpObject();
    xmlHttp.open('POST', "/content_comments/send_comment.html", true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange = function() {
        reponse_sendContentComment(xmlHttp, content_id, text)
    }
    xmlHttp.send(encodeURI(postContent));
}

function reponse_sendContentComment(xmlHttp,content_id,text) {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            var xmlDoc = xmlHttp.responseXML;
            if (xmlDoc.documentElement) {
                var getData = xmlDoc.documentElement.childNodes;
                if (getData[0].nodeValue == 'login_error') {
                    // not_login
                    window.location.href = "/login/index.html?forward=" + window.location.href;
                } else if (getData[0].nodeValue == 'id_error'){
                    // content_id is null
                    return false;
                } else if (getData[0].nodeValue == 'text_error') {
                    // text is null
                    alert('请输入评论的内容！');
                    return false;
                } else if (getData[0].nodeValue == 'ok') {
                    // response ok, start refresh comments.
                    //getContentComment(content_id, 1);
                    window.location.reload();
                }
            } else {
                // no response
            }
        }
    } else {
        // in process
    }
}

function getContentComment(content_id, page, size, order, volume) {
    var param = '?content_id=' + content_id;
    if (page != "") param += '&page=' + page;
    if (size != "") param += '&size=' + size;
    if (order != "") param += '&order=' + order;
    if (volume != "") param += '&volume=' + volume;
    
    var xmlHttp = getXmlHttpObject();
    xmlHttp.open('GET', '/content_comments/get_comment.html' + param, true);
    xmlHttp.onreadystatechange = function() {
        reponse_getContentComment(xmlHttp)
    }
    xmlHttp.send(null);
}

function reponse_getContentComment(xmlHttp) {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            var textDoc = xmlHttp.responseText;
            document.getElementById('content_comment').innerHTML = textDoc;
        }
    } else {
        document.getElementById('comments_loading').innerHTML = '<img src="/common/image/highslide/graphics/loader.white.gif">';
    }
}

function handleHttpResponse_getTemplate(xmlHttp) {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
	  //var textDoc = xmlHttp.responseText;
      //alert(textDoc);
      //document.getElementById('refresh').innerHTML = textDoc;
        
    }
}
function getAll() {
    var xmlHttp = getXmlHttpObject();
    xmlHttp.open('GET', '/content_comments/get_all.html', true);
    xmlHttp.onreadystatechange = function() {
        getAll_text(xmlHttp)
    }
    xmlHttp.send(null);
}


/////////////////////////////////////////////////
// 举报
function goReport(id) {
    var xmlHttp = getXmlHttpObject();
    xmlHttp.open('GET', '/login/check.html', true);
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
                var xmlDoc = xmlHttp.responseXML;
                if (xmlDoc.documentElement) {
                    var getData = xmlDoc.documentElement.childNodes;
                    if (getData[0].nodeValue == 'logined') {
                        document.getElementById('validateImage').src = "/product/validate_picture.html?" + Math.random();
                        document.getElementById('comment_id').value = id;
                        document.getElementById('report_text').value = '请输入举报说明'
                        var reportDiv = document.getElementById('report_div');
        				reportDiv.style.top = (document.body.scrollTop + (document.body.clientHeight - 240) / 2) + "px";
        				reportDiv.style.left = (document.body.scrollLeft + (document.body.clientWidth - 100) / 2) + "px";
                        reportDiv.style.display = "block";
                    } else {
                        alert('请先登录！');
                        document.location.href = '/login/index.html?forward=' + document.location.href;
                    }
                }
            }
        }
    }
    xmlHttp.send(null);
}

function closeReport() {
    var reportDiv = document.getElementById('report_div');
    var commentIdObj = document.getElementById('comment_id');
    var reportTextObj = document.getElementById('report_text');
    
    if (reportDiv) reportDiv.style.display = "none";
    if (commentIdObj) commentIdObj.value = "";
    if (reportTextObj) reportTextObj.value = "";
}

function doReport() {
    var comment_id = document.getElementById('comment_id').value;
    var text = document.getElementById('report_text').value;
    var validate = document.getElementById('validateText').value;
    
    if (comment_id != "" || !NaN(comment_id)) {
        var xmlHttp = getXmlHttpObject();
        
        var postContent = 'id=' + comment_id;
        if (text != "") postContent += '&text=' + text;
        if (validate != "") postContent += '&validate=' + validate;
        
        xmlHttp.open('POST', '/content_comments/report_complete.html', true);
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                    var xmlDoc = xmlHttp.responseXML;
                    if (xmlDoc.documentElement) {
                        var getData = xmlDoc.documentElement.childNodes;
                        if (getData[0].nodeValue == 'complete') {
                            closeReport();
                            alert('感谢您提供信息！');
                        } else if (getData[0].nodeValue == 'already') {
                            closeReport();
                            alert('您已经举报过本条评论！');
                        } else if (getData[0].nodeValue == 'not_exists') {
                            closeReport();
                            alert('您举报的评论已经删除！');
                        } else if (getData[0].nodeValue == 'validate_error') {
                            //closeReport();
                            alert('您输入的验证码错误！');
                        } else {
                            closeReport();
                            alert('发生错误，请联系管理员！');
                        }
                        document.getElementById('report_button').innerHTML = '<input type="button" value="确定" onclick="doReport()">&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="取消" onclick="closeReport()">';
                    }
                }
            } else {
                document.getElementById('report_button').innerHTML = ' 请稍候 <img src="/common/image/highslide/graphics/loader.white.gif">';
            }
        }
        xmlHttp.send(encodeURI(postContent));
    }
}

function display_report(id, action) {
    //document.getElementById(id).style.display = action;
}

function report_clear() {
    if (document.getElementById('report_text').value == '请输入举报说明') {
        document.getElementById('report_text').value = '';
    }
}