/* -----------------------------------------------------------------
		scripts for ESAB
*/

var lastActiveInfoTab = 1;
var ie = navigator.appVersion.indexOf("MSIE") >= 0;

/* -----------------------------------------------------------------
		invoke browser print function
*/
function printWindow() {
	window.print();
	return;
}

/* -----------------------------------------------------------------
		dropdown quick links
*/
function quickProductLink(e) {
	if (!e) var e = window.event;
	var target = (e.target) ? e.target : e.srcElement;
	var value = target.options[target.selectedIndex].value;
	if (value) {
		document.location.href = value;
	}
	return;
}

/* -----------------------------------------------------------------
		jump to URL on submit
*/
function jumpToURL(e) {
	if (!e) var e = window.event;
	var target = (e.target) ? e.target : e.srcElement;
	if (!(elm = document.getElementById(target.id + "Dropdown"))) return;
	var value = elm.options[elm.selectedIndex].value;
	if (value) {
		document.location.href = value;
	}
	return false;
}

/* -----------------------------------------------------------------
		get inner height of browser window
*/
function getWindowInnerHeight() {
	var x;

	if (self.innerHeight) {
		x = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		x = document.documentElement.clientHeight;
	} else if (document.body) {
		x = document.body.clientHeight;
	}
	return x;
}

/* -----------------------------------------------------------------
		get inner width of browser window
*/
function getWindowInnerWidth() {
	var x;

	if (self.innerWidth) {
		x = self.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		x = document.documentElement.clientWidth;
	} else if (document.body) {
		x = document.body.clientWidth;
	}
	return x;
}

/* -----------------------------------------------------------------
		adjusts width of content area so that the footer is always
		at the bottom of screen
*/
var originalContentAreaHeight = 0;
var originalContentAreaWideHeight = 0;

function adjustContentArea() {
	var elm, temp;

	// do this for contentArea
	if (elm = document.getElementById("contentArea")) {
		if (originalContentAreaHeight == 0) originalContentAreaHeight = elm.offsetHeight;
		temp = parseInt(getWindowInnerHeight()) - (ie ? 166 : 191);
		if ((originalContentAreaHeight + 157) < getWindowInnerHeight()) elm.style.height = temp + "px";
		return false;
	}

	// do this for contentAreaWide
	if (elm = document.getElementById("contentAreaWide")) {
		if (document.getElementById("sitemap") && !ie) return;
		if (originalContentAreaWideHeight == 0) originalContentAreaWideHeight = elm.offsetHeight;
		temp = parseInt(getWindowInnerHeight()) - (ie ? 166 : 191);
		if ((originalContentAreaWideHeight + 157) < getWindowInnerHeight()) elm.style.height = temp + "px";
		return false;
	}

	// do this for popupContentArea
	if (elm = document.getElementById("popupContentArea")) {
		if (originalContentAreaHeight == 0) originalContentAreaHeight = elm.offsetHeight;
		temp = parseInt(getWindowInnerHeight()) - (ie ? 40 : 46);
		if ((originalContentAreaHeight) < getWindowInnerHeight()) elm.style.height = temp + "px";
		return false;
	}
	return;
}

/* -----------------------------------------------------------------
		adjusts the width of the menu tabs (fix in Opera)
*/
function adjustMenuTabs() {
	var menuElm, i, tabWidth;
	var menuTabs = new Array;

	if (menuElm = document.getElementById("topMenu")) {
		for (i = 0; i < menuElm.childNodes.length; i++) {
			if (menuElm.childNodes[i].id && menuElm.childNodes[i].id.indexOf("menuItem") >= 0) {
				menuTabs.push(menuElm.childNodes[i].id);
			}
		}
		tabWidth = parseInt(792 / menuTabs.length);
		for (i = 0; i < menuTabs.length; i++) {
			document.getElementById(menuTabs[i]).style.width = tabWidth + "px";
		}
		document.getElementById(menuTabs[0]).style.width = tabWidth + (792 - tabWidth * menuTabs.length) + "px";
	}
	return;
}

