﻿var intContactListEntryID = 0;

$(document).ready(function () {

    // if user clicked on button, the overlay layer or the dialogbox, close the dialog  
    $('#mp-list-entry-dialog-overlay, #mp-list-entry-dialog-box').click(function () {
        $('#mp-list-entry-dialog-overlay, #mp-list-entry-dialog-box').hide();
        return false;
    });
    // if user resize the window, call the same function again
    // to make sure the overlay fills the screen and dialogbox aligned to center    
    $(window).resize(function () {

        //only do it if the dialog box is not hidden
        if (!$('#mp-list-entry-dialog-box').is(':hidden')) popup();
    });
    $('.mp-list-entry-dialog-content').append('<br />Press &lt;CTRL&gt;-C to copy the link.');
});

function PostComment(intListEntryID) {
    intContactListEntryID = intListEntryID;
    var strName = $("#inputName").val().replace("'", "\\'").replace("\"", "\\\"");
    var strEmail = $("#inputEmail").val();
    var strComment = $("#inputComment").val().replace("'", "\\'").replace("\"", "\\\"");
    var strWebsite = $("#inputWebsite").val().replace("'", "\\'").replace("\"", "\\\"");
    var strCaptcha = $("#inputCaptcha").val().replace("'", "\\'").replace("\"", "\\\"");

    if (IsValidEmail(strEmail)) {
        //alert("{intListEntryID: " + intListEntryID + ", strName: '" + strName + "', strEmail: '" + strEmail + "', strWebsite: '" + strWebsite + "', strComment: '" + strComment + "', strCaptcha: '" + strCaptcha + "', blnSendPostAlerts: " + (($('#inputSendPostAlerts').attr('checked') == 'checked') ? 'true' : 'false') + " }");
        if (strName != "" && strComment) {

            $.ajax({
                type: "POST",
                url: MPRootURL + "ajax/List.aspx/SaveListEntryComment",
                contentType: "application/json; charset=utf-8",
                data: "{intListEntryID: " + intListEntryID + ", strName: '" + strName + "', strEmail: '" + strEmail + "', strWebsite: '" + strWebsite + "', strComment: '" + strComment + "', strCaptcha: '" + strCaptcha + "', blnSendPostAlerts: " + (($('#inputSendPostAlerts').attr('checked') == 'checked') ? 'true' : 'false') + " }",
                dataType: "html",
                success: function AjaxSucceeded(e, xhr, settings) {
                    var result = $.parseJSON(e).d;
                    if (result != "error-captcha") {
                        $("#divPostedComments").replaceWith(result);
                        $('<div class="mp-list-entry-comment"><div class="mp-list-entry-comment-name MPListEntryDefault">Your Comment</div><div class="mp-list-entry-comment-content MPListEntryDefault">Your comment will not show until it has been approved.</div><br><div class="mp-list-entry-comment-date MPListEntrySub">Posted Just Now</div></div><br /><br /><br />').insertBefore('#mpListEntryCommentFormWrapper');
                        $("#inputName").val('');
                        $("#inputEmail").val('');
                        $("#inputWebsite").val('');
                        $("#inputCaptcha").val('');
                        $("#divCommentError").css('display', 'none');
                        $("#divCommentError").html('');
                        $("#imgCaptcha").attr('src', '/images/captcha?h=100&w=200&dt=' + new Date());
                        $("#inputComment").val('');
                    }
                    else {
                        $("#divCommentError").html("Sorry, you passed an invalid code. Please try again.");
                        $("#divCommentError").css("display", "block");
                        $("#imgCaptcha").attr('src', '/images/captcha?h=100&w=200&dt=' + new Date());
                    }
                },
                error: function AjaxFailed() { alert('Sorry, there was an error posting your comment'); }
            });
        }
        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?h=100&w=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?h=100&w=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!";
}


function ShowListEntryPermalink(url) {

    // get the screen height and width  
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    // calculate the values for center alignment
    var dialogTop = (maskHeight / 3) - ($('#mp-list-entry-dialog-box').height());
    var dialogLeft = (maskWidth / 2) - ($('#mp-list-entry-dialog-box').width() / 2);

    // assign values to the overlay and dialog box
    $('#mp-list-entry-dialog-overlay').css({ height: maskHeight, width: maskWidth }).show();
    $('#mp-list-entry-dialog-box').css({ top: dialogTop, left: dialogLeft }).show();

    // display the message
    $('#mp-list-entry-dialog-message').html(url);


    

    $('#mp-list-entry-dialog-message').select();
}
