function monitorAllForms(){
	monitorAllFormsOldWindowLoads = function(){}
	if( typeof window.onload != 'undefined' && window.onload != null )monitorAllFormsOldWindowLoads = window.onload;
	eval("window.onload=function(){ attachToAllForms();monitorAllFormsOldWindowLoads(); }");
}

function attachToAllForms(){
	var documentForms = document.getElementsByTagName('FORM');
	allFormsArray = new Array();
	for( var x=0; x < documentForms.length; ++x ){
		allFormsArray[x] = function(){}
		if( typeof documentForms[x].onsubmit != 'undefined' && documentForms[x].onsubmit != null )allFormsArray[x] = documentForms[x].onsubmit;
		eval("document.getElementsByTagName('FORM')["+x+"].onsubmit = function(){ var spy = allFormsArray["+x+"]();if( spy == false )return false;return VD_validateForm( this ); }");
	}
}

function VD_validateForm( targetForm ){
	var alerter = "";	
	alerter += validateValueInputs( targetForm );
	if( alerter.length ){ alert("The following error(s) occuried:\n"+alerter);return false; }
}

function validateValueInputs( targetForm ){
	var alerter = "";
	var required = false;
	var invalidMessage = null;
	var exception = null;
	var label = null;
	var validateType = null;
	var onInvalid = null;
	var valid = true;
	var atleastOneHasValue = null;
	var inputTypeArray = new Array("TEXTAREA","INPUT","SELECT");
	
	for( var inputs in inputTypeArray ){
		inputTypeTarget = inputTypeArray[inputs];
		/* Loop form targets */
			for( var inputTarget = 0; inputTarget < targetForm.getElementsByTagName(inputTypeTarget).length; ++inputTarget ){
				var targetedObjectNode = targetForm.getElementsByTagName(inputTypeTarget)[inputTarget];
				/* LOOP THROUGH ATTRIBUTES TO FIND VALUE */
					for( var attributeTarget = 0; attributeTarget < targetedObjectNode.attributes.length; ++attributeTarget ){
						if( !isNaN( attributeTarget ) ){
							/* FIND and DEFINE VALIDATION ATTRIBUTES */
								var targetedAttributeNode = targetedObjectNode.attributes[attributeTarget];
								if( targetedAttributeNode.nodeName.toLowerCase() == 'required' ){
									required = targetedAttributeNode.nodeValue;
								}
								if( targetedAttributeNode.nodeName.toLowerCase() == 'invalidmessage' ){
									invalidMessage = targetedAttributeNode.nodeValue;
								}
								if( targetedAttributeNode.nodeName.toLowerCase() == 'exception' ){
									exception = targetedAttributeNode.nodeValue;
								}
								if( targetedAttributeNode.nodeName.toLowerCase() == 'validatetype' ){
									validateType = targetedAttributeNode.nodeValue;
								}
								if( targetedAttributeNode.nodeName.toLowerCase() == 'oninvalid' ){
									onInvalid = targetedAttributeNode.nodeValue;
								}
								if( targetedAttributeNode.nodeName.toLowerCase() == 'label' ){
									label = targetedAttributeNode.nodeValue;
								}
								if( targetedAttributeNode.nodeName.toLowerCase() == 'atleastonehasvalue' ){
									atleastOneHasValue = targetedAttributeNode.nodeValue;
								}
							/* END */
						}
					}
				/* END */
				/* Process found actions */
					if( label == null && targetedObjectNode.name ){
						label = targetedObjectNode.name;
					}else if( label == null ){
						label = "UnNamed Field("+inputTypeTarget+")";
					}

					if( required == "yes" || eval(required) ){
						if( inputTypeTarget == "INPUT" || inputTypeTarget == "TEXTAREA" ){
							if( !targetedObjectNode.value.length ){
								valid = false;
							}
						}else if( inputTypeTarget == "SELECT" ){
							if( !targetedObjectNode.options[targetedObjectNode.selectedIndex].value.length ){
								valid = false;
							}
						}
					}
					/* process atleastOneHasValue */
						if( atleastOneHasValue != null ){
							var tempArray = atleastOneHasValue.split(',');
							var newArray = new Array();
							for( var x=0; x < tempArray.length; ++x ){
								if( tempArray[x] == 'this' ){
									newArray.push(targetedObjectNode);
								}else if( eval('typeof '+tempArray[x]+' == "undefined"') ){
									newArray.push(targetForm[tempArray[x]]);
								}else if( eval('typeof '+tempArray[x]+' == "object"') ){
									if( eval('typeof '+tempArray[x]+'.length != "undefined"') ){
										eval('var tempArray2 = '+tempArray[x]);
										for( var i=0; i < tempArray2.length; ++i ){
											newArray.push(tempArray2[i]);
										}
									}else{
										eval('newArray.push('+tempArray[x]+')');
									}
								}
							}
							var atleastVALID = false;
							for( x in newArray ){
								if( typeof newArray[x].value != 'undefined' ){
									if( newArray[x].value.length == 0 ){
										atleastVALID = false;
									}else{
										atleastVALID = true;
										break;
									}
								}else if( typeof newArray[x].value == 'undefined' ){
									var message = "One of the targets for \'atleastOneValue\' is invalid\n";
									message += "node Name:"+newArray[x].nodeName;
									alert(message);
									return false;
								}
							}
							if( !atleastVALID ){
								if(!invalidMessage){
									invalidMessage='One of the following fields needs value:\n'
										for( x in newArray ){
											if( typeof newArray[x].attributes['label'] != 'undefined' ){
												invalidMessage += '   '+x+'. '+newArray[x].attributes['label']+'\n';
											}else if( newArray[x].name ){
												invalidMessage += '   '+x+'. '+newArray[x].name+'\n';
											}else{
												invalidMessage += '   '+x+'. UnNamed Field('+newArray[x].nodeName+')\n';
											}
										}
								}
								valid = false;
							}
						}
					/* end */
					/* process validateType */
						if( validateType != null && targetedObjectNode.value.length > 0 ){
							var inputValue = targetedObjectNode.value;
							if( validateType.toLowerCase() == "email" && !isEmail( inputValue ) ){
								valid = false;
								if(!invalidMessage)invalidMessage = validateEmail( inputValue , label );
							}
							if( validateType.toLowerCase() == "integer" && !isInteger( inputValue ) ){
								valid = false;
								if(!invalidMessage)invalidMessage = validateInteger( inputValue , label );
							}
							if( validateType.toLowerCase() == "positiveinteger" && !isPositiveInt( inputValue ) ){
								valid = false;
								if(!invalidMessage)invalidMessage = validatePositiveInt( inputValue , label );
							}
							if( validateType.toLowerCase() == "money" && !isMoney( inputValue ) ){
								valid = false;
								if(!invalidMessage)invalidMessage = validateMoney( inputValue , label );
							}
						}
					/* end */
					
					/* process onInvalid */
						if( onInvalid != null && !valid ){
							eval('targetedObjectNode.onInvalid = function(){ '+onInvalid+' }');
							targetedObjectNode.onInvalid();
						}
					/* end */
					
					/* attach invalid messages */
						if( ( exception && !eval(exception) ) || !exception ){
							if( !valid ){
								if( invalidMessage ){
									alerter += " - " + invalidMessage +"\n";
								}else{
									alerter += " - " + label + " is a required field.\n";
								}
							}
						}
					/* and */
				/* end */
				/* Reset variables */
					required = false;
					invalidMessage = null;
					exception = null;
					label = null;
					onInvalid = null;
					atleastOneHasValue = null;
					valid = true;
					validateType = null;
				/* end */
			}
		/* end */
	}
	return alerter;
}

