// Set a regular expression for the format and dividers.var expressionToGetFormat = /[^A-Za-z0-9]/;var expressionToGetDividers =  /[A-Za-z0-9]/;function showCalendar(name, dateFormat) {	var calendar = document.getElementById(name + "Calendar");	if (calendar.style.visibility == "visible") {	    // Hide calendar		calendar.style.visibility = 'hidden';	} else {		// Show the calendar.		calendar.style.visibility = "visible";		var dateInput = document.getElementById(name +"DateInput");		var date = parseDate(dateInput.value, dateFormat);		// Set the month.		var monthSelect = document.getElementById(name + "Month");		monthSelect.value=date.getMonth();		// Set the year.		var yearSelect = document.getElementById(name + "Year");		setYears(yearSelect, date.getFullYear());		// Update the days.		updateCalendar(name, dateFormat);	}}function updateCalendar(name, dateFormat) {	var dateInput = document.getElementById(name +"DateInput");	var selectedDate = parseDate(dateInput.value, dateFormat);	var monthSelect = document.getElementById(name + "Month");	var month = monthSelect.value;	var yearSelect = document.getElementById(name + "Year");	var year = yearSelect.value;	// Set the years in the year select.	setYears(yearSelect, year);	// Figure out the date that the first day of the week should start with.	var date = new Date(year, month, 1);	var dayOfWeek = date.getDay();	if (dayOfWeek == 0) {		dayOfWeek = 7;	}	date.setDate(date.getDate() - dayOfWeek);	// Figure out the start of today so it can have a different style.	var now = new Date();	var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());	// Write each day of the month.	for (row=1; row<=6; row++) {		for (column=1; column<=7; column++) {			var dateCell = document.getElementById(name + row + column);			dateCell.innerHTML = date.getDate();			dateCell.name = date.getTime();			if (date.getMonth() != month) {				dateCell.className = "outside";			} else if (selectedDate != null && date.getTime() == selectedDate.getTime()) {				dateCell.className = "selected";			} else if (date.getTime() == today.getTime()) {				dateCell.className = "today";			} else if (date.getDay() == 0 || date.getDay() == 6) {				dateCell.className = "weekend";			} else {				dateCell.className = "weekday";			}			date.setDate(date.getDate() + 1);		}	}}function setYears(yearSelect, selectedYear) {	// Set the years in the years select.	yearSelect.options.length = 0;	for (i = 0; i < 11; i++) {		var yearOption = selectedYear - 5 + i;		yearSelect.options[i] = new Option(yearOption, yearOption);		if (yearOption == selectedYear) {			yearSelect.options[i].selected = "selected";		}	}}function setDateInput(dateCell, dateFormat) {	// Set the date input to be the selected day of week.	var name = dateCell.id.substring(0, dateCell.id.length - 2);	var date = new Date(dateCell.name);	var dateInput = document.getElementById(name + "DateInput");	dateInput.value = getFormattedDateOnly(date, dateFormat);	var calendar = document.getElementById(name + "Calendar");	calendar.style.visibility = "hidden";	setDateTimeInput(name);}function setDateTimeInput(name) {	// Set the hidden date time input to be the combined date and time.	var dateInput = document.getElementById(name + "DateInput");	var timeInput = document.getElementById(name + "TimeInput");	var dateTimeInput = document.getElementById(name + "DateTimeInput");	dateTimeInput.value = dateInput.value + " " + timeInput.value;}function addTime(name, add, addUnit, dateFormat, timeFormat) {	var dateInput = document.getElementById(name + "DateInput");	var timeInput = document.getElementById(name + "TimeInput");	var date = parseDateAndTime(dateInput.value, dateFormat, timeInput.value, timeFormat);	if (addUnit == "h") {		// Add the specified hours.		date.setHours(date.getHours() + add);	} else if (addUnit == "m") {		if (add == 15) {			// If the add is 15 minutes, round off to next 15 minutes.			if (date.getMinutes() < 15) {				date.setMinutes(15);			} else if (date.getMinutes() < 30) {				date.setMinutes(30);			} else if (date.getMinutes() < 45) {				date.setMinutes(45);			} else {				date.setMinutes(0);				date.setHours(date.getHours() + 1);			}		} else if (add == 30) {			// If the add is 30 minutes, round off to next 30 minutes.			if (date.getMinutes() < 30) {				date.setMinutes(30);			} else {				date.setMinutes(0);				date.setHours(date.getHours() + 1);			}		} else {			// Add the specified minutes.			date.setMinutes(date.getMinutes() + add);		}	} else {		alert("The addUnit '" + addUnit + "' is not valid. Use 'h' or 'm'.");	}	if (dateInput.value == "") {		dateInput.value = getFormattedDateOnly(date, dateFormat);	}	timeInput.value = getFormattedTimeOnly(date, timeFormat);	setDateTimeInput(name);}function parseDate(dateString, dateFormat) {	var date = new Date();	date.setHours(0);	date.setMinutes(0);	date.setSeconds(0);	date.setMilliseconds(0);	if (dateString == "") {		return date;	}	// Split the date string and date format up into arrays based on the expressionToGetFormat.	var splitDateString = dateString.split(expressionToGetFormat);	var splitDateFormat = dateFormat.split(expressionToGetFormat);	// Set the month, date, and year based on the split date string and date format arrays.	for (i = 0; i < splitDateFormat.length; i++) {		if (splitDateFormat[i].charAt(0) == 'm') {			date.setMonth(splitDateString[i] - 1);		} else if (splitDateFormat[i].charAt(0) == 'd') {			date.setDate(splitDateString[i]);		} else if (splitDateFormat[i].charAt(0) == 'y') {			date.setYear(splitDateString[i]);		} else {			alert("The parameter '" + splitDateFormat[i] + "' is not valid for a date format.");		}	}	return date;}function parseDateAndTime(dateString, dateFormat, timeString, timeFormat) {	var date = parseDate(dateString, dateFormat);	if (timeString == "") {		return date;	}	// Split the time string and time format up into arrays based on the expressionToGetFormat.	var splitTimeString = timeString.split(expressionToGetFormat);	var splitTimeFormat = timeFormat.split(expressionToGetFormat);	// Set the hours, minutes, seconds, and milliseconds based on the split time string and time format arrays.	for (i = 0; i < splitTimeFormat.length; i++) {		if (splitTimeFormat[i].charAt(0) == 'h') {			var pmExpression = /pm|p\.m\./;			date.setHours(splitTimeString[i]);			if (pmExpression.test(timeString) && date.getHours() != 12) {				// If 1pm or later, add 12 to hours.				date.setHours(date.getHours() + 12);			} else if (!pmExpression.test(timeString) && date.getHours() == 12) {				// If midnight, subtract 12 from hours.				date.setHours(date.getHours() - 12);			}		} else if (splitTimeFormat[i].charAt(0) == 'H') {			date.setHours(splitTimeString[i]);		} else if (splitTimeFormat[i].charAt(0) == 'm') {			date.setMinutes(splitTimeString[i]);		} else if (splitTimeFormat[i].charAt(0) == 's') {			date.setSeconds(splitTimeString[i]);		} else if (splitTimeFormat[i].charAt(0) == 'S') {			date.setMilliseconds(splitTimeString[i]);		} else {			alert("The parameter '" + splitTimeFormat[i] + "' is not valid for a time format.");		}	}	return date;}function getFormattedDateOnly(date, dateFormat) {	// Split the date format up into arrays based on the expressionToGetFormat.	var splitDateFormat = dateFormat.split(expressionToGetFormat);	// Get the dividers.	var dividers = dateFormat.split(expressionToGetDividers);	// Set the hours, minutes, seconds, and milliseconds based on the split time string and time format arrays.	var dateString = "";	for (i = 0; i < splitDateFormat.length; i++) {		if (splitDateFormat[i] == 'd') {			dateString = dateString + date.getDate();		} else if (splitDateFormat[i] == 'm') {			dateString = dateString + (date.getMonth() + 1);		} else if (splitDateFormat[i] == 'y') {			dateString = dateString + date.getFullYear();		} else if (splitDateFormat[i] == 'yyyy') {			dateString = dateString + date.getFullYear();		} else {			alert("The parameter '" + splitDateFormat[i] + "' is not valid for a date format.");		}		// Add the divider. The regular expression on IE splits the string into just the dividers		// but on Firefox and Safari, it includes an empty character in the first and last elements.		if (dividers[0] == "") {			if (i + 1 < dividers.length) {				dateString = dateString + dividers[i+1];			}		} else {			if (i < dividers.length) {				dateString = dateString + dividers[i];			}		}	}	return dateString;}function getFormattedTimeOnly(date, timeFormat) {	// Split the date format up into arrays based on the expressionToGetFormat.	var splitTimeFormat = timeFormat.split(expressionToGetFormat);	// Get the dividers.	var dividers = timeFormat.split(expressionToGetDividers);	// Set the hours, minutes, seconds, and milliseconds based on the split time string and time format arrays.	var timeString = "";	var ampm = "";	for (i = 0; i < splitTimeFormat.length; i++) {		if (splitTimeFormat[i] == 'h') {			// Format hours with am and pm.			if (date.getHours() == 0) {				timeString = timeString + 12;				ampm = " am";			} else if (date.getHours() == 12) {				timeString = timeString + 12;				ampm = " pm";			} else if (date.getHours() < 12) {				timeString = timeString + date.getHours();				ampm = " am";			} else {				timeString = timeString + (date.getHours() - 12);				ampm = " pm"			}		} else if (splitTimeFormat[i] == 'H') {			/// Format military time.			timeString = timeString + date.getHours();		} else if (splitTimeFormat[i] == 'm') {			// Format minutes.			if (date.getMinutes() < 10) {				timeString = timeString + "0";			}			timeString = timeString + date.getMinutes();		} else if (splitTimeFormat[i] == 's') {			// Format seconds.			if (date.getSeconds() < 10) {				timeString = timeString + "0";			}			timeString = timeString + date.getSeconds();		} else if (splitTimeFormat[i] == 'S') {			// Format milliseconds.			if (date.getMilliseconds() < 10) {				timeString = timeString + "000";			} else if (date.getMilliseconds() < 100) {				timeString = timeString + "00";			} else if (date.getMilliseconds() < 1000) {				timeString = timeString + "0";			}			timeString = timeString + date.getMilliseconds();		} else {			alert("The parameter '" + splitTimeFormat[i] + "' is not valid for a time format.");		}		// Add the divider. The regular expression on IE splits the string into just the dividers		// but on Firefox and Safari, it includes an empty character in the first and last elements.		if (dividers[0] == "") {			if (i + 1 < dividers.length) {				timeString = timeString + dividers[i+1];			}		} else {			if (i < dividers.length) {				timeString = timeString + dividers[i];			}		}	}	timeString = timeString + ampm;	return timeString;}