var defaultEmptyOK = true;

var digitsInZIPCode1 = 5;
var iMonth = "Month must be between 1 and 12.";
var iDay = "Day must be between 1 and 31.";
var iYear = "Year must be 4 digits.";
var INumeric = "This field must be numeric.";
var iZIPCode = "Zip Code must be a 5 digit U.S. ZIP Code.";
var iUSArea = "Area code must be 3 digits.";
var iUSPrefix= "Phone number prefix must be 3 digits.";
var iUSSuffix= "Phone number suffix must be 4 digits.";
var iDatePrefix = "The Day, Month, and Year for ";
var iDateSuffix = " do not form a valid date.";

function makeArray(n)
{
   for (var i = 1; i <= n; i++)
   {
      this[i] = 0;
   } 
   return this;
}

var daysInMonth = makeArray(12);
daysInMonth[1]  = 31;
daysInMonth[2]  = 29;   
daysInMonth[3]  = 31;
daysInMonth[4]  = 30;
daysInMonth[5]  = 31;
daysInMonth[6]  = 30;
daysInMonth[7]  = 31;
daysInMonth[8]  = 31;
daysInMonth[9]  = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

function isEmpty(s)
{
	return ((s == null) || (s.length == 0));
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isIntegerInRange (s, a, b)
{
	if (isEmpty(s)) 
		if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
		else return (isIntegerInRange.arguments[1] == true);
	if (!isInteger(s, false)) return false;

	var num = parseInt (s);
	return ((num >= a) && (num <= b));
}

function isInteger (s)
{
	var i;

	if (isEmpty(s)) 
		if (isInteger.arguments.length == 1) return defaultEmptyOK;
		else return (isInteger.arguments[1] == true);

    for (i = 0; i < s.length; i++) {   
		var c = s.charAt(i);
		if (!isDigit(c)) return false;
	}

    return true;
}

function isNonnegativeInteger (s)
{
	var secondArg = defaultEmptyOK;

	if (isNonnegativeInteger.arguments.length > 1)
		secondArg = isNonnegativeInteger.arguments[1];

	return (isSignedInteger(s, secondArg) && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

function isSignedInteger (s)
{
	if (isEmpty(s)) 
		if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
		else return (isSignedInteger.arguments[1] == true);

	else {
		var startPos = 0;
		var secondArg = defaultEmptyOK;

		if (isSignedInteger.arguments.length > 1)
			secondArg = isSignedInteger.arguments[1];

		if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
			startPos = 1;    
		
		return (isInteger(s.substring(startPos, s.length), secondArg))
	}
}

function daysInFebruary (year)
{
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function checkDate(yearField, monthField, dayField, labelString, OKtoOmitDay)
{
	if (checkDate.arguments.length == 4) OKtoOmitDay = false;
    if (!isYear(yearField.value)) return (iYear);
    if (!isMonth(monthField.value)) return (iMonth);

	if (isEmpty(monthField.value) || isEmpty(yearField.value) || (!OKtoOmitDay && isEmpty(dayField.value))) {
		return (iDatePrefix + labelString + iDateSuffix);
		return false;
    }
    
	if ( (OKtoOmitDay == true) && isEmpty(dayField.value) ) return true;
	else if (!isDay(dayField.value)) 
		return (iDay);
    if (isDate (yearField.value, monthField.value, dayField.value))
		return true;
    
    return (iDatePrefix + labelString + iDateSuffix);

    return false;

}

function isDate (year, month, day)
{ 
    if (!(isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

function isDay (s)
{   
	if (isEmpty(s)) 
		if (isDay.arguments.length == 1) return defaultEmptyOK;
		else return (isDay.arguments[1] == true);
       
	if (s.charAt(0) == '0') { s = s.charAt(1) }
    
    return isIntegerInRange (s, 1, 31);
}

function isMonth (s)
{   
	if (isEmpty(s)) 
		if (isMonth.arguments.length == 1) return defaultEmptyOK;
		else return (isMonth.arguments[1] == true);
       
	if (s.charAt(0) == '0') { s = s.charAt(1) }
	return isIntegerInRange (s, 1, 12);
}

function isYear (s)
{
	if (isEmpty(s)) 
		if (isYear.arguments.length == 1) return defaultEmptyOK;
		else return (isYear.arguments[1] == true);

	if (!isNonnegativeInteger(s)) return false;
	if (s<1753) return false;

    return (s.length == 4);
}

function checkNumber (theField, emptyOK)
//if emptyOK passed in is true, it means it's OK for the field to be empty
{
	//if this function is called with only one parameter, assume it's OK to be empty
	if (checkNumber.arguments.length == 1) {
		emptyOK = defaultEmptyOK;
	}

	//so if it's OK to be empty, and the field is empty, then checkNumber passed
	if ((emptyOK == true) && (isEmpty(theField.value))) {
		return true;
	}
	//if we made it here, it's not OK to be empty, or the value is not empty
	else
	{
		//if it's not an integer (including if it is empty), then checkNumber fails
		if (!isInteger(theField.value)) {
			//INumeric is a warning message set above
			return (INumeric);
		}
		else {
			return true;
		}
	}
}

function checkZIPCode1 (theField, emptyOK)
{
	if (checkZIPCode1.arguments.length == 1) emptyOK = defaultEmptyOK;

	if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else { 
		if (!isZIPCode1(theField.value, false)) 
			return (iZIPCode);
		else {  
			return true;
		}
	}
}

function isZIPCode1 (s)
{
	if (isEmpty(s)) 
		if (isZIPCode1.arguments.length == 1) return defaultEmptyOK;
		else return (isZIPCode1.arguments[1] == true);

	return (isInteger(s) && (s.length == digitsInZIPCode1));
}

function checkUSPhoneArea (theField, emptyOK)
{
	if (checkUSPhoneArea.arguments.length == 1) emptyOK = defaultEmptyOK;

	if ((emptyOK == true) && (isEmpty(theField.value))) return true;
	else {
		if ((!(theField.value.length == 3)) || (!(isInteger(theField.value))))
			return (iUSArea);
		else 
			return true;
    }
}

function checkUSPhonePrefix  (theField, emptyOK)
{
	if (checkUSPhonePrefix.arguments.length == 1) emptyOK = defaultEmptyOK;

	if ((emptyOK == true) && (isEmpty(theField.value))) return true;
	else {
		if ((!(theField.value.length == 3))||(!(isInteger(theField.value)))) 
			return (iUSPrefix);
		else 
			return true;
    }
}

function checkUSPhoneSuffix (theField, emptyOK)
{
	if (checkUSPhoneSuffix.arguments.length == 1) emptyOK = defaultEmptyOK;

	if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else {
		if (!(theField.value.length == 4)||!(isInteger (theField.value))) 
			return (iUSSuffix);
		else 
			return true;
    }
}
