
function strtrim() {
    //Match spaces at beginning and end of text and replace
    //with null strings
    return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

String.prototype.trim = strtrim;

function validateForm(xxx, yyy, zz){
	var errString = zz;
	with (xxx) {
       
	   	var requireds_array = yyy;
		
		for (var i = 0; i < (requireds_array.length) / 5; i++){
			thisField = eval(requireds_array[5*i]);
			thisType = requireds_array[5*i+1];
			thisDisplay = requireds_array[5*i+2];
			thisRegex = requireds_array[5*i+3];
			var boolRequired = new Boolean( requireds_array[5*i+4] );
			
			if ((thisType == "text")||(thisType == "textarea")){
				if (thisField.value.trim() == "")
					errString += "\n\t - " + thisDisplay + " cannot be blank";
				//else if (thisRegex != ""){
				//	var regX = eval(thisRegex);
				//	if ( !regX.test(thisField.value) )
				//		errString += "\r\t - " + + " must be in the correct format";
				//}
					
			}
			
			if (thisType == "select"){
				if (thisField[thisField.selectedIndex].value.trim() == "")
					errString += "\n\t - Please select " + thisDisplay;
			}
			
			if ((thisType == "checkbox")||(thisType == "radio")){
				var j, found = false;
				if (thisField.length){
					for(j=0; j< thisField.length; j++)
						if (thisField[j].checked)
							found = true;
				}
				else {
					if (thisField.checked)
						found = true;
				}
				
				
				if (!found)
					errString += "\r\t - At least one " + thisDisplay + " must be checked";
			}
			
			if (thisType == "currency"){
				if ((thisField.value.trim() == "") && boolRequired)
					errString += "\n\t - " + thisDisplay + " cannot be blank";
				else if ((thisField.value.trim() != "")&&(isNaN(thisField.value.trim())))
					errString += "\r\t - " + thisDisplay + " must be in the correct format";
					
			}
			
			if (thisType == "email"){
				var regEx_1 = /^\b[\w\.-]+[@][\w\.-]+\b$/;
				if ((thisField.value.trim() == "") && boolRequired)
					errString += "\n\t - " + thisDisplay + " cannot be blank";
				else if (!regEx_1.test(thisField.value.trim()))
					errString += "\n\t - " + thisDisplay + " must be in the correct format";

				
				if ((thisRegex.trim() != "") && (thisRegex.indexOf('.value') > 0)){
					var str = eval(thisRegex);
					if (thisField.value.trim() != str)
						errString += "\n\t - " + thisDisplay + " does not match";
				}
				
					
			}
			
			if (thisType == "password"){
				var numCount = 0;
				var regEx_1 = /(\d)+/;
				if (regEx_1.exec(thisField.value.trim()) != undefined){
					numCount = regEx_1.exec(thisField.value.trim()).length;
					//alert(numCount);
				}
				if ((thisField.value.trim() == "") && boolRequired)
					errString += "\n\t - " + thisDisplay + " cannot be blank";
				else if ((thisField.value.trim().length < 8)||(numCount < 2))
					errString += "\n\t - " + thisDisplay + " must be 8 characters long and contain two numbers";

					
			}
		}

	}
		
	if (errString != "") {
		alert("The following errors have occurred:\n" + errString);
		return false;
	}
	return true;
}
