jQuery.fn.errorMessage = function(options)
{
	var inputId = $(this).attr("id");
	var messageTopDefault = "This field cannot be left blank!";
	var messageMainDefault = "Please provide a value and resubmit the form";

	var defaults = {
		containerId: inputId,
		messageTop: messageTopDefault,
		messageMain: messageMainDefault
	};
	var options = jQuery.extend(defaults, options);

	var output = "\n<span id='error_"+options.containerId+"' class='errorCont'>";
		output += "\n\t<span class='body'>";
		output += "\n\t\t<span class='bold'>"+options.messageTop+"</span><br />"+options.messageMain;
		output += "\n\t</span>";
		output += "\n</span>";

	// apend the output AFTER the form element
	$("#"+options.containerId).after(output);
	$("#"+options.containerId).addClass("errorFocus");
};

$(document).ready(function() {
	instantiate_formEntry();
});


//-----------------------------------------------------------------------------------
// instantiate_
//-----------------------------------------------------------------------------------
function instantiate_formEntry() {
	var init = $("#contestEntry").size();
	if (init != 0) {

		/*
			In-line validation
			- alphabetical by comment name
		*/
		// Address - City
		$('input#city').blur(function() {
			var inputId = $(this).attr("id");
			var inputValue = $(this).val();
			var message = "Please enter your city";
			validate_presenceOf(inputId, inputValue, message);
		});

		// Address - State


		// Address - Street1
		$('input#address1').blur(function() {
			var inputId = $(this).attr("id");
			var inputValue = $(this).val();
			var message = "Please enter your street address";
			validate_presenceOf(inputId, inputValue, message);
		});

		// Address - Zip
		$('input#zip').blur(function() {
			var inputId = $(this).attr("id");
			var inputValue = $(this).val();
			validate_zipCode(inputId, inputValue);
		});
		
		// Checkbox - acceptRules
		$("input#acceptRules").click(function() {
			var isChecked = $(this).attr('checked');

			if (isChecked == true) {
				$('#error_acceptRules').remove();
			}
		});
		
		// Checkbox - over18
		$("input#over18").click(function() {
			var isChecked = $(this).attr('checked');

			if (isChecked == true) {
				$('#error_over18').remove();
			}
		});
		
		// Checkbox - truckIndustry
		$("input#truckIndustry").click(function() {
			var isChecked = $(this).attr('checked');

			if (isChecked == true) {
				$('#error_truckIndustry').remove();
			}
		});
		
		// Email
		$('input#email').blur(function() {
			var inputId = $(this).attr("id");
			var inputValue = $(this).val();
			validate_email(inputId, inputValue);
		});
		
		// Name - First
		$('input#first_name').blur(function() {
			var inputId = $(this).attr("id");
			var inputValue = $(this).val();
			var message = "Please enter your first name";
			validate_presenceOf(inputId, inputValue, message);
		});

		// Name - Last
		$('input#last_name').blur(function() {
			var inputId = $(this).attr("id");
			var inputValue = $(this).val();
			var message = "Please enter your last name";
			validate_presenceOf(inputId, inputValue, message);
		});

		// Name - Organization
		$('input#organization').blur(function() {
			var inputId = $(this).attr("id");
			var inputValue = $(this).val();
			var message = "Please enter the name of your organization";
			validate_presenceOf(inputId, inputValue, message);
		});
		
		// Name - Title
		$('input#title').blur(function() {
			var inputId = $(this).attr("id");
			var inputValue = $(this).val();
			var message = "Please enter your title";
			validate_presenceOf(inputId, inputValue, message);
		});
		
		// Phone
		$("input#phone").blur(function() {
			// proceed with validation
			var inputId = $(this).attr("id");
			var inputValue = $(this).val();
			validate_phoneNumber(inputId, inputValue);
		});

		// Button Interaction
		$('input#submitEntry').click(function(e) {
			e.preventDefault();
			submit_contestEntry();
			return false;
		})
	}
}


