/* 

Form Validation 

Requires two divs: The inner one can have a user defined name; the outer one must be called the inner ones
name suffixed by '_outer'.

The errorList custom attribute of the form should be set to the inner DIV name

*/

function validateForm (thisForm) {

	var blnError;
	var strErrorList;
	var errorList;
	
	strErrorList = "";
	
	blnFormValid = true;

	resetOtherForms(thisForm);

	for (i=0;i<thisForm.getElementsByTagName('input').length;i++) {
		thisInput = thisForm.getElementsByTagName('input')[i];

		if (thisInput.getAttribute('classBuffer')==undefined) {
			newattribute = document.createAttribute('classBuffer');
			newattribute.nodeValue = thisInput.className;
			thisInput.setAttributeNode(newattribute);
		}
	
		if ((thisInput.type=="text"||thisInput.type=="hidden"||thisInput.type=="password") && thisInput.getAttribute('required')!=undefined && thisInput.getAttribute('required')=="true" && thisInput.value=="") {
			thisInput.className += " " + thisInput.getAttribute('cssErrorClass');
			if (thisInput.getAttribute('errMsg')!=undefined) {
				strErrorList = strErrorList + thisInput.getAttribute('errMsg') + "<br />";
			}
			blnFormValid = false;
		} else if ((thisInput.type=="text"||thisInput.type=="hidden"||thisInput.type=="password") && thisInput.getAttribute('inputContent')=="email" && thisInput.value!="" && !isValidEmail(thisInput.value)) {
			thisInput.className += " " + thisInput.getAttribute('cssErrorClass');
			if (thisInput.getAttribute('errMsg')!=undefined) {
				strErrorList = strErrorList + thisInput.getAttribute('errMsg') + "<br />";
			}
			blnFormValid = false;			
		} else if ((thisInput.type=="text"||thisInput.type=="hidden"||thisInput.type=="password") && thisInput.getAttribute('inputContent')=="url" && thisInput.value!="" && !isValidURL(thisInput.value)) {
			thisInput.className += " " + thisInput.getAttribute('cssErrorClass');
			if (thisInput.getAttribute('errMsg')!=undefined) {
				strErrorList = strErrorList + thisInput.getAttribute('errMsg') + "<br />";
			}
			blnFormValid = false;
		} else if ((thisInput.type=="text"||thisInput.type=="hidden"||thisInput.type=="password") && thisInput.getAttribute('minlen')!=undefined && thisInput.value.length<thisInput.getAttribute('minlen')) {
			thisInput.className += " " + thisInput.getAttribute('cssErrorClass');
			if (thisInput.getAttribute('errMsg')!=undefined) {
				strErrorList = strErrorList + thisInput.getAttribute('errMsg') + "<br />";
			}
			blnFormValid = false;		
		} else if ((thisInput.type=="text"||thisInput.type=="hidden"||thisInput.type=="password") && thisInput.getAttribute('maxlen')!=undefined && thisInput.value.length>thisInput.getAttribute('maxlen')) {
			thisInput.className += " " + thisInput.getAttribute('cssErrorClass');
			if (thisInput.getAttribute('errMsg')!=undefined) {
				strErrorList = strErrorList + thisInput.getAttribute('errMsg') + "<br />";
			}
			blnFormValid = false;
		} else {
			if (thisInput.getAttribute('classBuffer')!="") {
				thisInput.className = thisInput.getAttribute('classBuffer');
			} else {
				thisInput.className = "";
			}		
		}
	}

	for (i=0;i<thisForm.getElementsByTagName('select').length;i++) {
		thisInput = thisForm.getElementsByTagName('select')[i];

		if (thisInput.getAttribute('classBuffer')==undefined) {
			newattribute = document.createAttribute('classBuffer');
			newattribute.nodeValue = thisInput.className;
			thisInput.setAttributeNode(newattribute);
		}
	
		if (thisInput.getAttribute('required')!=undefined && thisInput.getAttribute('required')=="true" && thisInput.value=="") {
			thisInput.className += " " + thisInput.getAttribute('cssErrorClass');
			if (thisInput.getAttribute('errMsg')!=undefined) {
				strErrorList = strErrorList + thisInput.getAttribute('errMsg') + "<br />";
			}
			blnFormValid = false;
		}
	}
	
	for (i=0;i<thisForm.getElementsByTagName('textarea').length;i++) {
		thisInput = thisForm.getElementsByTagName('textarea')[i];

		if (thisInput.getAttribute('classBuffer')==undefined) {
			newattribute = document.createAttribute('classBuffer');
			newattribute.nodeValue = thisInput.className;
			thisInput.setAttributeNode(newattribute);
		}
	
		if (thisInput.getAttribute('required')!=undefined && thisInput.getAttribute('required')=="true" && thisInput.value=="") {
			thisInput.className += " " + thisInput.getAttribute('cssErrorClass');
			if (thisInput.getAttribute('errMsg')!=undefined) {
				strErrorList = strErrorList + thisInput.getAttribute('errMsg') + "<br />";
			}
			blnFormValid = false;
		} else if (thisInput.getAttribute('inputContent')=="email" && thisInput.value!="" && !isValidEmail(thisInput.value)) {
			thisInput.className += " " + thisInput.getAttribute('cssErrorClass');
			if (thisInput.getAttribute('errMsg')!=undefined) {
				strErrorList = strErrorList + thisInput.getAttribute('errMsg') + "<br />";
			}
			blnFormValid = false;			
		} else if (thisInput.getAttribute('minlen')!=undefined && thisInput.value.length<thisInput.getAttribute('minlen')) {
			thisInput.className += " " + thisInput.getAttribute('cssErrorClass');
			if (thisInput.getAttribute('errMsg')!=undefined) {
				strErrorList = strErrorList + thisInput.getAttribute('errMsg') + "<br />";
			}
			blnFormValid = false;		
		} else if (thisInput.getAttribute('maxlen')!=undefined && thisInput.value.length>thisInput.getAttribute('maxlen')) {
			thisInput.className += " " + thisInput.getAttribute('cssErrorClass');
			if (thisInput.getAttribute('errMsg')!=undefined) {
				strErrorList = strErrorList + thisInput.getAttribute('errMsg') + "<br />";
			}
			blnFormValid = false;
		} else {
			if (thisInput.getAttribute('classBuffer')!="") {
				thisInput.className = thisInput.getAttribute('classBuffer');
			} else {
				thisInput.className = "";
			}		
		}
	}
	
	if (!blnFormValid) {
		if (thisForm.getAttribute('errorList')!=undefined) {
			if (document.getElementById(thisForm.getAttribute('errorList'))!=undefined) {
				errorList = document.getElementById(thisForm.getAttribute('errorList'));
				
				/*
				
				It appears you can just use errorList_outer to get to the main error box
				but this has not been defined! It works but not to be trusted.  It is now commented
				out again because it caused content folders to break and probably some other
				things.  The hunt continues...
				
				*/
				
				errorList.style.visibility = "visible";
				errorList.style.display = "";
				/*
				errorList_outer.style.display = "block";
				errorList_outer.style.visibility = "visible";
				*/
				errorList.innerHTML = strErrorList;
				/* errorList_outer.innerHTML = errorList_outer.innerHTML + strErrorList */;
			}
		}
		return blnFormValid;
	} else {
		return blnFormValid
	}
}

