/*	Author:		Daniel C. Richmond
	File:		validate.js
	Purpose:	form validation
*/

$(document).ready(
	function() {
		// on validated form submission...
		var myForm = $("form.validate");
		myForm.each(function() {
			$(this).submit(
				function submit() {
					//return false;
					// define fail counts and whether or not to validate non-required typed fields
					var fails	= 0;
					var picky	= $(this).hasClass("picky");
					//
					function matchesMatch() {
						// checks to see if all "match" classed items are identical
						// returns true/false
						var returner = true;
						var lastValue = "";
						$(this).find(".match").each(
							function () {
								if ($(this).val() != lastValue && lastValue != "") {
									returner = false;
								}
								lastValue = $(this).val();
							}
						)
						return returner;
					}
					//
					// for all items to be validated...
					$(this).find("input, textarea, select").each(
						function() {
							// define some basics
							var isSubmit	= ($(this).attr("type") == "submit");
							var match		= $(this).hasClass("match");
							var email		= $(this).hasClass("email");
							var zip			= $(this).hasClass("zip");
							var phone		= $(this).hasClass("phone");
							var req			= $(this).hasClass("req");
							var message 	= "";
							var myVal		= $(this).val();
							var warning		= false;
							//
							function makeAlert(msg, warning) {
								// creates the popup alert above the text boxes
								var _class = "alert";
								if (warning) {
									// if it's just a warning (in picky mode), also use this class
									// makes grey box rather than red
									_class += " warning";
								}
								// create html to return and inject
								var html = "";
								html += "<div class = \"" + _class + "\">";
								html += msg;
								html += "</div>";
								return html;
							}
							//
							function kill() {
								// removes element
								$(this).remove();
							}
										
							// if a user left a required field blank...
							if (req && myVal == "") {
								// create error message
								message = "This is a required field.";
							}
							
							// if the user entered something into an "email" field...
							if (email && myVal != "") {
								// this field should be an email
								// check for @ and .
								$regEx = /^.+?@.+?\./g;
								if(!myVal.match($regEx) || myVal == "you@domain.com") {
									// not a valid email address
									// if this field IS NOT required, set warning to TRUE
									req ? null : warning = true;
									// create message
									message = "Invalid email address.";
								}
							}						
							
							// if a user entered something into a "phone" field...
							if (phone && myVal != "") {
								// this field should only contain numbers, (), and -
								$regEx = /^([1]??)([-. ]??)([(]??[0-9]{3}[)]??)([-. ]??)([0-9]{3})([-. ]??)([0-9]{4})$/g;
								if(!myVal.match($regEx)) {
									// not a valid email address
									// if this field IS NOT required, set warning to TRUE
									req ? null : warning = true;
									// create message
									message = "Invalid phone number.";
								}
							}
							
							// if a user entered something into a "zip" field...
							if (zip && myVal != "") {
								// this field should only contain numbers, (), and -
								$regEx = /^\d{5}$/g;
								if(!myVal.match($regEx)) {
									// not a valid email address
									// if this field IS NOT required, set warning to TRUE
									req ? null : warning = true;
									// create message
									message = "Invalid zip code.";
								}
							}
							
							// if there are confirmation (should-be matching) fields AND the fields DO NOT match...
							if (match && !matchesMatch()) {
								// create message
								message = "Entries must match.";
							}
							
							// if message is defined...
							if (message != "" && !isSubmit) {
								// if the validator is picky or the field is required, increment main fail counter
								
								(picky || req) ? fails++ : null;
								// if an alert DIV exists...
								// console.log($(this));
								// console.log($(this).hasClass("mark"));
								if ($(this).hasClass("mark")) {
									$(this)
										.removeClass("valid")
										.addClass("mark-invalid");
									$(this).focus(function () {
										$(this)
											.removeClass("mark-invalid")
											.removeClass("valid");
									});
								} else {
									if ($(this).parent().find("div.alert").length) {
										// ... set its message to the new error message
										$(this).parent().find("div.alert").html(message);
									} else {
										// ... if not, create an alert DIV and make it slide into view
										$(this).parent().prepend(makeAlert(message, warning));
										$(this).parent().find("div.alert").slideDown(250);
									}
								}
							} else if (!isSubmit) {
								// ... if not, slide it out of view and remove it
								if($(this).hasClass("mark")) {
									$(this)
										.removeClass("mark-invalid")
										.addClass("valid");
								}
								$(this)
									.removeClass("mark-invalid")
									.parent().find("div.alert").slideUp(250, kill);
								
							}
						}
					)
					//return false;
					// if errors have been reported...
					if (fails) {
						// ... deny default form submission functionality
						return false;
					}
					// if we arrive here, all is well -- submit form
					return true;
				}
			)
		});
	}
);
