function isFormValid(frm) {

// STRING VARIABLES:
// Set the string variables for displaying the fields.

strDate = ""
strReqr = ""
strPrct = ""
strTime = ""
strNumeric = ""
strPhone = ""
strSSN = ""
strZip = ""
strCurr = ""

cntDate = 0
cntReqr = 0
cntPrct = 0
cntTime = 0
cntNumeric = 0
cntPhone = 0
cntSSN = 0
cntZip = 0
cntCurr = 0

// DATA COMPARISON
// Compare the strings to the required input, and update the alert string as necessary.

for (var i = 0; i < frm.length; i++) {


	// Date Validation.

	found = frm.elements[i].className.indexOf("date")

	if (found !== -1) {
		if (isFormValid_Date(frm.elements[i]) == false) {
			if (cntDate == 0) {
				strDate = "\nPlease enter proper date formats for: \n"
			}
			strDate = strDate + " - " + frm.elements[i].title + "\n"
			cntDate = eval(cntDate) + 1
		}
	}


	// Time Validation.

	found = frm.elements[i].className.indexOf("time")

	if (found !== -1) {
		if (isFormValid_Time(frm.elements[i].value,1) == false) {
			if (cntTime == 0) {
				strTime = "\nPlease enter proper time formats for: \n"
			}
			strTime = strTime + " - " + frm.elements[i].title + "\n"
			cntTime = eval(cntTime) + 1
		}
	}
	
	
	// Required Field Validation.
	
	found = frm.elements[i].className.indexOf("reqr")

	if (found !== -1) {
		if (frm.elements[i].value == "") {
			if (cntReqr == 0) {
				strReqr = "\nPlease enter required values for: \n"
			}
			strReqr = strReqr + " - " + frm.elements[i].title + "\n"
			cntReqr = eval(cntReqr) + 1
		}
	}
	
	
	// Numeric Validation
	
	found = frm.elements[i].className.indexOf("int")

	if (found !== -1) {
		if (isFormValid_Numeric(frm.elements[i]) == false) {
			if (cntNumeric == 0) {
				strNumeric = "\nPlease enter a numeric value only for: \n"
			}
			strNumeric = strNumeric + " - " + frm.elements[i].title + "\n"
			cntNumeric = eval(cntNumeric) + 1
		}
	}
	
	
	// Percentage Validation.
	
	found = frm.elements[i].className.indexOf("prct")

	if (found !== -1) {
		if (isFormValid_Numeric(frm.elements[i]) == false) {
			if (cntPrct == 0) {
				strPrct = "\nPlease enter proper percentage values for: \n"
			}
			strPrct = strPrct + " - " + frm.elements[i].title + "\n"
			cntPrct = eval(cntPrct) + 1
		} else {
			frm.elements[i].value = isFormValid_Percent(frm.elements[i].value)
		}
	}
	
	
	// Currency Formatting. (Automatically sets all non-numeric values to $0)
	
	found = frm.elements[i].className.indexOf("curr")

	if (found !== -1) {
		if (isFormValid_Currency(frm.elements[i].value,frm.elements[i].value,1) == false) {
			if (cntCurr == 0) {
				strCurr = "\nPlease enter proper currency values for: \n"
			}
			strCurr = strCurr + " - " + frm.elements[i].title + "\n"
			cntCurr = eval(cntCurr) + 1			
		} else {	
			if (frm.elements[i].value != "") {
				frm.elements[i].value = isFormValid_Currency(frm.elements[i].value,frm.elements[i].value,1)
			}
		}
	}	
	
	
	// Phone Number Validation.
	
	found = frm.elements[i].className.indexOf("phone")

	if (found !== -1) {
		if (isFormValid_Phone(frm.elements[i],1) == false) {
			if (cntPhone == 0) {
				strPhone = "\nPlease enter a proper phone number format for: \n"
			}
			strPhone = strPhone + " - " + frm.elements[i].title + "\n"
			cntPhone = eval(cntPhone) + 1
		}
	}
	
	
	// Social Security Validation.
	
	found = frm.elements[i].className.indexOf("ssn")

	if (found !== -1) {
		if (isFormValid_SSN(frm.elements[i].value,frm.elements[i],1) == false) {
			if (cntSSN == 0) {
				strSSN = "\nPlease enter proper social security values for: \n"
			}
			strSSN = strSSN + " - " + frm.elements[i].title + "\n"
			cntSSN = eval(cntSSN) + 1
		}
	}
	
	
	// Zip Code Validation.
	
	found = frm.elements[i].className.indexOf("zip")

	if (found !== -1) {
		if (isFormValid_ZipCode(frm.elements[i].value,1) == false) {
			if (cntZip == 0) {
				strZip = "\nPlease enter proper zipcode format for: \n"
			}
			strZip = strZip + " - " + frm.elements[i].title + "\n"
			cntZip = eval(cntZip) + 1
		}
	}
	
} 

// DISPLAY ALERT:
// Display the alert to the user only if the strings for alerting are not blank.

if (strReqr !== "" || strDate !== "" || strTime !== "" || strPrct !== "" || strPhone !== "" || strNumeric !== "" || strSSN !== "" || strZip !== "" || strCurr !== "") {
	alert(strReqr + strDate + strPrct + strTime + strNumeric + strPhone + strSSN + strZip + strCurr)
	return false;
} else {
	return true;
}

}



