$.extend($.validator.messages, {
	required: "Required",
	email: "Invalid email"
});

$.validator.addMethod("defaultInvalid", function(value, element) {
	switch (element.value) {
		case "*First Name":
			return false;
		case "*Last Name":
			return false;
		case "*Email":
			return false;
		case "*Home Phone":
			return false;
		case "*Street":
			return false;
		case "*Postal Code":
			return false;
		case "*Year":
			return false;
		case "*Make":
			return false;
		case "*Model":
			return false;
		case "*Kilometers":
			return false;
		case "*VIN":
			return false;
		case "*Social Insurance Number":
			return false;
		case "*Date of Birth":
			return false;
		case "*Street":
			return false;
		case "*City":
			return false;
		case "*Rent/Mortgage Monthly Amount":
			return false;
		case "*Employer":
			return false;
		case "*Occupation":
			return false;
		case "*Net Salary":
			return false;
		case "*Monthly Payment":
			return false;
		case "*I Authorize":
			return false;
	}
	return true;
}, "Required");

// Valid formats are (X1X 1X1) or (X1X1X1) or (X1X-1X1).
$.validator.addMethod("postalCode", function(value) {
	return value.match(/^[a-zA-Z][0-9][a-zA-Z](-| )?[0-9][a-zA-Z][0-9]$/);
}, "Invalid postal code");

