/* ============================================================= */

//Purpose: Check if an array is a valid date.
//Input: rarrDate: array of Month, Day, Year
//Output: false if not a valid date, true if it is a valid date

function checkDate(rarrDate) {
/* DOES IF HAVE EXACTLY 3 FIELDS? */
	var datToday = new Date();

	if (rarrDate.length != 3) {
		if ((rarrDate[0] >= 0) && (rarrDate[0] <= datToday.getMonth())) {
			rarrDate[2] = datToday.getYear()+1;
			if (rarrDate[2] < 1000) rarrDate[2] += 1900;
		} else {
			rarrDate[2] = datToday.getYear();
			if (rarrDate[2] < 1000) rarrDate[2] += 1900;
		}
//		return false;
	}

/* ARE ALL 3 FIELDS INTEGERS? */
	if (!((isInteger(rarrDate[0])) && (isInteger(rarrDate[1])) && (isInteger(rarrDate[2])))) {
		return false;
	}

/* IS THE MONTH OR DAY A 0? */
	if ((rarrDate[0] == 0) || (rarrDate[1] == 0)) {
		return false;
	}

/* CHANGE THE YEAR TO 4 DIGITS */
	if (rarrDate[2].length == 1) {
		rarrDate[2] = "200" + rarrDate[2];
	} else if (rarrDate[2].length == 2) {
		if (rarrDate[2] > 50) {
			rarrDate[2] = "19" + rarrDate[2];
		} else {
			rarrDate[2] = "20" + rarrDate[2];
		}
	} else if ((rarrDate[2].length == 3) || (rarrDate[2].length > 4)) {
		return false;
	}

/* DISPLAY THE DATE */
//	alert("Month: " + rarrDate[0] + "\nDay: " + rarrDate[1] + "\nYear: " + rarrDate[2]);

/* CHECK FOR VALID INTEGERS IN EACH FIELD */
	if (rarrDate[0] == 2) {
		if (rarrDate[2] % 4 == 0) {
			if (rarrDate[2] % 100 == 0) {
				if (rarrDate[2] % 400 == 0) {
					intDaysInMonth = 29;
				} else {
					intDaysInMonth = 28;
				}
			} else {
				intDaysInMonth = 29;
			}
		} else {
			intDaysInMonth = 28;
		}
		if (rarrDate[1] > intDaysInMonth) {
			return false;
		}
	} else if ((rarrDate[0] == 1) || (rarrDate[0] == 3) || (rarrDate[0] == 5) || (rarrDate[0] == 7) ||
		(rarrDate[0] == 8) || (rarrDate[0] == 10) || (rarrDate[0] == 12)) {
		if (rarrDate[1] > 31) {
			return false;
		}
	} else if ((rarrDate[0] == 4) || (rarrDate[0] == 6) || (rarrDate[0] == 9) || (rarrDate[0] == 11)) {
		if (rarrDate[1] > 30) {
			return false;
		}
	} else {
		return false;
	}

	return true;
}

/* ============================================================= */

//Purpose: Check if a string is a valid date. Separators allowed are '/' and '-'
//Input: rstrDate: Any string
//Output: false if not a valid date, true if it is a valid date

