﻿var intContactListEntryID = 0;

function PostComment(intListEntryID) {
    intContactListEntryID = intListEntryID;
    var strName = document.getElementById("inputName").value;
    var strEmail = document.getElementById("inputEmail").value;
    var strComment = document.getElementById("inputComment").value;

    if (IsValidEmail($("#inputEmail").val()))
    {
        if ($("#inputName").val() != "" && $("#inputComment").val() != "") {
            Render.PostComment(
                intListEntryID,
                $("#inputName").val(),
                $("#inputEmail").val(),
                $("#inputWebsite").val(),
                $("#inputComment").val(),
                $("#inputCaptcha").val(),
                PostComment_callback);
        }
        else {
            $("#divCommentError").html("Name and comment are required");
            $("#divCommentError").css("display", "block");
        }
    }
    else {
        $("#divCommentError").html("Invalid email address");
        $("#divCommentError").css("display", "block");
    }
}

function PostComment_callback(objResponse) {
    if (objResponse.value) {
        Render.GetListEntryComments(intContactListEntryID, GetListEntryComments_callback);
    }
    else {
        $("#divCommentError").html("Code did not match");
        $("#divCommentError").css("display", "block");
        $("#imgCaptcha").attr("src", "/images/captcha?height=100&width=200&dt" + new Date());
    }
}

function GetListEntryComments_callback(objResponse) {
    if (objResponse.value) {
        document.getElementById("divPostedComments").innerHTML = objResponse.value;
        document.getElementById("inputName").value = '';
        document.getElementById("inputEmail").value = '';
        $("#inputWebsite").val("");
        $("#inputCaptcha").val("");
        $("#divCommentError").css("display", "none");
        $("#divCommentError").html("");
        $("#imgCaptcha").attr("src", "/images/captcha?height=100&width=200&dt" + new Date());
        document.getElementById("inputComment").value = '';
    }
}

function SendEmail(strRootURL, intListEntryID) {
    var strNameFrom = document.getElementById("inputNameFrom").value;
    var strEmailFrom = document.getElementById("inputEmailFrom").value;
    var strEmailTo = document.getElementById("inputEmailTo").value;
    var strMessage = document.getElementById("inputMessage").value;

    Render.SendEmail(
        strRootURL,
        intListEntryID,
        strEmailFrom,
        strEmailTo,
        strNameFrom,
        strMessage,
        SendEmail_callback);
}

function SendEmail_callback(objResponse) {
    document.getElementById("divEmail").innerHTML = "Email Sent!";
}
