$(document).ready(function(){

	// Uppercase Postcodes
	$("input.zip").keypress(function(e) {
		if ((e.which >= 48 && e.which <= 57) || (e.which >= 65 && e.which <= 88) || (e.which >= 97 && e.which <= 120)) {
			// Force Uppercase
			$(this).val($(this).val() + String.fromCharCode(e.which).toUpperCase());
			return false;
		}
	});

	// Highlighting Current Inputs/Labels
	$("#form-apply input,#form-apply textarea,#form-apply select")
		.focus(function(){
			$(this).parents(".field").addClass('hover');
		})
		.blur(function(){
			$(this).parents(".field").removeClass('hover');
				// Validate Single Form Element
				$(this).valid();
		});
	$("#form-apply select").change(function(){
		// Validate Single Form Element
		$(this).valid();
	});

	// Dynamic Validation
	$(".eec_cntry").change(function(){
		// Define EEC Countries and check if selection is in list
		var a_cntry_eec = "'Belgium','Bulgaria','Cyprus','Czech Republic','Germany','Denmark','Estonia','Greece','Spain','Finland','France','Hungary','Republic of Ireland','Italy','Luxembourg','Lithuania','Latvia','Malta','The Netherlands','Poland','Portugal','Romania','Sweden','Slovak Republic','Slovenia'";
		if (a_cntry_eec.indexOf("'" + $(this).val() + "'") > -1) {
			// Is EEC country make the next input "Vat Reg No" class = "required" based on tabindex + 1
			$("#form" + (parseInt($(this).attr("tabindex")) + 1)).addClass('required');
		}
	});

	// Main Form Validation/Submit
	$("#form-apply").validate({
		highlight: function(element, errorClass) {
			$(element).parents(".field").addClass(errorClass);
		},
		unhighlight: function(element, errorClass) {
			$(element).parents(".field").removeClass(errorClass);
		},
		errorElement: "span",
		errorPlacement: function(error, element) {
			//error.appendTo("#main_content");
		},
		submitHandler: function(form) {
			$("select").each(function(){
				// Convert Selects to Read Only input (for printing)
				$(this).parent().html('<input type="text" name="' + $(this).attr("name") + '" id="' + $(this).attr("id") + '" value="' + $(this).val() + '" tabindex="' + $(this).attr("tabindex") + '" readonly="readonly" />');
			});		
			window.print();
		}
	});

});