/* DATA TYPE VALIDATION */
	function isEmail( inputValue ){
		if( inputValue.indexOf(" ") != -1 ){
			return false;
		} else if( inputValue.indexOf("@") == -1 ){
			return false;
		} else if( inputValue.indexOf("@") == 0 ){
			return false;
		} else if( inputValue.substring(inputValue.indexOf("@")+2).indexOf(".") == -1 ){
			return false;
		} else if( inputValue.lastIndexOf("@") == inputValue.length-1 ){
			return false;
		} else if( inputValue.lastIndexOf(".") == inputValue.length-1 ){
			return false;
		}
		return true;
	}

	function validateEmail( inputValue ,label ){
		if( inputValue.indexOf(" ") != -1 ){
			return "Invalid " + label + " address. An e-mail address should not contain a space.";
		} else if( inputValue.indexOf("@") == -1 ){
			return "Invalid " + label + " address. An e-mail address must contain the @ symbol.";
		} else if( inputValue.indexOf("@") == 0 ){
			return "Invalid " + label + " address. The @ symbol can not be the first character of an e-mail address.";
		} else if( inputValue.substring(inputValue.indexOf("@")+2).indexOf(".") == -1 ){
			return "Invalid " + label + " address. An e-mail address must contain at least one period after the @ symbol.";
		} else if( inputValue.lastIndexOf("@") == inputValue.length-1 ){
			return "Invalid " + label + " address. The @ symbol can not be the last character of an e-mail address.";
		} else if( inputValue.lastIndexOf(".") == inputValue.length-1 ){
			return "Invalid " + label + " address. A period can not be the last character of an e-mail address.";
		}
		return true;
	}

	function isMoney( inputValue ){
		inputValue = inputValue.replace(/\./,'');
		inputValue = inputValue.replace(/\$/,'');
		inputValue = inputValue.replace(/\,/g,'');
		inputValue = inputValue.replace(/\-/,'');
		return isInteger( inputValue );
	}

	function validateMoney( inputValue , label ){
		inputValue = inputValue.replace(/\./,'');
		inputValue = inputValue.replace(/\$/,'');
		inputValue = inputValue.replace(/\,/,'');
		inputValue = inputValue.replace(/\-/,'');
		if( !isInteger( inputValue ) ){
			return "The value for " + label + " is not a money type value. This field requires a valid currency rate.";
		}
		return true
	}

	function isInteger( inputValue ){
		if( isNaN(inputValue) ){
			return false;
		}
		return true;
	}

	function validateInteger( inputValue ,label ){
		if( isNaN(inputValue) ){
			return "The value for " + label + " is not a numeric value. This field requires a numeric value.";
		}
		return true;
	}

	function isPositiveInt( inputValue ){
	  // check to make sure the current value is greater then zero
	  if( parseInt(inputValue) < 1 ){
		// here's the error message to display
		return false;
	  }
	  return true;
	}
	
	function validatePositiveInt( inputValue ,label ){
	  // check to make sure the current value is greater then zero
	  if( parseInt(inputValue) < 1 ){
		// here's the error message to display
		return "The " + label + " field must contain an integer greater then zero.";
	  }
	  return true;
	}
/* END */