/*
   Modify js functions in New Perspectives on JavaScript, Tutorial 2, Tutorial Case

   Author: Steven Grindle
   Date:   22 Oct 2007 (web325 MidTerm Exam)

   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 TimeDateNow() {
//display the current date and time
//The today variable contains the current date and time
//var today = new Date("February 24, 2007 14:35:05"); //original code
var today = new Date();  //today is local TimeDate
document.getElementById("dateNow").value = showDate(today);
document.getElementById("timeNow").value = showTime(today);
//put user-friendly Time and Date into form boxes in the header
}

function showDate(dateObj) {
	var monthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
	thisDate = dateObj.getDate();
	monthName = monthNames[dateObj.getMonth()];
	//getMonth returns 0 for January, 1 for February, ... 11 for December
	thisYear = dateObj.getFullYear();
	return monthName +" "+ thisDate + ", " + thisYear;
	document.write("Hello")
}

function showTime(dateObj) {
	thisSecond = dateObj.getSeconds();
	thisMinute = dateObj.getMinutes();
	thisHour = dateObj.getHours();
	// change thisHour from 24-hour time to 12-hour time by:
	// 1) if thisHour < 12 then set ampm to " am" otherwise set it to " pm"
	var ampm = (thisHour < 12) ? " am" : " pm";
	// 2) subract 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;
	// apply a leading zero to seconds and minutes <10
	thisMinute = thisMinute < 10 ? "0" + thisMinute : thisMinute;
	thisSecond = thisSecond < 10 ? "0" + thisSecond : thisSecond;
	
	return thisHour + ":" + thisMinute + ":" + thisSecond + ampm;
}
	
function calcDays(currentDate) {
	//create a date object for January 1 of the next year
	newYear = new Date("January 1, 2007"); // insert a temporary date for January 1
	nextYear = currentDate.getFullYear()+1; // the year value of the next year
	newYear.setFullYear(nextYear); //change newYear to the next year
	
	// calculate the difference between currentDate and January 1
	days = (newYear - currentDate)/(1000*60*60*24); // convert milliseconds to days
	return days;
}

function TimeDateNow2(){
		document.write("<h2 style='color:#23238e;'>");
		var today = new Date();  //today is local TimeDate
		document.write("Your PC's system clock indicates that ");
		document.write("today is "+ showDate(today)+" and ");
		document.write("the time is "+ showTime(today));
		document.write("</h2>")
} //Put user friendly time and date into the body of the web page.
