﻿
var _CommentMaxLimit = 500;
var http_request = false;

var frmreplyprefix = "comment_form_id_";
var btnpostprefix = "imgPostComment";
var btndiscardprefix = "imgDiscardComment";
var txtCommentPrefix = "txtComment";
var watchcommentbody = "watch-comment-body";
var commentbody = "comment_body_";
var lblMessagePrefix = "lblMessage";
var lblCountPrefix = "lblCount";
var spamvotecountPrefix = "span_votecount";
var spamvoteactionPrefix = "span_voteaction";
var spamprefix = "markasspam";
var showlink = "showlink";
var hidelink = "hidelink";
var replylink = "replylink";
var removelink = "removelink";
var approvelink = "approvelink";


//For Edit Funtionality
var btnEditprefix = "imgEditComment";
var txtEditCommentPrefix = "txtEditComment";

/**************************************************************************************************/
function updateCharCount(textArea, label_id, bLimit, event) {
    //ValidateText(event, textArea);
    if (bLimit == "True") {
        var currentVal
        if (textArea.value.length > _CommentMaxLimit)
            textArea.value = textArea.value.substring(0, _CommentMaxLimit);
        else
            document.getElementById(label_id).innerHTML = _CommentMaxLimit - textArea.value.length;
    }
}

//For reset the comment textbox
function discard(textArea) {
    try {
        document.getElementById(textArea).value = "";
        document.getElementById(textArea).focus();
    } catch (e) { }

}
function discardSub(commentid, bLimit) {

    try {
        if (document.getElementById(txtEditCommentPrefix + commentid))
            document.getElementById(txtEditCommentPrefix + commentid).value = document.getElementById(watchcommentbody + commentid).innerHTML.replace(/<br>/g, ''); ;

        document.getElementById(txtCommentPrefix + commentid).value = "";
        if (bLimit == "True")
            document.getElementById(lblCountPrefix + commentid).innerHTML = _CommentMaxLimit;
        document.getElementById(frmreplyprefix + commentid).style.display = "none";
    } catch (e) { }

}

function validcomment(textArea) {

    var txt = document.getElementById(textArea).value.trim();
    if (txt.length < 1) {
        alert("You must enter a comment.");
        return false;
    }
}

function showCommentReplyForm(comment_form_id, id) {

    var frm = document.getElementById(comment_form_id);
    if (frm != null) {

        frm.style.display = "";
        //show edit object
        if (document.getElementById(btnEditprefix + id))
            document.getElementById(btnEditprefix + id).style.display = "none";
        if (document.getElementById(txtEditCommentPrefix + id))
            document.getElementById(txtEditCommentPrefix + id).style.display = "none";

        //hide add objects    
        if (document.getElementById(txtCommentPrefix + id))
            document.getElementById(txtCommentPrefix + id).style.display = "";
        if (document.getElementById(btnpostprefix + id))
            document.getElementById(btnpostprefix + id).style.display = "";
    }
}

function postCommentReplyForm(textArea, RecipeId, parentid, active) {
    
    vComment = document.getElementById(textArea).value.trim();
    vComment = vComment.replace(/&/g, '%26');
    if (vComment.length < 1) {
        alert("You must enter a comment.");
        document.getElementById(textArea).focus();
        return false;
    }
    else {
        var poststr = "txtcomment=" + encodeURI(vComment) +
                    "&RecipeId=" + RecipeId + "&parentid=" + parentid + "&active=" + active;

        makePOSTRequest('/App_UI/site/pHion/Public/Recipe/RecipeComment.aspx', poststr);

        document.getElementById(textArea).disabled = true;

        document.getElementById(btnpostprefix + parentid).src = "/images/site/pHion/v2/btn_post_comment_disabled.gif";
        document.getElementById(btndiscardprefix + parentid).src = "/images/site/pHion/v2/btn_discard_disable.gif";
        document.getElementById(btnpostprefix + parentid).disabled = true;
        document.getElementById(btndiscardprefix + parentid).disabled = true;

        document.getElementById("lblMessage" + parentid).innerHTML = "Thanks for comment.";
    }
}


