﻿$(document).ready(function() {

    $("#EmailAFriend").jqm({
        trigger: $("#BtnEmailAFriend"),
        modal: false,
        overlay:50,
        closeClass: "popup-but-close",
        onShow: function(hash){
            var isReferral = $("#IsReferAFriend").val();
            if (isReferral == "true")
            {
                $("#EmailFriendTitle").hide()
                $("#EmailFriendInfo").hide()
                $("#ReferFriendTitle").show()
                $("#ReferFriendInfo").show()
                $(".txtEmailFriendMessage").val("");
            }
            else
            {
                $("#EmailFriendTitle").show()
                $("#EmailFriendInfo").show()
                $("#ReferFriendTitle").hide()
                $("#ReferFriendInfo").hide()
                $(".txtEmailFriendMessage").val($(".EmailFriendMessage").val())
                $("#IsReferAFriend").val(false);
            }
            hash.w.css("position","absolute");
            hash.w.show();
        },
        onHide: function(hash){
            hash.w.fadeOut('4000', function(){
                hash.o.remove();
                $(".txtFriendFirstName").val("");
                $(".txtFriendEmail").val("");
            });
        }
    });
    
    $(".SubmitReferFriendEmail").click(function(event){
        event.preventDefault();
        
        var yourFirstName = $(".txtYourFirstName").val();
        var yourEmail = $(".txtYourEmail").val();
        var friendFirstName = $(".txtFriendFirstName").val();
        var friendEmail = $(".txtFriendEmail").val();
        var message = $(".txtEmailFriendMessage").val();
        if (validateEmailFriend(yourFirstName, yourEmail, friendFirstName, friendEmail))
        {
            var isReferral = $("#IsReferAFriend").val();
            var template = $("#ReferFriendTemplateName").val();
            var url = "/AjaxServices/FormSubmissionService.asmx/EmailFriend";
            var data = [];
            data.push("{");
            data.push("'yourFirstName':'" + yourFirstName + "', 'friendFirstName':'" + friendFirstName + "', 'yourEmailAddress':'" + yourEmail + "', 'friendEmailAddress':'" + friendEmail + "', 'message':'" + message + "'");
            if (isReferral == "true")
            {
                url = "/AjaxServices/FormSubmissionService.asmx/ReferFriend"
                data.push(", 'template':'" + template + "'");
            }
            data.push("}");
            $.ajax({
                type: "POST",
                url: url,
                data: data.join(''),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    $("#EmailAFriend").jqmHide();
                }
            });
            location.hash = "";
        }
    });
    var hash = location.hash;
    var template = hash.substring(14,hash.length);
    if (hash.substring(0,13) == "#ReferAFriend")
    {
        $("#IsReferAFriend").val(true);
        $("#ReferFriendTemplateName").val("" + hash.substring(14,hash.length));
        $("#EmailAFriend").jqmShow();
    }
});

function validateEmailFriend(yourFirstName, yourEmail, friendFirstName, friendEmail)
{
    var errors = [];
    if (yourFirstName.length == 0)
        errors.push("<li>* Your first name is required</li>");
    if (yourEmail.length == 0)
        errors.push("<li>* Your e-mail address is required</li>");
    else if (validateEmailAddress(yourEmail) == false)
        errors.push("<li>* Your e-mail address must be valid</li>");
    if (friendFirstName.length == 0)
        errors.push("<li>* Your friend's first name is required</li>");
    if (friendEmail.length == 0)
        errors.push("<li>* Your friend's e-mail address is required</li>");
    else if (validateEmailAddress(friendEmail) == false)
        errors.push("<li>* Your friend's e-mail address must be valid</li>");
    var errorStr = errors.join('');
    if (errorStr.length > 0)
    {
        $("ul#EmailAFriendErrors").empty().append(errorStr).show();
        return false;
    }
    else
    {
        $("ul#EmailAFriendErrors").empty().hide();
        return true;
    }
}

function validateEmailAddress(email)
{
    var emailRegExp = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    return emailRegExp.test(email);
}
