function setupFolioThumbs() { 
	if (document.getElementById) {
		if (document.getElementById("t1")) {
			var anchors = document.getElementById("t1").getElementsByTagName("a");
			for (var i = 0; i < anchors.length; i++) {
				var element = anchors[i];
				addEvent(element, 'mouseover', folioThumbs, false);
				addEvent(element, 'mouseout', folioThumbsOut, false);
			}
		}
	}
}

function folioThumbs(e) {

	var element = getElement(e);
	
	/* get the class of the element */
	var theClassName = element.className;

	// find all links in the sector list with this class too
	var links = document.getElementById("sectorsFrame").getElementsByTagName("a");
	for (var i = 0; i < links.length; i++) {
		var link = links[i];
		if (link.className == theClassName) {
			// if they share the same class name, highlight the text
			link.style.color = '#000';
		}
	}

}

function folioThumbsOut() { 
	// reset style properties of all anchors in sectorFrame
	var links = document.getElementById("sectorsFrame").getElementsByTagName("a");
	for (var i = 0; i < links.length; i++) {
		var link = links[i];
		link.style.color = '';
	}
}

function addEvent(elm, evType, fn, useCapture) {
	// cross-browser event handling for IE5+, NS6 and Mozilla 
	// By Scott Andrew 
	if (elm.addEventListener) { 
		elm.addEventListener(evType, fn, useCapture); 
		return true; 
	} else if (elm.attachEvent) { 
		var r = elm.attachEvent('on' + evType, fn); 
		return r; 
	} else {
		elm['on' + evType] = fn;
	}
}

function getElement(e) {

	var element;
	
	/* cross browser method of finding out who is calling this function */
	// this is the IE way
	if (window.event && window.event.srcElement) {
		element = window.event.srcElement;
	}
	if (e && e.target) {
		element = e.target;
	}
	
	return element;
}

function selectJump(formname, listname) {
	var sel = document.forms[formname].elements[listname];
	var foo = sel.options[sel.selectedIndex].value;
	alert(foo);
	self.location.href=foo;
}

function pop(ShowPage, intwidth, intheight) {

	var leftpos = (screen.width - intwidth) / 2;
	var toppos = (screen.height - intheight) / 2;
	var winprops = 'toolbar=0, scrollbars=1, location=0, statusbars=0, menubar=0, width=' + intwidth + ', height=' + intheight + ', left=' + leftpos + ', top=' + toppos + ', resizable=1';
    var popupwindow = window.open(ShowPage, 'poppage', winprops);

}

/* FORM validation functions */
/* Example use -

<form method="post" action="" onsubmit="return (validateString(this.name, 'Please provide your name.', 1, 200)  && validateEmail(this.email, 'Please supply a valid email address.', 1) && validateList(this.country, 'Please select a country.'));"> 

*/

function validateNumber(field, msg, min, max) {
	if (!min) { min = 0 }
	if (!max) { max = 255 }
	if ( (parseInt(field.value) != field.value) || field.value.length < min || field.value.length > max) {
		alert(msg);
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function validateString(field, msg, min, max) {
	if (!min) { min = 1 }
	if (!max) { max = 65535 }
	if (!field.value || field.value.length < min || field.value.max > max) {
		alert(msg);
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function validateEmail(email, msg, optional) {
	if (!email.value && optional) {
		return true;
	}

	var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	
	if (!re_mail.test(email.value)) {
		alert(msg);
		email.focus();
		email.select();
		return false;
	}
	return true;
}

function validateList(listname, msg) {

	var foo = listname.selectedIndex;

	if (foo == 0 || foo == -1) {
		alert(msg);
		return false;
	}
	
	return true;
	
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function findTarget(e) {
	var target;
	if (window.event && window.event.srcElement) {
		// IE does it differently... stores the event in a window.event object
		target = window.event.srcElement;
	}
	if (e && e.target) {
		target = e.target;
	}
	return target;
}

function toggleLabel(e) {
	var target = findTarget(e);
	// get the element ID
	var targetID = target.getAttribute('id');
	// find all labels
	var labels = document.getElementsByTagName('label');
	for (var i = 0; i < labels.length; i++) {
		label = labels[i];
		if (label.getAttribute('for') == targetID) {
			if (target.checked) {
				// add class
				label.className += ' checked';
			} else {
				// remove class
				label.className = label.className.replace(/\b ?checked\b/,'');
			}
		}
	}
}

function setUpCheckboxLabels() {
	// find all checkboxes
	var checkboxes = document.getElementsByTagName('input');
	for (var i = 0; i < checkboxes.length; i++) {
		checkbox = checkboxes[i];
		if (checkbox.getAttribute('type') == 'checkbox') {
			// attach onclick function
			addEvent(checkbox, 'click', toggleLabel, false);
			
			var targetID = checkbox.getAttribute('id');
			// find all labels
			var labels = document.getElementsByTagName('label');
			for (var x = 0; x < labels.length; x++) {
				label = labels[x];
				if (label.getAttribute('for') == targetID) {
					if (checkbox.checked) {
						// add class
						label.className += ' checked';
					} else {
						// remove class
						label.className = label.className.replace(/checked/g,'');
					}
				}
			}
		}
	}
}

addLoadEvent(setUpCheckboxLabels);
