﻿var c_infohash;
var c_filesize;
var c_commentsArray = new Array();

function outputComment(infohash, filesize)
{
    c_infohash = infohash;
    c_filesize = filesize;
    getCommentData(infohash, filesize, 1);
}

function getCommentData(infohash, filesize, page)
{
    //try
    {
        if(c_commentsArray[infohash + "_" + filesize + "_" + page] != null)
        {
            printComment(c_commentsArray[infohash + "_" + filesize + "_" + page]);
            return;
        }
        var xmlHttp = XmlHttp.create();
        var requestUrl = web_js_config.path+"/dynamic/comment.aspx?h=" + infohash + "&s=" + filesize + "&page=" + page + "&" + Math.random();
        xmlHttp.open("GET", requestUrl, true);
        xmlHttp.onreadystatechange = function(){
            //try
            {
                if(xmlHttp.readyState == 4)
                {
                    var data = xmlHttp.responseText;
                    printComment(data);
                    c_commentsArray[infohash + "_" + filesize + "_" + page] = data;
                }
            }
            //catch(exception)
            {
                ;
            }
            
        };
        xmlHttp.send(null);
    }
    //catch(exception)
    {
        ;
    }
    
}

function printComment(data)
{
    $("commentsBody").innerHTML = data;
}

function checkKeyDown(event)
{
    var evt = window.event?window.event:event;
    if(evt.ctrlKey && evt.keyCode == 13)
    {
        evt.cancelBubble = true;
        submitComment();
    }
}

function submitComment()
{
    var txtValidate = $("txtValidate");
    var txtCommentBody = $("txtCommentBody");
    if(txtValidate != null && txtCommentBody != null)
    {
        if(txtValidate.value.length != 4)
        {
            alert("Please type the characters in the picture");
            txtValidate.focus();
            return;
        }
        if(txtCommentBody.value == "")
        {
            alert("Please type your review");
            txtCommentBody.focus();
            return;
        }
    
        var s = "cc=" + encodeURIComponent(txtValidate.value) + "&h=" + encodeURIComponent(c_infohash)
              + "&s=" + encodeURIComponent(c_filesize) + "&cb=" + encodeURIComponent(txtCommentBody.value);
        var url = web_js_config.path+"/dynamic/submitcomment.aspx";
        var xmlHttp = XmlHttp.create();
        xmlHttp.open("POST", url, true);
        xmlHttp.setRequestHeader("Content-Length", s.length);
        xmlHttp.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = function(){
            if(xmlHttp.readyState == 4)
            {
                var t = parseInt(xmlHttp.responseText);
                if(t == -1)
                {
                    alert("Validation code error");
                    changeControlStatus(false);
                }
                else if(t == -2)
                    alert("error");
                else if(t == -4)
                    alert("Sorry, there are some bad words in your review");
                else if(t > 0)
                {
                    alert("Success, thank you.");
                    txtValidate.value = "";
                    txtCommentBody.value = "";
                    c_commentsArray = new Array();
                    getCommentData(c_infohash, c_filesize, 1);
                }
            }
        };
        xmlHttp.send(s);
        
        changeControlStatus(true);
        setTimeout('changeControlStatus(false);', 15000)
    }
}

function changeControlStatus(disable)
{
    var c0 = $("btnCommitComment"), c1 = $("txtValidate"), c2 = $("txtCommentBody");
    if(c0 != null)
        c0.disabled = disable;
    if(c1 != null)
        c1.disabled = disable;
    if(c2 != null)
        c2.disabled = disable;
    
    if(disable == false)
        reloadValidateImage();
}

function reloadValidateImage()
{
    var img = $("imgValidate");
    if(img != null)
    {
        img.src = web_js_config.path+"/dynamic/checkcode.aspx?" + Math.random();
    }
}