/*************************************************************
	website:	www.bestschoolsinmichigan.com	
	type:		Validation / Functionality
	desc:		javascript validation and functionality for
				Schedule a Test Drive form
	version:	1.0.0                      
-----------------------------------------------------------
	author:		Jeremy S. Ward
	date:		25 March 2009

*************************************************************/

$(document).ready(function() {
	//$('select#subjectCategory').focus();
	
	// form inputs
	$('input#myEmail').blur(function()
	{
		var inputValue = $(this).val();
		var reg = new RegExp("^[0-9a-zA-Z\._]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$");
		var match = reg.exec(inputValue);
		//alert("match = "+match);
		if (match == null)
		{
			if (inputValue.length == 0)
			{
				var topMessage = "This field cannot be left blank";
				var mainMessage = "Please enter your email address";
			}
			else
			{
				var topMessage = "The email you entered is not valid";
				var mainMessage = "Please enter a valid email address";
			}
			$("#error_myEmail, div.errorClear").remove();
			$(this).errorMessage(
			{
				messageTop: topMessage,
				messageMain: mainMessage
			});
		}
		else
		{
			$("#error_myEmail, div.errorClear").remove();
			$(this).removeClass("errorFocus");
		}
	});
	
	$('input#confirmMyEmail').blur(function()
	{
		var email1 = $('input#myEmail').val().toLowerCase();
		var email2 = $(this).val().toLowerCase();
		
		if (email2 != email1)
		{
			var topMessage = "The email addresses you entered do NOT match!";
			var mainMessage = "Please re-enter your email address.";
			
			$("#error_confirmMyEmail, div.errorClear").remove();
			$(this).errorMessage(
			{
				messageTop: topMessage,
				messageMain: mainMessage
			});
		}
		else
		{
			$("#error_confirmMyEmail, div.errorClear").remove();
			$(this).removeClass("errorFocus");
		}
	});
	
	$('input#fn').blur(function()
	{
		var inputValue = $(this).val();
		var nameLength = inputValue.length;
		
		if (nameLength < "2")
		{
			$("#error_fn, div.errorClear").remove();
			$(this).errorMessage(
			{
				messageMain: "Please enter your first name."
			});
		} 
		else
		{
			$("#error_fn, div.errorClear").remove();
			$(this).removeClass("errorFocus");
		}
	});
	
	$('input#ln').blur(function()
	{
		var inputValue = $(this).val();
		var nameLength = inputValue.length;
		
		if (nameLength < "2")
		{
			$("#error_ln, div.errorClear").remove();
			$(this).errorMessage(
			{
				messageMain: "Please enter your last name"
			});
		} 
		else
		{
			$("#error_ln, div.errorClear").remove();
			$(this).removeClass("errorFocus");
		}
	});
	
	
	
	// button interaction
	$('#submit_contactUs').click(function() {
		// check for obvious errors
		var fieldErrors = $('#form_contactUs').find("div.errorCont").length;
		
		// loop through each required field and check for a value
		$('#form_contactUs').find("label.required").each(function() {
			var elementID = $(this).next().attr("id");
			var elementValue = $(this).next().val();
			var elementLength = parseFloat(elementValue.length);
			if (elementLength == 0 || elementValue == "noSelection") {
				// set fieldErrors to 1
				fieldErrors = 1;
				
				// trigger an error
				$("#error_"+elementID+", div.errorClear").remove();
				$('#'+elementID).errorMessage();
			}	
		});
		
		if (fieldErrors == 0) {
			// submit the form
			$('#form_contactUs').submit();
		}
		else {
			// scroll to the top of the page so user can view any error messages
			window.scrollTo(0,0);
		}
		
		return false;
	});
	
	$('input.cancel').click(function() {
		$('.errorCont, div.errorClear').remove();
		$('input, select, textarea').removeClass("errorFocus");
		$('form#form_contactUs')[ 0 ].reset();
		window.scrollTo(0,0);
		
	});
});

