// JavaScript Document copyright Lashgraphics Pty Ltd 2008 - www.lashgraphics.com.au
// This file may not be used or reproduced without written consent from lashgraphics Pty Ltd


window.onload = initForms;

function initForms() {
	

	for (var i=0; i< document.forms.length; i++) {
		document.forms[i].onsubmit = function() {return validForm();}
	}
	
	var someTags = document.getElementsByTagName("*");
	
	for (var j=0; j<someTags.length; j++) {
	if (someTags[j].className.indexOf("reqd") > -1) {
	 someTags[j].onblur = fieldCheck;
	 }
	}
}

function fieldCheck() {
	 	if (this.value =="") {
	 	this.style.backgroundColor = "#FFFF99";
	 	this.style.border = "2px red inset";
//	 	this.focus();
	 	}
	 else {
		 this.style.backgroundColor = "#F2F2F2";
		 this.style.border="1px inset #999999";
	 }
}

function validForm() {
	var allGood = true;
	var allTags = document.getElementsByTagName("*");

	for (var i=0; i<allTags.length; i++) {
		if (!validTag(allTags[i])) {
			allGood = false;
		}
	}
	return allGood;

	function validTag(thisTag) {
		var outClass = "";
		var allClasses = thisTag.className.split(" ");
	
		for (var j=0; j<allClasses.length; j++) {
			outClass += validBasedOnClass(allClasses[j]) + " ";
		}
	
		thisTag.className = outClass;
	
		if (outClass.indexOf("invalid") > -1) {
			invalidLabel(thisTag.parentNode);
			thisTag.focus();
			if (thisTag.nodeName == "INPUT") {
				thisTag.select();
			}
			return false;
		}
		return true;
		
		function validBasedOnClass(thisClass) {
			var classBack = "";
		
			switch(thisClass) {
				case "":
				case "invalid":
					break;
				case "email":
					if (allGood && !validEmail(thisTag.value)) classBack = "invalid ";
					classBack += thisClass;
					break;
				case "radio":
					if (allGood && !radioPicked(thisTag.name)) classBack = "invalid ";
					classBack += thisClass;
					break;
				case "whatisthis":
					if (allGood && thisTag.value!="") classBack = "invalid ";
					classBack += thisClass;
					break;
				case "reqd":
					if (allGood && thisTag.value=="") classBack = "invalid ";
					classBack += thisClass;
					break;
				case "isNum":
					if (allGood && !isNum(thisTag.value)) classBack = "invalid ";
					classBack += thisClass;
					break;
				case "tmessage":
					if (allGood && isitSpam(thisTag.value)) classBack = "invalid ";
					classBack += thisClass;
					break;
//				case "dropdown":
//					if (allGood && checkdropdown(thisTag.value)) classBack = "invalid ";
//					classBack += thisClass;
//					break;
				default:
					classBack += thisClass;
			}
			return classBack;
		}
		
		function radioPicked(radioName) {
			var radioSet = "";

			for (var d=0; d<document.forms.length; d++) {
				if (!radioSet) {
					radioSet = document.forms[d][radioName];
				}
			}
			if (!radioSet) return false;
			for (d=0; d<radioSet.length; d++) {
				if (radioSet[d].checked) {
					return true;
				}
			}
			return false;
		}
		
		function isNum(passedVal) {
			if (passedVal == "") {
				return false;
			}
			for (var k=0; k<passedVal.length; k++) {
				if (passedVal.charAt(k) < "0") {
					return false;
				}
				if (passedVal.charAt(k) > "9") {
					return false;
				}
			}
			return true;
		}
				
		function validEmail(email) {
			var invalidChars = " /:,;";
		
			if (email == "") {
				return false;
			}
			
			var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

			return re.test(email);
		}
		
	
		function isitSpam(tmessage) {
			if (tmessage == "") {
				return false;
			}
			var re=/^(www|http)$/gi;

			return re.test(tmessage);
		}
		
		function invalidLabel(parentTag) {
			if (parentTag.nodeName == "LABEL") {
				parentTag.className += " invalid";
			}
		}
	}
	
	
	
}



function isitSpam(field) {
	re=/(www|http)/gi;
	if (re.test(field.value)){
	window.alert("Sorry the content of this message is invalid");
	field.value="";
	field.focus();
	}
}

