//<SCRIPT LANGUAGE="JavaScript">

///////////////////////////////////////////////////////////////////////////////////////
//Date Functions

//TODO: Add your new date function here

//Returns true or false based on if the date is valid 
function IsDate(strDate) {
	var d = new Date(strDate);

	if(isNaN(d.getDate()) || isNaN(d.getMonth()) || isNaN(d.getYear()))
		return false;
		
	return true;
}

//Returns a string that contains the input date in long format (Ex: Jul 09, 1970)
//or an empty string if the input value is not a date
function FormatDate(strDate) {
	var d = new Date(strDate);
	var strMonth = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	
	if(strDate != '' && !(isNaN(d.getDate()) || isNaN(d.getMonth()) || isNaN(d.getYear()))) 
		return(strMonth[d.getMonth()] + ' ' + String(d.getDate()) + ', ' + d.getFullYear());

	return '';
}

//Returns a string that contains the input date in military format (yyyy/mm/dd)
//or an empty string if the input value is not a date
function FormatShortDate(strDate) {
	var d = new Date(strDate);

	if(strDate != '' && !(isNaN(d.getDate()) || isNaN(d.getMonth()) || isNaN(d.getYear()))) {
		var strMonth = (d.getMonth() + 1 < 10? "0": "") + String(d.getMonth() + 1);
		var strDate  = (d.getDate() < 10? "0": "") + String(d.getDate());
		return(String(d.getFullYear()) + '/' + strMonth + '/' + strDate);
	}

	return '';
}

///////////////////////////////////////////////////////////////////////////////////////
//Time Functions

//TODO: Add your new time function here

//Returns true or false based on if the time is valid 
function IsTime(strTime) {
	var d = new Date(strTime);

	if(!IsDate(strTime))
		d = new Date('Jan 01, 1970 ' + strTime);

	if(strTime=='' || isNaN(d.getHours()) || isNaN(d.getMinutes()) || isNaN(d.getSeconds())) {
		return false;
	}
	return true;
}

//Returns a string that contains the input time in the 
//folowing format: hh:mm:ss AM/PM or an empty string if the 
//input value is not time
function FormatTime(strTime) {
	if(!IsTime(strTime))
		return '';

	var strHours, strAMPM, d = new Date(strTime);

	if(!IsDate(strTime))
		d = new Date('Jan 01, 1970 ' + strTime);

	if(d.getHours() < 12) {
		strAMPM	 = 'AM';
		strHours = String(100 + d.getHours());
	} else if(d.getHours() == 12) {
		strAMPM  = 'PM';
		strHours = String(100 + d.getHours());
	} else { //d.getHours() > 12
		strAMPM  = 'PM';
		strHours = String(100 + d.getHours() - 12);
	}

	if(d.getSeconds() == 0) //don't display seconds if 0
		return(strHours.substr(1,2) + ':' + String(100 + d.getMinutes()).substr(1,2) + ' ' + strAMPM);
	
	return(strHours.substr(1,2) + ':' + String(100 + d.getMinutes()).substr(1,2) + ':' + String(100 + d.getSeconds()).substr(1,2) + ' ' + strAMPM);
}


///////////////////////////////////////////////////////////////////////////////////////
//Date and Time Functions

//TODO: Add your new date time function here

function FormatDateTime(strDateTime)
{
	if(IsDate(strDateTime) && IsTime(strDateTime))
		return FormatDate(strDateTime) + ' ' + FormatTime(strDateTime);

	return '';
}














/*
//Old version - will be removed
function IsDate(date)
{
	return(ValidDate(date));
}
function ValidDate(date)
{
	// convert date to string
	var sDate = new String(date);

	// locate slashes
	var nSlash1 = sDate.indexOf("/", 0);
	var nSlash2 = sDate.indexOf("/", nSlash1 + 1);
	
	// fail if both slashes not there
	if ((nSlash1 == -1) || (nSlash2 == -1)) 
		return(false);

	// determine month, day, year
	var nMonth = sDate.substring(0, nSlash1);
	var nDay = sDate.substring(nSlash1 + 1, nSlash2);
	var nYear = sDate.substring(nSlash2 + 1, sDate.length);

	// fail if any of month, day, year isn't an integer
	if ((nMonth == "") || (nDay == "") || (nYear == "") ||
		(nMonth % 1 != 0) || (nDay % 1 != 0) || (nYear % 1 != 0)) 
		return(false);

	// validate year
	if ((nYear < 0) || (nYear > 9999))
		return(false);

	// validate month
	if ((nMonth < 1) || (nMonth > 12)) 
		return(false);

	// validate day
	if (nDay < 1) 
		return(false);
	else {
		if (nMonth == 2) {
			if (nYear % 4 == 0) {
				if (nDay > 29) return(false);
			}
			else {
				if (nDay > 28) return(false);
			}
		}
		else {
			if ((nMonth == 4) || (nMonth == 6) || (nMonth == 9) || (nMonth == 11)) {
				if (nDay > 30) return(false);
			}
			else {
				if (nDay > 31) return(false);
			}
		}
	}

	// valid date
	return(true);
}
*/
