/*
	MooValidate v1.0 for Mootools 1.2
		
	Usage:
		Works with these types of fields:
		- input (text, radio, checkbox)
		- textarea
		- select
		
		You just need to add a specific class to each fields you want to check. 
		For example, if you add the class
			(code)
			class="required"
			(end code)
		the fields value must be set.
		
		You can also add multiple validation checks.
		For example, if you add the class
			(code)
			class="required email"
			(end code)
		the fields value must be set (required) and be formatted like a proper email address (user@domain.com)
		
	Validation Types:
		Here is the list of classes to perform validation.
			
		required 			- The field becomes required.
		numeric				- The field must be all numeric (0-9) characters... decimal points allowed
		email				- The field must be formatted like a proper email address (user@domain.com)
		url					- The field must be formatted like a proper url (http://www.google.com)
		currency			- The field must be formatted like a proper US currency without the $ (1.99)
		phone				- The field must be formatted like a proper US phone number
		zip					- The field must be formatted like a proper US zip code (5 or 9 digits)
		date				- The field must be formatted like a proper date (mm/yyyy, mm/yy, mm/dd/yy, mm/dd/yyyy... any seperators)
		nospam				- The field must not contain the phrases "google rank" or "search engine optimization"
		confirm				- The fields value must match another field's value... also requires a confirm="field" tag added to the element, the word "field" should be the name of the element you are trying to confirm
		creditcard			- The fields value must be formatted like a proper credit card number... also requires a seperate field called "cardtype" to specify the card type (Values of "MasterCard", "American Express", "Discover" or "Diner's Club")
		monthyear			- The field must be formatted with a month/year date
		cvv					- 3 digit code found on the back of credit cards
		
	About:
		MooValidate.js v.1.0.0 for mootools v1.2 - 5/2009
		
		by ReliantWeb Ltd. (http://www.reliantweb.net/) MIT-style license
		
		Created by Don Walter, last modified by Don Walter
*/

