/*=========================================================================================
	cookie library
==========================================================================================*/


/*------------------------------------------------------------
*  Function:  getExpDate
*  
*  Description:
*  utility function to retrieve an expiration date in proper format
*  pass three integer parameters for the number of days, hours,
*  and minutes from now you want the cookie to expire (or
*  negative values for a past date)
*  all three parameters are required, so use zeros where appropriate
*  
*  Parameters:
*  days	integer
*  hours	integer
*  minutes	integer
*  
*  Return:
*  date	string
*------------------------------------------------------------*/
function getExpDate(days, hours, minutes) {
	var expDate = new Date();
	if (typeof days == "number" && typeof hours == "number" && typeof minutes == "number") {
		expDate.setDate(expDate.getDate() + parseInt(days));
		expDate.setHours(expDate.getHours() + parseInt(hours));
		expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
		return expDate.toGMTString();
	}
}


/*------------------------------------------------------------
*  Function:  getCookieVal
*  
*  Description:
*  utility function called by getCookie()
*  
*  Parameters:
*  offset	string
*  
*  Return:
*  cookie	string
*------------------------------------------------------------*/
function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

// 


/*------------------------------------------------------------
*  Function:  getCookie
*  
*  Description:
*  primary function to retrieve cookie by name
*  
*  Parameters:
*  name	string	name of cookie to be retrieved
*  
*  Return:
*  value	string	value of named cookie
*------------------------------------------------------------*/
function getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return "";
}



/*------------------------------------------------------------
*  Function:  setCookie
*  
*  Description:
*  store cookie value with optional details as needed
*  
*  Parameters:
*  name	string
*  value	string
*  expires	string
*  path	string
*  domain	string
*  secure	boolean
*  
*  Return:
*  none
*------------------------------------------------------------*/
function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}




