/*
   New Perspectives on JavaScript
   Tutorial 2
   Tutorial Case

   Author: Rachel Charron
   Date:   Oct 7 2009

   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 from24-hour time to 12-hour time by:
//1) if thisHour is <12 then set ampm to "am" or else set it to "pm"
var ampm = (thisHour < 12) ? "am" : "pm";
//2) subtract 12 from thisHour variable
thisHour = (thisHour > 12) ? thisHour - 12 : thisHour;
//3) if thisHour equals 0 change iot to 12
thisHour = (thisHour == 0) ? 12 : thisHour;
	
	//add leading zeros to minutes and seconds less than 10
	thisMinute = thisMinute < 10 ? "0"+thisMinute : thisMinute;
	thisSecond = thisSecond < 10 ? "0"+thisSecond : thisSecond;
	
	return thisHour + ":" + thisMinute + ":" + thisSecond + ampm;
}

function calcDays(currentDate) {
	// calculate a date object for January 1 of the next year
	newYear = new Date("August 12, 2010"); //insert a temporary date for jan 1
	nextYear = currentDate.getFullYear();  //the year value of the next year
	newYear.setFullYear(nextYear);
	
 
	// calculate the difference between currentDate and January 1
	
	days = (newYear - currentDate) / (1000*60*60*24); //convert milliseconds to days
	return days;
	
}