function isValidDate(robjTest) {
	var blnIsDate = true;
	var arrMDY = new Array();
	var intDaysInMonth;

/* SPLIT ALONG THE DELIMITERS */
	if (robjTest.value.indexOf("/") != -1) {
		arrMDY = robjTest.value.split("/");
	} else if (robjTest.value.indexOf("-") != -1) {
		arrMDY = robjTest.value.split("-");
	} else if (robjTest.value.indexOf(" ") != -1) {
		arrMDY = robjTest.value.split(" ");
	} else if (robjTest.value.indexOf(".") != -1) {
		arrMDY = robjTest.value.split(".");
	} else {
		if ((robjTest.value.length == 6) || (robjTest.value.length == 8)) {
			arrMDY[0] = robjTest.value.substring(0,2);
			arrMDY[1] = robjTest.value.substring(2,4);
			arrMDY[2] = robjTest.value.substring(4,robjTest.value.length);
		} else {
			blnIsDate = false;
			alert("Please enter a valid date (M/D/YY).");
			robjTest.focus();
			return blnIsDate;
		}
	}

/* DOES IF HAVE EXACTLY 3 FIELDS? */
	if (arrMDY.length != 3) {
		if (arrMDY.length == 2) {
			var d = new Date();
			var strTempYear = d.getYear();
			if (strTempYear < 1000) strTempYear += 1900;
			arrMDY[2] = strTempYear;
		} else {
			blnIsDate = false;
			alert("Please enter a valid date (M/D/YY).");
			robjTest.focus();
			return blnIsDate;
		}
	}

/* ARE ALL 3 FIELDS INTEGERS? */
	if (!((isInteger(arrMDY[0])) && (isInteger(arrMDY[1])) && (isInteger(arrMDY[2])))) {
		blnIsDate = false;
	}

/* IS THE MONTH OR DAY A 0? */
	if ((arrMDY[0] == 0) || (arrMDY[1] == 0)) {
		blnIsDate = false;
	}

/* CHANGE THE YEAR TO 4 DIGITS */
	if (arrMDY[2].length == 1) {
		arrMDY[2] = "200" + arrMDY[2];
	} else if (arrMDY[2].length == 2) {
		if (arrMDY[2] > 50) {
			arrMDY[2] = "19" + arrMDY[2];
		} else {
			arrMDY[2] = "20" + arrMDY[2];
		}
	} else if ((arrMDY[2].length == 3) || (arrMDY[2].length >4)) {
		blnIsDate = false;
	}

/* DISPLAY THE DATE */
//	alert("Month: " + arrMDY[0] + "\nDay: " + arrMDY[1] + "\nYear: " + arrMDY[2]);

/* RETURN THE DATE */
	robjTest.value = arrMDY[0] + "/" + arrMDY[1] + "/" + arrMDY[2];

/* CHECK FOR VALID INTEGERS IN EACH FIELD */
	if (arrMDY[0] == 2) {
		if (arrMDY[2] % 4 == 0) {
			if (arrMDY[2] % 100 == 0) {
				if (arrMDY[2] % 400 == 0) {
					intDaysInMonth = 29;
				} else {
					intDaysInMonth = 28;
				}
			} else {
				intDaysInMonth = 29;
			}
		} else {
			intDaysInMonth = 28;
		}
		if (arrMDY[1] > intDaysInMonth) {
			blnIsDate = false;
		}
	} else if ((arrMDY[0] == 1) || (arrMDY[0] == 3) || (arrMDY[0] == 5) || (arrMDY[0] == 7) ||
		(arrMDY[0] == 8) || (arrMDY[0] == 10) || (arrMDY[0] == 12)) {
		if (arrMDY[1] > 31) {
			blnIsDate = false;
		}
	} else if ((arrMDY[0] == 4) || (arrMDY[0] == 6) || (arrMDY[0] == 9) || (arrMDY[0] == 11)) {
		if (arrMDY[1] > 30) {
			blnIsDate = false;
		}
	} else {
		blnIsDate = false;
	}

	if (!(blnIsDate)) {
		alert("Please enter a valid date (M/D/YY).");
		robjTest.focus();
	}

	robjTest.value = arrMDY[0] + "/" + arrMDY[1] + "/" + arrMDY[2];
	return blnIsDate;
}

/* ============================================================= */

//Purpose: Check if a string is a valid date/time. Separators allowed are '/' and '-'
//Input: robjTest: Any string
//Output: false if not a valid date/time, true if it is a valid date/time