function isFieldValid(fld) {

// STRING VARIABLES:
// Set the string variables for displaying the fields.

strDate = ""
strReqr = ""
strPrct = ""
strTime = ""
strNumeric = ""
strPhone = ""
strSSN = ""
strZip = ""
strCurr = ""

cntDate = 0
cntReqr = 0
cntPrct = 0
cntTime = 0
cntNumeric = 0
cntPhone = 0
cntSSN = 0
cntZip = 0
cntCurr = 0

// DATA COMPARISON
// Compare the strings to the required input, and update the alert string as necessary.


	// Date Validation.

	found = fld.className.indexOf("date")

	if (found !== -1) {
		if (isFormValid_Date(fld) == false) {
			if (cntDate == 0) {
				strDate = "\nPlease enter proper date formats for: \n"
			}
			strDate = strDate + " - " + fld.title + "\n"
			cntDate = eval(cntDate) + 1
		}
	}


	// Time Validation.

	found = fld.className.indexOf("time")

	if (found !== -1) {
		if (isFormValid_Time(fld.value,1) == false) {
			if (cntTime == 0) {
				strTime = "\nPlease enter proper time formats for: \n"
			}
			strTime = strTime + " - " + fld.title + "\n"
			cntTime = eval(cntTime) + 1
		}
	}
	
	
	// Required Field Validation.
	
	found = fld.className.indexOf("reqr")

	if (found !== -1) {
		if (fld.value == "") {
			if (cntReqr == 0) {
				strReqr = "\nPlease enter required values for: \n"
			}
			strReqr = strReqr + " - " + fld.title + "\n"
			cntReqr = eval(cntReqr) + 1
		}
	}
	
	
	// Numeric Validation
	
	found = fld.className.indexOf("int")

	if (found !== -1) {
		if (isFormValid_Numeric(fld) == false) {
			if (cntNumeric == 0) {
				strNumeric = "\nPlease enter a numeric value only for: \n"
			}
			strNumeric = strNumeric + " - " + fld.title + "\n"
			cntNumeric = eval(cntNumeric) + 1
		}
	}
	
	
	// Percentage Validation.
	
	found = fld.className.indexOf("prct")

	if (found !== -1) {
		if (isFormValid_Numeric(fld) == false) {
			if (cntPrct == 0) {
				strPrct = "\nPlease enter proper percentage values for: \n"
			}
			strPrct = strPrct + " - " + fld.title + "\n"
			cntPrct = eval(cntPrct) + 1
		} else {
			fld.value = isFormValid_Percent(fld.value)
		}
	}
	
	
	// Currency Formatting. (Automatically sets all non-numeric values to $0)
	
	found = fld.className.indexOf("curr")

	if (found !== -1) {
		if (isFormValid_Currency(fld.value,fld.value,1) == false) {
			if (cntCurr == 0) {
				strCurr = "\nPlease enter proper currency values for: \n"
			}
			strCurr = strCurr + " - " + fld.title + "\n"
			cntCurr = eval(cntCurr) + 1			
		} else {	
			if (fld.value != "") {
				fld.value = isFormValid_Currency(fld.value,fld.value,1)
			}
		}
	}	
	
	
	// Phone Number Validation.
	
	found = fld.className.indexOf("phone")

	if (found !== -1) {
		if (isFormValid_Phone(fld,1) == false) {
			if (cntPhone == 0) {
				strPhone = "\nPlease enter a proper phone number format for: \n"
			}
			strPhone = strPhone + " - " + fld.title + "\n"
			cntPhone = eval(cntPhone) + 1
		}
	}
	
	
	// Social Security Validation.
	
	found = fld.className.indexOf("ssn")

	if (found !== -1) {
		if (isFormValid_SSN(fld.value,fld,1) == false) {
			if (cntSSN == 0) {
				strSSN = "\nPlease enter proper social security values for: \n"
			}
			strSSN = strSSN + " - " + fld.title + "\n"
			cntSSN = eval(cntSSN) + 1
		}
	}
	
	
	// Zip Code Validation.
	
	found = fld.className.indexOf("zip")

	if (found !== -1) {
		if (isFormValid_ZipCode(fld.value,1) == false) {
			if (cntZip == 0) {
				strZip = "\nPlease enter proper zipcode format for: \n"
			}
			strZip = strZip + " - " + fld.title + "\n"
			cntZip = eval(cntZip) + 1
		}
	}
	

/*
// DISPLAY ALERT:
// Display the alert to the user only if the strings for alerting are not blank.

if (strReqr !== "" || strDate !== "" || strTime !== "" || strPrct !== "" || strPhone !== "" || strNumeric !== "" || strSSN !== "" || strZip !== "" || strCurr !== "") {
	alert(strReqr + strDate + strPrct + strTime + strNumeric + strPhone + strSSN + strZip + strCurr)
	return false;
} else {
	return true;
}
*/

}

