window.addEvent('domready', function() {
	var varSlideDuration = 250;
	
	var errStyles = {
		'background': '#ffc1c1',
		'border-color': '#c40202'
	};
 
	var okStyles = {
		'background': '',
		'border-color': ''
	};
	
	if ($('thisform')) {
		// Loop through form elements
		$('thisform').getElements('input, textarea, select').each(function(input) {
			if ((input.get('tag') != 'textarea' && input.getProperty('type') != 'text' && input.get('tag') != 'select' && input.getProperty('type') != 'password' && input.getProperty('type') != 'checkbox' && input.getProperty('type') != 'radio') || input.className.match('novalidate')) return;
			
			// Inject text div below form element
			var varErrorMsgDiv = new Element('div', { 'id': input.getProperty('name') + '_text', 'class': 'form_text' });
			varErrorMsgDiv.inject($(input.getProperty('name')), 'after');
			
			if (!input.className.match('hidden_field') && input.className.match('monthyear') == null) {
				var varName = input.getProperty('name');
				
				// If the element exists, hide the text div
				if (varName != null) {
					new Fx.Slide(varName + '_text').hide();
				}
			} else if (!input.className.match('hidden_field') && input.className.match('monthyear')) {
				var varName = input.getProperty('name');
				
				// If the element exists, hide the text div
				if (varName != null && varName.indexOf('Year') != -1) {
					new Fx.Slide(varName.substring(0, varName.indexOf('Year')) + 'Date_text').hide();
				}
			}
		});
		
		$('thisform').addEvent('submit', function(e) {
			new Event(e).stop();
			
			var varError = '';
			
			// Loop through form elements
			//$$('input, textarea, select').each(function(input) {
			$('thisform').getElements('input, textarea, select').each(function(input) {
				if ((input.get('tag') != 'textarea' && input.getProperty('type') != 'text' && input.get('tag') != 'select' && input.getProperty('type') != 'password' && input.getProperty('type') != 'checkbox' && input.getProperty('type') != 'radio') || input.className.match('novalidate')) return;
				
				var varName = input.getProperty('name');
				
				// If the element exists, check the values
				if (varName != null && input.className.match('required') && (input.getProperty('type') == 'checkbox' || input.getProperty('type') == 'radio')) {
					var varChecked = 0;
					
					for (var i = 0; i < document.thisform.elements[varName].length; i++) {
						if (document.thisform.elements[varName][i].checked) {
							varChecked++;
						}
					}
					
					if (varChecked == 0) {					
						// Change the element's style to alert the user of problem
						$(varName).setStyles(errStyles);
						
						// Change the text div to alert the user of problem
						$(varName + '_text').set('text', 'This field is required');
						
						// Slide the text div out so the user can see it
						new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideIn();
						
						// Set this variable to yes, so the form doesn't submit
						varError = 'yes';
					} else {
						// Change the element's style back to original
						$(varName).setStyles(okStyles);
							
						// Slide the text div back in
						new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideOut();
							
						// Change the text div to nothing to work with stupid Internet Explorer
						$(varName + '_text').set('text', '');
					}
				} else if (varName != null && !input.className.match('hidden_field') && !input.className.match('novalidate')) {
					// If the element has a class of "required", make sure it's not blank
					if (input.className.match('required') && input.className.match('monthyear') == null && input.get('value') == '') {
						// Change the element's style to alert the user of problem
						input.setStyles(errStyles);
						
						// Change the text div to alert the user of problem
						$(varName + '_text').set('text', 'This field is required');
						
						// Slide the text div out so the user can see it
						new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideIn();
						
						// Set this variable to yes, so the form doesn't submit
						varError = 'yes';
					} else if (input.className.match('required') && input.className.match('monthyear') && input.get('value') == '') {
						// Change the element's style to alert the user of problem
						input.setStyles(errStyles);
						
						if (varName.indexOf('Month') != -1) {
							// Change the text div to alert the user of problem
							$(varName.substring(0, varName.indexOf('Month')) + 'Date_text').set('text', 'This field is required');
							
							// Slide the text div out so the user can see it
							new Fx.Slide(varName.substring(0, varName.indexOf('Month')) + 'Date_text', { duration: varSlideDuration }).slideIn();
						} else if (varName.indexOf('Year') != -1) {
							// Change the text div to alert the user of problem
							$(varName.substring(0, varName.indexOf('Year')) + 'Date_text').set('text', 'This field is required');
							
							// Slide the text div out so the user can see it
							new Fx.Slide(varName.substring(0, varName.indexOf('Year')) + 'Date_text', { duration: varSlideDuration }).slideIn();
						}
												
						// Set this variable to yes, so the form doesn't submit
						varError = 'yes';
					} else {
						if (input.className.match('email') && input.get('value') != '') {
							// If the element has a class of "email", make sure it's formatted like an email address
							var emailregex = "^((([a-z]|[A-Z]|[0-9]|!|#|$|%|&|'|\\*|\\+|\\-|/|=|\\?|\\^|_|`|\\{|\\||\\}|~)+(\\.([a-z]|[A-Z]|[0-9]|!|#|$|%|&|'|\\*|\\+|\\-|/|=|\\?|\\^|_|`|\\{|\\||\\}|~)+)*)@((((([a-z]|[A-Z]|[0-9])([a-z]|[A-Z]|[0-9]|\\-){0,61}([a-z]|[A-Z]|[0-9])\\.))*([a-z]|[A-Z]|[0-9])([a-z]|[A-Z]|[0-9]|\\-){0,61}([a-z]|[A-Z]|[0-9])\\.(([a-z]|[A-Z]){2,4}))|(((([0-9]){1,3}\\.){3}([0-9]){1,3}))|(\\[((([0-9]){1,3}\\.){3}([0-9]){1,3})\\])))$";
							var regex = new RegExp(emailregex);
							
							if (!regex.test(input.get('value'))) {
								// Change the element's style to alert the user of problem
								input.setStyles(errStyles);
								
								// Change the text div to alert the user of problem
								$(varName + '_text').set('text', 'Please check the formatting of your email address');
								
								// Slide the text div out so the user can see it
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideIn();
								
								// Set this variable to yes, so the form doesn't submit
								varError = 'yes';
							} else {
								// Change the element's style back to original
								input.setStyles(okStyles);
								
								// Slide the text div back in
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideOut();
								
								// Change the text div to nothing to work with stupid Internet Explorer
								$(varName + '_text').set('text', '');
							}
						} else if (input.className.match('url') && input.get('value') != '') {
							// If the element has a class of "url", make sure it's formatted like a valid url
							//var urlregex = "^[a-zA-Z0-9\\-]+\\.[a-zA-Z0-9/~\\-]+\\.[a-zA-Z0-9/~\\-_,&\\?\\.;]+[^\\.,\\s<]";
							var urlregex = "(https?:\\/\\/)(\\w+:{0,1}\\w*@)?(\\S+)(:[0-9]+)?(\\/|\\/([\\w#!:.?+=&%@!\\-\\/]))?";
							var regex = new RegExp(urlregex);
							
							if (!regex.test(input.get('value'))) {
								// Change the element's style to alert the user of problem
								input.setStyles(errStyles);
								
								// Change the text div to alert the user of problem
								$(varName + '_text').set('text', 'Should be in the format http://www.google.com');
								
								// Slide the text div out so the user can see it
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideIn();
								
								// Set this variable to yes, so the form doesn't submit
								varError = 'yes';
							} else {
								// Change the element's style back to original
								input.setStyles(okStyles);
								
								// Slide the text div back in
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideOut();
								
								// Change the text div to nothing to work with stupid Internet Explorer
								$(varName + '_text').set('text', '');
							}
						} else if (input.className.match('currency') && input.get('value') != '') {
							// If the element has a class of "currency", make sure it's formatted like a currency
							var currencyregex = "^((\\$)?((\\d+)|(\\d{1,3})(\\,\\d{3})*)(\\.\\d{2,})?)$";
							var regex = new RegExp(currencyregex);
							
							if (!regex.test(input.get('value'))) {
								// Change the element's style to alert the user of problem
								input.setStyles(errStyles);
								
								// Change the text div to alert the user of problem
								$(varName + '_text').set('text', 'Should be formatted like a currency');
								
								// Slide the text div out so the user can see it
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideIn();
								
								// Set this variable to yes, so the form doesn't submit
								varError = 'yes';
							} else {
								// Change the element's style back to original
								input.setStyles(okStyles);
								
								// Slide the text div back in
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideOut();
								
								// Change the text div to nothing to work with stupid Internet Explorer
								$(varName + '_text').set('text', '');
							}
						} else if (input.className.match('numeric') && input.get('value') != '') {
							// If the element has a class of "numeric", make sure it's all numeric digits (Decimal point allowed)
							//var numericregex = "^\\d*[0-9](|.\\d*[0-9])?$";
							var numericregex = "(0)|(^[0-9]*[1-9]+[0-9]*\\.[0-9]*$)|(^[0-9]*\\.[0-9]*[1-9]+[0-9]*$)|(^[0-9]*[1-9]+[0-9]*$)";
							var regex = new RegExp(numericregex);
							
							if (!regex.test(input.get('value'))) {
								// Change the element's style to alert the user of problem
								input.setStyles(errStyles);
								
								// Change the text div to alert the user of problem
								$(varName + '_text').set('text', 'Must be all numeric digits');
								
								// Slide the text div out so the user can see it
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideIn();
								
								// Set this variable to yes, so the form doesn't submit
								varError = 'yes';
							} else {
								// Change the element's style back to original
								input.setStyles(okStyles);
								
								// Slide the text div back in
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideOut();
								
								// Change the text div to nothing to work with stupid Internet Explorer
								$(varName + '_text').set('text', '');
							}
						} else if (input.className.match('phone') && input.get('value') != '') {
							// If the element has a class of "numeric", make sure it's all numeric digits (Decimal point allowed)
							var phoneregex = "^\\D?(\\d{3})\\D?\\D?(\\d{3})\\D?(\\d{4})$";
							var regex = new RegExp(phoneregex);
							
							if (!regex.test(input.get('value'))) {
								// Change the element's style to alert the user of problem
								input.setStyles(errStyles);
								
								// Change the text div to alert the user of problem
								$(varName + '_text').set('text', 'Invalid phone number');
								
								// Slide the text div out so the user can see it
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideIn();
								
								// Set this variable to yes, so the form doesn't submit
								varError = 'yes';
							} else {
								// Change the element's style back to original
								input.setStyles(okStyles);
								
								// Slide the text div back in
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideOut();
								
								// Change the text div to nothing to work with stupid Internet Explorer
								$(varName + '_text').set('text', '');
							}
						} else if (input.className.match('zip') && input.get('value') != '') {
							// If the element has a class of "numeric", make sure it's all numeric digits (Decimal point allowed)
							var zipregex = "^\\d{5}$|^\\d{5}-\\d{4}$";
							var regex = new RegExp(zipregex);
							
							if (!regex.test(input.get('value'))) {
								// Change the element's style to alert the user of problem
								input.setStyles(errStyles);
								
								// Change the text div to alert the user of problem
								$(varName + '_text').set('text', 'Should be either a 5 or 9 digit zip code');
								
								// Slide the text div out so the user can see it
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideIn();
								
								// Set this variable to yes, so the form doesn't submit
								varError = 'yes';
							} else {
								// Change the element's style back to original
								input.setStyles(okStyles);
								
								// Slide the text div back in
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideOut();
								
								// Change the text div to nothing to work with stupid Internet Explorer
								$(varName + '_text').set('text', '');
							}
						} else if (input.className.match('date') && input.get('value') != '') {
							// If the element has a class of "date", make sure it's formatted like a date
							//var dateregex = "^(([1-9])|(0[1-9])|(1[0-2]))\\/(([0-9])|([0-2][0-9])|(3[0-1]))\\/(([0-9][0-9])|([1-2][0,9][0-9][0-9]))$";
							var dateregex = "^(((([1-9])|(0[1-9])|(1[0-2])))|(([1-9])|(0[1-9])|(1[0-2]))\\/(([0-9])|([0-2][0-9])|(3[0-1])))\\/(([0-9][0-9])|([1-2][0,9][0-9][0-9]))$";
							var regex = new RegExp(dateregex);
							
							if (!regex.test(input.get('value'))) {
								// Change the element's style to alert the user of problem
								input.setStyles(errStyles);
								
								// Change the text div to alert the user of problem
								$(varName + '_text').set('text', 'Should be a valid date in a proper date format');
								
								// Slide the text div out so the user can see it
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideIn();
								
								// Set this variable to yes, so the form doesn't submit
								varError = 'yes';
							} else {
								// Change the element's style back to original
								input.setStyles(okStyles);
								
								// Slide the text div back in
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideOut();
								
								// Change the text div to nothing to work with stupid Internet Explorer
								$(varName + '_text').set('text', '');
							}
						} else if (input.className.match('nospam') && input.get('value') != '') {
							if (input.get('value').toLowerCase().indexOf('[url=') > -1 || input.get('value').toLowerCase().indexOf('[link=') > -1 || input.get('value').toLowerCase().indexOf('google rank') > -1 || input.get('value').toLowerCase().indexOf('search engine optimization') > -1 || input.get('value').toLowerCase().indexOf('without prescription') > -1) {
								// Change the element's style to alert the user of problem
								input.setStyles(errStyles);
								
								// Change the text div to alert the user of problem
								$(varName + '_text').set('text', 'Please take your spam elsewhere');
								
								// Slide the text div out so the user can see it
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideIn();
								
								// Set this variable to yes, so the form doesn't submit
								varError = 'yes';
							} else {
								// Change the element's style back to original
								input.setStyles(okStyles);
								
								// Slide the text div back in
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideOut();
								
								// Change the text div to nothing to work with stupid Internet Explorer
								$(varName + '_text').set('text', '');
							}
						} else if (input.className.match('confirm') && input.get('value') != '') {
							// If the element has a class of "confirm", make sure it matches the value of the field in the "confirm" attribute
							var varOriginalField = input.get('confirm');
							
							var varCurrentValue = $(varOriginalField).get('value');
							var varConfirmValue = input.get('value');
							
							if (varCurrentValue != varConfirmValue) {
								// Change the element's style to alert the user of problem
								input.setStyles(errStyles);
								
								// Change the text div to alert the user of problem
								$(varName + '_text').set('text', 'The confirmation does not match the original value');
								
								// Slide the text div out so the user can see it
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideIn();
								
								// Set this variable to yes, so the form doesn't submit
								varError = 'yes';
							} else {
								// Change the element's style back to original
								input.setStyles(okStyles);
								
								// Slide the text div back in
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideOut();
								
								// Change the text div to nothing to work with stupid Internet Explorer
								$(varName + '_text').set('text', '');
							}
						} else if (input.className.match('creditcard') && input.get('value') != '') {
							// If the element has a class of "creditcard", make sure it's all numeric digits and spaces
							var varCardTypeField = input.get('cardtype');
							
							var varCardType = $(varCardTypeField).get('value');
							var varCardNumber = input.get('value').replace(/ /g,'');
							
							var ccregex = '^[\\d\\s]*$';
							var regex = new RegExp(ccregex);
							
							if (!regex.test(varCardNumber)) {
								// Change the element's style to alert the user of problem
								input.setStyles(errStyles);
								
								// Change the text div to alert the user of problem
								$(varName + '_text').set('text', 'May contain only numeric digits and spaces');
								
								// Slide the text div out so the user can see it
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideIn();
								
								// Set this variable to yes, so the form doesn't submit
								varError = 'yes';
							} else {
								// Check the credit card type (Field in the "cardtype" attribute), find the appropriate regex to validate the number
								if (varCardType == 'Visa') {
									// Visa
									var ccregex = "^4\\d{3}-?\\d{4}-?\\d{4}-?\\d{4}$";
								} else if (varCardType == 'MasterCard') {
									// Mastercard
									var ccregex = "^5[1-5]\\d{2}-?\\d{4}-?\\d{4}-?\\d{4}$";
								} else if (varCardType == 'American Express') {
									// American Express
									var ccregex = "^3[4,7]\\d{13}$";
								} else if (varCardType == 'Discover') {
									// Discover
									var ccregex = "^6011-?\\d{4}-?\\d{4}-?\\d{4}$";
								} else if (varCardType == 'Diner\'s Club') {
									// Diner's Club
									var ccregex = "^3[0,6,8]\\d{12}$";
								} else {
									// Anything else
									var ccregex = "^$";
								}
								
								var regex = new RegExp(ccregex);
								
								if (!regex.test(varCardNumber)) {
									// Change the element's style to alert the user of problem
									input.setStyles(errStyles);
									
									// Change the text div to alert the user of problem
									$(varName + '_text').set('text', 'This number does not match the \'' + varCardType + '\' format');
									
									// Slide the text div out so the user can see it
									new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideIn();
									
									// Set this variable to yes, so the form doesn't submit
									varError = 'yes';
								} else {
									// Change the element's style back to original
									input.setStyles(okStyles);
									
									// Slide the text div back in
									new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideOut();
									
									// Change the text div to nothing to work with stupid Internet Explorer
									$(varName + '_text').set('text', '');
								}
							}
						} else if (input.className.match('monthyear') && input.get('value') != '') {
							// Change the element's style back to original
							input.setStyles(okStyles);
							
							if (varName.indexOf('Month') != -1) {
								if ($(varName).get('value') != '' && $(varName.substring(0, varName.indexOf('Month')) + 'Year').get('value') != '') {
									// Slide the text div back in
									new Fx.Slide(varName.substring(0, varName.indexOf('Month')) + 'Date_text', { duration: varSlideDuration }).slideOut();
									
									// Change the text div to nothing to work with stupid Internet Explorer
									$(varName.substring(0, varName.indexOf('Month')) + 'Date_text').set('text', '');
								}
							} else if (varName.indexOf('Year') != -1) {
								if ($(varName.substring(0, varName.indexOf('Year')) + 'Month').get('value') != '' && $(varName).get('value') != '') {
									// Slide the text div back in
									new Fx.Slide(varName.substring(0, varName.indexOf('Year')) + 'Date_text', { duration: varSlideDuration }).slideOut();
									
									// Change the text div to nothing to work with stupid Internet Explorer
									$(varName.substring(0, varName.indexOf('Year')) + 'Date_text').set('text', '');
								}
							}
						} else if (input.className.match('cvv') && input.get('value') != '') {
							// If the element has a class of "numeric", make sure it's all numeric digits (Decimal point allowed)
							var dateregex = "^\\d{3}$";
							var regex = new RegExp(dateregex);
							
							if (!regex.test(input.get('value'))) {
								// Change the element's style to alert the user of problem
								input.setStyles(errStyles);
								
								// Change the text div to alert the user of problem
								$(varName + '_text').set('text', 'Must be 3 numeric digits');
								
								// Slide the text div out so the user can see it
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideIn();
								
								// Set this variable to yes, so the form doesn't submit
								varError = 'yes';
							} else {
								// Change the element's style back to original
								input.setStyles(okStyles);
								
								// Slide the text div back in
								new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideOut();
								
								// Change the text div to nothing to work with stupid Internet Explorer
								$(varName + '_text').set('text', '');
							}
						} else {
							// Change the element's style back to original
							input.setStyles(okStyles);
								
							// Slide the text div back in
							new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideOut();
								
							// Change the text div to nothing to work with stupid Internet Explorer
							$(varName + '_text').set('text', '');
						}
					}
				} else {
					// Change the element's style back to original
					input.setStyles(okStyles);
						
					// Slide the text div back in
					new Fx.Slide(varName + '_text', { duration: varSlideDuration }).slideOut();
						
					// Change the text div to nothing to work with stupid Internet Explorer
					$(varName + '_text').set('text', '');
				}
			});
			
			// If there are no errors, submit the form
			if (varError == '') {
				$('thisform').submit();
			}
		});
	}
});