function displayHideCommentLink(commentid, RecipeId, voted, active) {
    document.getElementById(hidelink + commentid).style.display = "inline";
    document.getElementById(hidelink + commentid).style.visibility = "visible";

    document.getElementById(showlink + commentid).style.display = "none";
    document.getElementById(showlink + commentid).style.visibility = "hidden";
    document.getElementById(commentbody + commentid).style.display = "inline";

    if ((voted == "0" || voted == "False") && (active == "1" || active == "True"))
        var voteaction = "<span id=\"span_voteaction" + commentid + "\"><a onclick=\"voteComment('" + commentid + "', '" + RecipeId + "', '-1');\"><img src=\"/images/site/pHion/common/Thumbs.Down_Up.gif\" class=\"watch-comment-down-hover\" title=\"Poor comment\" alt=\"Poor comment\" onmouseover=\"this.src='/images/site/pHion/common/Thumbs.Down_Over.gif'\" onmouseout=\"this.src='/images/site/pHion/common/Thumbs.Down_Up.gif'\"></a>" +
            "&#160;<a onclick=\"voteComment('" + commentid + "', '" + commentid + "', '1');\"><img src=\"/images/site/pHion/common/Thumbs.Up_Up.gif\" class=\"watch-comment-down-hover\" title=\"Good comment\" alt=\"Good comment\" onmouseover=\"this.src='/images/site/pHion/common/Thumbs.Up_Over.gif'\" onmouseout=\"this.src='/images/site/pHion/common/Thumbs.Up_Up.gif'\"></a></span>";
    else
        var voteaction = "<span id=\"span_voteaction" + commentid + "\"><img src=\"/images/site/pHion/common/Thumbs.Down_Up.gif\" class=\"watch-comment-down-hover\" title=\"Poor comment\" alt=\"Poor comment\" >" +
            "&#160;<img src=\"/images/site/pHion/common/Thumbs.Up_Up.gif\" class=\"watch-comment-down-hover\" title=\"Good comment\" alt=\"Good comment\"></span>";
    document.getElementById(spamvoteactionPrefix + commentid).innerHTML = voteaction;

}
function displayShowCommentLink(commentid) {
    document.getElementById(showlink + commentid).style.display = "inline";
    document.getElementById(showlink + commentid).style.visibility = "visible";

    document.getElementById(hidelink + commentid).style.display = "none";
    document.getElementById(hidelink + commentid).style.visibility = "hidden";

    document.getElementById(commentbody + commentid).style.display = "none";
    var voteaction = "<span id=\"span_voteaction" + commentid + "\"><img src=\"/images/site/pHion/common/Thumbs.Down_Up.gif\" class=\"watch-comment-down-hover\" title=\"Poor comment\" alt=\"Poor comment\" >" +
            "&#160;<img src=\"/images/site/pHion/common/Thumbs.Up_Up.gif\" class=\"watch-comment-down-hover\" title=\"Good comment\" alt=\"Good comment\"></span>";
    document.getElementById(spamvoteactionPrefix + commentid).innerHTML = voteaction;
}

function removecomment(commentid) {
    if (confirm("Do you really want to remove this comment?")) {
        var poststr = "remove=1&commentid=" + commentid;
        makePOSTRequest('/App_UI/site/pHion/Public/Recipe/RecipeComment.aspx', poststr);
        document.getElementById("reply_comment_form_id_" + commentid).innerHTML = "<span class='watch-comment-spam-bug'>Removed</span>";

        var voteaction = "<span id='span_voteaction" + commentid + "'><img src='/images/site/pHion/common/Thumbs.Down_Up.gif' class='watch-comment-down-hover' title='Poor comment' alt='Poor comment'>" +
    "&#160;<img src='/images/site/pHion/common/Thumbs.Up_Up.gif' class='watch-comment-down-hover' title='Good comment' alt='Good comment'/> </span>";
        //document.getElementById(spamvoteactionPrefix+commentid).innerHTML =voteaction;            
    }
}

function approvecomment(commentid, RecipeId) {
    var poststr = "approve=1&commentid=" + commentid;
    makePOSTRequest('/App_UI/site/pHion/Public/Recipe/RecipeComment.aspx', poststr);
    document.getElementById(approvelink + commentid).innerHTML = "Approved";
    document.getElementById(approvelink + commentid).className = "watch-comment-spam-bug";
    var voteaction = "<span id=\"span_voteaction" + commentid + "\"><a onclick=\"voteComment('" + commentid + "', '" + RecipeId + "', '-1');\"><img src=\"/images/site/pHion/common/Thumbs.Down_Up.gif\" class=\"watch-comment-down-hover\" title=\"Poor comment\" alt=\"Poor comment\" onmouseover=\"this.src='/images/site/pHion/common/Thumbs.Down_Over.gif'\" onmouseout=\"this.src='/images/site/pHion/common/Thumbs.Down_Up.gif'\"></a>" +
            "&#160;<a onclick=\"voteComment('" + commentid + "', '" + commentid + "', '1');\"><img src=\"/images/site/pHion/common/Thumbs.Up_Up.gif\" class=\"watch-comment-down-hover\" title=\"Good comment\" alt=\"Good comment\" onmouseover=\"this.src='/images/site/pHion/common/Thumbs.Up_Over.gif'\" onmouseout=\"this.src='/images/site/pHion/common/Thumbs.Up_Up.gif'\"></a></span>";
    document.getElementById(spamvoteactionPrefix + commentid).innerHTML = voteaction;
    var scomment = document.getElementById(watchcommentbody + commentid).innerHTML;
    document.getElementById(watchcommentbody + commentid).innerHTML = scomment.replace('<strong>New Comment (Pending Approval)<br></strong>', '');

}

