$(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 phone = $('input[name=phone]');
		
		
		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if ((f_name.val()=='') || (f_name.val()=='First Name*') ) {
			f_name.css({'background': 'url(img/input_back2.png)', 'background-repeat': 'no-repeat', 'background-position':'bottom left'});
			return false;
		} else{
			f_name.css({'background': 'url(img/input_back.png)', 'background-repeat': 'no-repeat', 'background-position':'bottom left'});
		}
		
		if ((l_name.val()=='') || (l_name.val()=='Last Name*')) {
			l_name.css({'background': 'url(img/input_back2.png)', 'background-repeat': 'no-repeat', 'background-position':'bottom left'});
			return false;
		} else {
			l_name.css({'background': 'url(img/input_back.png)', 'background-repeat': 'no-repeat', 'background-position':'bottom left'});
		}
				
		if ((email.val()=='') || (email.val()=='E-Mail Address*')) {
			email.css({'background': 'url(img/input_back2.png)', 'background-repeat': 'no-repeat', 'background-position':'bottom left'});
			return false;		
		} else {
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			var emaddr = email.val();
			if(reg.test(emaddr) == false){
				email.css({'background': 'url(img/input_back2.png)', 'background-repeat': 'no-repeat', 'background-position':'bottom left'});
				return false;
			}else {
				email.css({'background': 'url(img/input_back.png)', 'background-repeat': 'no-repeat', 'background-position':'bottom left'});
			}
		}
		
		if ((phone.val()=='') || (phone.val()=='Last Name*')) {
			phone.css({'background': 'url(img/input_back2.png)', 'background-repeat': 'no-repeat', 'background-position':'bottom left'});
			return false;
		} else {
			phone.css({'background': 'url(img/input_back.png)', 'background-repeat': 'no-repeat', 'background-position':'bottom left'});
		}
		
		
		//organize the data properly
		var data = 	'f_name=' + encodeURIComponent(f_name.val()) + 
					'&l_name=' + encodeURIComponent(l_name.val()) + 
					'&email=' + encodeURIComponent(email.val()) + 
					'&phone=' + encodeURIComponent(phone.val());
		
		//disabled all the text fields
		//$('.inp').attr('disabled','true');
		//$('.disable').attr('disabled','true');
		//$('#submit').hide();
		
		//show the loading sign
		$('.loading').show();
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "post.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
					//$('.form').fadeOut('slow');					

					//show the success message
					//$('.done').fadeIn('slow');
					alert('Form submitted. Thank you.');
					
				//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;
	});	
});	

