/*******************************************************************

SUNSET CALCULATION and Date Display/Time of Day Display

  display_date(showDayOfWeek) displays the date
    If showDayOfWeek is true, day of week is shown:
    e.g., "Wednesday, March 15, 2000"
    If false:
    e.g., "March 15, 2000"

  display_title() displays the time of day
    e.g., "Good Morning"
    
  writesunset() displays the time of the sunset in Ottawa
    e.g., "23:09 UTC"
    - to display sunset for another locale, change the Latitude and Longitude in the
      writesunset() subroutine, below.

  Sunset formula was found at Ed William's excellent formulary site:

	http://www.best.com/~williams/index.html
	JavaScript (c) 2000, Mark Beamish (mbeamish@magi.com)
	Content (c) 2000, The Ottawa Flying Club (info@ofc.ca)

	Last update:  December 12, 2000

*************************************************/



function theHours(decTime) {

/***************************************************

	Return the hours portion of a decimal time

	Parameter:	decTime - decimal time

****************************************************/

return Math.floor(decTime);

}

function theMinutes(decTime) {

/***************************************************

	Return the minutes portion of a decimal time

	Parameter:	decTime - decimal time

*****************************************************/

return Math.floor((decTime - Math.floor(decTime)) * 60);

}

function zeroPlaceHolder( minit ) {

/*****************************************************

	Put a Zero in to correct for one-digit minutes

	Parameter:	minit - the minute of sunset

*****************************************************/

	ph = ((minit < 10) ? '0' : '');

	return ph;
}

function toRadians( deg ) {

/******************************************************

	Convert degrees to radians

	Parameter:	deg - degrees

*******************************************************/

  return deg * Math.PI / 180;
}

function toDegrees ( rad ) {

/*****************************************************

	Convert radians to degrees

*******************************************************/

  return rad * 180 / Math.PI;
}

function toDecimalDegrees(degrees, minutes) {

/******************************************************

	Convert degrees and minutes to decimal

	Parameters:	degrees - degrees of long/lat
			minutes - minutes of long/lat

**********************************************************/

  return degrees + (minutes / 60);

}

function dayofyear() {

/***********************************************************

	STEP 1:
	Calculate the Day of the Year

************************************************************/

  var today = new Date();		// get today
  var day = today.getDate();   	// day of the month
  var month = today.getMonth() + 1;	// month
  var year = ((today.getYear() < 10) ? (today.getYear() + 2000) : (today.getYear() + 1900));	// 4-digit year

  var n1 = Math.floor(275 * month / 9);
  var n2 = Math.floor((month + 9) / 12);
  var n3 = (1 + Math.floor((year - 4 * Math.floor(year / 4) + 2) / 3));
  return n1 - (n2 * n3) + day - 30;

}

function approxtime(longitude) {

/*****************************************************

	STEP 2:
	Convert the longitude to hour value and calculate an approximate time

	Parameter:	longitude - decimal longitude

*********************************************************/

  var lngHour = longitude / 15;

  return (dayofyear() + ((6 - lngHour) / 24));
}

function meananomaly(atime) {

/******************************************************

	STEP 3:
	Calculate the Sun's mean anomaly

	Parameters:	atime - approximate time

********************************************************/

  return (0.9856 * atime) - 3.289;
}

function suntl(ma) {

/*******************************************************

	STEP 4:
	Calculate the Sun's true longitude

	Parameters:  ma - sun's mean anomaly

*********************************************************/

  var tl = ma + (1.916 * Math.sin(toRadians(ma))) + (0.020 * Math.sin(2 * toRadians(ma))) + 282.634;

  //  put tl into range 0 - 360

  tl = ((tl < 0) ? (tl + 360) : tl);
  tl = ((tl > 360) ? (tl - 360) : tl);

  return tl;
}

function rtascension(tl) {

/*********************************************************

	STEP 5a:
	Calculate the Sun's right ascension

	Parameter:  tl - Sun's True Longitude

************************************************************/

  var ra = toDegrees(Math.atan(0.91764 * Math.tan(toRadians(tl))));

  //  put ra into range 0 - 360
  //  NOTE:  Ed says to do it, but doesn't do it in the example...

  //ra = ((ra < 0) ? (ra + 360) : ra);
  //ra = ((ra > 360) ? (ra - 360) : ra);

  //  STEP 5b:
  //  Now, put the RA into the same quadrant as the Sun's true longitude (tl)

  var tlquadrant = (Math.floor(tl / 90)) * 90;	//  true longitude quadrant
  var raquadrant = (Math.floor(ra / 90)) * 90;   //  right ascension quadrant

  ra = ra + (tlquadrant - raquadrant);

  //  STEP 5C
  //  Convert the right ascension value into hours

  ra = ra / 15;

  return ra;

}

