$(function() {

	// load the modal window
	$('a.modal').click(function(){

		// scroll to top
		$('html, body').animate({scrollTop:0}, 'fast');

		// before showing the modal window, reset the form incase of previous use.
        $('.success, .error').hide();
		$('form#contactForm').show();

		//show the mask and contact divs
		$('#mask').show().fadeTo('', 0.7);
		$('div#contact').fadeIn();

		// stop the modal link from doing its default action
		return false;
	});

	// close the modal window is close div or mask div are clicked.
	$('div#close, div#mask').click(function() {
		$('div#contact, div#mask').stop().fadeOut('slow');
	});

	// when the Submit button is clicked...
	$('input#submit').click(function() {

		// Gather data from the form
		var name = $('#name').val();
		var email = $('#email').val();
		var comment = $('#comment').val();
        var security = $('#security').val();

		// Regular expression to check the email address
		var emailOK = /[A-Za-z0-9._-]+@[A-Za-z0-9_-]+\.([a-zA-Z.]{2,5})/.test(email);

		// series of checks to make sure name and comment are not blank
        if (security === "beautility") {
    		if (name != '') { //make sure name field is not blank.
    			if (comment != '') { // make sure comment field is not blank
    	        	if (emailOK == true) { // make sure email passed the regex check.
    	        		// if all checks as passed, run ajax object.
    	        		$.ajax({
    	        			type: "post",
    						url: "send.php",
    						data: "name=" + name + "&email=" + email + "&comment=" + comment,
    						error: function() {
    							$('.error').hide();
    							$('#sendError').slideDown('slow');
    						},
    						success: function () {
    							$('.error').hide();
    							$('.success').slideDown('slow');
    							$('form#contactForm').fadeOut('slow');
    						}
    	        		});
    	        	}
    				else {
    					$('.error').hide();
    					$('#emailError').slideDown('slow');
    				}
    			}
    			else {
    				$('.error').hide();
    				$('#commentError').slideDown('slow');
    			}
    		} else {
    			$('.error').hide();
    			$('#nameError').slideDown('slow');
    		}
        } else {
            $('.error').hide();
            $('#securityError').slideDown('slow');
        }
		// stop the form from doing its default action
		return false;
	});
});
