/*
	Todd Markelz - ttm22@cornell.edu
	October 12th, 2001
	Einaudi Center
	
	This file was created to establish an organized validation scheme.
	This file can be included to any page that needs to verify information coming from an HTML 
	form and into the database. Include with the following line:
	
	<script language="javascript" src="http(s)://www.einaudi.cornell.edu/common_files/scripts/formValidation.js"></script>
	
	The (s) is required if you are including the script into a page that is running on a SSL

	All of the general checks like date, time, and email have their own functions which can
	be used to return a false or true of the field passes. Simple code must be added to the
	page with the form that requires validation. See examples for details.

*/


function checkTime(theField) {

	// All possible time formats that we will accept are defined below, check against each
	
	time_pattern = /^[1-9]\:[0-5][0-9]\s*(\AM|PM|am|pm?)\s*$/;
	time_pattern2 = /^[1-1][0-2]\:[0-5][0-9]\s*(\AM|PM|am|pm?)\s*$/;		
	time_pattern3 = /^[1-1][0-2]\:[0-5][0-9]\:[0-5][0-9]\s*(\AM|PM|am|pm?)\s*$/;
	time_pattern4 = /^[1-9]\:[0-5][0-9]\:[0-5][0-9]\s*(\AM|PM|am|pm?)\s*$/;	

	var theValue = theField.value;

	if (theValue != "") {
 
		if (!time_pattern.test(theValue) && !time_pattern2.test(theValue) &&
		    !time_pattern3.test(theValue) && !time_pattern4.test(theValue))
		{

		alert("Please enter a valid TIME: (HH:MM AM or PM) or leave BLANK unless required.");
		theField.focus();
		return false;
	
		}

		else { 
			return true;
		}
	}
	else {
		return true;
	}
}

function checkContent(theField) {

	//Check field for anything, as long as its not empty.
	
	var theValue = theField.value;
	
	if (theValue == "") {
		 
		alert("Please fill in ALL required fields.");
		theField.focus();
		return false;
	}

	else {
		return true;
	}
}

function checkFile(theField) {

	
	file_pattern = /^\w(.+\..+)$/;

	var theValue = theField.value;

	if (theValue != "") {
 
		if (!file_pattern.test(theValue))
		{

		alert("Please enter a valid filename: (<filename>.<ext>) or leave BLANK unless required.");
		theField.focus();
		return false;
	
		}

		else { 
			return true;
		}
	}
	else {
		return true;
	}
}


