// Here resides some javascript functions...

var http = getHTTPObject(); // Create the HTTP Object
var isPocessing = false;
var totalVotes_ID = "";
var cmntRating_ID = "";
var posRating_ID = "";
var negRating_ID = "";

// Send vote to server and process the result
function handleVoteHttpResponse() {
    if (http.readyState == 4) {
        if (http.responseText.indexOf('invalid') == -1) {
            result = http.responseText;
            if (result != "") {
                document.getElementById(totalVotes_ID).innerHTML = result;
				isPocessing = false;
            }
        }
    }
}

// Send comment rating to server and process the result
function handleRateHttpResponse() {
	var rating = new Array();
    if (http.readyState == 4) {
        if (http.responseText.indexOf('invalid') == -1) {
            result = http.responseText;
            if (result != "") {
				rating = result.split(",");
				if (eval(rating[0]) > 0) {
					rating[0] = "+" + rating[0];
				}
                document.getElementById(cmntRating_ID).innerHTML = rating[0];
                document.getElementById(posRating_ID).innerHTML = rating[1];
                document.getElementById(negRating_ID).innerHTML = rating[2];
				isPocessing = false;
            }
        }
    }
}

// Go get this HTTP Object thingy
function getHTTPObject() {
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
    return false;
}

// Hide content 
function hideContent(id) {
    // DOM3 = IE5, NS6
    if (document.getElementById) { 
        document.getElementById(id).style.display = 'none';
    } else {
        // Netscape 4
        if (document.layers) { 
            document.id.display = 'none';
        // IE 4
        } else { 
            document.all.id.style.display = 'none';
        }
    }
}

// Show content 
function showContent(id) {
    // DOM3 = IE5, NS6
    if (document.getElementById) { 
        document.getElementById(id).style.display = 'block';
    } else {
        // Netscape 4
        if (document.layers) {
            document.id.display = 'block';
        // IE 4
        } else { 
            document.all.id.style.display = 'block';
        }
    }
}

// Display the comment reply form
function showCommentReplyForm(reply_id, comment_id) {  
    var page_num = document.getElementById('page').innerHTML;

    document.getElementById(reply_id).innerHTML = "<form action=\"/include/class_deal_process.php\" name=\"frmPostCommentReply\" method=\"post\">" +
        "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
          "<tr><td><textarea name=\"comment\" class=\"commentReplyTextBox\"></textarea><br /><input type=\"submit\" value=\"Post Reply\" /> <input type=\"button\" value=\"Cancel\" onclick=\"hideContent('"+ reply_id +"')\" /></td></tr>" +
          "<tr><td><input type=\"hidden\" name=\"comment_id\" value="+ comment_id +" /></td></tr>" +
          "<tr><td><input type=\"hidden\" name=\"page_num\" value="+ page_num +" /></td></tr>" +
          "<tr><td><input type=\"hidden\" name=\"post_comment_reply\" value=\"1\" /></td></tr>" +
        "</table>" +
        "</form><br />"; 
    showContent(reply_id);
}
            
// Display the edit comment form
function editComment(id_num, comment_id, cmntreply_id) {
    var page_num = document.getElementById('page').innerHTML;
    var cmnt_id = "c" + id_num;
    var edit_id = "e" + id_num;
    var comment = filterText(document.getElementById(cmnt_id).innerHTML);
    hideContent(cmnt_id);

	if (cmntreply_id == 0) {
		document.getElementById(edit_id).innerHTML = "<form action=\"/include/class_deal_process.php\" name=\"frmEditComment\" method=\"post\">" +
			"<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
			  "<tr><td><textarea name=\"comment\" class=\"editCommentBox\">"+ comment +"</textarea><br /><input type=\"submit\" value=\"Save\" /> <input type=\"button\" value=\"Cancel\" onclick=\"cancelEditComment('"+ cmnt_id +"', '"+ edit_id +"')\" /></td></tr>" + 
			  "<tr><td><input type=\"hidden\" name=\"comment_id\" value="+ comment_id +" /></td></tr>" +
			  "<tr><td><input type=\"hidden\" name=\"page_num\" value="+ page_num +" /></td></tr>" +
			  "<tr><td><input type=\"hidden\" name=\"edit_comment\" value=\"1\" /></td></tr>" +
			"</table>" +
			"</form>"; 
	} else {
		document.getElementById(edit_id).innerHTML = "<form action=\"/include/class_deal_process.php\" name=\"frmEditComment\" method=\"post\">" +
			"<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
			  "<tr><td><textarea name=\"comment\" class=\"editCommentBox\">"+ comment +"</textarea><br /><input type=\"submit\" value=\"Save\" /> <input type=\"button\" value=\"Cancel\" onclick=\"cancelEditComment('"+ cmnt_id +"', '"+ edit_id +"')\" /></td></tr>" + 
			  "<tr><td><input type=\"hidden\" name=\"comment_id\" value="+ comment_id +" /></td></tr>" +
			  "<tr><td><input type=\"hidden\" name=\"comment_reply_id\" value="+ cmntreply_id +" /></td></tr>" +
			  "<tr><td><input type=\"hidden\" name=\"page_num\" value="+ page_num +" /></td></tr>" +
			  "<tr><td><input type=\"hidden\" name=\"edit_comment_reply\" value=\"1\" /></td></tr>" +
			"</table>" +
			"</form>"; 
	}
    showContent(edit_id);
}                