function isValidDateTime(robjTest) {
	var blnIsDateTime = true;
	var blnHasDate = false;
	var blnHasTime = false;
	var arrMDY = new Array();
	var intDaysInMonth;
	var strTime, strDate, strTempValue;

	// check if it looks like a date
	if (robjTest.value.indexOf("/") != -1) {
		blnHasDate = true;
	}

	// check if it looks like a time
	if (robjTest.value.indexOf(":") != -1) {
		blnHasTime = true;
	}

	if (blnHasDate && blnHasTime) {
		if (robjTest.value.indexOf("/") < robjTest.value.indexOf(":")) {
			strDate = robjTest.value.substring(0,robjTest.value.indexOf(" "));
			strTime = robjTest.value.substring(robjTest.value.indexOf(" "),robjTest.value.length);
		} else {
			strTime = robjTest.value.substring(0,robjTest.value.indexOf(" "));
			strDate = robjTest.value.substring(robjTest.value.indexOf(" "),robjTest.value.length);
		}
	} else if (blnHasDate) {
		strDate = robjTest.value;
		strTime = '';
	} else if (blnHasTime) {
		strDate = '';
		strTime = robjTest.value;
	} else {
		if (isInteger(robjTest.value)) {
			strDate = '';
			strTime = robjTest.value + ":00";
		} else {
			alert("Please enter a valid date/time.");
			robjTest.focus();
			return false;
		}
	}

	if (strDate != '') {
		strTempValue = '';
		for (var i = 0; i < strDate.length; i++) {
			if ((strDate.charAt(i) != ' ') && (strDate.charAt(i) != '	')) {
				strTempValue += strDate.toLowerCase().charAt(i);
			}
		}

		var arrMDY = new Array();
		arrMDY = strTempValue.split("/");

		if (!checkDate(arrMDY)) {
			alert("Please enter a valid date (M/D/YY).");
			robjTest.focus();
			return false;
		}

		strDate = arrMDY[0] + "/" + arrMDY[1] + "/" + arrMDY[2];
	} // END DATE STUFF

	if (strTime != '') {
		strTempValue = '';
		for (var i = 0; i < strTime.length; i++) {
			if ((strTime.charAt(i) != ' ') && (strTime.charAt(i) != '	')) {
				strTempValue += strTime.toLowerCase().charAt(i);
			}
		}

		var blnIsTime = true;
		var arrHMS = new Array();
		var blnIsAfternoon = false;
		var blnIsDefiniteTime = false;

		if (strTempValue.indexOf("pm") != -1) {
			strTempValue = strTempValue.substring(0,strTempValue.indexOf("pm"));
			blnIsAfternoon = true;
			blnIsDefiniteTime = true;
		} else if (strTempValue.indexOf("am") != -1) {
			strTempValue = strTempValue.substring(0,strTempValue.indexOf("am"));
			blnIsAfternoon = false;
			blnIsDefiniteTime = true;
		}

		arrHMS = strTempValue.split(":");

		if (!isInteger(arrHMS[0]) || !isInteger(arrHMS[1])) {
			blnIsTime = false;
		}

		if (arrHMS[0] == 0) {
			arrHMS[0] = 12;
		} else if (arrHMS[0] > 12) {
			blnIsAfternoon = true;
			blnIsDefiniteTime = true;
			arrHMS[0] = arrHMS[0] - 12;
		}

		if ((arrHMS[0] > 12) || (arrHMS[0] < 1)) {
			blnIsTime = false;
		}

		if ((arrHMS[1] > 59) || (arrHMS[1] < 0)) {
			blnIsTime = false;
		}

		// smart-time modification
		if (!blnIsDefiniteTime) {
			if (arrHMS[0] < 6) {
				blnIsAfternoon = true;
			} else if (arrHMS[0] == 12) {
				blnIsAfternoon = true;
			} else if (arrHMS[0] > 5) {
				blnIsAfternoon = false;
			}
		}

		if (!blnIsTime) {
			alert("Please enter a valid time.");
			robjTest.focus();
		} else {
			if (blnIsAfternoon) {
				strTempValue = arrHMS[0] + ":" + arrHMS[1] + " pm";
			} else {
				strTempValue = arrHMS[0] + ":" + arrHMS[1] + " am";
			}
		}

		strTime = strTempValue;
	} // END TIME STUFF


//	alert("Date: '" + strDate + "'\nTime: '" + strTime + "'");

	if (strDate == '') {
		robjTest.value = strTime;
	} else if (strTime == '') {
		robjTest.value = strDate;
	} else {
		robjTest.value = strDate + " " + strTime;
	}
	return blnIsDateTime;
}

