var yfn = 'Your First Name';
var yln = 'Your Surname';
var ypc = 'Your Postcode';
var yphn = 'Your Phone Number';
var yea = 'Your Email Address';
var yc = 'Enter your comment or query...';

$(function() {	
	// load the modal window
	$('a.modal').click(function(){
		// scroll to top
		$('html, body').animate({scrollTop:0}, 'fast');

		// before showing the modal window, reset the form incase of previous use.
		$('.success, .error').hide();
		$('form#contactForm').show();
		
		// Reset all the default values in the form fields
		$('#firstname').val(yfn);
		$('#lastname').val(yln);
		$('#postcode').val(ypc);
		$('#phone').val(yphn);
		$('#email').val(yea);
		$('#comments').val(yc);

		//show the mask and contact divs
		$('#mask').show().fadeTo('', 0.7);
		$('div#contact').fadeIn();

		// stop the modal link from doing its default action
		return false;
	});

	// close the modal window is close div or mask div are clicked.
	$('div#close, div#mask').click(function() {
		$('div#contact, div#mask').stop().fadeOut('slow');

	});

	$("#contactForm input[type='text']").focus(function() {
		$(this).val(' ');
	});
	
	$('#contactForm textarea').focus(function() {
        $(this).val(' ');
    });

	// when the Submit button is clicked...
	$('#submitBtn').click(function() {
		$('.error').remove();
		//Inputed Strings
		var firstname = $('#firstname').val(),
			lastname = $('#lastname').val(),
			postcode = $('#postcode').val(),
			phone = $('#phone').val(),
			email = $('#email').val(),
			comments = $('#comments').val();
		
	
		//Error Count
		var error_count = 0;
		
		//Regex Strings
		var email_regex = /^(.*?)@.*?$/;
		var postcode_regex = /^[0-9]{4}$/;
		
		//Test Firstname
		if(firstname == " " || firstname == yfn) {
			$('#contact_header_three').after('<p class=error>Invalid first name entered!</p>');
			error_count += 1;
		}
		
		//Test Surname
		if(lastname == " " || lastname == yln) {
			$('#contact_header_three').after('<p class=error>Invalid surname entered!</p>');
			error_count += 1;
		}
		
		//Test Postcode
		if(!postcode_regex.test(postcode) || postcode == ypc) {
			$('#contact_header_three').after('<p class=error>Invalid postcode entered!</p>');
			error_count += 1;
		}
		
		//Test Email
		if(!email_regex.test(email) || email == yea) {
			$('#contact_header_three').after('<p class=error>Invalid email entered!</p>');
			error_count += 1;
		}
		
		//No Errors?
		if(error_count === 0) {
			$.ajax({
				type: "post",
				url: "http://www.imortgage.com.au/wp-content/themes/theme/send.php",
				data: "firstname=" + firstname + "&lastname=" + lastname + "&postcode=" + postcode + "&phone=" + phone + "&email=" + email + "&comments=" + comments,
				error: function() {
					$('.error').hide();
					$('#sendError').slideDown('slow');
				},
				success: function () {
					$('.error').hide();
					$('.success').slideDown('slow');
					$('form#contactForm').fadeOut('slow');
				}				
			});	
		} else {
			$('.error').show();
		}
		
		return false;
	});
	
});
