
function search(value){
if (value != "") document.f.submit();
}
function mapAreaSelect(area){
document.f.a.value=area;
}
function win(x,y){
ext=window.open("","win","width="+x+",height="+y+",toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0");
ext.focus();
}
function w(u){
id=(new Date()).getTime();
eval("page"+id+"=window.open(u,'"+id+"','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=400,height=300');");
}
function w2(u){
id=(new Date()).getTime();
eval("page"+id+"=window.open(u,'"+id+"','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=400,height=300');");
}
function printpage(lang){
if (window.print) {
window.focus();
window.print(false,this.pageNum, this.numPages,false);
} else {
if(lang = "en") {
alert("Your browser does not support javascript printing. Print page by clicking browser's print icon");
}
else {
alert("Selaimesi ei tue javascript-tulostusta. KÃ¤ytÃ¤ selaimen omaa tulostin-ikonia.");
}
}
}

var menuHide=0;
function findPosX(obj){
var curleft = 0;
if (obj.offsetParent) {
while (obj.offsetParent) {
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
} else if (obj.x) curleft += obj.x;
return curleft;
}
function findPosY(obj){
var curtop = 0;
if (obj.offsetParent) {
while (obj.offsetParent) {
curtop += obj.offsetTop
obj = obj.offsetParent;
}
} else if (obj.y) curtop += obj.y;
return curtop;
}
function showMenu(){
var posY,posX;
posY=findPosY(document.getElementById('li_tuotteet'))+20;
posX=findPosX(document.getElementById('li_tuotteet'));
document.getElementById('tuotteet2').style.top=posY+"px";
document.getElementById('tuotteet2').style.left=posX+"px";
document.getElementById('tuotteet2').style.visibility='visible';
menuHide=0;
}
function hideMenu(){
menuHide=1;
window.setTimeout("hide()", 500);
}
function hide(){
if (menuHide==1) {
document.getElementById('tuotteet2').style.visibility='hidden';
}
}
function size(){
if (document.getElementById('main').clientHeight < 300) document.getElementById('main').style.height = 300;
}


function startup() {
document.f.a.value = "";
document.f.l.length = 0;
for (i=0; i<area.length; i++) {
document.f.l.options[i] = new Option(area[i], area[i], false, false);
}

document.f.l.options[0].selected=true;		
areaSelect(ph['1eab']);
}
function areaSelect(value) {
document.f.b.length = 0;
x=0;
selArray = new Array();
if (value == ph['1eab']) {
for (i=0; i<area.length; i++) {
areaTxt = area[i];
for (j=0; j<area[areaTxt].length; j++) {
selArray[x++] = area[areaTxt][j];
}}}else {
for(j=document.f.l.selectedIndex; j <document.f.l.length;j++) {
if(j==0) continue;
var ll = document.f.l.options[j].text;
if(document.f.l.options[j].selected==true) {
for (i=0; i<area[ll].length; i++) {
selArray[x++] = area[ll][i];
}}}}
selArray = selArray.sort(function(x,y){
var a = String(x).toUpperCase();
var b = String(y).toUpperCase();
if (a > b) return 1;
if (a < b) return -1;
return 0;});
// Add first option
document.f.b.options[0] = new Option(selArray[0], selArray[0], false, false);
x=1;
for (i=1; i<selArray.length; i++) {
// do not allow duplicates
if (document.f.b.options[x-1].text != selArray[i]) {
document.f.b.options[x++] = new Option(selArray[i], selArray[i], false, false);
}}}
function bookmark(url, name, lang) {
if( document.all ) { window.external.AddFavorite( url, name);
} else {
alert(ph['baf0']);
}}



// Declare namespace
var slm = new Object();
slm.SectorPopup = new Object();
slm.AreaPopup = new Object();
slm.HelpPopup = new Object();
slm.isIE = function() {
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}();
slm.sectorPopupVisible  = new Boolean();
slm.areaPopupVisible  = new Boolean();

slm.Overlay = new Object();
slm.Overlay.getInstance = function() {
	if(!this._element) {
		this._element = document.createElement("iframe");
		this._element.frameBorder = 0;
		this._element.style.display = "none";
		this._element.style.position = "absolute";
		this._element.style.zIndex = "1000";
		this._element.style.overflow = "auto";
	}
	return this;
}
slm.Overlay.insertUnder = function(element) {
	var tempDisplay = element.style.display;
	element.style.display = "block";
	this._element.style.width = element.offsetWidth + "px";
	this._element.style.height = element.offsetHeight + "px";
	this._element.style.top = getElementPosition(element).top + "px";
	this._element.style.left = getElementPosition(element).left + "px";
	element.style.display = tempDisplay;
	if(!this._inserted) {
		document.body.appendChild(this._element);
		this._inserted = true;
	}
	var el = element.parentNode.removeChild(element);
	document.body.appendChild(el);
	el.style.top = this._element.style.top;
	el.style.left = this._element.style.left;
	el.style.zIndex = parseInt(this._element.style.zIndex) + 1;
	el.style.position = "absolute";
	el.style.margin = "0";
	el.style.overflow = "auto";
}
slm.Overlay.show = function() {
	this._element.style.display = "block";
}
slm.Overlay.hide = function() {
	this._element.style.display = "none";
}
slm.SectorPopup.show = function() {
	slm.AreaPopup.hide();
	var el = document.getElementById("sector-popup");
	var ol = slm.Overlay.getInstance();
	ol.insertUnder(el);
	var sel = document.getElementById("sector-popup").getElementsByTagName("select")[0];
	if(sel.innerHTML == "")
		this.getOptions();
	ol.show();
	el.style.display = "block";
	slm.sectorPopupVisible = true;
}
slm.SectorPopup.hide = function() {
	document.getElementById("sector-popup").style.display = "none";
	slm.Overlay.getInstance().hide();
	slm.sectorPopupVisible = false;
}
slm.SectorPopup.toggle = function() {
	slm.sectorPopupVisible == true ? slm.SectorPopup.hide() : slm.SectorPopup.show();
}
slm.SectorPopup.getOptions = function() {
	var lang = document.getElementById("sector-popup").className;
	slm.Request.sendRequest("/gsec_"+lang+".js", true, this, this._parseSectors);
}
slm.SectorPopup.getSector = function() {
	var lang = document.getElementById("sector-popup").className;
	slm.Request.sendRequest("/gsec_"+lang+".js", true, this, this._parseSectors);
}
slm.SectorPopup._parseSectors = function(r, failed) {
	if(!failed) {
		var options = eval("("+r+")");
		var el = document.getElementById("sector-popup").getElementsByTagName("select")[0];
		var html = "";
		for(var i=0; i<options.length; i++) {
			var opt = document.createElement("option");
			opt.innerHTML = options[i]; 
			opt.value = options[i];
			el.appendChild(opt);
		}
	} else {
		// TODO show notification
	}
}
slm.SectorPopup.save = function() {
	var el = document.getElementById("sector-popup").getElementsByTagName("select")[0];
	var stringval = el.options[el.selectedIndex].value;
	document.getElementById("q").value = stringval.substring(0,stringval.indexOf(" ("));
	document.getElementById("bb").value = 1;
	this.hide();
}
slm.SectorPopup.search = function() {
	slm.SectorPopup.save();
	document.searchform.submit();
}


slm.AreaPopup.show = function() {
	slm.SectorPopup.hide();
	var el = document.getElementById("area-popup");
	var ol = slm.Overlay.getInstance();
	ol.insertUnder(el);
	if(!this.loaded) {
		this.getCityNames();
		this.loaded = true;
	}
	ol.show();
	el.style.display = "block";
	slm.areaPopupVisible = true;
}
slm.AreaPopup.getCityNames = function() {
	slm.Request.sendRequest("cities.js", true, this, this._parseCities);
}
slm.AreaPopup._parseCities = function(r, failed) {
	if(!failed) {
		this.cities = eval("("+r+")");
	} else {
		// TODO show notification
	}
}
slm.AreaPopup.hide = function() {
	document.getElementById("area-popup").style.display = "none";
	slm.Overlay.getInstance().hide();
	slm.areaPopupVisible = false;
}
slm.AreaPopup.toggle = function() {
	slm.areaPopupVisible == true ? slm.AreaPopup.hide() : slm.AreaPopup.show();
}
slm.AreaPopup.save = function() {
	var out = "";
	var map = document.getElementById("slm-areamap");
	var areas = document.getElementById("slm-areas").getElementsByTagName("input");
	for(var i=0; i < areas.length; i++) {
		if(areas[i].checked) {
			if(out.length > 0)
				out += ",";
			out += areas[i].nextSibling.data;
		}
	}
	if(out.length > 0)
		document.getElementById("q2").value = out;
	this.hide();
}
/* This gets called from inside Flash map component */
slm.AreaPopup.selectArea = function(name, value) {
	document.getElementById(name).checked = value;
}

/* This gets called from inside Flash map component */
slm.AreaPopup.hoverArea = function(name) {
	var hover = document.getElementById("area-popup-hover");
	if(!hover) {
		var parent = document.getElementById("area-popup");
		hover = document.createElement("div");
		hover.id = "area-popup-hover";
		hover.style.position = "absolute";
		hover.style.zIndex = "1000";
		hover.style.top = "130px";
		hover.style.left = "30px";
		hover.style.background = "#fff";
		hover.style.border = "2px solid #000";
		hover.style.padding = "10px";
		hover.style.width = "300px";
		parent.appendChild(hover);
	}
	hover.innerHTML = this.cities[name];
	hover.style.display = "";
}

/* This gets called from inside Flash map component */
slm.AreaPopup.hideAreaHover = function(name, value) {
	document.getElementById("area-popup-hover").style.display = "none";
}

/* Calling this will update Flash map component to correspond the state of the checkboxes */
slm.AreaPopup.updateMap = function() {
	var map = document.getElementById("slm-areamap");
	if(slm.isIE) {
		map = document.getElementById("slm-areamap-ie");
	}
	var areas = document.getElementById("slm-areas").getElementsByTagName("input");
	for(var i=0; i < areas.length; i++) {
		map.updateSel(areas[i].id, areas[i].checked);
	}
}


function getElementPosition(el) {
	var offsetTrail = el;
	var offsetLeft = 0;
	var offsetTop = 0;
		
	do {
		if(offsetTrail) {
			offsetLeft += offsetTrail.offsetLeft;
			offsetTop += offsetTrail.offsetTop;
		}
		if(el) el = el.parentNode;
		if(offsetTrail) offsetTrail = offsetTrail.offsetParent;
	} while (offsetTrail || (el && el.nodeName.toLowerCase() != "html"));
	
	return {left:offsetLeft,top:offsetTop};
}


slm.Request = function(){return new Object()}();
		
		slm.Request.newXHRObj = function() {
			var objType = false;
			try {
				objType = new ActiveXObject('Msxml2.XMLHTTP');
			} catch(e) {
				try {
					objType = new ActiveXObject('Microsoft.XMLHTTP');
				} catch(e) {
					objType = new XMLHttpRequest();
				}
			}
			return objType;
		}
		
		slm.Request.sendRequest = function(uri, async, scope, listenerFunc) {
						
			var async = (async)? true : false;
			var req = this.newXHRObj();
			if(req) {
				// Register state change listener
				req.onreadystatechange = function() {
					if(req.readyState == 4) {
						if(async) slm.Request.onResponse(req, scope, listenerFunc);
					}
				}
				
				// Send the request
				try {
					req.open("GET", uri, async);
					req.send(null);
				} catch(e) {
					for(var o in e)
						alert(e[o]);
				}
				if(!async) return slm.Request.onResponse(req, scope, listenerFunc);
			}
			
		}
		
		slm.Request.onResponse = function(req, scope, listenerFunc) {
			if(req.status == 200 || req.status == 0) {
            	if(listenerFunc) return listenerFunc.call(scope, req.responseText);
				return req.responseText;
			} else {
				if(listenerFunc) return listenerFunc.call(scope, req.responseText, true);
				return req.statusText;
			}
		}
slm.HelpPopup.show = function(item) {
	slm.HelpPopup.hide();
	var el = document.getElementById("help-popup");
	el.style.top = -50;// item.offsetTop + 1;
	el.style.left =205; //	item.offsetLeft + 2;
	el.style.width=490;
	//el.style.visibility = "Visible";
	
	//var ol = slm.Overlay.getInstance();
	//ol.insertUnder(el);
	//ol.show();
	
	el.style.display = "block";
}
slm.HelpPopup.hide = function() {
	document.getElementById("help-popup").style.display = "none";
	//slm.Overlay.getInstance().hide();
}
slm.isQ = function() {
	document.getElementById("bb").value = "";
}

function showAreaHelp(hoveritem)
{
hp = document.getElementById("hoverpopup");

// Set position of hover-over popup
hp.style.top = hoveritem.offsetTop + 18;
hp.style.left = hoveritem.offsetLeft + 20;

// Set popup to visible
hp.style.visibility = "Visible";
}

function HidePopup()
{
hp = document.getElementById("hoverpopup");
hp.style.visibility = "hidden";
}
var scrollstep=255;
var timerDown = 0;
var timerUp = 0;
function scrollDown(id){
if(parseInt(document.getElementById(id).scrollTop)+scrollstep < parseInt(document.getElementById(id).scrollHeight)) {
document.getElementById(id).scrollTop+=scrollstep;
document.getElementById('snro').innerHTML = parseInt(document.getElementById('snro').innerHTML) + 1;
timerDown=setTimeout("scrollDown('"+id+"')",200);

}

}

function scrollUp(id){
if(parseInt(document.getElementById(id).scrollTop)  > 0) {
document.getElementById(id).scrollTop-=scrollstep;
document.getElementById('snro').innerHTML = parseInt(document.getElementById('snro').innerHTML) -1;
timerUp=setTimeout("scrollUp('"+id+"')",200);
}
}
function tab(e) {
	e.style.backgroundColor = "#e6ecee";
	e.style.textDecoration = "none";
}

function untab(e) {
	e.style.border = "";
	e.style.backgroundColor = "";
	e.style.backgroundImage = "";
	e.style.textDecoration = "";

}
slm.swvisible = new Array();
slm.sw = new Object();
slm.sw.toggle = function(id, idn) {

	if(slm.swvisible[id] == 1) {	
		//untab(document.getElementById(WCDIV + id));
		document.getElementById(WCDIV + id).className = "a_down";
		document.getElementById(WDIV + id).style.display = "none";
		slm.swvisible[id] = 0;
	}
	else {
		ss = gmap.get(id);
		if(ss != null) {
			gmap.collapse(id);
			ss._visible = false;
		}
		//tab(document.getElementById(WCDIV + id));
		document.getElementById(WCDIV + id).className = "a_up";
		document.getElementById(WDIV + id).style.display = "block";
		if(slm.swvisible[id] == null) {
			slm.Request.sendRequest("/ws?f=sw&p1=" + id + "&p2=" + idn, true, this, this._parsesw);
		}
		slm.swvisible[id] = 1;
	}
}
slm.sw._parsesw = function(r, failed) {
	if(!failed) {
		var data = eval("("+r+")");
		var el = document.getElementById("words-div" + data[0]);
		el.innerHTML = data[1];
		
	} else {
		// TODO show notification
	}
}

// sub-tabs
slm.SubTabs = (function(){

	// ID of the element to sub-tab
	var selector = "sub-tabs";

	return {
	
		init : function() {
			var el = document.getElementById(selector);
			var tabs = el.childNodes;
			if(tabs.length == 0)
				return;
			var l = tabs.length;
			var pos = 0;
			var tabContainer = document.createElement("div");
			tabContainer.className = "sub-tabs-container";
			for(var i=l-1; i >= 0; i--) {
				var tab_content = tabs[i];
				if(tab_content.nodeType != 1)
					continue;
				
				var caption = tab_content.id;
				if(!caption)
					continue;
				var open = tab_content.className.indexOf("open") >= 0;
				tab_content.style.display = open? "" : "none";
				if(open)
					tabContainer._current = caption;
				// Create tab caption
				var tab = document.createElement("div");
				tab.innerHTML = caption;
				tab.className = open? "sub-tab sub-tab-open" : "sub-tab";
				tab.id = caption + "-tab";
				tabContainer.appendChild(tab);
				tab.onclick = function() {
					if(this.parentNode._current) {
						var current = document.getElementById(this.parentNode._current);
						current.style.display = "none";
						document.getElementById(this.parentNode._current + "-tab").className = "sub-tab";
					}
					var open = document.getElementById(this.innerHTML);
					open.style.display = "";
					this.className = "sub-tab sub-tab-open";
					this.parentNode._current = this.innerHTML;
				}
			}
			
			el.insertBefore(tabContainer, el.firstChild);
		}
		
	};

})();