/* ============================================================= */

//Purpose: Check if a string is a valid date and time. Separators allowed are '/' and '-'
//Input: robjTest: Any string
//Output: false if not a valid date and time, true if it is a valid date and time

function isValidDateAndTime(robjTest) {
	var blnIsDateTime = true;
	var blnHasDate = false;
	var blnHasTime = false;
	var arrMDY = new Array();
	var intDaysInMonth;
	var strTime, strDate, strTempValue;

	// check if it looks like a date
	if (robjTest.value.indexOf("/") != -1) {
		blnHasDate = true;
	}

	// check if it looks like a time
	if (robjTest.value.indexOf(":") != -1) {
		blnHasTime = true;
	}

	if (blnHasDate && blnHasTime) {
		if (robjTest.value.indexOf("/") < robjTest.value.indexOf(":")) {
			strDate = robjTest.value.substring(0,robjTest.value.indexOf(" "));
			strTime = robjTest.value.substring(robjTest.value.indexOf(" "),robjTest.value.length);
		} else {
			strTime = robjTest.value.substring(0,robjTest.value.indexOf(" "));
			strDate = robjTest.value.substring(robjTest.value.indexOf(" "),robjTest.value.length);
		}
	} else if (blnHasDate) {
		strDate = robjTest.value;
		strTime = '';
	} else if (blnHasTime) {
		strDate = '';
		strTime = robjTest.value;
	} else {
		if (isInteger(robjTest.value)) {
			strDate = '';
			strTime = robjTest.value + ":00";
		} else {
			alert("Please enter a valid date and time.");
			robjTest.focus();
			return false;
		}
	}

	if (strDate != '') {
		strTempValue = '';
		for (var i = 0; i < strDate.length; i++) {
			if ((strDate.charAt(i) != ' ') && (strDate.charAt(i) != '	')) {
				strTempValue += strDate.toLowerCase().charAt(i);
			}
		}

		var arrMDY = new Array();
		arrMDY = strTempValue.split("/");

		if (!checkDate(arrMDY)) {
			alert("Please enter a valid date (M/D/YY).");
			robjTest.focus();
			return false;
		}

		strDate = arrMDY[0] + "/" + arrMDY[1] + "/" + arrMDY[2];
	} // END DATE STUFF

	if (strTime != '') {
		strTempValue = '';
		for (var i = 0; i < strTime.length; i++) {
			if ((strTime.charAt(i) != ' ') && (strTime.charAt(i) != '	')) {
				strTempValue += strTime.toLowerCase().charAt(i);
			}
		}

		var blnIsTime = true;
		var arrHMS = new Array();
		var blnIsAfternoon = false;
		var blnIsDefiniteTime = false;

		if (strTempValue.indexOf("pm") != -1) {
			strTempValue = strTempValue.substring(0,strTempValue.indexOf("pm"));
			blnIsAfternoon = true;
			blnIsDefiniteTime = true;
		} else if (strTempValue.indexOf("am") != -1) {
			strTempValue = strTempValue.substring(0,strTempValue.indexOf("am"));
			blnIsAfternoon = false;
			blnIsDefiniteTime = true;
		}

		arrHMS = strTempValue.split(":");

		if (!isInteger(arrHMS[0]) || !isInteger(arrHMS[1])) {
			blnIsTime = false;
		}

		if (arrHMS[0] == 0) {
			arrHMS[0] = 12;
		} else if (arrHMS[0] > 12) {
			blnIsAfternoon = true;
			blnIsDefiniteTime = true;
			arrHMS[0] = arrHMS[0] - 12;
		}

		if ((arrHMS[0] > 12) || (arrHMS[0] < 1)) {
			blnIsTime = false;
		}

		if ((arrHMS[1] > 59) || (arrHMS[1] < 0)) {
			blnIsTime = false;
		}

		// smart-time modification
		if (!blnIsDefiniteTime) {
			if (arrHMS[0] < 6) {
				blnIsAfternoon = true;
			} else if (arrHMS[0] == 12) {
				blnIsAfternoon = true;
			} else if (arrHMS[0] > 5) {
				blnIsAfternoon = false;
			}
		}

		if (!blnIsTime) {
			alert("Please enter a valid time.");
			robjTest.focus();
		} else {
			if (blnIsAfternoon) {
				strTempValue = arrHMS[0] + ":" + arrHMS[1] + " pm";
			} else {
				strTempValue = arrHMS[0] + ":" + arrHMS[1] + " am";
			}
		}

		strTime = strTempValue;
	} // END TIME STUFF


//	alert("Date: '" + strDate + "'\nTime: '" + strTime + "'");

	if (strDate == '') {
		robjTest.value = strTime;
	} else if (strTime == '') {
		robjTest.value = strDate;
	} else {
		robjTest.value = strDate + " " + strTime;
	}
	if ((!blnHasDate) && (!blnHasTime)) {
		//alert("Please enter a valid date and time.");
		robjTest.focus();
		return false;
	} else if (!blnHasDate) {
		alert("Please enter a valid date and time.");
		robjTest.focus();
		return false;
	} else if (!blnHasTime) {
		alert("Please enter a valid date and time.");
		robjTest.focus();
		return false;
	}
	return blnIsDateTime;
}

