// JavaScript Document

$(document).ready(function() {
	
	$('#form #submit').click(function() {
		// Fade in the progress bar
		$('#form #formProgress').hide();
		$('#form #formProgress').html('Sending&hellip;');
		$('#form #formProgress').fadeIn();
		
		// Disable the submit button
		$('#form #submit').attr("disabled", "disabled");
		
		// Clear and hide any error messages
		$('#form .formError').html('');
		
		// Set temaprary variables for the script
		var isFocus=0;
		var isError=0;
		
		// Get the data from the form
		var name=$('#form #name').val();
		var email=$('#form #email').val();
		var age=$('#form #age').val();
		var location=$('#form #location').val();
		var best=$('#form #best').val();
		var budget=$('#form #budget').val();
		var service=$('#form #service').val();
		var style=$('#form #style').val();

		// Data validation
		if(name=='') {
			$('#form #errorName').html('This is a required field.');
			$('#form #name').focus();
			isFocus=1;
			isError=1;
		}
		if(email=='') {
			$('#form #errorEmail').html('This is a required field.');
			if(isFocus==0) {
				$('#form #email').focus();
				isFocus=1;
			}
			isError=1;
		} else {
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test(email)==false) {
				$('#form #errorEmail').html('Invalid email address.');
				if(isFocus==0) {
					$('#form #email').focus();
					isFocus=1;
				}
				isError=1;
			}
		}
		
/*		
		if(age=='') {
			$('#form #errorAge').html('This is a required field.');
			if(isFocus==0) {
				$('#form #age').focus();
				isFocus=1;
			}
			isError=1;
		} else {
			var reg = /^[0-9]+$/;
			if(reg.test(age)==false) {
				$('#form #errorAge').html('Invalid age.');
				if(isFocus==0) {
					$('#form #age').focus();
					isFocus=1;
				}
				isError=1;
			}
		}
*/		
		// Terminate the script if an error is found
		if(isError==1) {
			$('#form #formProgress').html('');
			$('#form #formProgress').hide();
			
			// Activate the submit button
			$('#form #submit').attr("disabled", "");
			
			return false;
		}
		
		$.ajaxSetup ({
			cache: false
		});
		
		var dataString = 'name='+ name + '&email=' + email + '&age=' + age + '&location=' + location + '&best=' + best + '&budget=' + budget + '&service=' + service + '&style=' + style;  
		$.ajax({
			type: "POST",
			url: "-post.php",
			data: dataString,
			success: function(msg) {
				
				// Check to see if the mail was successfully sent
				if(msg.substr(0,9)=='<!--OK-->') {
					// Progress message
					$('#form #formProgress').html('Message sent.').delay(2000).fadeOut(400);
					
					// Fields to clear after form submission
					$('#form #name').val('');
					$('#form #email').val('');
					$('#form #age').val('');
					$('#form #location').val('');
					$('#form #best').val('');
					$('#form #budget').val('');
				} else {
					$('#form #formProgress').html('');
					alert('There was an error sending your email. Please try again.');
				}
				
				// Submit button activation
				$('#form #submit').attr("disabled", "");
			},
			error: function(ob,errStr) {
				$('#form #formProgress').html('');
				alert('There was an error sending your email. Please try again.');
				
				// Submit button activation
				$('#form #submit').attr("disabled", "");
			}
		});
		
		return false;
	});
});
