// setup.js

// Collapse submenus and set main-menu click handlers to expand submenus.
// After each change, save the new state in a cookie.
// There can be up to 32 expandable nodes per document.

// In Opera 6 and earlier, changing the class of an element after the page is loaded
//  doesn't affect which CSS rules are applied to the element.

var isW3C = (document.getElementById) ? true : false;
var isAll = (document.all) ? true : false;
var navRoot,items;
var nodePrefix = 'm';		// starts ID of expandable menu items, which must have class~="complex"
var nodePattern = new RegExp('^' + nodePrefix + '\\d+$');
var cookieRefused;			// true if the cookie didn't set for whatever reason. Reload page to reset.

function getElem(elemID) {
    return isW3C ? document.getElementById(elemID) : (isAll ? document.all[elemID] : null);
}

// Get a cookie value by name

function getCookie(name) {
	var cookieArray = document.cookie.split('; ');
	for (var i=0; i<cookieArray.length; i++) {
		var binding = cookieArray[i].split('=');
		if (name == binding[0]) return unescape(binding[1]);
	}
	return '';
}

// Set a site-wide cookie value. Sets cookieRefused if the cookie didn't set.

function setCookie(name, value) {
	value = String(value);
	document.cookie = name + '=' + escape(value) + '; path=/';
	cookieRefused = getCookie(name) != value;
}

// mstate holds the menu state: the ith node is expanded if bit i-1 is set

var cookieValue = parseInt(getCookie('mstate'));
var mstate = isNaN(cookieValue) ? 0 : cookieValue;

function toggleState(node) {
	var elemID = node.id;
	var nodeNo = (nodePattern.test(elemID)) ? parseInt(elemID.substring(nodePrefix.length)) : 0;
	var mask = (nodeNo > 0) ? 1 << (nodeNo-1) : 0;
	mstate ^= mask;

	if (mask) with (node) {
		if (mstate & mask) {
			className = className.replace(/collapsed/,'expanded');
		} else {
			className = className.replace(/expanded/,'collapsed');
		}
	}
	if (!cookieRefused) setCookie('mstate', mstate);		// Don't annoy the user
}

// Handle a clisk or keypress event.
// With Mozilla/Firefox a SPACE, RETURN or ENTER keypress on a link also fires a click event to the link
//  as a part of the default action. So the key event needs to be cancelled.

function handleEvent(evt) {
	evt = evt || window.event;
	if (evt) {
		if (evt.type == 'keypress') {
			var key = evt.keyCode || evt.which || 0;
			if ((key != 3) && (key != 13) && (key != 32)) return true;	// only handle ENTER, RETURN, and SPACE
		}
		var elem = evt.currentTarget || this;
		if (elem) {
			elem = elem.parentNode || elem.parentElement;
			toggleState(elem);
			if (evt.preventDefault) evt.preventDefault();
			evt.returnValue = false;
			return false;
		}
	}
	return true;
}

// Install event handlers the <a> elements in nodes with submenus.
// We only want to expand and collapse if an <a> is directly clicked on.
// Note: Safari doesn't like a variable named 'item'.

//	startList = function() {
function startList() {
	var node;
	navRoot = getElem('nav');
	if (navRoot) {
		var i = 1, mask = 1;
		while (node = getElem(nodePrefix + i++)) {
			var link = isW3C && node.getElementsByTagName('A')[0] || isAll && node.children[0];
			if (link) {
				if (link.addEventListener) {
					link.addEventListener('click',handleEvent,false);
					link.addEventListener('keypress',handleEvent,false);
				} else {
					link.onclick = handleEvent;
					link.onkeypress = handleEvent;
				}
					node.className = node.className.replace(/complex/,
										(mstate & mask) ? 'expanded' : 'collapsed');
			}
			mask <<= 1;
		}
	navRoot.className = navRoot.className.replace(/tight/,'loose');
	}
}

// This hasn't been tested and isn't currently used

function collapseAll() {
	if (items) {
		for (var i=0; i<items.length; i++) {
			var node = items[i];
			var isComplexLI = node.tagName && (node.tagName == 'LI') &&
								node.className && node.className.indexOf('complex') >= 0;
			if (isComplexLI) {
				node.className = node.className.replace(/expanded/,'collapsed');
			}
		}
	}
}

// Preload bullet images
// This has the side effect of not showing any bullet-down marker in Opera 7.5 (instead of a disc).

function preload(sources) {
	var dummy = new Array(sources.length);
	for (var i=0; i<dummy.length; i++) {
		dummy[i] = new Image();
		dummy[i].src = '/siteResources/img/' + sources[i];
	}
}

preload(['bullets/bullet0down.gif', 
			'bullets/bullet1up.gif', 'bullets/bullet1down.gif', 
			'bullets/bullet2up.gif', 'bullets/bullet2down.gif', 
			'bullets/bullet3up.gif', 'bullets/bullet3down.gif']);

// Make fixed-position popup window for labels and pronunciation key
// Used by: product pages with label images; functions validate() and vs() below

function lw2(url,tgt,w,h) {
	var winl = (screen.width-w)/2 - 10, wint = (screen.height-h)/2 - 30;
	var	winprops = 'scrollbars,resizable,width=' +w+ ',height=' +h+ ',left=' +winl+ ',top=' +wint;
	var win = window.open(url,tgt,winprops);
//	win.focus();	// for Netscape
	return true;
}

function lw(obj,w,h) {		// obj can be a link or a form
	return lw2('',obj.target,w,h);
}

// Validation functions for the email subscription form in the sidebar

function valid_email(str) {
	var pattern = /^\S+\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/i;
	return pattern.test(str);
}

function strict_email(str) {
	var pattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/i;
	return pattern.test(str);
}

function check(form) {   
	if (form.Email.value.length < 1) {
		alert("Please enter an email address.");
		form.Email.focus();
		return false;
	}
	if (!valid_email(form.Email.value)) {
		alert("That doesn't appear to be a valid email address.");
		form.Email.focus();
		return false;
	}
	if (!strict_email(form.Email.value)) {
		if (confirm("Your email address contains unusual characters.\nUse it anyway?")) {
			return true;
		} else {
			form.Email.focus();
			return false;
		};
	}
	return true;
} // check()

function vs(form,w,h) {
	if (check(form)) {
		lw(form,w,h);
		return true;
	} else
		return false;
} // vs

function vb(baseurl,btn,w,h) {
	if (check(btn.form)) {
		var url = baseurl + '?Name=' + escape(btn.form.Name.value)
				+ '&Email=' + escape(btn.form.Email.value);
		lw2(url,'subPop',w,h);
	}
} // vb

// Keypress handler to intercept return and enter in text fields

function noenter(evt) {
	evt = evt || window.event;
	var key = evt.charCode || evt.which || evt.keyCode || 0;
	return (key!=13) && (key!=3);
} //noenter

