// JavaScript Document
var Validation = function() {
}

Validation.methods = {
	"required" : [
		'This is a required field.', 
		function(elm) {
			var v = elm.value;
			return  !((v == null) || (v.length == 0));
		}
	],
	"validate-date" : [
		'Please enter a valid date.',
		function(elm) {
				var v = elm.value;
				return !Validation.methods["required"][1](elm) || /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/.test(v);
			}
	],
	"validate-email" : [
		'Please enter a valid email address (i.e. fred@domain.com).',
		function (elm) {
			var v = elm.value;
			return !Validation.methods["required"][1](elm) || /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v)
		}
	],
	"validate-url" : [
		'Please enter a valid URL (i.e. http://www.somedomain.com).',
		function (elm) {
			var v = elm.value;
			return !Validation.methods["required"][1](elm) || /^https?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/.test(v)
		}
	],
	"validate-password" : [
		'Please the password retype must match the original password input.',
		function (elm) {
			var v = elm.value;
			if (!window.passElm) passElm = document.getElementById("pasPassword")
			if (passElm.value) return !Validation.methods["required"][1](elm) || (passElm.value == v);
			else return true;
		}
	],
	"validate-dollar" : [
		'Please enter a valid dollar amount (i.e. $5.00).',
		function (elm) {
			var v = elm.value;
			return !Validation.methods["required"][1](elm) || /^\$?\-?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}\d*(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/.test(v);
		}
	],
	"validate-digits" : [
		'Please use numbers only in this field. Please avoid spaces or other characters such as minus signs, dots or commas.',
		function (elm) {
			var v = elm.value;
			return !Validation.methods["required"][1](elm) || !/[^\d]/.test(v);
		}
	],
	"validate-number" : [
		'Please enter a valid number in this field.',
		function (elm) {
			var v = elm.value;
			return !Validation.methods["required"][1](elm) ||  (!isNaN(v) && !/^\s+$/.test(v));
		}
	]
}

Validation.firstErrorElement = null;

Validation.advise = function(element, advice) {
	var reference = element;
	var parent = reference.getParent();
	
	var node = $(element.id + "-advice");
	
	if ($type(node) != 'element') {
		parent.grab(new Element("div", {
			id: element.id + "-advice",
			html: advice,
			styles: {
				display: 'block',
				height: '8px',
				marginLeft: '-6px',
				padding: '10px 0 5px 0',
				width: '100%',
				color: 'red'
			}
		}));
		new Fx.Tween(parent).start('height', '23px', '46px');
	} else {
		node.set('html', advice);
	}
}

Validation.removeAdvice = function(element) {
	var node = $(element.id + "-advice");
	if ($type(node) == 'element') {
		new Fx.Tween(node.getParent()).start('height', '46px', '23px');
		node.dispose();
	}
}

Validation.validateInput = function(element, methods) {
	var advice;
	
	var result = true;
	
	for (var i = 0; i < methods.length; i++) {
		result = result && Validation.methods[methods[i]][1](element);
		if (!result) {
			advice = Validation.methods[methods[i]][0]
			break;
		}
	}
	
	if (!result) {
		Validation.advise(element, advice);
		if (Validation.firstErrorElement == null) Validation.firstErrorElement = element;
	} else {
		Validation.removeAdvice(element);
	}
}

Validation.validateForm = function(form, hasHiddenElements) {
	if (!hasHiddenElements) hasHiddenElements = false;
	
	Validation.firstErrorElement = null;
	
	for (var i = 0; i < form.elements.length; i++) {
		if (form.elements[i].type == "text" || form.elements[i].type == "password") {
			if (!hasHiddenElements || Validation.isVisible(form.elements[i])) {
				if (form.elements[i].onblur) form.elements[i].onblur();
			}
		}
	}
	
	if (Validation.firstErrorElement == null) {
		return true;
	} else {
		Validation.firstErrorElement.focus();
		return false;
	}
}

Validation.isVisible = function(elm) {
	var visible = true;
	
	do {
		if (elm.tagName == 'DIV')
			if (elm.style.display == 'none')
				visible = false;
		if (elm.tagName == 'BODY' || !visible) break;
	} while (elm = elm.parentNode)
	
	return visible;
}
