﻿// Javascript Namespace.
var CommentForm = function() {

    // form fields
    var txtFullName;
    var txtEmail;
    var txtWebSite;
    var txtComment;
    
    // validation messages
    var fullNameValidation;
    var emailValidation;
    var commentValidation;

    // status indicators
    var sendingMessage;
    var confirmationMessage;
    var errorMessage;
    
    // buttons
    var submitButton

    // Initialization
    $(document).ready(function() {

        // Get DOM references.
        txtFullName = $(".comment-form #txtFullName");
        txtEmail = $(".comment-form #txtEmail");
        txtWebSite = $(".comment-form #txtWebsite");
        txtComment = $(".comment-form #txtComment");
        sendingMessage = $(".comment-form #sendingMessage");
        confirmationMessage = $(".comment-form #confirmationMessage");
        errorMessage = $(".comment-form #errorMessage");
        submitButton = $(".comment-form #btnSendComment")
        fullNameValidation = $(".comment-form #FullNameValidator")
        emailValidation = $(".comment-form #EmailValidator")
        commentValidation = $(".comment-form #CommentValidator")

        // set up the focus behavior handler for all form text boxes.
        $(".comment-form .defaultText").focus(function(srcc) {
            if ($(this).val() == $(this)[0].title) {
                $(this).removeClass("defaultTextActive");
                $(this).val("");
            }
        });

        // set up the blur behavior handler for all form text boxes.
        $(".comment-form .defaultText").blur(function() {
            if ($(this).val() == "") {
                $(this).addClass("defaultTextActive");
                $(this).val($(this)[0].title);
            }
        });

        // Blur all text boxes.
        $(".comment-form .defaultText").blur();

        // Set up the click handler for the form button.
        submitButton.click(function() {
            if (validateUserInput()) {
                disableUserInput();
                sendCommentForm();
            }
        });

    });

    function disableUserInput() {
        txtFullName.attr("disabled", "true");
        txtEmail.attr("disabled", "true");
        txtWebSite.attr("disabled", "true");
        txtComment.attr("disabled", "true");
        sendingMessage.attr("disabled", "true");
        confirmationMessage.attr("disabled", "true");
        errorMessage.attr("disabled", "true");
        submitButton.attr("disabled", "true");
        submitButton.attr("value", "Please Wait...");
    }

    function enableUserInput() {
        txtFullName.removeAttr('disabled');
        txtEmail.removeAttr('disabled');
        txtWebSite.removeAttr('disabled');
        txtComment.removeAttr('disabled');
        sendingMessage.removeAttr('disabled');
        confirmationMessage.removeAttr('disabled');
        errorMessage.removeAttr('disabled');
        submitButton.removeAttr('disabled');
        submitButton.attr("value", "Submit Comment");
    }
    
    function resetControls() {
        $(txtFullName).val($(txtFullName)[0].title);
        $(txtEmail).val($(txtEmail)[0].title)
        $(txtWebSite).val($(txtWebSite)[0].title);
        $(txtComment).val($(txtComment)[0].title);      
        $(".comment-form .defaultText").addClass("defaultTextActive");
    }

    function validateUserInput() {
        var isInputValid = true;
        if (!validateName()) { isInputValid = false; };
        if (!validateEmail()) { isInputValid = false; };
        if (!validateComment()) { isInputValid = false; };
        return isInputValid;
    }

    function validateName() {
        var isNameValid = true;
        if (txtFullName.attr("title") == txtFullName.val() || IsEmpty(txtFullName.val())) {
            isNameValid = false;           
            fullNameValidation.slideDown("slow");
        } else {
            fullNameValidation.hide();
        }        
        return isNameValid
    }
    
    function validateEmail() {
        var isEmailValid = true;
        if (txtEmail.attr("title") == txtEmail.val() || IsEmpty(txtEmail.val())) {
            isEmailValid = false;
            emailValidation.slideDown("slow");
        } else {
            emailValidation.hide();
        }
        return isEmailValid
    }

    function validateComment() {
        var isCommentValid = true;
        if (txtComment.attr("title") == txtComment.val() || IsEmpty(txtComment.val())) {
            isCommentValid = false;
            commentValidation.slideDown("slow");
        } else {
            commentValidation.hide();
        }
        return isCommentValid
    }
        
function sendCommentForm() {

    sendingMessage.slideDown("slow");
    confirmationMessage.hide();
    errorMessage.hide();
    
    var fullName = txtFullName.val();
    var emailAddress = txtEmail.val();
    var website = txtWebSite.val();
    var blogPostID = $(".blog-post").find(":input").val();
    var comment = txtComment.val();
    
    if (comment == "www.google.ca") {
        website = "";
    }   
    
    $.ajax({
        type: "POST",
        url: "../../../../../../App_Services/BlogComment.asmx/SubmitComment",
        data: "{'fullName':'" + fullName + "','emailAddress':'" + emailAddress + "','website':'" + website + "','comment':'" + comment + "','blogPostID':'" + blogPostID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success:
            function(msg) {
                if (msg.d == "Success") {
                    confirmationMessage.slideDown("slow");
                    sendingMessage.hide("slow");
                    resetControls();
                    enableUserInput();
                } else {
                    errorMessage.slideDown("slow");
                    sendingMessage.hide("slow");
                    enableUserInput();
                }
            },
        error: function(xhr, status, error) {
            // Display a generic error for now.
            sendingMessage.hide("slow");
            errorMessage.slideDown("slow");
        }
    });   
}

function IsEmpty(String) {
    if ((String.trim().length == 0) ||
    (String.trim() == null)) {
        return true;
    }
    else { return false; }
}

String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

} ();