$(function () {
	
	$(".fname").change(function () {
		var pattern = /^[A-Z]{1}[a-z]+$/;
		var input = $(this);					
		var err = $("#"+input.attr("id")+"err");
		input.val($.trim(ucwords(input.val().toLowerCase())));
		var name = input.val();
		input.val(name.replace(/\s{2,}/g, " "));
		if (!pattern.test(input.val())) {						
			input.focus();						
			err.html("Please enter a first name");
			err.fadeIn();
		} else {err.fadeOut();}
	});
	//	Description:	Change function for text inputs that expect a first name to be entered. Any name in the format of 1 capital 
	//					letter followed by one or more lowercase letters is accepted. Function will try to format input this way. 
	//					A message appears in the error box on deviating input.
	
	$(".lname").change(function () {
		var pattern = /^[A-Z]{1}[a-z]+((\s|-|\.|\\|\/){1}[A-Z]{1}[a-z]*)?$/;
		var input = $(this);					
		var err = $("#"+input.attr("id")+"err");
		input.val($.trim(ucwords(input.val().toLowerCase())));
		var name = input.val();
		input.val(name.replace(/\s{2,}/g, " "));
		if (!pattern.test(input.val())) {						
			input.focus();
			err.html("Please enter a last name");
			err.fadeIn();
		} else {err.fadeOut();}
	});
	//	Description:	Change function for text inputs that expect a last name to be entered. Any name in the format of 1 capital
	//					letter followed by one or more lowercase letters is accepted. Multiple names are valid too, as long as they are
	//					in the same format, and separated from the previous name by a space, hyphen, period, or slash. Function will try
	//					for format input this way. A message appears in the error box on deviating input.
	
	$(".fullname").change(function () {		
		var pattern = /^[A-Z]{1}[a-z]+\s{1}[A-Z]{1}[a-z]+((\s|-|\.|\\|\/){1}[A-Z]{1}[a-z]*)?$/;
		var input = $(this);
		var err = $("#"+input.attr("id")+"err");
		input.val($.trim(ucwords(input.val().toLowerCase())));
		input.val(input.val().replace(/\s{2,}/g, " "));
		if (!pattern.test(input.val())) {
			input.focus();
			err.html("Please enter a full name");
			err.fadeIn();		
		} else {err.fadeOut();}
	});
	//	Description:	Change function for text inputs that expect a full name to be entered. Any two names in the format of 1 capital
	//					letter followed by one or more lowercase letters is accepted. Multiple last names are valid too, as long as
	//					thy are in the same format, and separated from the previous last name by a space, hyphen, period, or slash. 
	//					Function will try to format input this way. A message appears in the error box on deviating input.
	
	$(".locale").change(function () {
		var input = $(this);
		var errbox = $("#"+input.attr("id")+"err");
		var pattern = /^([A-Z]{1}[a-z]+)+(\s{1}[A-Z]{1}[a-z]+)?$/;
		input.val($.trim(ucwords(input.val().toLowerCase())));
		var name = input.val();
		input.val(name.replace(/\s{2,}/g, " "));
		if (!pattern.test(input.val())) {
			input.focus();
			errbox.html("Please enter a proper name");
			errbox.fadeIn();
		}
		else {errbox.fadeOut();}
	});
	//	Description:	Change function for text inputs that expect a locale name to be entered. For example, this could be a typed
	//					city name, country name, etc. It accepts input where each new word is capitalized. (ie. 'San Jose' is good, but
	//					'San jose' is not). The function will try to format the input this way. A message appears in the error box on
	//					deviating input.
	
	$(".address").change(function () {		
		var pattern=/^([A-z]|[0-9])+\s{1}([A-z]|[0-9])+/;
		var input = $(this);
		var errbox = $("div #"+input.attr("id")+"err");					
		var addy = ucwords($.trim(input.val().toLowerCase()));
		input.val(addy.replace(/\s{2,}/g, " "));
		if (!pattern.test(input.val())) {
			input.focus();
			errbox.html("Please enter valid address information (Street name and number required at minimum)");
			errbox.fadeIn();
		} else {errbox.fadeOut();}
	});
	//	Description:	Change function for text inputs that expect an address line to be entered. Miniumum requirements are two 
	//					alpanumeric components. (ie. '123 Street' is acceptable, '456' is not, and bare 'Street' name without number 
	//					is not). Function will attempt to format input this way. A message appears in the error box when deviant
	//					input is encountered.
	
	$(".email").change(function () {
		var input = $(this);
		var err = $("#"+input.attr("id")+"err");
		var eml = ($.trim(input.val().toLowerCase()));
		input.val(eml.replace(/\s+/g, ""));
		$.ajax({
			type: "POST",
			url: "/a/ajax/checkemail.php",
			data: "eml=" + input.val(),
			success: function (msg) {
				if (msg) {err.fadeOut();}
				else {
					input.focus();
					err.html("Please enter a valid email address");
					err.fadeIn();
				}
			}
		});
	});
	//	Description:	Change function for text inputs that expect an email address to be entered. Requirements for use are 
	//					'validator.php' in the 'a/inc/' directory (this performs the actual validations), and 'checkEmail.php' in the 
	//					'a/ajax/' directory. Requirements for input data are an address conforming to IETF specifications, using a 
	//					domain that responds to nslookups on it's 'A' and 'MX' records.	
	
	$(".phone").change(function () {
		var pattern = /^(\({1}[0-9]{3}\){1}|[0-9]{3}){1}(\s|-|\.)?[0-9]{3}(\s|-|\.)?[0-9]{4}(\s?(x|ext(\.)?|extension)\s?[0-9]+)?$/
		var input = $(this);
		var errbox = $("div #"+input.attr("id")+"err");
		var area = $.trim(input.val());
		input.val(area.replace(/\s{2,}/g, " "));
		if (!pattern.test(input.val())) {
			input.focus();						
			errbox.html("Please enter a valid phone number");
			errbox.fadeIn();
		} else {errbox.fadeOut();}
	});	
	//	Description:	Change function for text inputs that expect a phone number to be entered. Fairly flexible, only 10 digits are 
	//					required. Parentheses wrapping the area code and/or spaces, hyphens, or periods separating area code, prefix, 
	//					and line number are valid, but not mandatory. Extensions also permitted, but not required. Tested on North
	//					American phone numbers. May require modification for international numbers.
	
	$(".postal").change(function () {					
		var input = $(this);
		var errbox = $("#"+input.attr("id")+"err");
		var can =/^[A-CEGHJ-NPR-TV-Z]{1}[0-9]{1}[A-CEGHJ-NPR-TV-Z]{1}\s?[0-9]{1}[A-CEGHJ-NPR-TV-Z]{1}[0-9]$/;
		var us =/^([0-9]{5}|[0-9]{5}(\s|-)?[0-9]{4})$/;
		input.val($.trim(input.val().toUpperCase().replace(/\s{2,}/g, " ")));
		if (!can.test(input.val()) && !us.test(input.val())) {
			input.focus();
			errbox.html("Please enter a valid postal code");
			errbox.fadeIn();
		}
		else {errbox.fadeOut();}
	});
	//	Description:	Change function for text inputs that expect a postal code to be entered. Tested on North American postal codes.
	//					May require modification for international postal codes.	
	
	$(".vercode").change(function () {		
		var input = $(this);
		var errbox = $("#"+input.attr("id")+"err");
		$.ajax({
			type: "POST",
			url: "/a/ajax/checkVerCode.php",
			data: "code=" + input.val(),
			success: function (msg) {
				if (msg) {errbox.fadeOut();}
				else {
					input.focus();
					errbox.html("The input verification code does not match the code displayed");
					errbox.fadeIn();
				}
			}
		});
	});
	//	Description:	Change functions for text inputs that expect a verification code to be entered. Compares input code against a verification
	//					code stored in session. Works as a CAPTCHA function.
	
});