$(document).ready(function() {
	$("#contact-form label").each(function(){ labelToValues(this); });	
	$("#directions label").each(function(){ labelToValues(this); });
	$("#finance label").each(function(){ labelToValues(this); });
	$("#parts label").each(function(){ labelToValues(this); });
	$("#service label").each(function(){ labelToValues(this); });
	$("#pricequote label").each(function(){ labelToValues(this); });
	$("#carfinder-form label").each(function(){ labelToValues(this); });
	$("#financeApplication label").each(function(){ labelToValues(this); });
	$("#tradeInForm label").each(function(){ labelToValues(this); });
	
	$(".addField a").click(function() {
		addFormField();
		return false;
	});
	
	var submitIndicatorContainer = $("#submit-indicator");
	var submitIndicator = '<img src="/template/ajax-loader.gif" alt="Submitting..." />';
	
	var validator_a = $("#contact-form").validate({
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				beforeSerialize: function() {
					submitIndicatorContainer.hide().html(submitIndicator).fadeIn("slow");
				},
				beforeSubmit: function(arr, $form, options) {
					clearDefaultValues(arr);
				},
				success: function() {
					$("#contact-form").hide("slow").after("<p>Thank you for submitting your information! A dealership representative will be in contact with you shortly.</p>");
				}
			});
			return false;
		},
		rules: {
			first_name: { defaultInvalid: true },
			last_name: { defaultInvalid: true },
			sender_email: { defaultInvalid: true },
			sender_phone: { defaultInvalid: true }
		}
	});
	
	var validator_b = $("#directions").validate({
		rules: {
			street: { defaultInvalid: true },
			postal_code: { postalCode: true, defaultInvalid: true }
		}
	});
	
	var validator_c = $("#finance").validate({
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				beforeSerialize: function() {
					submitIndicatorContainer.hide().html(submitIndicator).fadeIn("slow");
				},
				beforeSubmit: function(arr, $form, options) {
					clearDefaultValues(arr);
				},
				success: function() {
					$("#finance").hide("slow").after("<p>Thank you for submitting your information! A dealership representative will be in contact with you shortly.</p>");
				}
			});
			return false;
		},
		rules: {
			first_name: { defaultInvalid: true },
			last_name: { defaultInvalid: true },
			sender_email: { defaultInvalid: true }
		}
	});
	
	var validator_d = $("#parts").validate({
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				beforeSerialize: function() {
					submitIndicatorContainer.hide().html(submitIndicator).fadeIn("slow");
				},
				beforeSubmit: function(arr, $form, options) {
					clearDefaultValues(arr);
				},
				success: function() {
					$("#parts").hide("slow").after("<p>Thank you for submitting your information! A dealership representative will be in contact with you shortly.</p>");
				}
			});
			return false;
		},
		rules: {
			first_name: { defaultInvalid: true },
			last_name: { defaultInvalid: true },
			sender_email: { defaultInvalid: true },
			sender_phone: { defaultInvalid: true },
			vehicle_year: { defaultInvalid: true },
			vehicle_make: { defaultInvalid: true },
			vehicle_model: { defaultInvalid: true }
		}
	});
	
	var validator_d = $("#service").validate({
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				beforeSerialize: function() {
					submitIndicatorContainer.hide().html(submitIndicator).fadeIn("slow");
				},
				beforeSubmit: function(arr, $form, options) {
					clearDefaultValues(arr);
				},
				success: function() {
					$("#service").hide("slow").after("<p>Thank you for submitting your information! A dealership representative will be in contact with you shortly.</p>");
				}
			});
			return false;
		},
		rules: {
			first_name: { defaultInvalid: true },
			last_name: { defaultInvalid: true },
			sender_email: { defaultInvalid: true },
			appointment_date: { date: false },
			vehicle_year: { defaultInvalid: true },
			vehicle_make: { defaultInvalid: true },
			vehicle_model: { defaultInvalid: true }
		}
	});
	
	var validator_e = $("#pricequote").validate({
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				beforeSerialize: function() {
					submitIndicatorContainer.hide().html(submitIndicator).fadeIn("slow");
				},
				beforeSubmit: function(arr, $form, options) {
					clearDefaultValues(arr);
				},
				success: function() {
					$("#pricequote").hide("slow").after("<p>Thank you for submitting your information! A dealership representative will be in contact with you shortly.</p>");
				}
			});
			return false;
		},
		rules: {
			first_name: { defaultInvalid: true },
			last_name: { defaultInvalid: true },
			sender_email: { defaultInvalid: true }
		}
	});
	
	var validator_f = $("#carfinder-form").validate({
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				beforeSerialize: function() {
					submitIndicatorContainer.hide().html(submitIndicator).fadeIn("slow");
				},
				beforeSubmit: function(arr, $form, options) {
					clearDefaultValues(arr);
				},
				success: function() {
					$("#carfinder-form").hide("slow").after("<p>Thank you for submitting your information! A dealership representative will be in contact.</p>");
				}
			});
			return false;
		},
		rules: {
			first_name: { defaultInvalid: true },
			last_name: { defaultInvalid: true },
			sender_phone: { defaultInvalid: true },
			sender_email: { defaultInvalid: true }
		}
	});
	
	var validator_g = $("#financeApplication").validate({
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				beforeSerialize: function() {
					submitIndicatorContainer.hide().html(submitIndicator).fadeIn("slow");
				},
				beforeSubmit: function(arr, $form, options) {
					clearDefaultValues(arr);
				},
				success: function() {
					$("#financeApplication").hide("slow").after("<p>Thank you for submitting your information! A dealership representative will be in contact with you shortly.</p>");
				}
			});
			return false;
		},
		rules: {
			first_name: { defaultInvalid: true },
			last_name: { defaultInvalid: true },
			sender_email: { defaultInvalid: true },
			sin: { defaultInvalid: true },
			sender_phone: { defaultInvalid: true },
			street: { defaultInvalid: true },
			city: { defaultInvalid: true },
			postal_code: { defaultInvalid: true },
			rent_or_mortgage_amount: { defaultInvalid: true },
			employer: { defaultInvalid: true },
			occupation: { defaultInvalid: true },
			net_salary: { defaultInvalid: true },
			signature: { defaultInvalid: true }
		}
	});
	
	var validator_h = $("#tradeInForm").validate({
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				beforeSerialize: function() {
					submitIndicatorContainer.hide().html(submitIndicator).fadeIn("slow");
				},
				beforeSubmit: function(arr, $form, options) {
					clearDefaultValues(arr);
				},
				success: function() {
					$("#tradeInForm").hide("slow").after("<p>Thank you for submitting your information! A dealership representative will be in contact with you shortly.</p>");
				}
			});
			return false;
		},
		rules: {
			first_name: { defaultInvalid: true },
			last_name: { defaultInvalid: true },
			sender_phone: { defaultInvalid: true },
			sender_email: { defaultInvalid: true },
			vehicle_year: { defaultInvalid: true },
			vehicle_make: { defaultInvalid: true },
			vehicle_model: { defaultInvalid: true },
			vin: { defaultInvalid: true },
			vehicle_kilometers: { defaultInvalid: true }
		}
	});
});

