// **BEGIN GENERIC VALIDATION FUNCTIONS**
// general purpose function to see if an input value has been entered at all
function isEmpty(inputStr) {
	if (inputStr == "" || inputStr == null) {
		return true
	}
	return false
}

// general purpose function to see if a quantity is numeric
function isNumeric(field) {
	if (isEmpty(field)) {
		alert("This field requires a numeric value.")
		select(field)
		return false
	} else 
		var input = parseInt(field.value, 10)
		if (isNaN(input)) {
			alert("This field requires a numeric value.")
			select(field)
			return false
	}
	return true
}

// function to determine if value is in acceptable range for this application
function inRange(inputStr, lo, hi) {
	var num = parseInt(inputStr, 10)
	if (num < lo || num > hi) {
		return false
	}
	return true
}

//function to determine if an email address is in the correct form
function validEmail(email) {
	invalidChars = " /:,;"
	
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false
		}
	}
	atPos = email.indexOf("@",1)
	if (atPos == -1) {
		return false
	}
	if (email.indexOf("@",atPos+1) > -1) {
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {
		return false
	}
	if (periodPos+3 > email.length) {
		return false
	}
	return true
}

//function to validate the zipcode
function validZip(inZip) {	
	for (i=0; i<5; i++) {
		if (inZip.charAt(i) < "0") {
			return false
		}
		if (inZip.charAt(i) > "9") {
			return false
		}
	}
	return true
}

//function to validate phone numbers
function validPhone(inPhone) {
	cnt=0
	for (i=0; i<inPhone.length; i++) {
		if (inPhone.charAt(i) >= "0") {
			if (inPhone.charAt(i) <= "9") {
				cnt++
			}
		}
	}
	if (cnt < 10) {
		return false
	}
	return true
}

// **END GENERIC VALIDATION FUNCTIONS**

function validateMonth(field, bypassUpdate) {
	var input = parseInt(field.value, 10)
	if (isEmpty(input)) {
		alert("Be sure to enter a month value.")
		select(field)
		return false
	} else {
		if (isNaN(input)) {
			alert("Entries must be numbers only.")
			select(field)
			return false
		} else {
			if (!inRange(input,1,12)) {
				alert("Enter a number between 1 (January) and 12 (December).")
				select(field)
				return false
			}
		}
	}
	return true
}

function validateDate(field) {
	var input = parseInt(field.value, 10)
	if (isEmpty(input)) {
		alert("Be sure to enter a date value.")
		select(field)
		return false
	} else {
		if (isNaN(input)) {
			alert("Entries must be numbers only.")
			select(field)
			return false
		} else {
			var monthField = document.forms[0].OEClosingMM
			if (!validateMonth(monthField, true)) return false
			var monthVal = parseInt(monthField.value, 10)
			var monthMax = new Array(31,31,29,31,30,31,30,31,31,30,31,30,31)
			var top = monthMax[monthVal]
			if (!inRange(input,1,top)) {
				alert("Enter a number between 1 and " + top + ".")
				select(field)
				return false
			}
		}
	}
	return true
}

function validateYear(field) {
	var input = parseInt(field.value, 10)
	if (isEmpty(input)) {
		alert("Be sure to enter a month value.")
		select(field)
		return false
	} else {
		if (isNaN(input)) {
			alert("Entries must be numbers only.")
			select(field)
			return false
		} else {
			if (!inRange(input,2001,2020)) {
				alert("Enter a number between 2001 and 2020.")
				select(field)
				return false
			}
		}
	}
	return true
}

function select(field) {
	field.focus()
	field.select()
}