// Cancel edit by hiding the form
function cancelEditComment(cmnt_id, edit_id) {
    hideContent(edit_id);
    showContent(cmnt_id);
}

// Filter the text by removing some stuff
function filterText(text) {
    str = text;
    str = str.replace(/\n/gi, "");
    str = str.replace(/<br>/gi, "\n");
    str = str.replace(/<a href=\"/gi, "");
    str = str.replace(/\">.*?<\/a>/gi, "");
    return str;
}

// Rate comment up
function rateUp(id_num, cmnt_id, cmntreply_id) {
    var d_id = document.getElementById('d_id').innerHTML;
    var u_id = document.getElementById('u_id').innerHTML;
	var rateValue = 1;
    cmntRating_ID = "cr" + id_num;
	posRating_ID = "pr" + id_num;
	negRating_ID = "nr" + id_num;

    if (!isPocessing && http) {
        if (cmntreply_id == 0) {
            http.open("GET", "/rate_comment.php?did="+escape(d_id)+"&cid="+escape(cmnt_id)+"&uid="+escape(u_id)+"&r="+escape(rateValue), true);
        } else {
            http.open("GET", "/rate_comment.php?did="+escape(d_id)+"&cid="+escape(cmnt_id)+"&crid="+escape(cmntreply_id)+"&uid="+escape(u_id)+"&r="+escape(rateValue), true);
        }
        http.onreadystatechange = handleRateHttpResponse;
        isPocessing = true;
        http.send(null);
    }
    disableRating(id_num);
}

// Rate comment down
function rateDown(id_num, cmnt_id, cmntreply_id) {
    var d_id = document.getElementById('d_id').innerHTML;
    var u_id = document.getElementById('u_id').innerHTML;
	var rateValue = -1;
    cmntRating_ID = "cr" + id_num;
	posRating_ID = "pr" + id_num;
	negRating_ID = "nr" + id_num;

    if (!isPocessing && http) {
        if (cmntreply_id == 0) {
            http.open("GET", "/rate_comment.php?did="+escape(d_id)+"&cid="+escape(cmnt_id)+"&uid="+escape(u_id)+"&r="+escape(rateValue), true);
        } else {
            http.open("GET", "/rate_comment.php?did="+escape(d_id)+"&cid="+escape(cmnt_id)+"&crid="+escape(cmntreply_id)+"&uid="+escape(u_id)+"&r="+escape(rateValue), true);
        }
        http.onreadystatechange = handleRateHttpResponse;
        isPocessing = true;
        http.send(null);
    }
    disableRating(id_num);
}

// Disable the comment rating buttons
function disableRating(id_num) {
    var rateButton_id = "rb" + id_num;
    document.getElementById(rateButton_id).innerHTML = "<img class=\"rateButtons\" src=\"/img/rate_up_disable.gif\" width=\"12\" height=\"12\" border=\"0\" /><img class=\"rateButtons\" src=\"/img/rate_down_disable.gif\" width=\"12\" height=\"12\" border=\"0\" />";
}

// Show comment
function showComment(id_num) {
	var cmntBody_id = "cb" + id_num;
    var rateButton_id = "rb" + id_num;
	var disableRB_id = "drb" + id_num;

	hideContent(disableRB_id);
	showContent(cmntBody_id);
	showContent(rateButton_id);
}

// Hide comment
function hideComment(id_num) {
	var cmntBody_id = "cb" + id_num;
    var rateButton_id = "rb" + id_num;
	var disableRB_id = "drb" + id_num;

	showContent(disableRB_id);
	hideContent(cmntBody_id);
	hideContent(rateButton_id);
}

// Submit the vote
function submitVote(d_id, v_id, vb_id) {
	totalVotes_ID = v_id;
    var u_id = document.getElementById('u_id').innerHTML;
	document.getElementById(vb_id).innerHTML = "<img src=\"/img/voted.gif\" class=\"voteButton\" width=\"53\" height=\"14\" border=\"0\" />";
    
	if (!isPocessing && http) {
        http.open("GET", "/submit_vote.php?did="+escape(d_id)+"&uid="+escape(u_id), true);
        http.onreadystatechange = handleVoteHttpResponse;
        isPocessing = true;
        http.send(null);
    }
}