// DATE: Validate a date value.
function isFormValid_Date(field,fromW) {
var datefield = field;
if (isFormValid_Date_ChkDate(field,fromW) == false) {
  if (fromW == "1") {
    return false;
  } else {
    alert("Invalid date.")
    //field.focus();
    //field.select();
    return false;
  }
} else {
  if (isFormValid_Date_ChkDate(field,fromW) == "Error") {
    alert("You must enter a valid date to continue. The date cannot be greater than 30 days prior to the current date.");
    return false;
  } else {
    return true;
  }
}   
}


function isFormValid_Date_ChkDate(field,fromW) {
var strDatestyle = "US"; //United States date style
//var strDatestyle = "EU";  //European date style
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var datefield = field;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);
strMonthArray[0] = "January";
strMonthArray[1] = "February";
strMonthArray[2] = "March";
strMonthArray[3] = "April";
strMonthArray[4] = "May";
strMonthArray[5] = "June";
strMonthArray[6] = "July";
strMonthArray[7] = "August";
strMonthArray[8] = "September";
strMonthArray[9] = "October";
strMonthArray[10] = "November";
strMonthArray[11] = "December";
strDate = datefield.value;
if (strDate.length < 1) {
return true;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
strDateArray = strDate.split(strSeparatorArray[intElementNr]);
if (strDateArray.length != 3) {
err = 1;
return false;
}
else {
strDay = strDateArray[0];
strMonth = strDateArray[1];
strYear = strDateArray[2];
}
booFound = true;
   }
}
if (booFound == false) {
if (strDate.length>5) {
strDay = strDate.substr(0, 2);
strMonth = strDate.substr(2, 2);
strYear = strDate.substr(4);
   }
}

if (strYear == null) {
	alert("Invalid date.");
	field.focus();
	return false;
} else {
if (strYear.length == 2) {
	if (parseInt(strYear) >= 50) {
		strYear = '19' + strYear;
	} else {
		strYear = '20' + strYear;
	}	
}
}


// US style
if (strDatestyle == "US") {
strTemp = strDay;
strDay = strMonth;
strMonth = strTemp;
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
err = 2;
return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
for (i = 0;i<12;i++) {
if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
intMonth = i+1;
strMonth = strMonthArray[i];
i = 12;
   }
}
if (isNaN(intMonth)) {
err = 3;
return false;
   }
}
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
err = 4;
return false;
}
if (intMonth>12 || intMonth<1) {
err = 5;
return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
err = 6;
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
err = 7;
return false;
}
if (intMonth == 2) {
if (intday < 1) {
err = 8;
return false;
}
if (isFormValid_Date_LeapYear(intYear) == true) {
if (intday > 29) {
err = 9;
return false;
}
}
else {
if (intday > 28) {
err = 10;
return false;
}
}
}

if (fromW == "4") {

	// Verify that the date entered is greater than or equal to
	// today's date minus 25 days. If not, kick out an error.
	ed = new Date(intMonth + "/" + intday + "/" + strYear)    //today's date
	td = new Date()    //Next millennium start date
	diff = ed - td    //difference in milliseconds
	mtg = new String(diff/86400000)    //calculate days and convert to string

	if (parseInt(mtg) < -25) {
		return "Error";
	}
	
	datefield.value = strMonthArray[intMonth-1] + " " + intday + ", " + strYear;

} else if (fromW == "5") {

	// Verify that the date entered is greater than or equal to
	// today's date minus 30 days. If not, kick out an error.
	ed = new Date(intMonth + "/" + intday + "/" + strYear)    //today's date
	td = new Date()    //Next millennium start date
	diff = ed - td    //difference in milliseconds
	mtg = new String(diff/86400000)    //calculate days and convert to string

	if (parseInt(mtg) < -30) {
		return "Error";
	}
	
	datefield.value = strMonthArray[intMonth-1] + " " + intday + ", " + strYear;

} else {

	datefield.value = intMonth + "/" + intday + "/" + strYear;

}

return true;

}


function isFormValid_Date_LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}




// NUMERIC: Validate a numeric value.
function isFormValid_Numeric(field) {
var valid = "1234567890.-()%"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
  return false;
}
}