/* -----------------------------------------------------------------
		handle tabs on product display page
*/
function infoTabs(e) {
	var target, x;

	// get source element of the event
	if (!e) var e = window.event;
	target = (e.target) ? e.target : e.srcElement;
	if (!target) return;

	if (lastActiveInfoTab != x) {
		// set class on clicked tab to "active"
		target.className = "infoTab active";

		// hide previous product info and set class on tab
		if (elm = document.getElementById("infoTab" + lastActiveInfoTab)) {
			elm.className = "infoTab";
		}
		if (elm = document.getElementById("infoBox" + lastActiveInfoTab)) {
			elm.style.display = "none";
		}

		x = target.id.substring(target.id.length - 1, target.id.length);

		if (elm = document.getElementById("infoBox" + lastActiveInfoTab)) {
			elm.style.display = "none";
		}

		if (elm = document.getElementById("infoBox" + x)) {
			elm.style.display = "block";
		}

		lastActiveInfoTab = x;
		originalContentAreaHeight = 0;
		originalContentAreaWideHeight = 0;
		adjustContentArea();
	}

	return false;
}

/* -----------------------------------------------------------------
		handle tabs on product display page
*/
function dealerTabs(e) {
	var target, x;

	// get source element of the event
	if (!e) var e = window.event;
	target = (e.target) ? e.target : e.srcElement;
	if (!target) return;

	if (lastActiveInfoTab != x) {
		// set class on clicked tab to "active"
		target.className = "infoTab active";

		// hide previous product info and set class on tab
		if (elm = document.getElementById("dealerTab" + lastActiveInfoTab)) {
			elm.className = "infoTab";
		}
		if (elm = document.getElementById("findDealerForm" + lastActiveInfoTab)) {
			elm.style.display = "none";
		}

		x = target.id.substring(target.id.length - 1, target.id.length);

		if (elm = document.getElementById("findDealerForm" + lastActiveInfoTab)) {
			elm.style.display = "none";
		}

		if (elm = document.getElementById("findDealerForm" + x)) {
			elm.style.display = "block";
		}
		if (elm = document.getElementById("findDealerForm" + x)) {
			elm.blur();
		}

		lastActiveInfoTab = x;
		originalContentAreaHeight = 0;
		originalContentAreaWideHeight = 0;
		adjustContentArea();
	}

	return false;
}

/* -----------------------------------------------------------------
		sitemap, expanding and contracting submenus
*/
function sitemap() {
	var elm;

	for (i = 0; i <= 1; i++) {
		if (i == 0) {
			if (!(elm = document.getElementById("sitemapLeft"))) break;
		} else {
			if (!(elm = document.getElementById("sitemapRight"))) break;
		}

		for (j = 0; j < elm.childNodes.length; j++) {
			if (elm.childNodes[j].nodeName == "UL") {
				for (k = 0; k < elm.childNodes[j].childNodes.length; k++) {
					if (elm.childNodes[j].childNodes[k].nodeName == "LI") {
						elm.childNodes[j].childNodes[k].onclick = expandList;
					}
				}
			}
		}
	}
	return false;
}

function expandList(e) {
	if (!e) var e = window.event;
	var target = (e.target) ? e.target : e.srcElement;
	var x = null;

	// nodeName should be either A or IMG
	// if A then do nothing
	if (target.nodeName == "A") return true;

	// if IMG, show child list if one exists
	if (target.nodeName == "IMG") {
		// get parent LI
		elm = target.parentNode;

		// check for UL in child nodes
		if (x = findChildNodeName(elm, "UL")) {
			if (x.className == "expanded") {
				x.className = "";
				x.style.display = "none"
				target.src = "media/list_style_plus.png";
			} else {
				x.className = "expanded";
				x.style.display = "block"
				target.src = "media/list_style_minus.png";
			}
			adjustContentArea();
		}
	}

	return false;
}

function findChildNodeName(elm, name) {
	var i, x;

	for (i = 0; i < elm.childNodes.length; i++) {
		if (elm.childNodes[i].nodeName == name) {
			return elm.childNodes[i];
		}
		if (elm.childNodes[i].hasChildNodes) {
			x = findChildNodeName(elm.childNodes[i], name);
			if (x) {
				return x;
			}
		}
	}
	return null;
}

