// JavaScript Document
$(document).ready (function() {
	
	function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	};
	
	// check all inputs on blur, evaluate their val() and add / remove .error as necessary
	$("#rightWrapper #kitRequestWidget form input").blur(function() {
		var value = $(this).val();
		if((value === null)||(value === "")) { 
			$(this).addClass("error");
		} else if ($(this).hasClass("error")) {
			$(this).removeClass("error");
		}
	});
	
	// check the community on blur, if they're not a 3 letter code add .error
	$("#rightWrapper #kitRequestWidget form select").blur(function() {
		var value = $(this).val();
		if(value.length !== 3) { 
			$(this).addClass("error");
		} else if ($(this).hasClass("error")) {
			$(this).removeClass("error");
		} 
	});
	
	/******************************************************************************************
	** This handles the form submit action of the widget. AM 5/20/11                         **
	** Notes:                                                                                **
	** If the form values are all there, we submit to an intermediate URL on                 **
	** thecareexperts.com to ensure all cookies are set. That page auto-submits to the       **
	** correct itrac/Unica page so everything seems to work smoothly.                        **
	******************************************************************************************/
	
	$("#rightWrapper #kitRequestWidget form input.downloadNow").click(function(event) {
		event.preventDefault(); 						//prevent form.submit()
		var missingData = 0;							// missing required fields counter
		$("#rightWrapper #kitRequestWidget form input[type='text']").each(function() {
			var value = $(this).val();					// get form field value
			if((value === null)||(value === "")) { 		// if empty
				$(this).addClass("error");				// add error class to empty field
				missingData++;							// add one to  counter
			} else if ($(this).hasClass("error")) {		// not empty but has had error class applied
				$(this).removeClass("error");			// clear error class
			} 
		});
		
		$("#rightWrapper #kitRequestWidget form input[type='email']").each(function() {
			var value = $(this).val();					// get form field value
			if((value === null)||(value === "")) { 		// if empty
				$(this).addClass("error");				// add error class to empty field
				missingData++;							// add one to  counter
			} else if(!isValidEmailAddress(value)) {
				$(this).addClass("error");				// add error class to empty field
				missingData++;
			} else if ($(this).hasClass("error")) {		// not empty but has had error class applied
				$(this).removeClass("error");			// clear error class
			}
		});
		
		$("#rightWrapper #kitRequestWidget form select").each(function() {
			var value = $(this).val();
			if(value.length !== 3) { 					// data must be 3 chars long to be a valid COM code
				$(this).addClass("error");
				missingData++;
			} else if ($(this).hasClass("error")) {
				$(this).removeClass("error");
			} 
		});
		
		if(missingData === 0 ) $("#rightWrapper #kitRequestWidget form").submit();
	});	
});