/* ============================================================= */

//Purpose: Check if a string is a valid time. Characters allowed are ':', 'am', and 'pm'
//Input: robjTest: Any string
//Output: false if not a valid time, true if it is a valid time

function isValidTime(robjTest) {
	var blnIsTime = true;
	var arrHMS = new Array();
	var blnIsAfternoon = false;
	var blnIsDefiniteTime = false;
	var strTempValue = '';

	// get rid of whitespace
	for (var i = 0; i < robjTest.value.length; i++) {
		if ((robjTest.value.charAt(i) != ' ') && (robjTest.value.charAt(i) != '	')) {
			strTempValue += robjTest.value.toLowerCase().charAt(i);
		}
	}

	// check if it has 'am' or 'pm'
	if (strTempValue.indexOf("pm") != -1) {
//		alert("It has a PM");
		strTempValue = strTempValue.substring(0,strTempValue.indexOf("pm"));
		blnIsAfternoon = true;
		blnIsDefiniteTime = true;
	} else if (strTempValue.indexOf("am") != -1) {
//		alert("It has an AM");
		strTempValue = strTempValue.substring(0,strTempValue.indexOf("am"));
		blnIsAfternoon = false;
		blnIsDefiniteTime = true;
	}

	// check if it looks like a time
	if (strTempValue.indexOf(":") == -1) {
		arrHMS[0] = strTempValue;
		arrHMS[1] = '00';
	} else {
		arrHMS = strTempValue.split(":");
	}

	if (!isInteger(arrHMS[0]) || !isInteger(arrHMS[1])) {
		blnIsTime = false;
	}

	if (arrHMS[0] == 0) {
		arrHMS[0] = 12;
	} else if (arrHMS[0] > 12) {
		blnIsAfternoon = true;
		blnIsDefiniteTime = true;
		arrHMS[0] = arrHMS[0] - 12;
	}

	if ((arrHMS[0] > 12) || (arrHMS[0] < 1)) {
		blnIsTime = false;
	}

	if ((arrHMS[1] > 59) || (arrHMS[1] < 0)) {
		blnIsTime = false;
	}

	// smart-time modification
	if (!blnIsDefiniteTime) {
		if (arrHMS[0] < 6) {
			blnIsAfternoon = true;
		} else if (arrHMS[0] == 12) {
			blnIsAfternoon = true;
		} else if (arrHMS[0] > 5) {
			blnIsAfternoon = false;
		}
	}

	if (!blnIsTime) {
		alert("Please enter a valid time.");
		robjTest.focus();
	} else {
		if (blnIsAfternoon) {
			robjTest.value = arrHMS[0] + ":" + arrHMS[1]; //+ " pm";
		} else {
			robjTest.value = arrHMS[0] + ":" + arrHMS[1];// + " am";
		}
	}
	return blnIsTime;
}

/* ============================================================= */