function checkEmail(theField) {

	//Check to make sure a valid email format was entered.

	var theValue = theField.value;

	email_pattern = /^.+\@.+\..+$/;

	if (theValue != "") {

		if (!email_pattern.test(theValue)) {
			alert("Please enter a valid EMAIL address or leave BLANK unless required.");
			theField.focus();
			return false;
		}

		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function checkDate(theField) {

	// Confirming a date is tricky because users could enter an correct format, but incorrect date
	// such as 02/31/02. Obviously there are not 31 days in February. This would clear a format check, but if the
	// database tried to CDate on it, it would throw an error. Because of this, we have to break the date apart by the
	// "/" and validate that the month and day are possible. This is done in the isDateValid function.

	date_pattern = /^(\d{1}|\d{2})\/(\d{1}|\d{2})\/(\d{2}|\d{4})\s*$/;

	var theValue = theField.value;

	if (theValue != "") {

		// Check the date value for proper format, then validity 
		if (!date_pattern.test(theValue))
		{
		
			alert("Please enter a valid DATE: (MM/DD/YYYY) or leave BLANK unless required.");
			theField.focus();
			return false;
		}

		else { 
			if (!isDateValid(theValue)) {
				alert("Please enter a valid DATE: (MM/DD/YYYY) or leave BLANK unless required.");
				theField.focus();
				return false;
			}
			else {
				return true;
			}
		}
	}
	else {
		return true;
	}
}


// If it is formatted correctly, check to see if the values are valid

function isDateValid(dateString) {

// First we create an array to hold the day count in each month
var daysInMonth = new Array(13);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
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;

var theDate = dateString.split("/");

// Explicitly change type to integer to make code work in both
// JavaScript 1.1 and JavaScript 1.2.
var intYear = parseInt(theDate[2],10);
var intMonth = parseInt(theDate[0],10);
var intDay = parseInt(theDate[1],10);

if (intMonth < 1 || intMonth > 12 ) return false;
if (intDay > daysInMonth[intMonth]) return false; 
if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

return true;

}

// This function is used by checkDate to see how many days are in february of the given year
// 
// Given integer argument year,
// returns number of days in February of that year.

function daysInFebruary(year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function checkURL(theField) {

	// All possible url formats that we will accept are defined below, check against each
	
	url_pattern = /^(\http:|https:|mms:)\/\/.+\..+\s*$/;

	var theValue = theField.value;

	if (theValue != "") {
 
		if (!url_pattern.test(theValue))
		{

		alert("Please enter a valid URL: (http(s)://<address>.<address>) or leave BLANK unless required.");
		theField.focus();
		return false;
	
		}

		else { 
			return true;
		}
	}
	else {
		return true;
	}
}

function checkZip(theField) {

	//Check to make sure a valid zipcode format was entered.

	var theValue = theField.value;

	zip_pattern = /^\d{5}(\-?\d{4})?$/;

	if (theValue != "") {

		if (!zip_pattern.test(theValue)) {
			alert("Please enter a valid ZIPCODE (##### or #####-####) or leave BLANK unless required.");
			theField.focus();
			return false;
		}

		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function checkSSN(theField) {

	//Check to make sure a valid zipcode format was entered.

	var theValue = theField.value;

	ssn_pattern = /^\d{3}\-+\d{2}\-+\d{4}$/;

	if (theValue != "") {

		if (!ssn_pattern.test(theValue)) {
			alert("Please enter a valid SSN (###-##-####) or leave BLANK unless required.");
			theField.focus();
			return false;
		}

		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function checkCUID(theField) {

	//Check to make sure a valid zipcode format was entered.

	var theValue = theField.value;

	id_pattern = /^(\d{7}|\d{6})$/;

	if (theValue != "") {

		if (!id_pattern.test(theValue)) {
			alert("Please enter a valid Cornell ID (#######) or leave BLANK unless required.");
			theField.focus();
			return false;
		}

		else {
			return true;
		}
	}
	else {
		return true;
	}
}


function checkAccount(theField) {

	//Check to make sure a valid zipcode format was entered.

	var theValue = theField.value;

	account_pattern = /^(\D{1}\d{2}|\d{3})\-+\d{4}$/;

	if (theValue != "") {

		if (!account_pattern.test(theValue)) {
			alert("Please enter a valid ACCOUNT NUMBER (XXX-XXXX) or leave BLANK unless required.");
			theField.focus();
			return false;
		}

		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function checkNumber(theField) {

	//Check to make sure value enter is a number with no other characters

	var theValue = theField.value;

	number_pattern = /^\d+$/;

	if (theValue != "") {

		if (!number_pattern.test(theValue)) {
			alert("Please enter a valid NUMBER. Only 0-9 characters are allowed, no letters or symbols.");
			theField.focus();
			return false;
		}

		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function checkFile(theField,theExt) {

	// All possible url formats that we will accept are defined below, check against each
	
	if (theExt == "pdf") {
		file_pattern = /^.+\.(\pdf)$/;
	}
	else {
		file_pattern = /^.+\.\D{3}$/;
	}

	var theValue = theField.value;

	if (theValue != "") {
 
		if (!file_pattern.test(theValue))
		{

		alert("Please enter a valid FILE or leave BLANK unless required.");
		theField.focus();
		return false;
	
		}

		else { 
			return true;
		}
	}
	else {
		return true;
	}
}

function checkPass(theField,theField2) {

	var theValue = theField.value;
	var theValue2 = theField2.value;

	if (theValue != theValue2) {
 		alert("Your passwords do not match, please re-enter your password.");
		theField.focus();
		return false;
	}
	else { 
		return true;
	}
}