﻿function SetFormMessage(text)
{
    $('#sendToFriendMessage').css('display', 'block');
    $('#sendToFriendMessage').text(text);
}
                
function ValidateForm()
{
    if ($('#txtName').val().length == 0 || $('#txtName').val() == 'Your first name')
    {
        SetFormMessage("Your name is required.");
        return false;
    }
    if ($('#txtEmail').val().length == 0 || $('#txtEmail').val() == 'Your email address')
    {
        SetFormMessage("Your email address is required.");
        return false;
    }
    if ($('#txtFriendEmail').val().length == 0 || $('#txtFriendEmail').val() == "Your friend's email address")
    {
        SetFormMessage("Your friend's email address is required.");
        return false;
    }
    
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    
    if (!emailPattern.test($('#txtEmail').val()))
    {
        SetFormMessage("Your email address is not in the correct format.");
        return false;
    }
    
    if (!emailPattern.test($('#txtFriendEmail').val()))
    {
        SetFormMessage("Your friend's email address is not in the correct format.");
        return false;
    }
    
    return true;    
}
                
$(document).ready(function()
    {
        swapValues=[];
        $(".sendToFriendInput").each(
            function(i){
                swapValues[i]=$(this).attr("watermarktext");
                $(this).val(swapValues[i]);
                
                $(this).focus(function()
                {
                    if($(this).val()==swapValues[i])
                    {
                        $(this).val("")
                        $(this).removeClass("sendToFriendWatermark");
                    }
                }
                ).blur(function()
                {
                if($.trim($(this).val())=="")
                {
                    $(this).val(swapValues[i])
                    $(this).addClass("sendToFriendWatermark");
                }
            }
            )
        }
        )
        
        $('#frmSendToFriend').submit(function()
        {
            if (ValidateForm())
            {
                // send data
                var inputs = [];
                $(':input', this).each(function()
                {
                    inputs.push(this.name + '=' + escape(this.value));
                });
                
                jQuery.ajax(
                {
                    data: inputs.join("&"),
                    url: "/templates/forms/sendtofriend.aspx",
                    dataType: "json",
                    timeout: 2000,
                    type: 'POST',
                    error: function(request, textStatus, errorThrown)
                    {
                        SetFormMessage('An error occurred when sending your email.');
                    },
                    success: function(data)
                    {
                        if (data.code == 1)
                        {
                            // reset form
                            $(".sendToFriendInput").each(
                                function(i){
                                    $(this).val($(this).attr("watermarktext")).addClass("sendToFriendWatermark");
                                });
                                                        
                            SetFormMessage(data.message);
                        }
                        else
                        {
                            SetFormMessage(data.message);
                        }
                    }
                });
            }
            
            return false;
        });
    }
);