function slideSwap(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;
	}
	
	// make sure we get an anchor tag
	while (element != document.body && element.nodeName.toLowerCase() != 'a') {
		element = element.parentNode;
	}
	
	// get the value of the element
	//var src = element.src;
	// change the src of the main image
	//var mainImage = document.getElementById("imagePlaceholder");
	//mainImage.src = src;
	
	// get the id of the div to toggle (found in the rel attribute of the anchor)
	var toggleDiv = element.getAttribute("rel");
	//alert(toggleDiv);
	
	var toggleDivs = new Array('slide_0', 'slide_1', 'slide_2', 'slide_3');
	for (var i = 0; i < toggleDivs.length; i++) {
		
		var tempToggleDiv = toggleDivs[i];
		if (document.getElementById(tempToggleDiv)) {
			//alert(element);
			if (tempToggleDiv == toggleDiv) {
				document.getElementById(tempToggleDiv).style.display = '';
			} else {
				document.getElementById(tempToggleDiv).style.display = 'none';
			}
		}
	}
	
	// turn off all borders first
	var image_anchors = document.getElementById("slide_selector").getElementsByTagName("a");
	for (var i = 0; i < image_anchors.length; i++) {
		var anchor = image_anchors[i];
		// unhighlight all other anchors
		anchor.className = anchor.className.replace(/\b ?selected\b/,'');
	}
	
	// highlight clicked anchor
	element.className += 'selected';
	
	// cancel default anchor link
	if (window.event) {
		window.event.returnValue = false;
	} else {
		e.preventDefault();
	}
}

function ini_slideSwap() {	
	if (document.getElementById) {
		if (document.getElementById("slide_selector")) {
			var anchors = document.getElementById("slide_selector").getElementsByTagName("a");
			for (var i = 0; i < anchors.length; i++) {
				var element = anchors[i];
				addEvent(element, 'click', slideSwap, false);
				element.onclick = cancelClick; // for the benefit of Safari
			}
		}
	}	
}

if (document.images) {

	var myImages = ["/images/nav_home_on.gif", "/images/nav_why_on.gif", "/images/nav_find_on.gif", "/images/nav_become_on.gif", "/images/nav_building_on.gif", "/images/nav_products_on.gif", "/images/nav_faqs_on.gif", "/images/nav_contact_on.gif", "/images/nav_news_on.gif", "/images/nav_sitemap_on.gif", "/images/button_build_on.gif", "/images/button_findstockist_on.gif", "/images/button_download_on.gif"];
	
	for (i = 0; i < myImages.length; i++) {
		foo = 'image' + i;
		foo = new Image();
		foo.src = myImages[i];
	}
	
}

// toggle visibility

function toggle(targetID) {
	if (document.getElementById) {
		target = document.getElementById(targetID);
		if (target.style.display == "none") {
			target.style.display = "";
		} else {
			target.style.display = "none";
		}
	}
}

function toggleItem(targetID, target2ID) {
	if (document.getElementById) {
		target = document.getElementById(targetID);
		target2 = document.getElementById(target2ID);
		if (target.style.display == "none") {
			target.style.display = "";
			target2.style.backgroundImage = "url('../images/triangle-open.gif')";
		} else {
			target.style.display = "none";
			target2.style.backgroundImage = "url('../images/triangle-close.gif')";
		}
	}
}

function newImage(arg) {	
	if (document.images) {		
		rslt = new Image();		
		rslt.src = arg;		
		return rslt;	
	}
}

function changeImages() {	
	if (document.images && (preloadFlag == true)) {		
		for (var i=0; i<changeImages.arguments.length; i+=2) {			
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];		
		}	
	}
}
	
function goJump(which_form) {
	self.location.href=which_form.jump.options[which_form.jump.selectedIndex].value;
}

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

/*
form validation functions 
combine function calls in an onsubmit handler on the form tag
e.g.  onSubmit="return (validateList(this.contact, 'Please select a contact') && validateString(this.firstname, 'Please provide a surname', 1, 200))"
*/

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 validateCheckbox(boxname, msg) {

	foo = boxname.checked;
	
	if (foo) {
		return true;
	} else {
		alert(msg);
		return false;
	}
	
}

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;
	
}

addLoadEvent(ini_slideSwap);