var timeFormatVerifyFailed = false;
var RICOLATimeErrorColor   = "#FFCC66";
var RICOLATimeNormalColor  = "#FFFFFF";
var RICOLAPageContainedTimeError = false;
var RICOLATime_OrigColor = new String("#FFFFFF");


function timeFormatVerifyBlur(inTF) {
	if (timeFormatVerifyFailed) {
		inTF.focus();
	}
	timeFormatVerifyFailed=false;
}

function RICOLATimeFormat_MilitaryAM(inHours) {
	if (inHours == 12) {
		inHours = 0;
	} 
	return inHours;
}

function RICOLATimeFormat_MilitaryPM(inHours) {
	if (inHours < 12) {
		inHours+=12;
	} 
	return inHours;
}

function RICOLATimeHasErrored(inObj) {
	timeFormatVerifyFailed = true;
	RICOLAPageContainedTimeError = true;
	inObj.style.backgroundColor = RICOLATimeErrorColor;
	inObj.focus();
}

/* ***************************************************************************************************** */

function timeFormatVerify() {

	/* If "ASAP" is entered, do not validate */
	if (arguments[0].value.toLowerCase() == "asap") {

		// Force ASAP to uppercase
		arguments[0].value = arguments[0].value.toUpperCase();
		// Make sure the background color is white
		arguments[0].style.backgroundColor = "#fff";
		
		return;
	}

	timeFormatVerifyFailed = false;
	RICOLAPageContainedTimeError = false;
	if (arguments.length < 1) {
		alert("Time Format Verify Error #1024: There were no parameters passed to timeFormatVerify().\nPlease specify at least the \"this\" parameter.\nExample:\n\ttimeFormatVerify(this);\n\nFor more documentation, visit http://home.www.uprr.com/emp/it/hf/ricola/time/");
		return false;
	}
	var inTimeObj=arguments[0];
	if (typeof(inTimeObj) != "object") {
		alert("Time Format Verify Error #1025: The first parameter passed to timeFormatVerify() must be the time field to check.\nPlease specify the \"this\" parameter.\nExample:\n\ttimeFormatVerify(this);\n\nFor more documentation, visit http://home.www.uprr.com/emp/it/hf/ricola/time/");
		return false;
	}
	if (inTimeObj.style.backgroundColor.toString().toLowerCase() != RICOLATimeErrorColor.toLowerCase()) {
		RICOLATime_OrigColor = inTimeObj.style.backgroundColor;
	}
	inTimeObj.style.backgroundColor = RICOLATime_OrigColor;
	var inTimeStr=inTimeObj.value.replace(/\s*/g, "");  // Space-less Time Value Entered
	if (inTimeStr == "") { return true; }		// No Input Entered
	var showAMPM = false;
	var showSeconds = false;
	var useMilitary = true;
	var TimeZoneType = new String("0");
	var currTimeZoneType = new String("");
	var preventTimeZone = new String("");
	var useDaylightSaving = false;
	var minuteIncrement = 1;
	var i=0;
	var currArg=new String("");
	var currHours=0;
	var currMinutes=0;
	var currSeconds=0;
	var currTimeZone=new String("");
	var TimeStrOutput=new String("");
	var ErrorStart = new String(" The time entered (\"" + inTimeObj.value + "\") is not a valid time.\n");
	var FormatExplaination = new String("\n Please input a time that follows the format:\n\t  HH:MM");
	for (i=1; i < arguments.length; i++) {
		currArg=arguments[i];
		if (currArg.search(/^\s*seconds:/i) != -1) {
			if (currArg.search(/^\s*seconds:\s*1\s*$/i) != -1) {
				showSeconds = true;
				FormatExplaination = FormatExplaination + ":SS";
			}
		} else if (currArg.search(/^\s*ampm:/i) != -1) {
			if (currArg.search(/^\s*ampm:\s*1\s*$/i) != -1) {
				showAMPM = true;
				FormatExplaination = FormatExplaination + " (AM|PM)";
			}
		} else if (currArg.search(/^\s*style:/i) != -1) {
			if (currArg.search(/^\s*style:\s*12\s*$/i) != -1) {
				useMilitary = false;
				FormatExplaination = FormatExplaination + "\n\t- 12hr Format";
			} else {
				FormatExplaination = FormatExplaination + "\n\t- 24hr (Military) Format";
			}
		} else if (currArg.search(/^\s*timezone:/i) != -1) {
			currTimeZoneType = currArg.replace(/^\s*timezone:\s*(.*)\s*$/i, "$1").toUpperCase();
			if (currTimeZoneType.search(/prevent/i) != -1) {
				if (currTimeZoneType.search(/^prevent\s*(eastern|central|mountain|pacific)$/i) != -1) {
					preventTimeZone+=currTimeZoneType.replace(/^prevent\s*(eastern|central|mountain|pacific)$/i, "$1").charAt(0).toUpperCase();
					FormatExplaination = FormatExplaination + "\n\t- No " + currTimeZoneType.replace(/^prevent\s*(eastern|central|mountain|pacific)$/i, "$1").toLowerCase() + " Timezone";
				} else {
					alert("Time Format Verify Error #1028: Argument #" + (i + 1) + " (" + arguments[i] + ") denotes to prevent a time zone but it is not Eastern, Central, Mountain, or Pacific.\n\nPlease check the documentation at http://home.www.uprr.com/emp/it/hf/ricola/time/ for more information.");
					return false;					
				}
			} else {
				TimeZoneType = currTimeZoneType;
				if (TimeZoneType == "2") {
					FormatExplaination = FormatExplaination + "\n\t- Timezone: [ ET | CT | MT | PT ]";
				} else if (TimeZoneType == "3") {
					FormatExplaination = FormatExplaination + "\n\t- Timezone: [ EST | EDT | CST | CDT | MST | MDT | PST | PDT ]";
				}
			}
		} else if (currArg.search(/^daylightsavings\:/i) != -1) {
			useDaylightSaving=true;
			var tempDSV=currArg.replace(/\s*$/, "").replace(/^daylightsavings\:\s*/i, "");
			var daylightSavingDateVar=eval(tempDSV);
			if (typeof(daylightSavingDateVar) != "object") {
				alert("Time Format Verify Error #1027: Argument #" + (i + 1) + " (" + arguments[i] + ") denotes to check for Daylight Savings Time but does not reference a valid date field on the page.\n\nPlease check the documentation at http://home.www.uprr.com/emp/it/hf/ricola/time/ for more information.");
				return false;
			}
		} else if (currArg.search(/^\s*increments:\s*(5|10|15|30)\s*$/i) != -1) {
			minuteIncrement = parseInt(currArg.replace(/^\s*increments:\s*(5|10|15|30)\s*$/i, "$1"),10);
			FormatExplaination = FormatExplaination + "\n\t- Minutes can only be in " + minuteIncrement + " minute increments";
		} else if (currArg.search(/notselectable/) != -1) {
//			var test=
		} else {
			alert("Time Format Verify Error #1026: Argument #" + (i + 1) + " (" + arguments[i] + ") is not a valid parameter.\n\nPlease check the documentation at http://home.www.uprr.com/emp/it/hf/ricola/time/ for valid parameters and format.");
			return false;
		}
	}
	// Add ":" if user forgot
	inTimeStr = inTimeStr.replace(/(\d\d)(\d\d)(\d\d)/, "$1:$2:$3");	// HHMMSS Entered to HH:MM:SS format
	inTimeStr = inTimeStr.replace(/(\d)(\d\d)(\d\d)/, "0$1:$2:$3");		// HMMSS Entered to 0H:MM:SS format
	inTimeStr = inTimeStr.replace(/(\d\d)(\d\d)/, "$1:$2");			// HHMM Entered to HH:MM format
	inTimeStr = inTimeStr.replace(/(\d)(\d\d)/, "0$1:$2");			// HMM Entered to 0H:MM format
	inTimeStr = inTimeStr.replace(/;/g, ":"); 				// in case user forgot "shift" key when trying ":"

	// Check to see if at least dd:dd was entered
	if (inTimeStr.search(/\d{1,2}:\d{1,2}/) == -1) {
		alert(ErrorStart + FormatExplaination);
		RICOLATimeHasErrored(inTimeObj);
		return false;
	}
	currHours = parseInt(inTimeStr.replace(/(\d{1,2}):\d{1,2}(:\d{1,2})?/, "$1"), 10);
	currMinutes = parseInt(inTimeStr.replace(/\d{1,2}:(\d{1,2})(:\d{1,2})?/, "$1"), 10);

// Seconds
	// Check to see Seconds Entered
	if (inTimeStr.search(/\d{1,2}:\d{1,2}:\d{1,2}/) != -1) {
		currSeconds = parseInt(inTimeStr.replace(/\d{1,2}:\d{1,2}:(\d{1,2})/, "$1"), 10);
	}

	// Check to see if showing Seconds and if the seconds are valid
	if (showSeconds && (currSeconds >= 60)) {
		alert(ErrorStart + " The seconds must be less than 60." + FormatExplaination);
		RICOLATimeHasErrored(inTimeObj);
		return false;
	}

// Minutes
	if (currMinutes >= 60) {
		alert(ErrorStart + " The minutes must be less than 60." + FormatExplaination);
		RICOLATimeHasErrored(inTimeObj);
		return false;
	}

	// if mandating a minute increment, make sure what is entered by user is adheres
	if ((currMinutes % minuteIncrement) != 0) {
		alert(ErrorStart + " The minutes must be in increments of " + minuteIncrement + "." + FormatExplaination);
		RICOLATimeHasErrored(inTimeObj);
		return false;
	}

// Hours

	var MilitaryHour=0;
	var isEVMS = (window.location.pathname.search(/\/evm\//) != -1)? true : false;
		// Determines if this is the Electronic Van Management System Application (they have special rules)
	var multipleP = (inTimeStr.toUpperCase().indexOf("P") != inTimeStr.toUpperCase().lastIndexOf("P") )? true : false;
	var hasP = (inTimeStr.search(/p/i) != -1)? true : false;
	var hasA = (inTimeStr.search(/a/i) != -1)? true : false;
	var isPM = false;

	if (useMilitary) {			// 24 hour format
		if (currHours > 23) {
			alert(ErrorStart + " The hours must be less than 24." + FormatExplaination);
			RICOLATimeHasErrored(inTimeObj);
			return false;
		}
		if ((multipleP) || (hasP && (TimeZoneType == "0"))) {   	// If user passed "P"	
			currHours = RICOLATimeFormat_MilitaryPM(currHours);	// Add 12 to hours
		} else if (hasA) {						// If has "A" and the hour is "12"
			currHours = RICOLATimeFormat_MilitaryAM(currHours);	// set the hour to "0"
		} else if (hasP && (inTimeStr.search(/(E|C|M)/i) != -1)) {	// Has "P" and a Eastern, Central, or Mountain Timezone too
			currHours = RICOLATimeFormat_MilitaryPM(currHours);	// Add 12 to hours
		}
		MilitaryHour = currHours;
	} else { 				// 12 hour format
		if (hasP && !hasA) {   		// If user passed "P" and has no "A"
			isPM = true;
		} else if (hasP && hasA) {   	// If user passed "P" and "A"
			isPM = false;		// User wants "Pacific" and "AM"
		}
		if (currHours > 23) {
			alert(ErrorStart + " The hours must be less than 13." + FormatExplaination);
			RICOLATimeHasErrored(inTimeObj);
			return false;
		}
		if (currHours > 12) {
			currHours -= 12;
			isPM = true;
		} else if ((currHours == 12) && (!hasA)) {
			isPM = true;
		}
		if (isPM) {
			MilitaryHour = currHours + 12;
		}
	}
	
// Time Zone
	
/*

TimeZone Types (TimeZoneType var)

0:  No TimeZone
1:  Can Have TimeZone
2:  Must Have TimeZone
3:  Must Have 3 letter TimeZone
anychar:  If No Timezone specified, use this

Preventing TimeZones (preventTimeZone var)
E:  Eastern
C:  Central
M:  Mountain
P:  Pacific

*/

var lettersLeft=inTimeStr.replace(/[^(E|C|M|P|ES|ED|CS|CD|MS|MD|PS|PD)]/gi, "");
if (lettersLeft.length > 1) {
	if (lettersLeft.search(/^(ES|ED|CS|CD|MS|MD|PS|PD)$/i) == -1) {
		lettersLeft = lettersLeft.replace(/p/gi, "");
		if (lettersLeft.length == 0) {
			lettersLeft = "P";
		} 
	}
}

if (TimeZoneType != "0") {
	if (TimeZoneType == "2") {	// Must have Time Zone (2 letter) 
		if (useMilitary) {
			if (lettersLeft.search(/(E|C|M|P)/i) != -1) {
				currTimeZone = lettersLeft.replace(/^.*?(E|C|M|P)S?D?/i, "$1T");
			} else {
				alert(ErrorStart + " No Time Zone was Specified." + FormatExplaination);
				RICOLATimeHasErrored(inTimeObj);
				return false;
			}
		} else {			
			if (multipleP) {
				currTimeZone = "PT";
			} else if (hasA) {
				currTimeZone = lettersLeft.replace(/^.*?(E|C|M|P)S?D?/i, "$1T");			
			} else if (hasP && !multipleP) {
				alert(ErrorStart + " No Time Zone was Specified (If you want to show PM and Pacific Time Zone, use the letter 'P' twice)." + FormatExplaination);
				RICOLATimeHasErrored(inTimeObj);
				return false;
			} else if (lettersLeft.search(/(E|C|M)/i) != -1) {
				currTimeZone = lettersLeft.replace(/^.*?(E|C|M)S?D?/i, "$1T");
			} else {
				alert(ErrorStart + " No Time Zone was Specified." + FormatExplaination);
				RICOLATimeHasErrored(inTimeObj);
				return false;
			}
		}
	} else if (TimeZoneType == "3") { 	// Must Have 3-letter Type

		
		if ((lettersLeft.search(/(E|C|M|P)(S|D)/i) == -1) && (!useDaylightSaving)) {
			alert(ErrorStart + " The Time Zone must be 3-letters (ex. CST)." + FormatExplaination);
			RICOLATimeHasErrored(inTimeObj);
			return false;
		} else if (lettersLeft.search(/(E|C|M|P)(S|D)/i) != -1) {
			currTimeZone = lettersLeft.replace(/^.*?(E|C|M|P)(S|D)?/i, "$1$2T");
		} else if ((lettersLeft.search(/(E|C|M|P)/i) != -1) && (useDaylightSaving)) {		// If two letter timezone and daylight savings
			lettersLeft = lettersLeft.replace(/^.*?(E|C|M|P)/i, "$1");
			currTimeZone = lettersLeft.replace(/(E|C|M|P)(S|D)?/i, "$1T");
		} else {
			alert(ErrorStart + " The Time Zone must be 3-letters (ex. MST)." + FormatExplaination);
			RICOLATimeHasErrored(inTimeObj);
			return false;
		}
		currTimeZone = currTimeZone.replace(/(E|C|M|P)(S|D)?T.*$/i, "$1$2T");
	} else {		// If TimeZone is entered, keep it. (1)  or If no TimeZone Entered, use Default
		if (useMilitary) {
			if (lettersLeft.search(/(E|C|M|P)/i) != -1) {
				currTimeZone = lettersLeft.replace(/^.*?(E|C|M|P)S?D?/i, "$1T");
			} 
		} else {
			if (multipleP) {
				currTimeZone = "PT";
			} else if (hasA) {
				currTimeZone = lettersLeft.replace(/^.*?(E|C|M|P)S?D?/i, "$1T");			
			} else if (lettersLeft.search(/(E|C|M)/i) != -1) {
				currTimeZone = lettersLeft.replace(/^.*?(E|C|M)S?D?/i, "$1T");
			} 
		}
		if ((TimeZoneType != "1") && (currTimeZone == "")) {
			currTimeZone = TimeZoneType;
		}
	} 
// Daylight Savings Time Check
	if (useDaylightSaving) {
		currTimeZone=RICOLA_Time_CheckDaylightSavingsTime(inTimeObj, currTimeZone, daylightSavingDateVar, currHours);	
		if (timeFormatVerifyFailed == true) { return false; }
	}
// Preventing TimeZone
	currTimeZone=currTimeZone.toUpperCase();
	if (preventTimeZone != "") {
		if (preventTimeZone.indexOf(currTimeZone.charAt(0)) != -1) {  // if a TimeZone that Should be prevented
			alert(ErrorStart + " The Time Zone \"" + currTimeZone + "\" is not Allowed. " + FormatExplaination);
			RICOLATimeHasErrored(inTimeObj);
			return false;
		}
	}
}


// Formatting the Output of the new time
	var outHours=new String("");
	var outMinutes=new String("");
	var outSeconds=new String("");

	outHours=currHours.toString();
	outMinutes=currMinutes.toString();
	outSeconds=currSeconds.toString();
	if (currHours < 10) { outHours = "0" + currHours; }
	if (currMinutes < 10) { outMinutes = "0" + currMinutes; }
	if (currSeconds < 10) { outSeconds = "0" + currSeconds; }

	TimeStrOutput=outHours + ":" + outMinutes;
	if (showSeconds) {
		TimeStrOutput+= ":" + outSeconds;
	}
	if (showAMPM && !useMilitary) {
		TimeStrOutput+= (isPM)? "P" : "A";
	}
	if (TimeZoneType != "0") {
		TimeStrOutput+= " " + currTimeZone;
	}
	inTimeObj.value = TimeStrOutput;
	return true;
}

/* ***************************************************************************************************** */


function RICOLA_Time_CheckDaylightSavingsTime(inTimeItem, inTimeStr, inDateItem, currHour) {
	
	if (inDateItem.value.replace(/\s*/g,"") == "") {
		inTimeStr="";
		alert("Please input a date before you input a time.");
		inDateItem.focus();
		return inTimeStr;
	}
	if (inDateItem.value.search(/^\s*\d{1,2}\/\d{1,2}\/(\d{2}|\d{4})\s*$/) == -1) {
		inTimeStr="";
		alert("Please input a date in mm/dd/yyyy or mm/dd/yy format before you input a time.");
		inDateItem.focus();
		return inTimeStr;
	}

	// If user entered a 2-digit date, assume it's "20", and prepend to year
	if (inDateItem.value.search(/^\s*\d{1,2}\/\d{1,2}\/\d{2}\s*$/) > -1) {
		var tmpDate = inDateItem.value.replace(/\s*/g,"");
		var tmpDateArray = tmpDate.split("/");
		var tmpYear = "20" + tmpDateArray[2];
		var inputDate = new Date(tmpDateArray[0] + "/" + tmpDateArray[1] + "/" + tmpYear);
	} else {
		var inputDate=new Date(inDateItem.value.replace(/\s*/g,""));
	}
	
  		var FallDST=new Date(inputDate.getFullYear(),10,1);
  		var SpringDST=new Date(inputDate.getFullYear(),2,8);
	var continueLoop=true;

	// Find first Sunday in November
	while (continueLoop) {
		if (FallDST.getDay() == 0) {
			continueLoop=false;
		} else {
			FallDST=new Date(FallDST.getFullYear(), FallDST.getMonth() , (FallDST.getDate() + 1));
		}
	}
	// Find second Sunday in March
	continueLoop=true;
	while (continueLoop) {
		if (SpringDST.getDay() == 0) {
			continueLoop=false;
		} else {
			SpringDST=new Date(SpringDST.getFullYear(), SpringDST.getMonth() , (SpringDST.getDate() + 1));
		}
	}

	if ((inputDate.getMonth() == FallDST.getMonth()) && (inputDate.getDate() == FallDST.getDate())) {	// If first Sunday of Nov.
		if (currHour == 1) {	// If the time is between 1:00 and 1:59 Military Time
//			if (inTimeStr.search(/^\s*\d{1,2}\:\d{1,2}\s*(est|cst|mst|pst|edt|cdt|mdt|pdt)\s*$/i) == -1) {	// If Three letter time zone not entered
			if (inTimeStr.search(/^\s*(est|cst|mst|pst|edt|cdt|mdt|pdt)\s*$/i) == -1) {	// If Three letter time zone not entered
				alert("The inputted date " + inDateItem.value.replace(/\s*/g,"") + " falls on the Fall Daylight Savings day.  Please input whether it is standard time or daylight savings time in the three letter acronym for the time zone (example:  CST for Central Standard Time).\n\nSummer Dates: \t Daylight Savings Time (ex. EDT, CDT, MDT, PDT)\nFall Dates: \t Standard Time (ex. EST, CST, MST, PST)");
				RICOLATimeHasErrored(inTimeItem);
				inTimeStr="";
				return inTimeStr;
			}
		} else if (currHour == 0) {	// If the time is before 1:00 Military Time
			inTimeStr=inTimeStr.replace(/(E|C|M|P)S?D?T/gi, "$1DT");
		} else {		// After 1:59 Military Time
			inTimeStr=inTimeStr.replace(/(E|C|M|P)S?D?T/gi, "$1ST");
		}
	} else if ((inputDate.getMonth() == SpringDST.getMonth()) && (inputDate.getDate() == SpringDST.getDate())) {	// If First Sunday of April
		if (currHour == 2) {	// If the time is between 2:00 and 2:59 Military Time
			alert("The inputted date " + inDateItem.value.replace(/\s*/g,"") + " falls on the Spring Daylight Savings day.  The time between 02:00 and 02:59 is invalid.  Please input a different time.");
			RICOLATimeHasErrored(inTimeItem);
			inTimeStr="";
			return inTimeStr;
		} else if ((currHour == 0) || (currHour == 1)) {	// If the time is before 2:00 Military Time
			inTimeStr=inTimeStr.replace(/(E|C|M|P)S?D?T/gi, "$1ST");
		} else {				// After 2:59 Military Time
			inTimeStr=inTimeStr.replace(/(E|C|M|P)S?D?T/gi, "$1DT");
		}
	} else if ((inputDate > FallDST) || (inputDate < SpringDST)) {	// If Winter Date (Standard Time)
		inTimeStr=inTimeStr.replace(/(E|C|M|P)S?D?T/gi, "$1ST");
	} else {	// If Summer Date (Daylight Savings)
		inTimeStr=inTimeStr.replace(/(E|C|M|P)S?D?T/gi, "$1DT");
	}
	return inTimeStr;
   }

