// function to highlight error fields
function highlightField(fieldNames, useClass) {

	var fieldArray = fieldNames.split(",");

	for (var i = 0; i < fieldArray.length; i++) {
		document.getElementById(fieldArray[i] + "Label").className = useClass;
	}
	
}

// check if string has no characters (other than white space chars)
function isEmpty(string) {

	var result = "";
	
	if (string.replace(/^\s+/,'') == "") {
		result = true;
	}
	else {
		result = false;
	}
	
	return result;
	
}

// check if string is a valid email address
function isInvalidEmail(email) {

	if (email.indexOf("@") > 0 && email.lastIndexOf("@") != (email.length - 1)) {
		return false;
	}
	else {
		return true;
	}
	
}

// check if a string is a valid alpha-numeric string
function isInvalidAlphaNum(string) {
	
	var result = false;
	var pattern = /[^0-9a-zA-Z]/g;
	
	if (pattern.test(string)) {
	
		result = true;
	
	}
	
	return result;

}

// check if a number is a valid integer
function isInvalidInteger(number) {

	var string = number.toString();
	
	if (string == "")
		return true;
	
	var curChar = 0;
	var result = false;
	
	for (var i = 0; i < string.length; i++ ) {
	
		curChar = string.charAt(i).charCodeAt(0);
	
		if (curChar < 48 || curChar > 57) {
			result = true;
			break;
		}
	
	}
	
	return result;

}

// check if a number is a valid floating point number
function isInvalidFloat(number) {

	var string = number.toString();
	
	if (string == "")
		return true;
	
	var haveDecimal = false;
	var curChar = 0;
	var result = false;
	
	for (var i = 0; i < string.length; i++ ) {
	
		curChar = string.charAt(i).charCodeAt(0);
	
		if (curChar == 46) {
			
			if (!haveDecimal) {
				continue;
			}
			
			else {
				result = true;
				break;
			}
			
		}
		
		if (curChar < 48 || curChar > 57) {
			result = true;
			break;
		}
	
	}
	
	return result;
	
}

// check if a date is valid
function isInvalidDate(theDate) {

	if (isNaN(Date.parse(theDate)) == false) {
		return true;
	}	
	else {
		return false;
	}

}

// see if the from date is less than the to date
// when you have drop downs to select a date range
function isFDateLessThanTDate(fromDate, toDate) {

	if (Date.parse(fromDate) > Date.parse(toDate)) {
		return false;
	}
	else {
		return true;
	}

}

// remove the colored highlighting from around the invalid field label
function removeError(obj, defaultClass) {

	document.getElementById(obj.name + "Label").className = defaultClass;

}

var formFields = new Object();

// perform all validation functions for form
function doValidate(obj, errorClass) {
	
	var errMsgs = new Array();
	
	// called to add a message to the array of errors
	function addMsg(msg) {
	
		errMsgs[errMsgs.length] = msg;
	
	}
	
	// loop over form fields
	for (var i = 0; i < obj.length; i++) {
	
		var field = obj.elements[i];
		
		// if the field is found in the object of fields to validate (defined on the page of the form)
		if (formFields[field.name] != null) {
		
			var invalid = false;
			
			// check for required fields that don't have a value or a selected index
			if (formFields[field.name]["required"] == true) {
			
				// if field is a text or textarea, check for empty value
				if ((field.type == "text" || field.type == "textarea" || field.type == "file") && isEmpty(field.value)) {
					invalid = true;
				}
				
				// if field type is a select list of some type, check for selectedIndex value
				else if ((field.type == "select-multiple" || field.type == "select-one") && field.selectedIndex < 1) {
					invalid = true;
				}
				
			}
			
			// check for a min text length
			if (formFields[field.name]["minlength"] != null && !invalid) {
			
				if (field.value.length < formFields[field.name]["minlength"]) {
					invalid = true;
				}
			
			}
			
			// check for a max text length
			if (formFields[field.name]["maxlength"] != null && !invalid) {
			
				if (field.value.length > formFields[field.name]["maxlength"]) {
					invalid = true;
				}
			
			}
			
			// check if field needs to be compared against another field value
			// useful for comparing retyped passwords, email addresses
			if (formFields[field.name]["compare"] != null && !invalid) {
			
				if (field.value != obj[formFields[field.name]["compare"]].value) {
					invalid = true;
				}
			
			}
			
			// check for valid datatype values
			if (formFields[field.name]["datatype"] != null && !invalid) {
			
				switch (formFields[field.name]["datatype"]) {
				
					case "integer":
						invalid = isInvalidInteger(field.value);
						break;
						
					case "float":
						invalid = isInvalidFloat(field.value);
						break;
						
					case "date":
						invalid = isInvalidDate(field.value);
						break;
						
					case "emailaddress":
						invalid = isInvalidEmail(field.value);
						break;
						
					case "alphanumeric":
						invalid = isInvalidAlphaNum(field.value);
						break;
						
				}
				
				// if field is of datatypes float or integer, we can check for minimum and maximum values
				if ((formFields[field.name]["datatype"] == "float" || formFields[field.name]["datatype"] == "integer") && !invalid) {
				
					// check for minimum field value
					if (formFields[field.name]["minvalue"] != null) {
					
						if (field.value < formFields[field.name]["minvalue"]) {
							invalid = true;
						}
					
					}
					
					// check for maximum field value
					if (formFields[field.name]["maxvalue"] != null) {
					
						if (field.value > formFields[field.name]["maxvalue"]) {
							invalid = true;
						}
					
					}
					
				}
				
			}
			
			// if form field is invalid in any way, add it's message
			if (invalid) {
				addMsg(formFields[field.name]["message"]);
				highlightField(field.name, errorClass);
			}			
			
		}
	
	}
	
	// if there are any messages in the array
	if (errMsgs.length) {
	
		var errors = "Please correct the following before continuing:      \n\n";
	
		for (var i=0; i < errMsgs.length; i++) {
			errors += "--  " + errMsgs[i] + "     \n";
		}
		
		errors += "\nThe fields with errors are highlighted in red";
		
		alert(errors);
		return false;
	
	}
	
	else {
	
		return true;
		
	}

}