function lochourangle(tl, zenith, latitude) {

/*********************************************************

	STEP 6:
	Calculate the Sun's declination

	Parameters:	tl - sun's true longitude
			zenith - sun's Zenith, in decimal degrees
			latitude - latitude, in decimal degrees

************************************************************/

  sinDec = 0.39782 * Math.sin(toRadians(tl));
  cosDec = Math.cos(Math.asin(sinDec));

/*-----------------------------------------------------------

	STEP 7a:
	Calculate the Sun's local hour angle

-----------------------------------------------------------*/

 var cosH = (Math.cos(toRadians(zenith)) - (sinDec * Math.sin(toRadians(latitude)))) / (cosDec * Math.cos(toRadians(latitude)));

/*------------------------------------------------------------

	STEP 7b:
	Finish calculating H and convert it to hours

---------------------------------------------------------------*/

 return toDegrees(Math.acos(cosH)) / 15;

}

function lmtriseset(h, ra, t) {

/***********************************************************

	STEP 8:
	Calculate local mean time of rising / setting

	Parameters:	h - sun's local hour angle (in hours)
			ra - sun's right ascension (in hours)
			t - approximate time

*****************************************************************/

  return h + ra - (0.06571 * t) - 6.622;

}

function adjustToUTC(t, longitude) {

/*****************************************************************

	STEP 9:
	Adjust back to UTC

	Parameters:	t - local mean time of rising / setting
			longitude - longitude

********************************************************************/

  var lngHour = longitude / 15;
  var ut = t - lngHour;

  // make sure it's in the range 0 -> 24

  ut = ((ut < 0) ? (ut + 24) : ut);
  ut = ((ut > 24) ? (ut - 24) : ut);

  return ut;

}

function writesunset()  {

/*******************************************************************

	Output the sunset time

*******************************************************************/

 var longDeg = -75;	//	longitude in Ottawa (degrees)
 var longMin = -40;	//	longitude in Ottawa (minutes)
 var latDeg = 45;	//	latitude at Ottawa (degrees)
 var latMin = 19;	//	latitude at Ottawa (minutes)
 var zenith = toDecimalDegrees(90, 50);  // official zenith is 90 degrees 50'
 var longitude = toDecimalDegrees(longDeg, longMin);
 var latitude = toDecimalDegrees(latDeg, latMin);
 var doy = dayofyear();
 var atime = approxtime(longitude);
 var ma = meananomaly(atime);
 var tl = suntl(ma);
 var ra = rtascension(tl);
 var lha = lochourangle(tl, zenith, latitude);
 var lt = lmtriseset(lha, ra, atime);
 var ut = adjustToUTC(lt, longitude);
 var zph = zeroPlaceHolder(theMinutes(ut));

 document.write("Sun sets at " + theHours(ut) + ":" + zph + theMinutes(ut) + " UTC");

}

function display_title() {
var hours = date.getHours(); 
var message = '';

 if (hours < 12) { time_of_day = 'Morning'; message='Morning Message';}
 if ((hours >= 12) && (hours < 17)) { time_of_day = 'Afternoon'; message='Afternoon Message'}
 if (hours >= 17) { time_of_day = 'Evening'; message='Evening Message';}  

   
      document.write('Good ' + time_of_day + '!');

   }
//   document.write('<CENTER>' + message + '<CENTER>'); 
//end display title

function display_date(showDayOfWeek) {

   date = new Date();

   var day_of_week_number = date.getDay();
   var day_of_month = date.getDate();
   var month_number = date.getMonth();
//   var year = date.getYear();
   var year = date.getFullYear();
   var day_of_week = '';
   var month = ''

   if(month_number == 0){month = 'January';}
   if(month_number == 1){month = 'February';}
   if(month_number == 2){month = 'March';}
   if(month_number == 3){month = 'April';}
   if(month_number == 4){month = 'May';} 
   if(month_number == 5){month = 'June';}
   if(month_number == 6){month = 'July';}
   if(month_number == 7){month = 'August';}
   if(month_number == 8){month = 'September';}
   if(month_number == 9){month = 'October';}
   if(month_number == 10){month = 'November';}
   if(month_number == 11){month ='December';}


   if(day_of_week_number == 0){day_of_week = 'Sunday';}
   if(day_of_week_number == 1){day_of_week = 'Monday';}
   if(day_of_week_number == 2){day_of_week = 'Tuesday';}
   if(day_of_week_number == 3){day_of_week = 'Wednesday';}
   if(day_of_week_number == 4){day_of_week = 'Thursday';}
   if(day_of_week_number == 5){day_of_week = 'Friday';}
   if(day_of_week_number == 6){day_of_week = 'Saturday';}


   var date_to_show = month + ' ' + day_of_month + ', ' + year; 

   if (showDayOfWeek) {
   	date_to_show = day_of_week + ', ' + date_to_show;
   }

   document.write(date_to_show);
 
} //end display date

