/* 
 * This JavaScript is for the AJAX send boat to form.
 */
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

$(function(){
    $("#dialog").dialog({
        bgiframe: false,
        autoOpen: false,
        width: 440,
        height: 380,
        modal: true,
        draggable: false,
        buttons: {
            "Close": function() {
                $(this).dialog("close");
            },
            "Send": function(){
                /*******
                 * Peform the validation of the form
                 */
                if( $("#name").val()=="" )
                {
                    alert( "Please enter your name." );
                    $("#name").attr( 'class', 'invalid' );
                    $("#name").focus();
                    return false;
                }
                else
                {
                    $("#name").attr( 'class', 'valid' );
                }

                if( $("#sendTo").val()=="" )
                {
                    alert( "Please an email address to send to." );
                    $("#sendTo").attr( 'class', 'invalid' );
                    $("#sendTo").focus();
                    return false;
                }
                else if( !filter.test( $("#sendTo").val()) )
                {
                    alert( "Email address to send to is invalid.\n" +
                        "Please enter a valid email address." );
                    $("#sendTo").attr( 'class', 'invalid' );
                    $("#sendTo").focus();
                    return false;
                }
                else
                {
                    $("#sendTo").attr( 'class', 'valid' );
                }

                if( $("#sendFrom").val()=="" )
                {
                    alert( "Please an email address to send from." );
                    $("#sendFrom").attr( 'class', 'invalid' );
                    $("#sendFrom").focus();
                    return false;
                }
                else if( !filter.test( $("#sendFrom").val()) )
                {
                    alert( "Email address to send from is invalid.\nPlease enter a valid email address." );
                    $("#sendFrom").attr( 'class', 'invalid' );
                    $("#sendFrom").focus();
                    return false;
                }
                else
                {
                    $("#sendFrom").attr( 'class', 'valid' );
                }

                if( $("#subject").val()=="" )
                {
                    alert( "Please enter a subject." );
                    $("#subject").attr( 'class', 'invalid' );
                    $("#subject").focus();
                    return false;
                }
                else
                {
                    $("#subject").attr( 'class', 'valid' );
                }

                if( $("#message").val()=="" )
                {
                    alert( "Please eneter a message." );
                    $("#message").attr( 'class', 'invalid' );
                    $("#message").focus();
                    return false;
                }
                else
                {
                    $("#message").attr( 'class', 'valid' );
                }

                $.post( '/send_boat_to_submit.php',
                {
                    name: $("#name").val(),
                    sendFrom: $("#sendFrom").val(),
                    sendTo: $("#sendTo").val(),
                    copySender: $("#copySender").val(),
                    subject: $("#subject").val(),
                    message: $("#message").val(),
                    linkToBoat: $("#linkToBoat").val()
                },
                function( data ){
                    if( data==true )
                    {
                        $("#theForm").fadeOut( 100 );
                        $("#sent").fadeIn( 400 );
                    }
                    else
                    {
                        $("#theForm").fadeOut( 100 );
                        $("#error").fadeIn( 400 );
                    }
                });
            }
        }
    });

    $("#sendToFriend").click(function(){
        $('#dialog').load( "/send_boat_to_form.html" ).dialog('open');
    });
});