// ZIPCODE: Validate a zipcode.
function isFormValid_ZipCode(field,fromW) {
if (field == "") {
   return true;
} else {
var valid = "0123456789-";
var hyphencount = 0;

if (field.length!=5 && field.length!=10) {
  if (fromW == "1") { 
  return false;
  } 
  if (fromW == "2") {
  alert("Incorrect zip code format.");
  return false;
  }
}
if (field.length != "") {
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
  if (fromW == "1") { 
  return false;
  } 
  if (fromW == "2") {
  alert("Incorrect zip code format.");
  return false;
  }
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
  if (fromW == "1") { 
  return false;
  } 
  if (fromW == "2") {
  alert("Incorrect zip code format.");
  return false;
  }
}
}
}
}
return true;
}


// SSN: Validate Social Security
function isFormValid_SSN(field,inputbox,fromW) {
if (inputbox.value.length == 9 || inputbox.value.length == 10 || inputbox.value.length == 11 || inputbox.value == "") {

	if (field == "") {	
	} else {		
		field = field.toString().replace(/\-|\./g,'')
		
		if (field.length == 9) {		
			re = /^(\d{3})(\d{2})(\d{4})$/;
			newstring = field.replace(re, "$1-$2-$3")			
			inputbox.value = newstring
		}
	
	}	

} else {
	if (fromW == "1") {
		return false;
	} else {
		alert("Invalid Social Security number.")
		return false;
	}
}

}


// CURRENCY
function isFormValid_Currency(field,inputbox,fromW) {

if (field != "") {
field = field.toString().replace(/\$|\,/g,'');

if(isNaN(field)) {
  if (fromW == "1") {
	return false;
  }
  if (fromW == "2") {
	alert("Invalid currency value.")
	return (field)
  }
} else {

sign = (field == (field = Math.abs(field)));
field = Math.floor(field*100+0.50000000001);
cents = field%100;
field = Math.floor(field/100).toString();
  
if(cents<10)
  cents = "0" + cents;

for (var i = 0; i < Math.floor((field.length-(1+i))/3); i++)
field = field.substring(0,field.length-(4*i+3))+','+
field.substring(field.length-(4*i+3));
return (((sign)?'':'-') + '$' + field + '.' + cents);
}
} else {
  if (fromW == "1") {
	inputbox = ""
	return true;
  }
  if (fromW == "2") {
	return (field)
  }
}
}


// PERCENTAGE
function isFormValid_Percent(field) {
	
offset = field.indexOf("%");

if (offset == -1 && field != "") {
  return (field + "%");
} else {
  return (field);
}
}


// PHONE NUMBER
function isFormValid_Phone(field,fromW) {
if (field.value.length >= 10 || field.value == "") {

	if (field == "") {	
	} else {		
		field.value = field.value.toString().replace(/\-|\./g,'')
		field.value = field.value.replace(" ","")
		field.value = field.value.replace("(","")
		field.value = field.value.replace(")","")
		
		if (field.value.length == 10) {		
			re = /^(\d{3})(\d{3})(\d{4})$/;			
			newstring = field.value.replace(re, "($1) $2-$3")			
			field.value = newstring		
		}
	
	}	

}

if (field.value.length < 10 && field.value !== "") { 
	if (fromW == "1") {
	return false;
	}
	if (fromW == "2") {
	alert("Invalid phone number format.")
	return false;
	}
}

}


// TIME
function isFormValid_Time(field,fromW) {

if (field != "") {
var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

var matchArray = field.match(timePat);
if (matchArray == null) {
  if (fromW == "1") { 
  return false;
  }
  if (fromW == "2") { 
  alert("Invalid time format.");
  return false;
  }
}

hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];
ampm = matchArray[6];

if (second=="") { second = null; }
if (ampm=="") { ampm = null }

if (hour < 0  || hour > 23) {
  if (fromW == "1") { 
  return false;
  }
  if (fromW == "2") { 
  alert("Invalid time format.");
  return false;
  }
}
if (hour <= 12 && ampm == null) {
if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
  if (fromW == "1") { 
  return false;
  }
  if (fromW == "2") { 
  alert("Invalid time format.");
  return false;
  }
   }
}
if  (hour > 12 && ampm != null) {
  if (fromW == "1") { 
  return false;
  }
  if (fromW == "2") { 
  alert("Invalid time format.");
  return false;
  }
}
if (minute<0 || minute > 59) {
  if (fromW == "1") { 
  return false;
  }
  if (fromW == "2") { 
  alert("Invalid time format.");
  return false;
  }
}
if (second != null && (second < 0 || second > 59)) {
  if (fromW == "1") { 
  return false;
  }
  if (fromW == "2") { 
  alert("Invalid time format.");
  return false;
  }
}
}
}