/* -----------------------------------------------------------------
		show large product image and center it on screen but always
		leave room for the upper menu
*/
function viewLargeImage() {
	var elm, x;
	var imageHeight, imageWidth, windowWidth, windowHeight;

	windowWidth = getWindowInnerWidth();
	windowHeight = getWindowInnerHeight();

	if (!(elm = document.getElementById("largeProductImage"))) return;
	imageWidth = (elm.offsetWidth == 0 ? 365 : elm.offsetWidth);
	imageHeight = (elm.offsetHeight == 0 ? 400 : elm.offsetHeight);

	if (!(elm = document.getElementById("zoomLayer"))) return;
	elm.style.left = parseInt((windowWidth - imageWidth) / 2) + "px";

	x = parseInt((windowHeight - imageHeight) / 2);

	elm.style.top = (x < 145 ? "145px" : x + "px")
	elm.style.visibility = "visible";

	return false;
}

/* -----------------------------------------------------------------
		open search tips popup window
*/
function openSearchTips() {
	window.open("24A_pop_up.html", "ESABPopupWindow", "width=520, height=575, resizable, scrollbars");
	return false;
}

/* -----------------------------------------------------------------
		show event signup form
*/
function showEventSignupForm() {
	var elm;

	if (!(elm = document.getElementById("eventSignupForm"))) return;
	windowWidth = getWindowInnerWidth();
	elm.style.left = parseInt((windowWidth - 400) / 2) + "px";
	elm.style.visibility = "visible";

	return false;
}

/* -----------------------------------------------------------------
		close event signup form
*/
function closeEventSignupForm() {
	var elm;

	if (!(elm = document.getElementById("eventSignupForm"))) return;
	elm.style.visibility = "hidden";

	return false;
}

/* -----------------------------------------------------------------
		hide large image layer
*/
function hideLargeImage() {
	var elm;

	if (!(elm = document.getElementById("zoomLayer"))) return;
	elm.style.visibility = "hidden";

	return false;
}

/* -----------------------------------------------------------------
		execute on page load
*/
function onLoadDo() {
	var i, elm;

	adjustContentArea();
	adjustMenuTabs();
	window.onresize = onResizeDo;

	// onsubmit on select country dropdown on start page
	if (elm = document.getElementById("countrySelect")) elm.onsubmit = jumpToURL;
	// onclick on print icon
	if (elm = document.getElementById("printPage")) elm.onclick = printWindow;
	// onchange in product quick links
	for (i = 1; i <= 8; i++) {
		if (elm = document.getElementById("quickProductLink" + i)) elm.onchange = quickProductLink;
	}
	// onclick on product display tabs
	for (i = 1; i <= 5; i++) {
		if (elm = document.getElementById("infoTab" + i)) elm.onclick = infoTabs;
	}
	// onclick on find a dealer/service center tabs
	for (i = 1; i <= 5; i++) {
		if (elm = document.getElementById("dealerTab" + i)) elm.onclick = dealerTabs;
	}
	// onclick on "view large image"
	if (elm = document.getElementById("viewLargeImage")) elm.onclick = viewLargeImage;
	// onclick on "hide image"
	if (elm = document.getElementById("zoomClose")) elm.onclick = hideLargeImage;
	// onclick on search tips link
	if (elm = document.getElementById("searchTipsLink")) elm.onclick = openSearchTips;
	// event signup link
	if (elm = document.getElementById("eventSignupLink")) elm.onclick = showEventSignupForm;
	// event signup link
	if (elm = document.getElementById("eventSignupCloseButton")) elm.onclick = closeEventSignupForm;
	// onclick on sitemap list elements
	if (document.getElementById("sitemap")) {
		sitemap();
	}
	return;
}

/* -----------------------------------------------------------------
		execute when window is resized
*/
function onResizeDo() {
	adjustContentArea();
	return;
}

/* -----------------------------------------------------------------
		attach onload event
*/


//for IE browsers
if (window.attachEvent) window.attachEvent("onload", onLoadDo);

// for Mozilla browsers
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", onLoadDo, false);
}

// original call --  not working in CommonSpot
window.onload = onLoadDo;



/* -----------------------------------------------------------------
		Select Email on Contact Page
*/

function selectEmail(x) {
	document.theForm.EmailContact.selectedIndex = x;
}

/* -----------------------------------------------------------------
		open email This Page popup window
*/
function emailThis(x,y) {
	//window.open("http://" + y + "/emailThis.cfm?PageId=" + x, "ESABPopupWindow", "width=450, height=525, resizable, scrollbars");
	window.open("/emailThis.cfm?PageId=" + x, "ESABPopupWindow", "width=450, height=525, resizable, scrollbars");
	return false;
}