//-----------------------------------------------------------------------------------
// submit_
//-----------------------------------------------------------------------------------
function submit_contestEntry() {
	// check for obvious errors
	var fieldErrors = $('#contestEntry').find("span.errorCont").length;
	
	// loop through each required field and check for a value
	$('#contestEntry').find("input.required, select.required").each(function() {
		var elementID = $(this).attr("id");
		var elementValue = $(this).val();
		var elementLength = parseFloat(elementValue.length);
		if (elementLength == 0 || elementValue == "noSelection") {
			// set fieldErrors to 1
			fieldErrors = 1;

			// trigger an error
			$("#error_"+elementID).remove();
			$('#'+elementID).errorMessage();
		}
	});
	
	// verify checkboxes were selected
	var isChecked = false;
	var checked_over18 = $('input#over18').attr('checked');
	if (checked_over18 == true) {
		var checked_acceptRules = $('input#acceptRules').attr('checked');
		if (checked_acceptRules == true) {
			var checked_truckIndustry = $('input#truckIndustry').attr('checked');
			if (checked_truckIndustry == true) {
				isChecked = true;
			}
			else {
				$('#error_truckIndustry').remove();
				$('#truckIndustry').errorMessage({
					messageTop: "You must acknowledge you are an Owner/Operator or an employee of a transportation related company.",
					messageMain: "Please select the checkbox to acknowledge."
				});
			}
		}
		else {
			$('#error_acceptRules').remove();
			$('#acceptRules').errorMessage({
				messageTop: "You must accept the Official Rules and Requirements.",
				messageMain: "Please select the checkbox if you accept."
			});
		}
	}
	else {
		$('#error_over18').remove();
		$('#over18').errorMessage({
			messageTop: "You must be over 18.",
			messageMain: "Please select the checkbox if you are over 18."
		});
	}
	
	
	
	$("input[name=terms]").click(function() {
		var isChecked = $(this).attr('checked');

		if (isChecked == true) {
			$("#error_privacyPolicy, div.errorClear").remove();
			$('#error_terms').hide();
		}
	});
	
	if (fieldErrors == 0 && isChecked == true) {
		// submit the form
		$('#contestEntry').submit();
	}
}

//-----------------------------------------------------------------------------------
// validate_
//-----------------------------------------------------------------------------------
function validate_email(inputId, inputValue) {
	var isValid = "no";
	var value = jQuery.trim(inputValue.toUpperCase());
	var regEx = new RegExp("^[A-Z0-9._+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$");

	if (value.match(regEx)) {
		isValid = "yes";
		$("#error_"+inputId+", div.errorClear").remove();
		$("#"+inputId).removeClass("errorFocus");
	}
	else {
		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_"+inputId).remove();
		$(this).errorMessage({
			containerId: inputId,
			messageTop: topMessage,
			messageMain: mainMessage
		});
	}
}

function validate_phoneNumber(inputId, inputValue) {
	if (inputValue.length > 0) {
		// validate the field
		var inputDigits = inputValue.replace(/[^\d]/g, "");
		var areaCode = inputDigits.substr(0,3);
		var phonePrefix = inputDigits.substr(3,3);
		var lineNumber = inputDigits.substr(6,4);
		var digitLength = inputDigits.length;
		if (digitLength < 10 || digitLength > 10)
		{
			var topMessage = "The phone number you entered is not valid";
			var mainMessage = "Please enter a valid, 10 digit phone number";

			$("#error_"+inputId).remove();
			$(this).errorMessage(
			{
				containerId: inputId,
				messageTop: topMessage,
				messageMain: mainMessage
			});
		}
		else {
			$("#error_"+inputId).remove();
			$("#"+inputId).removeClass("errorFocus");
			// re-format the phone number
			var formatedNumber = areaCode+"-"+phonePrefix+"-"+lineNumber;
			$("#"+inputId).val(formatedNumber);
		}
	}
	else {
		$("#error_"+inputId).remove();
		$(this).errorMessage(
		{
			containerId: inputId,
			messageMain: "Please enter a valid, 10 digit phone number"
		});
	}
}

function validate_presenceOf(inputId, inputValue, message) {
	if (inputValue.length < 2) {
		$("#error_"+inputId).remove();
		$(this).errorMessage({
			containerId: inputId,
			messageMain: message
		});
	}
	else {
		$("#error_"+inputId).remove();
		$("#"+inputId).removeClass("errorFocus");
	}
}

function validate_zipCode(inputId, inputValue) {
	var inputDigits = inputValue.replace(/[^\d]/g, "");
	var digitLength = inputDigits.length;
	var validated = "no";

	if (digitLength < 5 || digitLength > 5) {
		$("#error_"+inputId).remove();
		$(this).errorMessage(
		{
			containerId: inputId,
			messageTop: "The zip code must contain only 5 digits",
			messageMain: "Please enter a 5 digit zip code"
		});
		validated = "no";
	}
	else {
		$("#error_"+inputId).remove();
		$("#"+inputId).removeClass("errorFocus");
		validated = "yes";
	}
	return validated;
}