var vField, i

// main validation function
function validate(field, method) {
		 vField = eval("document.forms[0]."  + field.name)
		 if (method == "isDateFormat") {
			return isDateFormat()	
		 } else {
		   if (method == "isPhoneFormat") {
		 	  return isPhoneFormat()
		   } else {
		   	 if (method == "isEmail") {
			 	return isEmail()
			 }		   
		   }
		 }
		 return false
}

// phone validatation routine
function isPhoneFormat() {
	var str = vField.value
	while (str.indexOf("/") != -1)  {
		str = replaceString(str,"/","-")
	}		 
	if (str.length != 12) {
	   alert("Invalid Phone Number.")
	   return false
	}
	var areac = str.substring(0,3)
	var h1 = str.substring(3,4)
	var exc = str.substring(4,7)
	var h2 = str.substring(7,8)
	var nbr = str.substring(8,12)
	
	if (h1 != "-" || h2 != "-") {
	   alert("Invalid Phone Number Format.")
	   return false
	}
	for (var i = 0; i < areac.length; i++) {
		var theChar = areac.substring(i, i + 1)
		if (theChar < "0" || theChar > "9") {
	   	   alert("Phone Number Must be Numeric.")
	   	   return false		
		}
	}
	for (var i = 0; i < exc.length; i++) {
		var theChar = exc.substring(i, i + 1)
		if (theChar < "0" || theChar > "9") {
	   	   alert("Phone Number Must be Numeric.")
	   	   return false		
		}
	}
	for (var i = 0; i < nbr.length; i++) {
		var theChar = nbr.substring(i, i + 1)
		if (theChar < "0" || theChar > "9") {
	   	   alert("Phone Number Must be Numeric.")
	   	   return false		
		}
	}
	vField.value = str	
	return true
}

function isDateFormat() {
	return isDate(1900,2100)
} 

// functions for string manipulation
function getFrontOfString(str, searchStr)  {
	foundIt = str.indexOf(searchStr)
	if (foundIt == -1) {
		return null
	}
	return str.substring(0,foundIt)
}

function getEndOfString(str, searchStr)  {
	foundIt = str.indexOf(searchStr)
	if (foundIt == -1) {
		return null
	}
	return str.substring(foundIt+searchStr.length,str.length)
}

function replaceString(startStr, searchStr, replaceStr) {
	var front = getFrontOfString(startStr,searchStr)
	var end = getEndOfString(startStr,searchStr)
	if (front != null && end != null) {
		return front + replaceStr + end
	}
	return startStr
}