/*------------------------------------------------------------
*  Function:  deleteCookie
*  
*  Description:
*  remove the cookie by setting ancient expiration date
*  
*  Parameters:
*  name	string
*  path	string
*  domain	string
*  
*  Return:
*  none
*------------------------------------------------------------*/
function deleteCookie(name,path,domain) {
	/* BUGFIX ID 25 */
	if (getCookie(name) != "") {
		document.cookie = name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

/*=========================================================================================
	end cookie library
==========================================================================================*/




/*=========================================================================================
	font resize library
==========================================================================================*/

var styles = new Array();		// Global style array for storing all available stylesheets.

var DEFAULT_STYLE = 1;			// Index of default style.  In this case, "Small Fonts".

var STYLE_COOKIE = "FSStyle";	// Name of cookie to store ID of active stylesheet.

var TWIZZLER_COOKIE = "FSTwizzler";  // Name of cookie to store open/close status of LHN menus

var INCREASE_BTN = "change-lrg";		// ID of font increase button
var DECREASE_BTN = "change-sm";		// ID of font decrease button

var INCREASE_BTN_TITLE = "";		// Tooltip for the font increase button
var DECREASE_BTN_TITLE = "";		// Tooltip for the font decrease button

// as necessary, buttons can be 'frozen' to keep them in a certain state
// and to prevent rollovers from affecting them until unfrozen
var FROZEN_CLASS = "buttonImg frozen";		// class for frozen buttons
var UNFROZEN_CLASS = "buttonImg";			// class for unfrozen buttons



/*------------------------------------------------------------
*  Function:  getObject
*  
*  Description:
*  accepts an object reference or a string, returns an object reference
*  
*  Parameters:
*  which	string or object	ID of HTML dom entity, or reference to an object
*  
*  Return:
*  obj		object				reference to named or passed DOM object
*------------------------------------------------------------*/
function getObject(which) {
	var obj;
	if (typeof(which) == "object") {
		obj = which;
	} else if (typeof(which) == "string") {
		obj = document.getElementById(which);
	} else {
		obj = null;
	}
	
	return obj;
}



/*------------------------------------------------------------
*  Function:  loadStyles
*  
*  Description:
*  Stores available stylesheets (with titles) in an array
*  for tracking.  Styles are ordered in alphabetical order
*  by ID.
*  
*  Parameters:
*  none
*  
*  Return:
*  none
*------------------------------------------------------------*/
function loadStyles() {
	var linkElements = document.getElementsByTagName("link");
	
	for (var i = 0; i < linkElements.length; i++) {
	
		var linkElement = linkElements[i];
		
		if (linkElement.getAttribute("rel").indexOf("style") != -1
			&& linkElement.getAttribute("title")) {
			styles[styles.length] = linkElement;
		}
	}
	
	styles.sort(compareById);
}



/*------------------------------------------------------------
*  Function:  compareById
*  
*  Description:
*  Comparison function for alphabetic comparison of DOM
*  elements by ID.
*  
*  Parameters:
*  elementA		element		First element to compare
*  elementB		element		Second element to compare
*  
*  Return:
*  integer		-1 (b is greater), 0 (a == b), 1 (a is greater)
*------------------------------------------------------------*/
function compareById(elementA, elementB) {
	if (elementA.id > elementB.id) {
		return 1;
	} else if (elementA.id == elementB.id) {
		return 0;
	} else {
		return -1;
	}
}



/*------------------------------------------------------------
*  Function:  getStyleFromCookie
*  
*  Description:
*  Determines the stylesheet to activate from a cookie.  Assumes
*  styles have been loaded into global style array.  The ID of
*  the stylesheet is used to identify the style.
*  
*  Parameters:
*  none
*  
*  Return:
*  integer		The index of the stylesheet in styles array.
*------------------------------------------------------------*/
function getStyleFromCookie() {
	var style = getCookie(STYLE_COOKIE);
	
	for (var i = 0; i < styles.length; i++) {
		if (styles[i].id == style) {
			return i;
		}
	}

	return DEFAULT_STYLE;
}



/*------------------------------------------------------------
*  Function:  setStyleInCookie
*  
*  Description:
*  Saves the ID of the active stylesheet in a cookie.  Assumes
*  styles have been loaded into global style array.
*  
*  Parameters:
*  styleNum		integer		The index of the style in the global
*							styles array.
*  
*  Return:
*  none
*------------------------------------------------------------*/
function setStyleInCookie(styleNum) {
	var exp = new Date();			// expiration date
	
	/* begin BUGFIX ID 25 */
	/* Set expiration for next year */
	exp.setTime(exp.getTime() + (365 * 24 * 60 * 60 * 1000));
	
	if (styles[styleNum]) {
		setCookie(STYLE_COOKIE, styles[styleNum].id, exp, "/");
	}
	/* end BUGFIX ID 25 */
}



/*------------------------------------------------------------
*  Function:  getActiveStyle
*  
*  Description:
*  Determines the active stylesheet.  Assumes styles have been
*  loaded into global style array.
*  
*  Parameters:
*  none
*  
*  Return:
*  integer		The index of the active stylesheet in styles array.
*------------------------------------------------------------*/
function getActiveStyle() {
	for (var i = 0; i < styles.length; i++) {
		if (!styles[i].disabled) {
			return i;
		}
	}

	return DEFAULT_STYLE;
}



/*------------------------------------------------------------
*  Function:  setActiveStyle
*  
*  Description:
*  Sets the active stylesheet.  Assumes styles have been
*  loaded into global style array.
*  
*  Parameters:
*  styleNum		integer		The index of the style in the global
*							styles array to activate.
*  
*  Return:
*  none
*------------------------------------------------------------*/
function setActiveStyle(styleNum) {
	// MSIE does not handle disabled property correctly.
	// We must manually set this property for all stylesheets.
	// Also, MSIE does not update the styles unless
	// there is a change.  So we must first disable the active
	// stylesheet.
	for (var i = 0; i < styles.length; i++) {
		styles[i].disabled = true;
	}

	// Now we can enable the stylsheet we want.
	styles[styleNum].disabled = false;
}



/*------------------------------------------------------------
*  Function:  getNextStyle
*  
*  Description:
*  Determines the index of the next style in the style array.  Assumes
*  styles have been loaded into global style array.  Last style
*  returns itself.
*  
*  Parameters:
*  none
*  
*  Return:
*  integer		Index of the next style
*------------------------------------------------------------*/
function getNextStyle() {
	var styleNum = getActiveStyle();
	if (styleNum >= (styles.length - 1)) {
		return styleNum;
	} else {
		return ++styleNum;
	}
}



/*------------------------------------------------------------
*  Function:  getPrevStyle
*  
*  Description:
*  Determines the index of the previous style in the style array.  Assumes
*  styles have been loaded into global style array.  First style
*  returns itself.
*  
*  Parameters:
*  none
*  
*  Return:
*  integer		Index of the previous style
*------------------------------------------------------------*/
function getPrevStyle() {
	var styleNum = getActiveStyle();
	return (styleNum > 0) ? --styleNum : 0;
}



/*------------------------------------------------------------
*  Function:  defaultFont
*  
*  Description:
*  Sets font to default size
*  
*  Parameters:
*  none
*  
*  Return:
*  false
*------------------------------------------------------------*/
function defaultFont() {
	setActiveStyle(1);
	setFontControls(1);
	setStyleInCookie(1);
	return false;
}



/*------------------------------------------------------------
*  Function:  increaseFont
*  
*  Description:
*  Increases the size of the fonts in the main content area of
*  the page by changing stylesheets.  Assumes styles have been
*  loaded into global style array.  Also assumes styles are
*  ordered in style array from smallest font to largest font.
*  Saves the active style in a cookie.
*  
*  Parameters:
*  none
*  
*  Return:
*  false
*------------------------------------------------------------*/
function increaseFont() {
	var styleNum = getNextStyle();
	setActiveStyle(styleNum);
	setFontControls(styleNum);
	setStyleInCookie(styleNum);
	return false;
}



/*------------------------------------------------------------
*  Function:  decreaseFont
*  
*  Description:
*  Decreases the size of the fonts in the main content area of
*  the page by changing stylesheets.  Assumes styles have been
*  loaded into global style array.  Also assumes styles are
*  ordered in style array from smallest font to largest font.
*  Saves the active style in a cookie.
*  
*  Parameters:
*  none
*  
*  Return:
*  false
*------------------------------------------------------------*/
function decreaseFont() {
	var styleNum = getPrevStyle();
	setActiveStyle(styleNum);
	setFontControls(styleNum);
	setStyleInCookie(styleNum);
	return false;
}



/*------------------------------------------------------------
*  Function:  setFontControls
*  
*  Description:
*  Enables and disables font controls on the page.  Assumes
*  styles have been loaded into global style array.  Also
*  assumes styles are ordered in style array from smallest to
*  largest font.  If smallest font is enabled, disables the
*  decrease font control.  If largest font is enabled, disables
*  the increase font control.
*  
*  Parameters:
*  styleNum		integer		The index of the style in the global
*							styles array.
*  
*  Return:
*  none
*------------------------------------------------------------*/
function setFontControls(styleNum) {
	
	var increaseFontBtn = getObject(INCREASE_BTN);
	var decreaseFontBtn = getObject(DECREASE_BTN);
	
	if (increaseFontBtn) {
		if (styleNum >= (styles.length - 1)) {
			disableButton(increaseFontBtn);
		} else {
			enableButton(increaseFontBtn, INCREASE_BTN_TITLE);
		}
	}
	
	if (decreaseFontBtn) {
		if (styleNum <= 0) {
			disableButton(decreaseFontBtn);
		} else {
			enableButton(decreaseFontBtn, DECREASE_BTN_TITLE);
		}
	}
}



/*------------------------------------------------------------
*  Function:  disableButton
*  
*  Description:
*  Disables a button element.  For use with font controls.
*  
*  Parameters:
*  button	object	The button to disable
*  
*  Return:
*  none
*------------------------------------------------------------*/
function disableButton(button) {
	//imgSwap(button.id,'disabled');			// Display disabled image
	//rollovers[button.id].frozen = true;		// Turn off rollovers
	button.className = FROZEN_CLASS;			// Set disabled class
	//button.title = "";						// Remove tooltip
}



/*------------------------------------------------------------
*  Function:  enableButton
*  
*  Description:
*  Enables a button element.  For use with font controls.
*  
*  Parameters:
*  button	object	The button to enable
*  titleStr	string	The tooltip text of the button
*  
*  Return:
*  none
*------------------------------------------------------------*/
function enableButton(button, titleStr) {
	//rollovers[button.id].frozen = false;	// Turn on rollovers
	//imgOff(button.id);						// Display off image
	button.className = UNFROZEN_CLASS;		// Set enabled class
	//button.title = titleStr;				// Set tooltip
}

/*=========================================================================================
	end font resize library
==========================================================================================*/