$(document).ready(function() {
	$('#contactForm').submit(function() {
		
		// variables
		var first_name = $('#first_name').attr('value');
		var last_name = $('#last_name').attr('value');
		var email = $('#email').attr('value');
		var home_phone = $('#home_phone').attr('value');
		var work_phone = $('#work_phone').attr('value');
		var best_time = $('#best_time').attr('value');
		var address = $('#address').attr('value');
		var city = $('#city').attr('value');
		var state = $('#state').attr('value');
		var zip = $('#zip').attr('value');
		var country = $('#country').attr('value');
		var hear = $('#hear').attr('value');
		var method = $('#method').attr('value');
		
		var e = false;
		var m = '<p><strong>The following fields need to be completed:</strong></p><ul>';
		
		// check if they are filled out
		
		if(first_name == '') { e = true; m += '<li>First Name</li>'; }
		if(last_name == '') { e = true; m += '<li>Last Name</li>'; }
		if(address == '') { e = true; m += '<li>Address</li>'; }
		if(email == '') { e = true; m += '<li>Email</li>'; }
		if(home_phone == '') { e = true; m += '<li>Home Phone</li>'; }
		if(city == '') { e = true; m += '<li>City</li>'; }
		if(state == '') { e = true; m += '<li>State</li>'; }
		if(zip == '') { e = true; m += '<li>Zip</li>'; }
		if(country == '') { e = true; m += '<li>Country</li>'; }
		
		// add to variables
		m += '</ul>';
		
		// if errors then proccess error reports
		if(e) {
			$.facebox(m);
		} else {
			
			// dataparams are the post data to send to the php script
			var dataparams = '&first_name=' + first_name + '&last_name=' + last_name + '&email=' + email + '&hphone=' + home_phone + '&wphone=' + work_phone;
			dataparams += '&address=' + address + '&city=' + city + '&state=' + state + '&zip=' + zip + '&country=' + country + '&time=' + best_time + '&hear=' + hear + '&method=' + method;
			
			// ajax function proccess the data with a seperate php file
			$.ajax({
				type: "POST",
				url: "app/email.php",
				data: dataparams,
				success: function(msg){
				
					// if the proccess is successfull then alert the user
					$.facebox('<p>Thank You! We appreciate your interest in wanting to know more about Craigsanquhar. You will be hearing from us shortly. Again, thank you.</p>');
					// also make sure to clear all the form inputs
				
					$('#first_name').attr('value', '');
					$('#last_name').attr('value', '');
					$('#email').attr('value', '');
					$('#home_phone').attr('value', '');
					$('#work_phone').attr('value', '');
					$('#best_time').attr('value', '');
					$('#address').attr('value', '');
					$('#city').attr('value', '');
					$('#state').attr('value', '');
					$('#zip').attr('value', '');
					$('#country').attr('value', '');
					$('#hear').attr('value', '');
					$('#method').attr('value', '');
				}
			});
			
			pageTracker._trackPageview('/lead/converted');
			
		}
		
		// return false no matter what so the page does not reload
		return false;
	});
});