function imageMouseOver(e) {
	var x;
	// get source element of the event
	if (!e) var e = window.event;
	target = (e.target) ? e.target : e.srcElement;
	if (!target) return;

	if (target.src.indexOf("active") < 0) {
		x = target.src.substring(0, target.src.indexOf(".gif"));
		target.src = x + "_hover.gif";
	}
	return false;
}

/* --------------------------------------------------------
		imageMouseOut
*/
function imageMouseOut(e) {
	var x;
	// get source element of the event
	if (!e) var e = window.event;
	target = (e.target) ? e.target : e.srcElement;
	if (!target) return;

	if (target.src.indexOf("_hover") >= 0) {
		x = target.src.substring(0, target.src.indexOf("_hover.gif"));
		target.src = x + ".gif";
	}
	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;
}

/* -----------------------------------------------------------------
		hide large image (image gallery)
*/
function hideLargeImage() {
	var elm;

	if (!(elm = document.getElementById("zoomLayer"))) return;
	elm.style.visibility = "hidden";

	return false;
}

/* --------------------------------------------------------
		country select dropdown
*/
function countrySelect(e) {
	var x;
	if (!e) var e = window.event;
	var target = (e.target) ? e.target : e.srcElement;
	if (e.type == "submit") {
		x = findChildNodeByName(target, "SELECT")
		location.href = x.options[x.selectedIndex].value;
	} else if (target && target.options[target.selectedIndex].value != "") {
		//location.href = target.options[target.selectedIndex].value

		location.href = target.options[target.selectedIndex].value + location.pathname.substring(14);
	}
	return false;
}

/* --------------------------------------------------------
		share display drop down
*/
function shareDisplay(e) {
	if (!e) var e = window.event;
	var target = (e.target) ? e.target : e.srcElement;
	if (target.options[target.selectedIndex].value == "select") {
		if (elm = document.getElementById("shareDisplayInputDates")) elm.style.display = "block";
	} else {
		if (elm = document.getElementById("shareDisplayInputDates")) elm.style.display = "none";
	}
	return;
}

/* --------------------------------------------------------
		open file in a new window
*/
function openFile(path) {

	imageMouseOut(window.event);

	var newWin = window.open(path, null, "resizeable");
}


/* --------------------------------------------------------
		open file in a new window
*/
function submitMagazineOrder(e) {
	if (!e) var e = window.event;
	var target = (e.target) ? e.target : e.srcElement;

	/*var Magazines = '';
	var Form = '';
	var list;
	var elm;
	var type;

	// Iterate the checkboxes
	for (i = 0; i < target.orderBox.length; i++) {

		if (target.orderBox[i].checked) {
			Magazines += target.orderBox[i].value + '/';
		}
	}

	//if (Magazines == '') {
	if (target.orderBox.length < 1) {
		alert("No magazine choosen");
		return false;
	}

	Form = '&email=' + target.email.value;
	Form += '&name=' + target.name.value;
	Form += '&adress=' + target.adress.value
	Form += '&postalNumber=' + target.postalNumber.value;
	Form += '&city=' + target.city.value;
	Form += '&sendto=' + target.sendto.value;

	location.href = location.pathname + '?mag=' + Magazines + Form;
	return false;*/
}

