$(document).ready(function() {

	//if submit button is clicked
	$('#submit').click(function () {

		//Get the data from all the fields
		var name = $('input[name=name]');
		var email = $('input[name=email]');
		var city = $('input[name=city]');
		var phone = $('input[name=phone]');
		var serviceaddress = $('input[name=serviceaddress]');
		var servicetype = $('select[name=servicetype]');
		var sqfoot = $('input[name=sqfoot]');
		var frequency = $('select[name=frequency]');

		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field


		var ck_name = /^[A-Za-z ]{3,25}$/;
		var ck_address = /^[A-Za-z0-9 ]{3,30}$/;
		var ck_city = /^[A-Za-z ]{3,20}$/;
		var ck_phone= /\d{3}\-\d{3}\-\d{4}/;
		var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		var ck_sqfoot = /^[0-9]{2,10}$/;

		if (!ck_name.test(name.val())) {
			name.addClass('hightlight');
			document.getElementById('entername').style.display = 'block';
			document.form.name.focus();

			return false;
		} else {

		name.removeClass('hightlight');

		document.getElementById('entername').style.display = 'none';
		}


		if (!ck_address.test(serviceaddress.val())) {
			serviceaddress.addClass('hightlight');
			document.getElementById('enteraddress').style.display = 'block';
			document.form.serviceaddress.focus();
			return false;
		} else {

		serviceaddress.removeClass('hightlight');
		document.getElementById('enteraddress').style.display = 'none';
		}



		if (!ck_city.test(city.val())) {
			city.addClass('hightlight');
			document.getElementById('entercity').style.display = 'block';
			document.form.city.focus();
			return false;
		} else {

		city.removeClass('hightlight');
		document.getElementById('entercity').style.display = 'none';
		}

		if (!ck_phone.test(phone.val())) {
		phone.addClass('hightlight');
		document.getElementById('enterphone').style.display = 'block';
		document.form.phone.focus();
		return false;
		} else {

		phone.removeClass('hightlight');
		document.getElementById('enterphone').style.display = 'none';
		}

		if (!ck_email.test(email.val())) {
		email.addClass('hightlight');
		document.getElementById('enteremail').style.display = 'block';
		document.form.email.focus();
		return false;
		} else {
		email.removeClass('hightlight');
		document.getElementById('enteremail').style.display = 'none';
		}

		if (servicetype.val()=='') {
				servicetype.addClass('hightlight');
				document.getElementById('enterservice').style.display = 'block';
				document.form.servicetype.focus();
				return false;
				} else {
				servicetype.removeClass('hightlight');
				document.getElementById('enterservice').style.display = 'none';
		}

		if (!ck_sqfoot.test(sqfoot.val())) {
			sqfoot.addClass('hightlight');
			document.getElementById('entersqfoot').style.display = 'block';
			document.form.sqfoot.focus();
			return false;
			} else {
			sqfoot.removeClass('hightlight');
			document.getElementById('entersqfoot').style.display = 'none';
		}


		if (frequency.val()=='') {
			frequency.addClass('hightlight');
			document.getElementById('enterfrequency').style.display = 'block';
			document.form.frequency.focus();
			return false;
			} else {
			frequency.removeClass('hightlight');
			document.getElementById('enterfrequency').style.display = 'none';
		}

		//organize the data properly
		var data = 'name=' + encodeURIComponent(name.val()) + '&email=' + encodeURIComponent(email.val()) + '&city=' + encodeURIComponent(city.val()) + '&phone=' + encodeURIComponent(phone.val()) + '&serviceaddress=' + encodeURIComponent(serviceaddress.val()) + '&servicetype=' + servicetype.val() + '&sqfoot=' + encodeURIComponent(sqfoot.val()) + '&frequency=' + frequency.val();

		//disabled all the text fields
		$('.text').attr('disabled','true');

		//show the loading sign
		$('.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
					$('.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;
	});
});