$(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 address = $('input[name=address]');
		var city = $('input[name=city]');
		var state = $('input[name=state]');
		var zip = $('input[name=zip]');
		var phone = $('input[name=phone]');
		var email = $('input[name=email]');
		var comments = $('textarea[name=comments]');
		var product = $('input[name=product]:checked');
		

		//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.addClass('hightlight');
			return false;
		} else f_name.removeClass('hightlight');
		
		if ((l_name.val()=='') || (l_name.val()=='Last Name*')) {
			l_name.addClass('hightlight');
			return false;
		} else l_name.removeClass('hightlight');
		
		if ((email.val()=='') || (email.val()=='E-Mail Address*')) {
			
			email.addClass('hightlight');
			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.addClass('hightlight');
				return false;
			}else email.removeClass('hightlight');
		}
		
		
		//organize the data properly
		var data = 	'f_name=' + f_name.val() + 
					'&l_name=' + l_name.val() + 
					'&address=' + address.val() + 
					'&city=' + city.val() + 
					'&state=' + state.val() + 
					'&zip=' + zip.val() +
					'&phone=' + phone.val() + 
					'&email=' + email.val() + 
					'&comments='  + encodeURIComponent(comments.val()) +
					'&product=' + product.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: "submit-man.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');
					
				//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;
	});	
});	