function ValidateText(e, txt) {
    var evt = (e) ? e : (window.event) ? window.event : null;
    if (evt) {
        var key = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
        if (key != "8" && key != "35" && key != "36" && key != "37" && key != "39" && key != "46" && key != "50" && !(key >= "51" && key <= "56") && !(key >= "65" && key <= "90")) {
            var str = txt;
            var SearchTxt = str.value;
            SearchTxt = SearchTxt.replace('<', '');
            SearchTxt = SearchTxt.replace('>', '');
            str.value = SearchTxt;

        }
    }
}

function adjustDivs() {

    var df = document.getElementById('progTemplate');
    var foreItem = document.getElementById('progDisplay');
    var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    scrollTop = scrollTop - 50;
    var clientWidth;
    if (window.innerWidth) {
        clientWidth = (window.__safari ? window.innerWidth : Math.min(window.innerWidth, document.documentElement.clientWidth));
    } else {
        clientWidth = document.documentElement.clientWidth;
    }
    var clientHeight;
    if (window.innerHeight) {
        clientHeight = (window.__safari ? window.innerHeight : Math.min(window.innerHeight, document.documentElement.clientHeight));
    } else {
        clientHeight = document.documentElement.clientHeight;
    }
    if (df != null)
        df.style.position = 'absolute';
    if (foreItem != null)
        foreItem.style.position = 'absolute';

    df.style.left = scrollLeft + 'px';
    df.style.top = scrollTop + 50 + 'px';
    df.style.width = clientWidth + 'px';
    df.style.height = clientHeight + 'px';
    foreItem.style.left = scrollLeft + ((clientWidth - foreItem.offsetWidth) / 2) + 'px';
    foreItem.style.top = scrollTop + ((clientHeight - foreItem.offsetHeight) / 2) + 'px';

}

/*Edit Funtionality*/
function showCommentEditForm(comment_form_id, id) {
    var frm = document.getElementById(comment_form_id);
    if (frm != null) {

        frm.style.display = "";
        //show edit object
        if (document.getElementById(btnEditprefix + id))
            document.getElementById(btnEditprefix + id).style.display = "";
        if (document.getElementById(txtEditCommentPrefix + id))
            document.getElementById(txtEditCommentPrefix + id).style.display = "";

        //hide add objects    
        if (document.getElementById(txtCommentPrefix + id))
            document.getElementById(txtCommentPrefix + id).style.display = "none";
        if (document.getElementById(btnpostprefix + id))
            document.getElementById(btnpostprefix + id).style.display = "none";
    }
}

function postCommentEditForm(textArea, commentid) {
    vComment = document.getElementById(textArea).value.trim();
    vComment = vComment.replace(/&/g, '%26');

    if (vComment.length < 1) {
        alert("You must enter a comment.");
        document.getElementById(textArea).focus();
        return false;
    }
    else {
        var poststr = "edit=1&txtcomment=" + encodeURI(vComment) +
                    "&commentid=" + commentid;
        makePOSTRequest('/App_UI/site/pHion/Public/Recipe/RecipeComment.aspx', poststr);

        document.getElementById(textArea).disabled = true;

        document.getElementById(btnEditprefix + commentid).src = "/images/site/pHion/v2/btn_post_comment_disabled.gif";
        document.getElementById(btndiscardprefix + commentid).src = "/images/site/pHion/v2/btn_discard_disable.gif";
        document.getElementById(btnEditprefix + commentid).disabled = true;
        document.getElementById(btndiscardprefix + commentid).disabled = true;
        document.getElementById("lblMessage" + commentid).innerHTML = "Comment has been updated.";
    }
}


