// A custom validation object
// v: 0.9
var validate = function () {
	// Set up some private configuration vars
	var defaultErrorMessage = 'There are errors with your information.  Please review, and re-submit.';
	var errorBorderColor = '#FF0000';
	var errorTextColor = '#FF0000';
	var defaultBorderColor = '#000000';
	var defaultTextColor = '#000000';
	var globalErrorMessageId = 'error'; // if the form has a larger error message area, this is where to find it
	
	// A list of all the elements to flag when an error occurs, and how to flag the error:
	//	|text indicates the text color should be changed
	//	|border indicates the border color should be changed
	//	|visibility indicates the element should be turned visible/invisible
	//
	//	element ids should take the form: idOfFormElementElementType, so, for example: nameLabel (keep in mind the camel casing!)
	var errorElements = Array ('|border,text','Label|text','Message|visibility');
	var formID = '';
	
	// Set up some private regexes to use to check various data types
	var validators = {
		valid_not_empty : /.+/i,
		valid_number : /^[-+]?\\b[0-9]*\\.?[0-9]+\\b$/i,
		valid_positive_number : /^([1-9][0-9\\.\\,]*)$/i,
		valid_email : /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,
		valid_year : /^[12][0-9]{3}$/i,
		valid_url : /((http:\/\/)?([a-z0-9][a-z0-9\\.\\-]{0,63}\\.(com|org|net|biz|info|name|net|pro|aero|coop|museum|[a-z]{2,4}))$)\\z/i
	};
	
	// add some private functions
	function validateForm (formID) {
		formID = formID;
		form = document.getElementById(formID);
		isValid = true;
		
		// run through each form element, and validate it
		for (var validateFormLoop=0; validateFormLoop<form.elements.length; validateFormLoop++) {
			element = form.elements[validateFormLoop];

			if (element.getAttribute('data-valid') != null) {
				if (element.getAttribute('data-default') != null && element.value == element.getAttribute('data-default')) value = '';
				else value = element.value;
				
				/*
				alert (
					'element.id: '+element.id+"\n"+
					'element.value: '+value+"\n"+
					'data-valid: '+element.getAttribute('data-valid')+"\n"+
					'match regex: '+validators[element.getAttribute('data_valid')]+"\n"+
					'match: '+value.match(validators[element.getAttribute('data-valid')])
				);
				*/
				
				if ((element.getAttribute('type') != 'checkbox' && value.match(validators[element.getAttribute('data-valid')]) == null) || (element.getAttribute('type') == 'checkbox' && element.getAttribute('data-valid') == 'checked' && element.checked == false)) {
					isValid = false;
					showError(element.id);
				}
			}

			if (element.getAttribute('data-match') != null) {
				if (element.value != document.getElementById(element.getAttribute('data-match')).value) {
					isValid = false;
					showError(element.id);
				}
			}
		}
	
		// run the special validation function, if found
		if (!specialValidation()) isValid = false;
	
		// throw the error or submit the form
		if (!isValid) {
			globalErrorElement = document.getElementById(globalErrorMessageId);
			if (globalErrorElement != null) {
				globalErrorElement.innerHTML = defaultErrorMessage;
				globalErrorElement.style.display = 'block';
			}
			
			alert(defaultErrorMessage);
		}
		
		return isValid;
	}
	
	function showError (element_id) {
//		alert('showError: '+element_id);
		for (showErrorLoop=0; showErrorLoop<errorElements.length; showErrorLoop++) {
			elementInfo = errorElements[showErrorLoop].split('|');
			elementType = elementInfo[0];
			elementErrorTypes = elementInfo[1].split(',');
			
			errorElement = document.getElementById(element_id+elementType);
			
			if (errorElement != null) {
				for (typeLoop=0; typeLoop<elementErrorTypes.length; typeLoop++) {
					type = elementErrorTypes[typeLoop];
					
					switch (type) {
						case 'text':
							errorElement.style.color = errorTextColor;
							break;
						case 'border':
							errorElement.style.borderColor = errorBorderColor;
							break;
						case 'visibility':
							errorElement.style.visibility = 'visible';
							break;
					}
				}
			}
		}
	}
	
	function hideError (element_id) {
		//alert ('hideError: '+element_id);
		for (hideErrorLoop=0; hideErrorLoop<errorElements.length; hideErrorLoop++) {
			elementInfo = errorElements[hideErrorLoop].split('|');
			elementType = elementInfo[0];
			elementErrorTypes = elementInfo[1].split(',');
			
			errorElement = document.getElementById(element_id+elementType);
			
			if (errorElement != null) {
				for (typeLoop=0; typeLoop<elementErrorTypes.length; typeLoop++) {
					type = elementErrorTypes[typeLoop];
					
					switch (type) {
						case 'text':
							errorElement.style.color = defaultTextColor;
							break;
						case 'border':
							errorElement.style.borderColor = defaultBorderColor;
							break;
						case 'visibility':
							errorElement.style.visibility = 'hidden';
							break;
					}
				}
			}
		}
	}
	
	function resetErrors (formID) {
		var form = document.getElementById(formID);
		
		// run through each form element, and hide its error
		for (var resetErrorsLoop=0; resetErrorsLoop<form.elements.length; resetErrorsLoop++) {
			if (form.elements[resetErrorsLoop].id != null && form.elements[resetErrorsLoop].id != '') hideError(form.elements[resetErrorsLoop].id);
		}
		
		specialValidationReset();
	}
	
	// a couple functions to handle any special validation on the page
	function specialValidation () { return true; }
	function specialValidationReset () {}
	
	return {
		checkAndSubmit : function (formID) {
			resetErrors(formID);
			if (validateForm(formID)) document.getElementById(formID).submit();
		},
		checkForm : function (formID) {
			resetErrors(formID);
			return validateForm(formID);
		},

		setSpecialValidation : function  (specialValidationFunction) { specialValidation = specialValidationFunction; },
		setSpecialValidationReset : function  (specialValidationFunctionReset) { specialValidation = specialValidationFunction; },
		setDefaultErrorMessage : function  (error) { defaultErrorMessage = error; },
		setErrorBorderColor : function  (color) { errorBorderColor = color; },
		setErrorTextColor : function  (color) { errorTextColor = color; },
		setDefaultBorderColor : function  (color) { defaultBorderColor = color; },
		setTextColor : function  (color) { defaultTextColor = color; },
		setGlobalErrorMessageId : function  (id) { globalErrorMessageId = id; },
		invalidateField : function (fieldName) { showError(fieldName); },
		
		clearDefault: function (element_id) {
			cleardefault_element = document.getElementById(element_id);
			if (cleardefault_element.value == cleardefault_element.getAttribute('data-default')) cleardefault_element.value = '';
		},
		restoreDefault: function (element_id) {
			restoredefault_element = document.getElementById(element_id);
			if (restoredefault_element.value == '') restoredefault_element.value = restoredefault_element.getAttribute('data-default');
		}
	};
}();
/*	need to add:
	- reset form
*/