/* Called to reset other forms to their original state when you move on a new form */

function resetOtherForms (thisForm) {
	/* 
	errorList_outer.style.display = "none";
	errorList_outer.style.visibility = "hidden";
	*/
	
	for (i=0;i<document.getElementsByTagName('form').length;i++) {
		otherForm = document.getElementsByTagName('form')[i];
		if (otherForm.getAttribute('errorList')!=undefined) {
			if (document.getElementById(otherForm.getAttribute('errorList'))!=undefined) {
				document.getElementById(otherForm.getAttribute('errorList')).innerHTML = "";
				document.getElementById(otherForm.getAttribute('errorList')).style.visibility = "hidden";
			}
		}
		if (otherForm.id!=thisForm.id) {
			for (j=0;j<otherForm.getElementsByTagName('input').length;j++) {
				otherInput = otherForm.getElementsByTagName('input')[j];
				if (otherInput.getAttribute('classBuffer')!=undefined) {
					otherInput.className = otherInput.getAttribute('classBuffer');
				}
			}
			for (j=0;j<otherForm.getElementsByTagName('textarea').length;j++) {
				otherInput = otherForm.getElementsByTagName('textarea')[j];
				if (otherInput.getAttribute('classBuffer')!=undefined) {
					otherInput.className = otherInput.getAttribute('classBuffer');
				}
			}
			for (j=0;j<otherForm.getElementsByTagName('select').length;j++) {
				otherInput = otherForm.getElementsByTagName('select')[j];
				if (otherInput.getAttribute('classBuffer')!=undefined) {
					otherInput.className = otherInput.getAttribute('classBuffer');
				}
			}
		}
	}
}

/* Input Validation */

function isValidEmail(str) {
	var reg = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return reg.test(str);
}

function isValidURL(str) {
	var re = /^(http:\/\/|https:\/\/|ftp:\/\/|callto:\/\/){1}[0-9A-Za-z\.\-]*\.[0-9A-Za-z\.\-\/]*[\S]*$/;
	return re.test(str);
}