function clearDefaultValues(arr) {
	$.each(arr, function(element, values) {						
		switch(values.value) {
			case "First Name":
				values.value = "";
				break;
			case "Last Name":
				values.value = "";
				break;
			case "Email":
				values.value = "";
				break;
			case "Home Phone":
				values.value = "";
				break;
			case "Work Phone":
				values.value = "";
				break;
			case "Social Insurance Number":
				values.value = "";
				break;
			case "Date of Birth":
				values.value = "";
				break;
			case "Street":
				values.value = "";
				break;
			case "Apartment":
				values.value = "";
				break;
			case "City":
				values.value = "";
				break;
			case "Postal Code":
				values.value = "";
				break;
			case "Employer":
				values.value = "";
				break;
			case "Occupation":
				values.value = "";
				break;
			case "Business Phone":
				values.value = "";
				break;
			case "Net Salary":
				values.value = "";
				break;
			case "City":
				values.value = "";
				break;
			case "City":
				values.value = "";
				break;
			case "Comments":
				values.value = "";
				break;
			case "Part Number":
				values.value = "";
				break;
			case "Address":
				values.value = "";
				break;
			case "Apartment":
				values.value = "";
				break;
			case "City":
				values.value = "";
				break;
			case "Postal Code":
				values.value = "";
				break;
			case "Home Phone":
				values.value = "";
				break;
			case "Preferred Appointment Date":
				values.value = "";
				break;
			case "Middle Initial":
				values.value = "";
				break;
			case "Work Phone":
				values.value = "";
				break;
			case "Landlord/Mortgage Holder":
				values.value = "";
				break;
			case "Previous Street":
				values.value = "";
				break;
			case "Previous Apartment":
				values.value = "";
				break;
			case "Business Phone":
				values.value = "";
				break;
			case "Gross Salary":
				values.value = "";
				break;
			case "Other Income":
				values.value = "";
				break;
			case "Previous Employer":
				values.value = "";
				break;
			case "Previous Occupation":
				values.value = "";
				break;
			case "Previous Employer Phone":
				values.value = "";
				break;
			case "Bank Name":
				values.value = "";
				break;
			case "Account Types":
				values.value = "";
				break;
			case "Lender":
				values.value = "";
				break;
			case "Total Debt":
				values.value = "";
				break;
		}
	});
}

function addFormField() {
	var id = document.getElementById("partNumberCount").value;
	$("div.multiField").append('<div class="formRow"><input type="text" name="part_number_' + id + '" id="partNumber' + id + '" /></div>');
	
	id = (id - 1) + 2;
	document.getElementById("partNumberCount").value = id;
}

function labelToValues(currentLabel) {
	if ($(currentLabel).hasClass("show")) {
		return false;
	}
	label = $(currentLabel).html();
	selector = "#" + $(currentLabel).attr("for");
	createValueLabel(selector, label);
	$(currentLabel).addClass("hidden");
}

function createValueLabel (selector, defaultValue) {
	// assign the default value to the form selector
	$(selector).data("default", defaultValue);

	// if the selector is empty, initialize the default value
	if(!$(selector).val()) $(selector).val(defaultValue);

	// assign a function to the focus and blur events
	$(selector).bind("focus blur", function(){
		value = $(this).val();

		// if the current and default value are the same, clear the input field
		if(value==defaultValue) $(this).val("");

		// if the field is empty, set the value to default
		if(!value) $(this).val(defaultValue);
	});
}