﻿$(document).ready(function() {
    
    try {
        var max = 350;
        var len = ($("#jNewsArticleComment").val().length);
        $("#jNewsArticleCommentCharLimit").html((max-len)+" characters");
    } catch(exception){} // errors when the user is not logged in (ie. we're not dispalying the textarea). Catch to stop browser error.
    
    $("#jAddNewsArticleComment").click(function() {
        $("#jAddCommentResponse").slideUp(300, function() { // clear current errors
            $("#jAddCommentResponse").html("");
        
            var rxStr=/^[^']+$/;
            var sComment = $("#jNewsArticleComment").val();
            //if(!Boolean(rxStr.test($("#jRecipeComment").val()))) { // on click we need to validate the comment box.
            if(false) {
                $("#jAddCommentResponse").slideUp(100, function() {
                    $("#jAddCommentResponse").html("<strong>Error</strong><br />There were invalid characters in your comment. Please note that single quotes (') are not allowed.");
                    $("#jAddCommentResponse").slideDown(500);
                });
            } else { // if valid... 
                var nId = $("input[id*='hidNewsArticleId']").val();
                $("#jAddCommentNewsArticleBox").slideUp(800, function() { // ... hide controls ...
                    $("#jAddNewsArticleCommentLoader").css("visibility","visible");
                    $("#jAddNewsArticleCommentLoader").css("display","block");
                    $("#jAddNewsArticleCommentLoader").slideDown(500, function() { // ... show loader ...
                        $.ajax ({       // ... send ajax call ...
                            type: "POST",
                            cache: false,
                            url: "Resource/ajax/submitNewsArticleComment.aspx",
                            data: "article="+nId+"&comment="+sComment,
                            dataType: "xml",
                            success: handleNewsArticleCommentPass,
                            error: handleNewsArticleCommentError
                        });
                    });
                });
            }
        });
    });
    
    $("#jNewsArticleComment").keydown(function(e) {
        var max = 350;
        var len = ($(this).val().length);
        //if(e.Which!=8){len++;}
        //$("#jRecipeCommentCharLimit").html((max-len)+"");
        if(e.which==8) {
            if(len>0) {
                len--;
            }
            $("#jNewsArticleCommentCharLimit").html((max-len)+" characters");
        } else {
            len++;
            var remainder = parseInt(max-len);
            $("#jNewsArticleCommentCharLimit").html((max-len)+" characters");
            if(remainder<1) {
                return false;
            }
        }
    });
});


function handleNewsArticleCommentPass(xml, textStatus) {
    if($(xml).find("error").length<1) {
        $("#jAddNewsArticleCommentLoader").slideUp(600, function() { // ... hide loader ...
            $("#jAddCommentResponse").html("<strong>Thanks</strong><br />Your comment has been submitted - it will be visible once our Admin team has authorised the comment.");
            $("#jAddCommentResponse").slideDown(500);
        });
    } else {
        handleNewsArticleCommentError(null,null,null);
    }
}

function handleNewsArticleCommentError(XMLHttpRequest, textStatus, errorThrown) {
    $("#jAddNewsArticleCommentLoader").slideUp(600, function() { // ... hide loader ...
        $("#jAddCommentNewsArticleBox").slideDown(800, function() { // Show controls ...
            $("#jAddCommentResponse").html("<strong>Error</strong><br />A server error occured and your comment was not successfully submitted. Please try again later.");
            $("#jAddCommentResponse").slideDown(500);
        });
    });
}
