$(function() {

//alert("This is from contact1.js");

		// before showing the modal window, reset the form incase of previous use.
		$('.success, .error').hide();
		$('#sendError').hide();
		$('form#contactForm').show();
		
		// Reset all the default values in the form fields
		var name_default = "Your name";
		var email_default = "Your email address";
		var message_default = "Enter a short message....";
		
		$('#name').val(name_default);
		$('#email').val(email_default);
		$('#message').val(message_default);

	// 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');

	});
	
	//Clearing the contents of the textbox when clicked or mouse is focused
	$('#contactForm input[type=text]').focus(function() {
		$(this).val(' ');
	});
		
	//clearing the contents of the textarea when mouse is clicked or focused
	$('#contactForm textarea').focus(function() {
        $(this).val('');
    });
	

	// when the Submit button is clicked...
	$('a#submit').click(function() {
									
							//alert("in click fn");		
		$('.error').hide().remove();
		
		//Inputed Strings
		var name = $('#name').val(),
			email = $('#email').val(),
			message = $('#message').val();
				
		//Error Count
		var error_count = 0;
		
		//trimming the white spaces from beginning and end of the string
		jQuery.trim(name);
		jQuery.trim(email);
		jQuery.trim(message);
		
		//alert("Name: '" + name +"', " + "Email: '" + email + "', " + "Message: '" + message + "'"); 
		
		//Regex Strings
		//var username_regex = /^[a-z0-9_-]{3,16}$/,
		//var email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/i;
		//var username_regex = new RegExp('/^[a-zA-Z\s]+$/','');
		//var email_regex = new RegExp('/^([a-z0-9_\.-\s]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/','');
		
		
		/*
		if(!(/^([a-z0-9_\.\s\-]+)@([\da-z\.\-]+)\.([a-z\.]{2,6})$/i.test(email)))
		{
			alert(name + ": not matched");
		}
		else
		{
			alert(name + ":  matched");
		}
		*/
			//Test Username
			if(!(/^[a-zA-Z\s]+$/i.test(name)) || (name == name_default) || (name == '') ) {
				$('#contact #name').after("<p class='error'>Invalid name entered!</p>");
				error_count += 1;
			}
			
			
			//Test Email
			if(!(/^([a-z0-9_\.\s\-]+)@([\da-z\.\-]+)\.([a-z\.\s]{3,7})$/i.test(email)) || (email == email_default) || (email == '') ) {
				$('#contact #email').after("<p class='error'>Invalid email entered!</p>");
				error_count += 1;
			}
			
			//Blank Comment?
			if((message == '') || (message == message_default)) {
				$('#contact #message').after("<p class='error'>No message entered!</p>");
				error_count += 1;
			}
			
			//No Errors?
			if(error_count === 0) {
				$.ajax({
					type: "post",
					url: "./media/contact/contact-submit.php",
					data: "name=" + name + "&email=" + email + "&message=" + message,
					error: function() {
						$('.error').hide();
						//$('#sendError').slideDown('slow');
						$('#sendError').fadeIn('slow');
					},
					success: function () {
						$('.error').hide();
						//$('.success').slideDown('slow');
						
						$('form#contactForm').hide();
						$('.success').fadeIn('slow');
					}				
				});	
				
			}
			
			else {
                $('.error').show();
            }
		
		
		return false;
		
	}); //end submit click
	
	






})(jQuery);

