
//  Client-side js routines for usa map www.marlenesite.com\aspmar\usamap

// **********************************************************************************************
//					Time Zone Routines
//
// NOTES:
//  London UTC value = 0
//  West of London has a negative UTC offset.  East of London has a positive offset.
//  To Compute UTC offset:
// 		1) Figure out how many hours destination is from London in hours.
//		2) Absolute value is 60 minutes * number of hours away from London.
//		3) West of London has a negative UTC offset.  East of London has a positive offset.
//
// Example:  New York is 5 hours West (negative value) of London.  Therefore, it's UTC offset is:  
//			 60 minutes * 5 hours * (-1) = -300
// **********************************************************************************************
//  UTC Offsets

timeZone = new Array(
	-480,		// PST -  example: California 	0
	-420,		// MST -  example: Wyoming 		1
	-360,		// CST -  example: Minnesota 	2 
	-300,		// EST -  example: NY 			3
	-540,		// AKST - example: Alaska 		4
	-600		// HST -  example: Hawaii 		5
);

var timeDiff;
var intervalId = null;
var daylightsavingshr;  


//**** Start Routines *********************************************************
// Starting Routine for all time zones, 
// called onLoad() in body tag of default.asp
//******************************************************************************
function UpdateTimeZone() {
	for(var i=0; i<timeZone.length; i++)  {
		timeDiff = timeZone[i];								// UTC offset (from London)
		UpdateTime(i, timeDiff);							// update time
	}  // end for
} // end function UpdateTimeZone()

//******************************************************************************
// Starting Routine for one time zone,
// called onLoad() in body tag of mastates.asp and nesates.asp
//******************************************************************************
function UpdateOneTimeZone() { 
		timeDiff = timeZone[3];								// UTC offset (from London)
		Update1Time(timeDiff);								// update time
} // end function UpdateOneTimeZone()
//**** END Start Routines ****************************************************


// **** Update Time Routines ***************************************************
// called onLoad() in body tag of default.asp
function UpdateTime(zone, timeDiff) {
	var tForm = window.document.mForm;
   	var nowTime = new Date();
   
   	nowTime.setMinutes(nowTime.getMinutes() + nowTime.getTimezoneOffset() + 
      		parseInt(timeDiff));

 	tForm.elements[zone].value = GetTimeString(nowTime);
} // end function UpdateTime()

//******************************************************************************
// called onLoad() in body tag of mastates.asp and nesates.asp
function Update1Time(timeDiff) {
	var tForm = window.document.mForm;
   	var nowTime = new Date();
   
   	nowTime.setMinutes(nowTime.getMinutes() + nowTime.getTimezoneOffset() + 
      		parseInt(timeDiff));

 	tForm.ClockTime3.value = GetTimeString(nowTime);
} // end function Update1Time()
// **** END Update Time Routines **********************************************


//  ****  Interval Routines ****************************************************
// called onLoad() in body tag of default.asp, mastates.asp and nesates.asp
function ClockOnUnload() {
   ClockClearInterval();  
}  // end function ClockOnUnload()

function ClockClearInterval() {
    if (intervalId) {
        clearInterval(intervalId);
        intervalId = null;
    }
} // end function ClockClearInterval()

//******************************************************************************
// called onLoad() in body tag of mastates.asp and nesates.asp
function TimeOneUpdate()  {									// refresh/update time zones
   	UpdateOneTimeZone(3);
 	intervalId = setInterval("CallOneUpdateTime()", 1000);
} // end TimeOneUpdate()

function CallOneUpdateTime() {
	timeDiff = timeZone[3];								// UTC offset (from London)
	Update1Time(timeDiff);								// update time
}  // end function CallOneUpdateTime()

//******************************************************************************
// called onLoad() in body tag of default.asp
function TimeUpdate()  {								// refresh/update time zones
   UpdateTimeZone();
   intervalId = setInterval("CallUpdateTime()", 1000);
} // end TimeUpdate()

function CallUpdateTime() {
	for(var i=0; i<timeZone.length; i++)  {
		timeDiff = timeZone[i];						// UTC offset (from London)
		UpdateTime(i, timeDiff);					// update time
	} // end for
}  // end function CallUpdateTime()

//  ****  END Interval Routines ************************************************


// **** Common Routines ******************************************************
//******************************************************************************************
// NOTE: Daylight Saving Time begins for most of the United States at 2 a.m. on the first 
// Sunday of April. Time reverts to standard time at 2 a.m. on the last Sunday of October.
// Find the 1st Sunday of April (Spring ahead) and last last Sunday of October.
//******************************************************************************************
function CheckDaylightSavings(dateObject) {
	daylightsavingshr = 0;										// daylight savings offset, 0 = Winter, 1= Spring
	var myDate, myDayOfWeek, fSunday;							// variables for April
	var octDate, octDayOfWeek, lastSunday;						// variables for October
	
	for (var i=0; i<30; i++)  {
		myDate  = new Date(dateObject.getFullYear(), 3, i);  	// Get day of week April
		myDayOfWeek =  myDate.getDay();            				// Offset for day of month (0 = Sunday through 6 = Saturday)
		if (myDayOfWeek == 0)  {
			fSunday = i;
			break;
		} // end if
	} // end for
	
	for (var i=0; i<31; i++)  {
		octDate  = new Date(dateObject.getFullYear(), 9, i);  	// Get day of week October
		octDayOfWeek =  octDate.getDay();            			// Offset for day of month (0 = Sunday through 6 = Saturday)
		if (octDayOfWeek == 0)  {
			lastSunday = i;
		} // end if
	} // end for
	
	// Today's Values for Current Time Zone, in USA, currently being processed
	var myThisHours = dateObject.getHours();					// today's current hour
	var myThisMonth = dateObject.getMonth();					// today's month
	var myThisDay   = dateObject.getDate();						// today's day
	
	if (myThisMonth >= 3 &&  myThisMonth <= 9)  {										// Between April (month=3) and October (month=9)
		daylightsavingshr = 1;															// "spring ahead" one hour
		if (myThisMonth == 3 && myThisDay == fSunday && myThisHours < 2)  {				// 1st Sunday in April < 2:00AM winter daylight saving time
			daylightsavingshr = 0;														// winter offset
		} // end if April 1st Sunday before 2:00AM
		else if (myThisMonth == 9 && myThisDay == lastSunday && myThisHours >= 2)  {  	// last Sunday in October >= 2:00AM winter daylight saving time
			daylightsavingshr = 0;														// winter offset
		} // end if October last Sunday after 2:00AM
	} // end if month
	
} // end function CheckDaylightSavings()
// **********************************************************************************************************


// *********** Format Time ********************************************
function GetTimeString(dateObject) {				// format time					
	var ampm; 
   	var timeString;
	
	CheckDaylightSavings(dateObject); 				// get daylight saving offset
 	  
  	var hours = dateObject.getHours() + daylightsavingshr;
  	 if (hours > 12) {
     	 hours = hours - 12;
	 	 ampm = "  PM";
		}  // end if
	else ampm = "  AM";
	
    if (hours < 10) hours = "0" + hours;

   	var minutes = dateObject.getMinutes();
   	if (minutes < 10) minutes = "0" + minutes;

  	 var seconds = dateObject.getSeconds()
  	 if (seconds < 10) seconds = "0" + seconds;	  

  	 timeString = hours + ":" + minutes + ":" + seconds + ampm;

  	 return timeString;
} // end getTimeString()
//  **** END  Common Routines *************************************************


