﻿/* ////////////////////////////////////////// */
/* ////////////////////////////////////////// */
/* ////////////////////////////////////////// */
/* ////////////////////////////////////////// */
/* ///                                                     ///////////////////////////////////////////// */
/* ///       EC Javascript Library        ///////////////////////////////////////////// */
/* ///                                                    ///////////////////////////////////////////// */
/* ////////////////////////////////////////// */
/* ////////////////////////////////////////// */
/* ////////////////////////////////////////// */
/* ////////////////////////////////////////// */



/* 
This file adds the Javascript Behavior to the page.
*/



/* /////////////////////////// */
/*                                                        */
/*       Javascript Clock                    */
/*                                                        */
/* /////////////////////////// */



function ecClock() {
	// Check to see if the clockBox element exists
	if (document.getElementById("clockBox")) {

		// Define Several arrays; Javascript returns only numbers which represent various values
		// We define several arrays carefully ordered so that the numbers which the methods return will be replaced by the corresponding Name... 0 = Sunday, etc.
		var arrDayNames = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
		var arrMonthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
		
		
		// Call the date object and name variables for it's needed components
		var dateDate = new Date();

		var	intYear = dateDate.getFullYear();
		var	intMonth = dateDate.getMonth();
		var	intDate = dateDate.getDate();
		var	intDay = dateDate.getDay();
		var	intHours = dateDate.getHours();
		var	intMinutes = dateDate.getMinutes();
		var	intSeconds = dateDate.getSeconds();
		var	intAm = "";

		// determine whether its am or pm
		if (intHours < 12) {
			intAm = "AM";
		}
		else {
			intHours -= 12;
			intAm = "PM";
		}
		
		// set hour 0 to hour 12
		if (intHours == 0) {
			intHours = 12;
		}

		//Add a leading zero to single digit numbers
		intHours = twoDigitString(intHours);
		intMinutes = twoDigitString(intMinutes);
		intSeconds = twoDigitString(intSeconds);

		//Put it all together
		var strClock = intYear + " " + arrMonthNames[intMonth] + " " + intDate + ", " + arrDayNames[intDay] + "   " + intAm + " " + intHours + ":" + intMinutes + ":" + intSeconds;

		//Print the clock to the innerHTML of the #clockBox div; reset each second
		document.getElementById('clockBox').innerHTML = strClock;
		dateDate=setTimeout(ecClock,1000); 
	}
}



/* /////////////////////////////////// */
/*                                                                        */
/*       Image Rollover Functions                      */
/*                                                                        */
/* /////////////////////////////////// */



// This is the swap function for image rollovers.
function rollover(newPath) {
		document.images['rolloverImage'].src = newPath;
}

/* 
		This set of event handlers crashes the script if the id does not exist on the page. Therefore it's wrapped in an if statement to sense if the rollover image is present.
		The event handler grabs the ids of the rollover images and monitors their activity sending the image to swap into the rollover() function.
		
		New rollover images, simply need to use name="rolloverImage" and duplicate the if block, modifying it for the id and the image to swap.
		Since there's a lot of repetition if we add more calls, it may be possible to build two or three dimensional arrays that indicate the id, mouseover and mouseout images and looping through. This may also be a case where the event handlers would be better added in a page specific file.
*/

if (document.getElementById("ctl00_compContactForm_contactImage")) {
	document.getElementById("ctl00_compContactForm_contactImage").onmouseover = function() {rollover("/ECBeta/Images/ContactUsBoxHighlight.jpg")};
	document.getElementById("ctl00_compContactForm_contactImage").onmouseout = function() {rollover("/ECBeta/Images/ContactUsBox.jpg")};
}



/* /////////////////////////// */
/*                                                        */
/*       Helper Functions                    */
/*                                                        */
/* /////////////////////////// */



// Adds a leading zero to single digit number (string)
//**REWRITE** When possible, move the <10 check out of the function. Why call it, if it's not needed? However, it may be a good idea to have this duplicate check here, just in case two digit data is sent here by mistake. (also, this is a small enough function, it would have to be sent here a lot to slow anything noticably)
function twoDigitString(data) {
	if (data < 10) {
		data = "0" + data;
		return data;
	} else {
		return data;
	}
}



/*  addLoadEvent allows you to call any number of functions when the onload event occurs
		It is fully compatible with other scripts without wiping out their onload functions.
	addLoadEvent function was designed by Sitepoint guru Simon Willison  */
	
function addLoadEvent(func) {
	
	//assigns a variable for the onload event
	var oldonload = window.onload;
	
	// checks to see if there is a function already assigned to onload
	if (typeof window.onload != 'function') {
		window.onload = func; // if not, onload executes our function
	} 
	
	// otherwise the variable is called to run the previously called function(s)
	else {
		window.onload = function() {
			if (oldonload) {
				oldonload(); //calls the old function(s)
			}
			func(); //calls the new function
		}
	}
}

// Call the functions you want to insert, one per addLoadEvent
addLoadEvent(ecClock);
