﻿$(document).ready(function() {
$("#btnSubmit").click(function() {

        $(".error").hide();
        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var nameVal = $("#name").val();
        if (nameVal == '') {
            $("#name").after('<p class="error">You forgot to enter your name.</p');
            hasError = true;
        }
 
        var emailFromVal = $("#email").val();
        if (emailFromVal == '') {
            $("#email").after('<p class="error">You forgot to enter the email address to send from.</p>');
            hasError = true;
        } else if (!emailReg.test(emailFromVal)) {
            $("#email").after('<p class="error">Enter a valid email address to send from.</p');
            hasError = true;
        }

        var subjectVal = $("#subject").val();
        if (subjectVal == '') {
            $("#subject").after('<p class="error">You forgot to enter the subject.</p>');
            hasError = true;
        }

        var messageVal = $("#message").val();
        if (messageVal == '') {
            $("#message").after('<p class="error">You forgot to enter the message.</p>');
            hasError = true;
        }

        if(hasError == false) {
        $(this).hide();
        $("#contactform .btnhold").append('<img src="image/loading.gif" alt="Loading" style="padding-top:5px; padding-left:5px;"/>');

        $.post("php/contactform.php",
            { name: nameVal, email: emailFromVal, subject: subjectVal, message: messageVal },
            function(data){
            $("#contactform").slideUp("normal", function() {

            $("#contactform").before('<h1>Success</h1><p>Your email was sent.</p>');
                });
            }
          );
        }
        return false;
    });
});