/*
   New Perspectives on HTML, XHTML, and DHTML
   Tutorial 11
   Tutorial Case

   Author: Luke Stoltenberg
   Date:   12/11/2010

   Function List:
   showDate(dateObj)
      Returns the current date in the format mm/dd/yyyy

   showTime(dateObj)
      Returns the current time in the format hh:mm:ss am/pm

   calcDays(currentDate)
      Returns the number of days between the current date and January 1st
      of the next year

*/

function showDate(dateObj) {
		thisDate = dateObj.getDate();
		thisMonth = dateObj.getMonth() +1;
		thisYear = dateObj.getFullYear();
		return thisMonth + "/" + thisDate + "/" + thisYear;
}

function showTime(dateObj) {
		thisSecond=dateObj.getSeconds();
		thisMinute=dateObj.getMinutes();
		thisHour=dateObj.getHours();
		
		// change thisHour from 24-hourtime to 12-hour time by:
		// 1) if thisHour < 12 then set ampmto "a.m." otherwise set it to "p.m."
		var ampm = (thisHour < 12) ? "a.m.": "p.m.";
		
		// 2) subtract 12 from the thisHour variable
		thisHour = (thisHour > 12) ? thisHour - 12: thisHour;
		
		// 3) if thisHour equals 0, change it to 12
		thisHour = (thisHour == 0) ? 12: thisHour;
		
		return thisHour + ":" + thisMinute + ":" + thisSecond + ampm;
		
		// add leading 0 to minutes less than 10
		thisMinute = (thisMinute < 10) ? "0" + thisMinute: thisMinute;
		thisSecond = (thisSecond < 10) ? "0" + thisSecond: thisSecond;
}

function calcDays(currentDate) {
		
		// create a date object for January 1st next year		
		newYear = new Date("January 1, 2011");
		nextYear = currentDate.getFullYear()+1;
		newYear.setFullYear(nextYear);
		
		// calculate the difference between currentDate and January 1st
		days = (newYear - currentDate)/(1000*60*60*24);
		
		return days;
}
									   