// function to check Date Format etc.
function isDate(minYear, maxYear, minDays, maxDays) {
		 var inputStr = vField.value
		 // convert hyphen delimiters to slashes
		 while (inputStr.indexOf("-") != -1) {
		 	   inputStr = replaceString(inputStr,"-","/")
		 }
		 
		 var delim1 = inputStr.indexOf("/")
		 var delim2 = inputStr.lastIndexOf("/")
		 if (delim1 != -1 && delim1 == delim2) {
		 	alert("The date is not in an acceptable format.\n\nPlease enter the date using as mm/dd/yyyy")
			vField.focus()
			vField.select()
			return false
		 }
		 
		 if (delim1 != -1) {
		 	// there are delimiters; extract component values
			var mm = parseInt(inputStr.substring(0,delim1),10)
			var dd = parseInt(inputStr.substring(delim1 + 1,delim2),10)
			var yyyy = parseInt(inputStr.substring(delim2 + 1,inputStr.length),10)		
		 } else {
		   //there are no delmiters; extract component values
		   var mm = parseInt(inputStr.substring(0,2),10)
		   var dd = parseInt(inputStr.substring(2,4),10)
		   var yyyy = parseInt(inputStr.substring(4,inputStr.length),10)
		 }
		 
		 if (isNaN(mm) || isNaN(dd) || isNaN(yyyy)) {
		 	//there is a non-numeric character in one of the component values
			alert("The date entry is not an acceptable format.\n\nPlease enter the date using as mm/dd/yyyy")
			vField.focus()
			vField.select()
			return false
		 }
		 
		 if (mm < 1 || mm > 12) {
		 	// month value is not 1 to 12
			alert("Month must be between 01 and 12.")
			vField.focus()
			vField.select()
			return false
		 }
		 
		 if (dd < 1 || dd > 31) {
		 	// day value not between 1 and 31
			alert("Day must be between 01 and 31 (depending on month and year).")
			vField.focus()
			vField.select()
			return false
		 }
		 
		 if (yyyy < 100) {
		 	// entered value is two digits; allow 2000 +
			yyyy += 2000
		 }
		 
		 var today = new Date()
		 
		 if (!minYear) {
		 	//function called with specific day range parameters
			var dateStr = new String(monthDateFormat(mm) + "/" + monthDayFormat(dd) + "/" + yyyy)
			var testDate = new Date(dateStr)
			if (testDate.getTime() < (today.getTime()  + (minDays * 24 * 60 * 60 * 1000))) {
			   alert("The most likely range for this entry begins " + minDays + " days from today.")
			}
			if (testDate.getTime() > (today.getTime() + (maxDays * 24 * 60 * 60 *1000))) {
			   alert("The most likely range for this entry ends " + maxDays + " days from today.")
			}
		 } else if (minYear && maxYear) {
		   //function called with specific year range parameters
		   if (yyyy < minYear || yyyy > maxYear) {
		   	  // entered year is outside of range
			  alert("The most likely range for this entry is between the years " + minYear + " and " + maxYear + ".")	 
		   }
		 } else {
		   //default year range (now set to (this year - 5) and (this year + 25))
		   var thisYear = today.getYear()
		   if (thisYear < 100) {
		   	  thisYear += 2000
		   }
		   maxYear = parseInt(thisYear) + 25
		   minYear = parseInt(thisYear) - 5 
		   if (yyyy < minYear || yyyy > maxYear) {
		   	  alert("It is unusual for a date entry to be before " + minYear + " or after " + maxYear +". Please verfiy the date.") 
		   }
		 }
		 
		 if (!checkMonthLength(mm,dd)) {
		 	vField.focus()
			vField.select()
		 	return false
		 }
		 if (mm == 2) {
		 	if (!checkLeapMonth(mm,dd,yyyy)) {
			   vField.focus()
			   vField.select()
			   return false
			}
		 }
		 
		 //put it all back together in mm/dd/yyyy fromat
		 vField.value = monthDayFormat(mm) + "/" + monthDayFormat(dd) + "/" + yyyy
		 return true
}		 
function checkMonthLength(mm,dd) {
	var months = new Array("","January","February","March","April","May","June","July","August","September","October","November","December")
	if ((mm ==4 || mm == 6 || mm ==9 || mm == 11) && dd > 30) {
		alert(months[mm] + " has only 30 days.")
		return false
	} else if (dd > 31) {
		alert (months[mm] + " has only 31 days.")
		return false
	}
	return true
}
function checkLeapMonth(mm,dd,yyyy) {
	if (yyyy % 4 > 0 && dd > 28)  {
		alert("February of "+ yyyy + " has only 28 days.")
		return false
	} else if (dd > 29) {
		alert("February of " +yyyy+ " has only 29 days.")
		return false
	}
	return true
}
function monthDayFormat(ss) {
	if (parseInt(ss) < 10) {	 	 
		ss = "0"+ss
	}
	return ss
}

function isEmail() {
	var str = vField.value
	if (str.length < 5) {
	   alert("Invalid Email Address.")
	   return false
	}
	if (str.indexOf(" ") != -1) {
	   alert("Invalid Email Address.")
	   return false
	}
	if (str.indexOf("@") == -1) {
	   alert("Invalid Email Address.")
	   return false	   
	}
	if (str.indexOf(".") == -1) {
	   alert("Invalid Email Address.")
	   return false	   
	}	
	if (str.indexOf("@") == 0) {
	   alert("Invalid Email Address.")
	   return false	   
	}	
	if (str.indexOf("@") == (str.length-1)) {
	   alert("Invalid Email Address.")
	   return false	   
	}	
	if (str.lastIndexOf(".") < (str.indexOf("@")+2)) {
	   alert("Invalid Email Address.")
	   return false	
	}
 	return true	
}