/* --------------------------------------------------------
		attach event handlers on load
*/
function doOnLoad() {
	var elm, list, x, i, j;

	/*if (elm = document.getElementsByTagName('body')[0])
		body = elm;*/

        /* share info */
	if (elm = document.getElementById("shareInfoPre")) {

		var locale = location.pathname.substring(9);
		locale = locale.substring(0, locale.indexOf('_')).toUpperCase();

		var query = '/opencms/system/modules/com.kmt/resources/ajaxproxy.jsp?url=http://wpy.observer.se/client/kmt/ShareGraph/ShareTickerSmall.aspx?wpymodulecode=SHARETICKER!lang=' + locale;

		//elm.innerHTML = query;
		//alert(query);
		contentRequest(query, 'shareInfoPre', shareQuery);

	}

	if (elm = document.getElementById("shareGraphInfo")) {

		var locale = location.pathname.substring(9);
		locale = locale.substring(0, locale.indexOf('_')).toUpperCase();

		var query = '/opencms/system/modules/com.kmt/resources/ajaxproxy.jsp?url=http://wpy.observer.se/client/kmt/ShareGraph/ShareTicker.aspx?wpymodulecode=SHARETICKER!lang=' + locale;

		//elm.innerHTML = query;
		//alert(query);
		contentRequest(query, 'shareGraphInfo',  shareGraphQuery);

		ShareGraphInit();
	}

	/* menu tabs */
	for (i = 1; i <= 10; i++) {
		if (elm = document.getElementById("menuTab" + i)) {
			elm.onmouseover = imageMouseOver;
			elm.onmouseout = imageMouseOut;
		}
	}

	/* submenu */
	if (list = document.getElementsByTagName('li'))	{
		// try and find a selected subitem in list
		for (i = 1; i <= list.length; i++) {

			elm = list.item( (i - 1) );
			if (elm.className.indexOf('selected') > -1 && elm.id.indexOf("_") > -1) {
				// modify parent elements link element
				var e = elm.parentNode.parentNode.childNodes[0]; //.getElementByTagName('a');
			
				var url = location.pathname;
				if (url.lastIndexOf('index.html') > -1) {
					url = url.substring(0, url.lastIndexOf('/') - 1);
				}

				/*if (url.lastIndexOf('/') >= url.length - 1) {
					url = url.substring(0, url.length - 2);
				}

				e.href = url.substring(0, url.lastIndexOf('/') + 1);*/
				e.href = url;

				e.style.cursor = "pointer";
				break;
			}
		}
	}

	/* site search - submit button */
	if (elm = document.getElementById("siteSearchSubmit")) {
		elm.onmouseover = imageMouseOver;
		elm.onmouseout = imageMouseOut;
	}

	/* news search submit */
	if (elm = document.getElementById("newsSearchSubmit")) {
		elm.onmouseover = imageMouseOver;
		elm.onmouseout = imageMouseOut;
	}

	/* news subscribe submit */
	if (elm = document.getElementById("newsSubscribeSubmit")) {
		elm.onmouseover = function(e) {
			imageMouseOver(e);

			if (!e) var e = window.event;
			target = (e.target) ? e.target : e.srcElement;

			var newElement   = document.createElement("input");
			newElement.type  = "hidden";
			newElement.name  = "sendto";
			newElement.id    = "sendto";
			newElement.value = sendToAdress;

			target.parentNode.appendChild(newElement);
		}

		elm.onmouseout = function(e) {
			imageMouseOut(e);		

			if (!e) var e = window.event;
			target = (e.target) ? e.target : e.srcElement;

			target.parentNode.removeChild( document.getElementById("sendto") );
		}
	}

	if (elm = document.getElementById("formNewsSubscribe")) {
		elm.onsubmit = function(e) {
			if (!e) var e = window.event;
			target = (e.target) ? e.target : e.srcElement;

			var f;
			for (i=1; i <= 10; i++) {
				f = document.getElementById("optionalField" + i + "my");

				if (!f)
					continue;
				else {
					// check mandatory (is field not empty)
					if (f.value.length > 0) {
						f.name = f.name.substring(0, f.name.length - 2);
					}
					else {
						/* Mark mandatory field label that wasn't filled, and information fields */
						f.previousSibling.previousSibling.style.color = '#FF0000';

						var t = document.getElementById("mandatoryFields");
						t.style.color = '#FF0000';

						return false;
					}
				}
			}
			return true;
		}
	}

	/* share display submit */
	if (elm = document.getElementById("shareDisplaySubmit")) {
		elm.onmouseover = imageMouseOver;
		elm.onmouseout = imageMouseOut;
	}

	/* share display submit */
	if (elm = document.getElementById("shareDownloadSubmit")) {
		elm.onmouseover = imageMouseOver;
		elm.onmouseout = imageMouseOut;
	}

	/* general form submit */
	if (elm = document.getElementById("formSubmit")) {
		elm.onmouseover = imageMouseOver;
		elm.onmouseout = imageMouseOut;
	}

	/* emailform submit */
	if (elm = document.getElementById("emailFormSubmit")) {
		elm.onmouseover = function(e) {
			imageMouseOver(e);

			if (!e) var e = window.event;
			target = (e.target) ? e.target : e.srcElement;

			var newElement   = document.createElement("input");
			newElement.type  = "hidden";
			newElement.name  = "sendto";
			newElement.id    = "sendto";
			newElement.value = sendToAdress;

			target.parentNode.appendChild(newElement);
		}

		elm.onmouseout = function(e) {
			imageMouseOut(e);		

			if (!e) var e = window.event;
			target = (e.target) ? e.target : e.srcElement;

			target.parentNode.removeChild( document.getElementById("sendto") );
		}
	}

	/* flags in quick navigation */
	for (i = 1; i <= 10; i++) {
		if (elm = document.getElementById("flag" + i)) {
			elm.onmouseover = imageMouseOver;
			elm.onmouseout = imageMouseOut;
			elm.onchange = countrySelect;
		}
	}

	/* promotion boxes,  */
	for (i = 1; i <= 9; i++) {
		if (elm = document.getElementById("promoBox" + i)) {
			elm.onmouseover = function(e) {
				e = e ? e : window.event;
				target = (e.target) ? e.target : e.srcElement;
				if (target.parentNode.nodeName == "P") {
					target.style.textDecoration = "underline";
					// get first H1 child node
					if (x = findChildNodeByName(this, "H1")) {
						// find the A node
						if (x = findChildNodeByName(x, "A")) {
							x.style.textDecoration = "none";
						}
					}
				}
				if (checkMouseEnter(this, e)) {
					if (this.className.indexOf("type2") >= 0) {
						this.className = "box type2 boxHover clearfix";
					} else {
						this.className = "box boxHover clearfix";
					}
					// get first H1 child node
					if (x = findChildNodeByName(this, "H1")) {
						// find the A node
						if (x = findChildNodeByName(x, "A")) {
							x.style.textDecoration = "underline";
						}
					}
					this.style.cursor = "pointer";
				}
			}
			elm.onmouseout = function(e) {
				e = e ? e : window.event;
				target = (e.target) ? e.target : e.srcElement;
				if (target.parentNode.nodeName == "P") {
					target.style.textDecoration = "none";
					// get first H1 child node
					if (x = findChildNodeByName(this, "H1")) {
						// find the A node
						if (x = findChildNodeByName(x, "A")) {
							x.style.textDecoration = "underline";
						}
					}
				}
				if (checkMouseLeave(this, e)) {
					if (this.className.indexOf("type2") >= 0) {
						this.className = "box type2 clearfix";
					} else {
						this.className = "box clearfix";
					}
					// get first H1 child node
					if (x = findChildNodeByName(this, "H1")) {
						// find the A node
						if (x = findChildNodeByName(x, "A")) {
							x.style.textDecoration = "";
						}
					}
					this.style.cursor = "default";
				}
			}
			// redirect to first url given
			elm.onclick = function() {
				location.href = findChildNodeByName(this, "A");
			}
		}
	}

	/* promotion boxes (home page)  */
	for (i = 1; i <= 9; i++) {
		if (elm = document.getElementById("promoBoxHome" + i)) {
			elm.onmouseover = function(e) {
				e = e ? e : window.event;
				target = (e.target) ? e.target : e.srcElement;
				if (checkMouseEnter(this, e)) {
					if (this.className.indexOf("type2") >= 0) {
						this.className = "box type2 boxHover clearfix";
					} else {
						this.className = "box boxHover clearfix";
					}
				}
			}
			elm.onmouseout = function(e) {
				e = e ? e : window.event;
				target = (e.target) ? e.target : e.srcElement;
				if (checkMouseLeave(this, e)) {
					if (this.className.indexOf("type2") >= 0) {
						this.className = "box type2 clearfix";
					} else {
						this.className = "box clearfix";
					}
				}
			}
		}
	}

	/* promotion boxes (home page global startpage)  */
	for (i = 1; i <= 9; i++) {
		if (elm = document.getElementById("promoBoxHomeStart" + i)) {
			elm.onmouseover = function(e) {
				e = e ? e : window.event;
				target = (e.target) ? e.target : e.srcElement;
				if (checkMouseEnter(this, e)) {
					if (this.className.indexOf("type2") >= 0) {
						this.className = "box type2 boxHover clearfix";
					} else {
						this.className = "box boxHover clearfix";
					}
				}
			}
			elm.onmouseout = function(e) {
				e = e ? e : window.event;
				target = (e.target) ? e.target : e.srcElement;
				if (checkMouseLeave(this, e)) {
					if (this.className.indexOf("type2") >= 0) {
						this.className = "box type2 clearfix";
					} else {
						this.className = "box clearfix";
					}
				}
			}
		}
	}


	/* country select dropdown */
	if (elm = document.getElementById("countrySelect")) {
		//elm.onchange = countrySelect;

		elm.onchange = function(e) {
			//
			var x;
			if (!e) var e = window.event;
			var target = (e.target) ? e.target : e.srcElement;
			if (e.type == "submit") {
				x = findChildNodeByName(target, "SELECT");
				location.href = x.options[x.selectedIndex].value;
			} else if (target && target.options[target.selectedIndex].value != "") {
					location.href = target.options[target.selectedIndex].value;

					//location.href = target.options[target.selectedIndex].value + location.pathname.substring(14);
			}
			return false;
		}
	}

	/* company site select dropdown */
	if (elm = document.getElementById("companysiteSelect")) {
		//elm.onchange = countrySelect;

		elm.onchange = function(e) {
			//
			var x;
			if (!e) var e = window.event;
			var target = (e.target) ? e.target : e.srcElement;
			if (e.type == "submit") {
				x = findChildNodeByName(target, "SELECT");
				location.href = x.options[x.selectedIndex].value;
			} else if (target && target.options[target.selectedIndex].value != "") {
					location.href = target.options[target.selectedIndex].value;

					//location.href = target.options[target.selectedIndex].value + location.pathname.substring(14);
			}
			return false;
		}
	}

	/* share display drop down */
	if (elm = document.getElementById("shareDisplaySelectPeriod")) {
		if (elm.options[elm.selectedIndex].value == "select") {
			if (x = document.getElementById("shareDisplayInputDates")) x.style.display = "block";
		} else {
			if (x = document.getElementById("shareDisplayInputDates")) x.style.display = "none";
		}
		elm.onchange = shareDisplay;
	}

	/* alternate country selector */
	if (elm = document.getElementById("formCountrySelect2")) {
		elm.onsubmit = countrySelect;
	}

	/* contact selector */
/*	if (elm = document.getElementById("formContactSelect")) {
		elm.onsubmit = countrySelect;
	}

	if (elm = document.getElementById("contactSelect")) {
		elm.onchange = function() {
			contentRequest('contactareas.xml', 'contactText', getContact);
		}
	}*/

	/* image gallery functionality */
	if (elm = document.getElementById("thumbnails")) {
		for (i = 0; i < elm.childNodes.length; i++) {
			if (elm.childNodes[i].nodeName == "A") {
				elm.childNodes[i].onclick = showMediumImage;
			}
		}
	}

	if (elm = document.getElementById("zoomButton")) {
		elm.onclick = showLargeImage;
	}

	if (elm = document.getElementById("prevButton")) {
		elm.onclick = showPrevImage;
	}

	if (elm = document.getElementById("nextButton")) {
		elm.onclick = showNextImage;
	}

	if (elm = document.getElementById("thumbnail0")) {
		oldTarget = elm;
		imageCurrentlyShown = elm;
	}

	/* email forms */
	if (elm = document.getElementById("sendto")) {
		sendToAdress = elm.value;

		// remove the element that hold the adress
		elm.parentNode.removeChild(elm);
	}

	/* back button */
	if (elm = document.getElementById("backButton")) {
		elm.onclick = function() {
			history.go(-1);
			return false;
		}
	}

	/* calculate form */
	if (elm = document.getElementById("calculatorForm")) {

		elm.onsubmit = function(e) {
/*			var target;
			if (!e) var e = window.event;
			target = (e.target) ? e.target : e.srcElement;*/

			calculateStocks()

			return false;
		}
	}


	if (elm = document.getElementById("dateSelector")) {
		// get todays stocks by default
		var form = document.forms["calculator"];

		var option = form.dateSelector.options[form.dateSelector.selectedIndex].value; 
		var currency;
		for (i=0; i < form.currency.length; i++) {

			if (form.currency[i].checked) {
				currency = form.currency[i].value;
			}
		}
		var url = 'http://se.yhp.waymaker.net/KMT/new/calc/sharehistoryinc.asp?date=' + option + '!currency=' + currency;
		contentRequest('/opencms/system/modules/com.kmt/resources/ajaxproxy.jsp?url=' + url, null, storeStockValue);

		//get selected dates stockvalue
		elm.onchange = function(e) {
			e = e ? e : window.event;
			target = (e.target) ? e.target : e.srcElement;

			// get todays stocks by default
			var form = document.forms["calculator"];

			var option = form.dateSelector.options[form.dateSelector.selectedIndex].value; 
			var currency;
			for (i=0; i < form.currency.length; i++) {

				if (form.currency[i].checked) {
					currency = form.currency[i].value;
				}
			}
			var url = 'http://se.yhp.waymaker.net/KMT/new/calc/sharehistoryinc.asp?date=' + option + '!currency=' + currency;
			contentRequest('/opencms/system/modules/com.kmt/resources/ajaxproxy.jsp?url=' + url, null, storeStockValue);
		}
	}

	for (i = 1; i <= 10; i++) {
		if (elm = document.getElementById("currency" + i)) {
				// 
				elm.onchange = function(e) {
				e = e ? e : window.event;
				target = (e.target) ? e.target : e.srcElement;

				// get todays stocks by default
				var form = document.forms["calculator"];

				var option = form.dateSelector.options[form.dateSelector.selectedIndex].value; 
				var currency;
				for (i=0; i < form.currency.length; i++) {

					if (form.currency[i].checked) {
						currency = form.currency[i].value;
					}
				}
				var url = 'http://se.yhp.waymaker.net/KMT/new/calc/sharehistoryinc.asp?date=' + option + '!currency=' + currency;
				contentRequest('/opencms/system/modules/com.kmt/resources/ajaxproxy.jsp?url=' + url, null, storeStockValue);
			}
		}
	}


	if (elm = document.getElementById("stocksField")) {
		elm.onkeypress = validateFormNumberInput;
	}

	if (elm = document.getElementById("calcSubmit")) {
		elm.onmouseover = imageMouseOver;
		elm.onmouseout = imageMouseOut;
	}

        if (elm = document.getElementById("orderButtonSubmit")) {
		elm.onmouseover = imageMouseOver;
		elm.onmouseout = imageMouseOut;
	}

        if (elm = document.getElementById("subscribeButtonSubmit")) {
		elm.onmouseover = imageMouseOver;
		elm.onmouseout = imageMouseOut;
	}

        if (elm = document.getElementById("unsubscribeButtonSubmit")) {
		elm.onmouseover = imageMouseOver;
		elm.onmouseout = imageMouseOut;
	}

        if (elm = document.getElementById("subscribe2ButtonSubmit")) {
		elm.onmouseover = imageMouseOver;
		elm.onmouseout = imageMouseOut;
	}

        if (elm = document.getElementById("unsubscribe2ButtonSubmit")) {
		elm.onmouseover = imageMouseOver;
		elm.onmouseout = imageMouseOut;
	}


	/* news magazines download button */
	for (i = 1; i <= 20; i++) {
		if (elm = document.getElementById("dlMag" + i)) {
			elm.onmouseover = function(e) {
				e = e ? e : window.event;
				target = (e.target) ? e.target : e.srcElement;

				if (checkMouseEnter(this, e)) {
					imageMouseOver(e);
					target.style.cursor = "pointer";
				}
			}
			elm.onmouseout = function(e) {
				e = e ? e : window.event;
				target = (e.target) ? e.target : e.srcElement;

				if (checkMouseLeave(this, e)) {
					imageMouseOut(e);
					target.style.cursor = "default";
				}
			}
		}
	}

	/* news order form */
	if (elm = document.getElementById("orderMagForm")) {
		elm.onsubmit = submitMagazineOrder;
	}

	/* news order button */
	if (elm = document.getElementById("orderMagSubmit")) {
		elm.onmouseover = function(e) {
			e = e ? e : window.event;
			target = (e.target) ? e.target : e.srcElement;

			imageMouseOver(e);
			target.style.cursor = "pointer";
		}
		elm.onmouseout = function(e) {
			e = e ? e : window.event;
			target = (e.target) ? e.target : e.srcElement;

			imageMouseOut(e);
			target.style.cursor = "default";
		}
	}

	/* sitemap */
	for (i = 1; i <= 10; i++) {
		if (elm = document.getElementById("sitemapArea" + i)) {

			if (list = elm.getElementsByTagName('li')) {
				var item;
				for (j = 0; j < list.length; j++) {
					item = list.item(j)
					
					item.onclick = sitemap;
				}
			}
		}
	}

	return false;
}

window.onload = doOnLoad;