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
		$$('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.className.match('novalidate')) return;
			
			if (!input.className.match('hidden_field') && input.getProperty('name') != 'List' && 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.getProperty('name') != 'List' && 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) {
				if (input.get('tag') != 'textarea' && input.getProperty('type') != 'text' && input.get('tag') != 'select' && input.getProperty('type') != 'password' && input.getProperty('type') != 'checkbox') return;
				
				var varName = input.getProperty('name');
				
				// If the element exists, check the values
				if (varName != null && !input.className.match('hidden_field') && varName != 'List' && !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 "currency", make sure it's formatted like a currency							
							var urlregex = "^[a-zA-Z0-9\\-]+\\.[a-zA-Z0-9/~\\-]+\\.[a-zA-Z0-9/~\\-_,&\\?\\.;]+[^\\.,\\s<]";
							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 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 "numeric", make sure it's all numeric digits (Decimal point allowed)							
							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 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 in the format mm/dd/yyyy');
								
								// 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', '');
						}
					}
				}
			});
			
			// If there are no errors, use XML to send form data to the validation page
			if (varError == '') {
				var varQueryString = '';
				
				var varFirstItem = true;
				
				for (i = 0; i < document.thisform.elements.length; i++) {
					if (document.thisform.elements[i].type != 'button' && (document.thisform.elements[i].type != 'checkbox' || (document.thisform.elements[i].type == 'checkbox' && document.thisform.elements[i].checked == true))) {
						if (varFirstItem == true) {
							varQueryString = varQueryString + '?';
							varFirstItem = false;
						} else {
							varQueryString = varQueryString + '&';
						}
						
						if (document.thisform.elements[i].type == 'select-multiple') {
							var varMultiSelected = '';
							
							for (var x = 0; x < document.thisform.elements[i].length; x++){
								if (document.thisform.elements[i].options[x].selected) {
									varMultiSelected = varMultiSelected + document.thisform.elements[i].options[x].value + ', ';
								}
							}
							
							if (varMultiSelected.length > 0) {
								varMultiSelected = varMultiSelected.substring(0, varMultiSelected.length - 2);
							}
							
              // I replaced the old broken url escape algorythm with the one built into js that actually works right out of the box. -CT
							varQueryString = varQueryString + document.thisform.elements[i].name + '=' + encodeURIComponent(  varMultiSelected  );
						} else {
							varQueryString = varQueryString + document.thisform.elements[i].name + '=' + encodeURIComponent(  document.thisform.elements[i].value  );
						}
					}
				}
				
				var xmlHttp = createXMLHttpRequest();
				xmlHttp.open('GET', document.getElementById('ValidatePage').value + varQueryString, false);
				xmlHttp.send(null);
				
				// When the response from the validation page has been received, perform whatever actions where specified
				if (xmlHttp.readyState==4) {
					if (xmlHttp.status == 200) {
						var arrResponse = xmlHttp.responseText.split("||");
						
						if (arrResponse[0] == 'Validated') {
							// Continue on to the next page
							//location.replace(arrResponse[1]);
							location.href = arrResponse[1];
						} else {
							$(arrResponse[0]).setStyles(errStyles);
							
							// Change the text div to alert the user of problem
							$(arrResponse[0] + '_text').set('text', arrResponse[1]);
							
							// Slide the text div out so the user can see it
							new Fx.Slide(arrResponse[0] + '_text', { duration: varSlideDuration }).slideIn.delay(100, new Fx.Slide(arrResponse[0] + '_text', { duration: varSlideDuration }));
						  
            }
					}
          else
          {
            ////////////////////////////////// Error handling in this project SUCKS! This is added as a catch-all to get some feedback when there is a server error in the response...
            alert('There was an unexpected error!\nPlease report this error to web@dnet.net\nTechnical information about the error follows:\n\n'+xmlHttp.responseText);
            //////////////////////////////////
          }
				}
			}
		});
	}
});
