// JavaScript Document

function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}

$(document).ready(function() {
	
	//if submit button is clicked
	$('#submit').click(function () {		
		
		//Get the data from all the fields
    	var f_name = $('input[name=f_name]');
		var l_name = $('input[name=l_name]');
		var email = $('input[name=email]');
		var msg = $('textarea[name=msg]');
		var ver = $('input[name=verified]');
		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if (f_name.val()=='') {
			f_name.addClass('hightlight');
			return false;
		} else f_name.removeClass('hightlight');
				
		if (l_name.val()=='') {
			l_name.addClass('hightlight');
			return false;
		} else l_name.removeClass('hightlight');
		
		if (email.val()=='') {
			email.addClass('hightlight');
			return false;
		} else email.removeClass('hightlight');
		
		if(!isValidEmailAddress(email.val())){
		alert("The email address you entered does not appear to be formated correctly.  Please try something like you@yourdomain.com.");
			return false;
		}
		

		
		//organize the data properly
		var data = 'f_name=' + f_name.val() + '&l_name=' + l_name.val() + '&email=' + email.val() + '&msg=' + msg.val() + '&ver=' + ver.val();

	//disabled all the text fields
		//$('.text').attr('disabled','false');
		
		//show the loading sign
		$('.form_heading').fadeOut('slow');	
		$('.loading').show();
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "process.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {				
				//if process.php returned 1/true (send mail success)
				if (html==1) {					
					//hide the form
					
					$('.loading').fadeOut('slow');				
					
					//show the success message
					$('.done').fadeIn('slow');
					$('.contact').val("");
					setTimeout(function(){
			}, 4000);
					

					
				//if process.php returned 0/false (send mail failed)
				} else alert('Sorry, unexpected error. Please try again later.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
});	

