/************************************************************************
	Trading Places International - Validation Library
		Checks forms for validity before processing.
		
	Version 3.0 Alpha, by Matan Lurey
	No dependancies required.
************************************************************************/
var TPIValid = {
	Version: "3.00a",
	setup: function(){
		// Search for all forms, and retrieve ones with class of validate
		var forms = document.getElementsByTagName("form");
		for (var i=0;i<forms.length;i++)
			if (forms[i].className == "validate")
				TPIValid.watch(forms[i]);
	},
	watch: function(form){
		// Watch a form for submission
		form.onsubmit = function(e)
		{			
			// Find event
			if (!e) var e = window.event;
			var form = e.target || e.srcElement;
			
			// Is the form asked to allow?
			if (form.className == "_done") return;
			
			// Process items (input or select)
			var inputs = $array(form.getElementsByTagName("input"));
			var selects = $array(form.getElementsByTagName("select"));
			
			// Merge arrays
			var els = inputs.concat(selects);
			
			// Go through all items, and check validation
			for (var i=0;i<els.length;i++)
			{
				var testAs = els[i].className.split(" ").first();
				var rX;
				var minLength;
				var valBy;
				
				switch (testAs.toLowerCase())
				{
					case "any-value":
						rX = /^\X/;
						minLength = 1;
						valBy = false;
					break;
					case "name":
						rX = /^a-zA-Z\s/;
						minLength = 2;
						valBy = false;
					break;
					case "email":
						rX = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
						minLength = 6;	
						valBy = true;
					break;
					case "maint-fee":
						rX = /^\X/;
						minLength = 2;
						valBy = false;
					break;
					case "number":
						rX = /^\D/;
						minLength = 1;
						valBy = false;
					break;
					case "date":
						rX = /[0-3][0-9]-0|1[0-9]-19|20[0-9]{2}/;
						minLength = 6;
						valBy = false;
					break;
					default:
						minLength = -1;
				}
				
				// Now test it
				var value = $value(els[i]) || "";
				// alert(els[i].name + ": " + testAs + " == " + value);
				if (testAs == "checked" && value != true || minLength != -1 && (value.length < minLength || rX.test(value.trim()) != valBy)) {
					els[i].style.backgroundColor = "#FF9797";
					var failed = true;
				} else {
					els[i].style.backgroundColor = "";
				}
			}
			
			if (failed)
			{
				var old = document.getElementById("_bad_form");
				if (old){old.parentNode.removeChild(old);}
				
				var div = document.createElement("div");
				div.id = "_bad_form";
				div.style.backgroundColor = "#FFBABA";
				div.style.padding = "3px;";
				div.style.margin = "5px;";
				div.style.textAlign = "center";
				// div.style.fontSize = "13px;";
				
				div.innerHTML = "Please check one or more elements below, the form is incorrect";
				
				form.parentNode.insertBefore(div, form);
				
				if (e.stopPropagation) e.stopPropagation(); else e.cancelBubble = true;
				
				return false;
			}
				
		}
	}
}

// Startup watcher ------------------------------------------------------
if (window.addEventListener) window.addEventListener("load", TPIValid.setup, false);
else window.attachEvent("onload", TPIValid.setup);

// Convert to Array -----------------------------------------------------
function $array(collection)
{
	var a = [];
	for (var i = 0; i < collection.length; i++)
		a.push(collection[i]);
	return a;
}

// Get first element ----------------------------------------------------
if (!Array.prototype.first){Array.prototype.first = function(){return this[0];}}

// Get forms value ------------------------------------------------------
function $value(element)
{
	switch (element.tagName.toLowerCase())
	{
		case "input":
			switch (element.type.toLowerCase())
			{
				case "checkbox":
					return element.checked ? true : false;
				break;
				case "radio":
					return element.checked ? true : false;
				break;
				default:
					return element.value;
				break;
			}
		break;
		case "select":
			return element.options[element.selectedIndex].value;
		break;
		default:
			return element.value || null;
		break;
	}
	return null;
}

// String Trimming Prototypes
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}