String.prototype.trim = function() {
	return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"");
}

String.prototype.fulltrim = function() {
	return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"").replace(/\s+/g," ");
}
// Validator Object
var valid = new Object();
// REGEX Elements
// matches zip codes
valid.zipcode = /\d{5}(-\d{4})?/;
// matches $17.23 or $14,281,545.45 or ...
valid.currency = /\$\d{1,3}(,\d{3})*\.\d{2}/;
// matches 5:04 or 12:34 but not 75:83
valid.time = /^([1-9]|1[0-2]):[0-5]\d$/;
//matches email
valid.emailaddress = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
///(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
// matches phone ###-###-####
valid.phonenumber = /^\(?\d{3}\)?\s?|-\d{3}-\d{4}$/;
// International Phone Number
valid.phonenumberinternational = /^\d(\d|-){7,20}/;
// IP Address
valid.ipaddress = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
// Date xx/xx/xxxx
valid.date = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
// State Abbreviation
valid.state = /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i;
// Social Security Number
valid.ssn = /^\d{3}\-\d{2}\-\d{4}$/;
//Password 
valid.password=/^.{6,10}$/;          
valid.text=/^.{1,1024}$/; 
valid.number=/^.*[0,9]$/; 
/*
^           # anchor at the start
                       (?=.*\d)     # must contain at least one numeric character
                       (?=.*[a-z])  # must contain one lowercase character
                       (?=.*[A-Z])  # must contain one uppercase character
                             # From 8 to 10 characters in length
                       \s           # allows a space 
*/

function validateForm(theForm) {
	var alertMessage="";
	var formElements = theForm.elements; 
	for(var i = 0; i < formElements.length; i++) {
	   with(formElements[i]) { 
	   		if((formElements[i].getAttribute('required')=='false')&&(formElements[i].value.length==0)){
				
			}else{
				validatorField=formElements[i].getAttribute('validator');
				if(!validatorField) continue; 
				var patternField = valid[validatorField]; 
				if(!patternField){
					if(formElements[i].value.trim()!=document.getElementById(validatorField).value.trim()){
						alertMessage+=formElements[i].getAttribute('error')+'<br>';					
					}
				}else{
					var isMatching = patternField.exec(value.trim());
					if(isMatching==null){
					  alertMessage+=formElements[i].getAttribute('error')+'<br>';
					}				
				}
			}
	   }
	}
	return alertMessage;
}