//
// cookie functions
//

// getCookieVal -- utility called by getCookie
function getCookieVal(offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	
	return unescape(document.cookie.substring(offset, endstr));
}

// getCookie -- retrieve cookie by name
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 "";
}

// setCookie -- store cookie value with optional expiration, path, domain
function setCookie(name, value, expires, path, domain, secure)
{
	document.cookie = name + "=" + escape(value) +
				((expires) ? "; expires=" + expires : "") +
				((path) ? "; path=" + path : "") +
				((domain) ? "; domain=" + domain : "") +
				((secure) ? "; secure" : "");
}

// deleteCookie -- delete cookie by name by setting a past expiration date
function deleteCookie(name, path, domain)
{
	if (getCookie(name) != "")
	{
		document.cookie = name + "=" +
					((path) ? "; path=" + path : "") +
					((domain) ? "; domain=" + domain : "") +
					"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}



//
// font resizing
//

var styles = new Array();			// Global style array for storing all available stylesheets.
var DEFAULT_STYLE = 2;				// Index of default style.
var STYLE_COOKIE = "GenStyle";		// Name of cookie to store ID of active stylesheet.

var INCREASE_BTN = "btn_plusA";		// ID of font increase button
var DECREASE_BTN = "btn_minusA";	// 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


// loadStyles -- load available stylesheets into array
function loadStyles()
{
	styles[0] = $("style_0");		// smallest
	styles[1] = $("style_1");
	styles[2] = $("style_2");		// default
	styles[3] = $("style_3");
	styles[4] = $("style_4");		// largest
}

// getStyleFromCookie -- get desired stylesheet from cookie, or default style
// returns index 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;
}


// setStyleInCookie -- save ID of active stylesheet in cookie
function setStyleInCookie(styleNum) {
	var exp = new Date();			// expiration date
	
	/* Set expiration for next year */
	exp.setTime(exp.getTime() + (365 * 24 * 60 * 60 * 1000));
	
	if (styles[styleNum])
	{
		setCookie(STYLE_COOKIE, styles[styleNum].id, exp, "/");
	}
}

// getActiveStyle -- determine active stylesheet, returns index in styles array
function getActiveStyle() {
	for (var i = 0; i < styles.length; i++)
	{
		if (!styles[i].disabled)
		{
			return i;
		}
	}

	return DEFAULT_STYLE;
}

// setActiveStyle -- set the active style given index into styles array
function setActiveStyle(style)
{
	// 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 stylesheet we want.
	styles[style].disabled = false;
}


// getNextStyle -- return index of next (larger) style in the styles array
// last style returns itself
function getNextStyle()
{
	var style = getActiveStyle();
	return (style >= (styles.length - 1)) ? style : ++style;
}


// getPrevStyle -- return index of previous (smaller) style in the styles array
function getPrevStyle()
{
	var style = getActiveStyle();
	return (style > 0) ? --style : 0;
}


// increaseFont -- increase to next larger font size
// saves new active style in a cookie
function increaseFont()
{
	var style = getNextStyle();
	setActiveStyle(style);
	setFontControls(style);
	setStyleInCookie(style);
	return false;
}


// decreaseFont -- decrease to previouse smaller font size
// saves new active style in a cookie
function decreaseFont()
{
	var style = getPrevStyle();
	setActiveStyle(style);
	setFontControls(style);
	setStyleInCookie(style);
	return false;
}


// setFontControls -- enables/disables font controls on the page
// depending on active style
function setFontControls(style)
{
	
	var increaseFontBtn = $(INCREASE_BTN);
	var decreaseFontBtn = $(DECREASE_BTN);
	
	if (increaseFontBtn)
	{
		if (style >= (styles.length - 1))
		{
			disableButton(increaseFontBtn);
		}
		else
		{
			enableButton(increaseFontBtn, INCREASE_BTN_TITLE);
		}
	}
	
	if (decreaseFontBtn)
	{
		if (style <= 0)
		{
			disableButton(decreaseFontBtn);
		}
		else
		{
			enableButton(decreaseFontBtn, DECREASE_BTN_TITLE);
		}
	}
}


// disableButton -- disable a font control button
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
}


// enableButton -- enable a font control button
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
}

//
// email forms display code
//

var emailFormTop;									// integer used to track position of email form
var emailFormID = "emailform";						// html ID of email form
var emailViewerID = "emailformview";				// html ID of viewer div surrounding email form
var emailFormInterval;								// timeout variable
var emailScrollIncrements = new Array(40,10,3,1);	// array of scroll increments to create an 'ease out' effect. last item should always be '1'
var emailScrollIncrement = 0;						// index variable to step through the above array

// show email form
function emailThisPage(formname, property)
{
	// only run if email form isn't already showing
	if ($(emailViewerID).style.visibility != "inherit") 
	{
		window.scrollTo(0,0);

		// Using prototype.js framework to retrieve confirmation page.
		var ajaxUpdater = new Ajax.Updater(
			$(emailViewerID),
			'/forms/' + formname + '.php',
			{
				evalScripts: true,
				method:		'get',
				onComplete:	gotEmailForm,
				parameters:	"formname=" + escape(formname) + "&property=" + escape(property)
			}
		);
	}
}

// AJAX completion function, called when email form is retrieved
function gotEmailForm() 
{
	//move email form upward out of view
	var e = $(emailFormID);
	emailFormTop =  0 - (e.offsetHeight + 10);
	e.style.top = emailFormTop + "px";

	// line below sets height of viewer div to 10px larger than email form
	// may need to increase this value if error messages are inserted into the form
	// or push it out to a separate function that can be called by the error display function
	var ev = $(emailViewerID)
	ev.style.height = (e.offsetHeight + 10) + "px";
	ev.style.visibility = "inherit"

	scrollEmailForm();
}

// scroll the email form into view using steps defined in emailScrollIncrements
function scrollEmailForm()
{
	var distance = 0 - emailFormTop;

	// if distance is less than 3 x current scroll increment, use next increment
	if (distance < emailScrollIncrements[emailScrollIncrement] * 3)
		emailScrollIncrement++;

	// make sure we don't exceed the bounds
	if (emailScrollIncrement >= emailScrollIncrements.length)
		emailScrollIncrement = emailScrollIncrements.length - 1;

	// Only scroll down if top edge is hidden
	if (emailFormTop < 0)
	{
		emailFormTop += emailScrollIncrements[emailScrollIncrement];

		// make sure we don't go past zero
		if (emailFormTop > 0)
			emailFormTop = 0;

		$(emailFormID).style.top = emailFormTop + "px";

		// Wait to see if flag is reset. Otherwise, keep scrolling
		emailFormInterval = setTimeout("scrollEmailForm()", 10);
	}
	else
	{
		// stop scrolling
		clearTimeout(emailFormInterval);
		emailScrollIncrement = 0;
	}

	if (emailFormTop >= 0)
	{
		Form.focusFirstElement('theform');
	}
}

// hide email form
function hideEmailForm()
{
	$(emailViewerID).style.visibility = "hidden";
}
