$(document).ready(function() {
	
	//if submit button is clicked
	$('#freeforms-form').submit(function () {		
		
		//Get the data from all the fields
		var aa1 = $('input[name=aa1]');
		var aa2 = $('input[name=aa2]');
		var aa3 = $('input[name=aa3]');

		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if (aa1.val()=='') {
			aa1.addClass('free-forms-error ');
		alert('Please Enter your First Name');
			return false;
		} else aa1.removeClass('hightlight');
		
		if (aa2.val()=='') {
			aa2.addClass('free-forms-error ');
		alert('Please Enter your Last Name');
			return false;
		} else aa2.removeClass('hightlight');
		
		if (aa3.val()=='') {
			aa3.addClass('free-forms-error');
		alert('Please Enter your Email Address');
			return false;
		} else aa3.removeClass('hightlight');
		
		//organize the data properly
		var data = 
		'aa1=' + aa1.val() 
		+ '&aa2=' + aa2.val() 
		+ '&aa3=' + aa3.val()

		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "freeforms-process/index.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) {	
					
					$('#freeforms-wrap').fadeSliderToggle(600);

					setTimeout (function(){
					//show the success message and the thank-you message
					$('#freeforms-wrap-finished').slideDown(1500); },1200);
	
				//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;
	});	
});	