/*
form validation
written by Caleb Pierce

These classes make a field required as well as
validate the value:
- notBlank
- decimal
- email
- url
- validDate

These class check a field if it has value but
does not make the field required:
- decimal_o
- email_o
- url_o
- validDate_o
*/

$(document).ready(function() { //javascript here will load after the document is ready
	
	//automatically adds 'required' stars to the labels of required input fields
	//note: this requires that the form is layed out according to the design standards
	$(".check .notBlank,.check .url,.check .decimal,.check .email,.check .validDate").parent("td").prev(":nth-child(1)").find("label").before("<span>*</span> ");
	
	$(".check").submit(function(){
		var errors = 0;

		//checks for a valid email address
		$(".email",this).each(function(){
			var e = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
			if (!e.test($(this).val())){
				errors++;
				$(this).css("background-color", "#FFA1A1");
			}
		});
		//checks for a valid email address - optional
		$(".email_o",this).each(function(){
			if ($(this).val() != ''){
				var e = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
				if (!e.test($(this).val())){
					errors++;
					$(this).css("background-color", "#FFA1A1");
				}
			}
		});

		//checks for a blank field
		//also works for radio buttons and checkboxes
		$(".notBlank",this).each(function(){
			if ($(this).is(":radio")){
				var name = $(this).attr("name");
				if ($(this).siblings(":radio[name='"+ name + "']").andSelf().filter(":checked").length == 0){
					errors++;
				}
			}
			else if ($(this).is(":checkbox")){
				var name = $(this).attr("name");
				if ($(this).siblings(":checkbox[name='"+ name + "']").andSelf().filter(":checked").length == 0){
					errors++;
				}
			}
			else {
				if($(this).val() == '') {
					errors++;
					$(this).css("background-color", "#FFA1A1");
				}
			}
		});

		//tests for a valid url
		$(".url",this).each(function(){
			var e = /^(https?|ftp):\/\/(((([a-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(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/;
			if (!e.test($(this).val())){
				errors++;
				$(this).css("background-color", "#FFA1A1");
			}
		});
		//tests for a valid url
		$(".url_o",this).each(function(){
			if ($(this).val() != ''){
				var e = /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/;
				if (!e.test($(this).val())){
					errors++;
					$(this).css("background-color", "#FFA1A1");
				}
			}	
		});

		//checks for a valid date
		$(".validDate",this).each(function(){
			if (/Invalid|NaN/.test(new Date($(this).val()))){
				errors++;
				$(this).css("background-color", "#FFA1A1");
			}
		});
		//checks for a valid date
		$(".validDate_o",this).each(function(){
			if ($(this).val() != ''){
				if (/Invalid|NaN/.test(new Date($(this).val()))){
					errors++;
					$(this).css("background-color", "#FFA1A1");
				}
			}
		});

		//checks for a valid decimal number
		$(".decimal",this).each(function(){
			var e = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/;
			if (!e.test($(this).val())){
				errors++;
				$(this).css("background-color", "#FFA1A1");
			}
		});
		//checks for a valid decimal number
		$(".decimal_o",this).each(function(){
			if ($(this).val() != ''){
				var e = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/;
				if (!e.test($(this).val())){
					errors++;
					$(this).css("background-color", "#FFA1A1");
				}
			}
		});
		
		//handle select boxes
		if (errors != 0)
			alert("Please check the form for errors.\n(Fields with a red star are required.)");
			
		//return the results
		return errors == 0;
	}); //validate
	
	//clear the error divs on field focus
	$(".email,.email_o,.notBlank,.url,.url_o,.validDate,.validDate_o,.decimal,.decimal_o",this).focus(function(){
		$(this).css("background-color", "#fff");
	});

});