
/* YLEISESSÄ KÄYTÖSSÄ OLEVIA APUMUUTTUJIA */
var ua = navigator.userAgent.toLowerCase();

// selain
ie = ((ua.indexOf('msie') != -1) && (ua.indexOf('opera') == -1) && (ua.indexOf('webtv') == -1));
//ie = ua.indexOf("msie") >= 0 ? true : false;
ff = ua.indexOf("gecko") >= 0 ? true : false;
op = ua.indexOf("opera") >= 0 ? true : false;

// selaimen versio
if (ie) browser_ver = parseFloat(ua.split("msie")[1]);
if (ff) browser_ver = parseFloat(ua.replace(/\//g,"").split("firefox")[1]); //replace / merkeille koska useragentissa on yleensä firefox/x.x.x.x
if (op) browser_ver = parseFloat(ua.replace(/\//g,"").split("opera")[1]);  //replace / merkeille koska useragentissa on yleensä opera/x.x.x.x

// info selainikkunasta
function getBrowserWidth() { return window.innerWidth ? window.innerWidth : document.documentElement.offsetWidth; }
function getBrowserHeight() { return window.innerHeight ? window.innerHeight : document.documentElement.offsetHeight; }
function getBrowserXScroll() { return document.documentElement.scrollLeft; }
function getBrowserYScroll() { return document.documentElement.scrollTop; }

// without scrollbars - scrollbarit ovat yleensä 16-17px paksuisia, mutta käytetään 20px varmuudeksi
function getBrowserHeightWS() { return getBrowserHeight() - 20; }
function getBrowserWidthWS() { return getBrowserWidth() - 20; }

function trim(stringToTrim)		{ return stringToTrim.replace(/^\s+|\s+$/g,""); }
function ltrim(stringToTrim)	{ return stringToTrim.replace(/^\s+/,""); }
function rtrim(stringToTrim)	{ return stringToTrim.replace(/\s+$/,""); }
function cdbl(string_to_convert) {
	string_to_convert = string_to_convert.replace(" ","").replace(/\s+/g,"");
	if (trim(string_to_convert) == "") {
		return 0;
	} else {
		parsed = parseFloat(string_to_convert.replace(",","."))
		if (isNaN(parsed) == true) { 
			return 0;
		} else {
			return parsed;
		}
	}
}


function openWindow(urli,leveys,korkeus) {
	scrn_x = window.screen.width/2;
	scrn_y = window.screen.height/2;
	x = scrn_x - leveys/2 - 3;
	y = scrn_y - korkeus/2 - 3;

	random_id = Math.floor(Math.random()*100+1);

	newWindow2 = window.open(urli,'newWindow2'+random_id,'left='+x+',top='+y+',height='+korkeus+',width='+leveys+',resizable=yes,scrollbars=yes');
	setTimeout('newWindow2.focus()',250);
}

function upload(urli) {
	openWindow(urli,350,190);
}

function changelang(langid) {
	document.forms[0].changelanguage.value = langid;
	document.forms[0].submit();
}

function sort(sort, order) {
	document.form1.sort.value = sort;
	document.form1.order.value = order;
	document.form1.submit();
}


	var dropdownhovering = false;
	var currenthover = null;

	function showdropdown(id) {
		var d = document.getElementById(id);
		if (d) {
			// hide previous if there is one
			if(currenthover != null) { currenthover.style.display = ""; /* currenthover.style.visibility = "hidden"; */ }

			// show dropdown
			currenthover = d;
			dropdownhovering = true;
			//d.style.visibility = "visible";
			if (ie) { d.style.marginTop = "51px"; }
			d.style.display = "";
		}
	}
	
	function dd_mouseout() {
		dropdownhovering = false;
		setTimeout("hidedropdown()",1000);
	}
	
	function hidedropdown() {
		if(dropdownhovering == false) {
			if (currenthover != null) {
				//currenthover.style.visibility = "hidden";
				currenthover.style.display='none';
			}
			dropdownhovering = false;
			currenthover = null;
		}
	}

/*
      __    __      __    ____       ___      _
     /  \  /  \    /  \  |    \     / _ \    | |
    / /\ \/ /\ \  / /\ \ | |\  |   / /_\ \   | |
   / /  \  /  \ \ \ \/ / | |/  |  / //_\\ \  | |__
  /_/    \/    \_\ \__/  |____/  /_/     \_\ |____|

*/

/* MODAL IKKUNAN APUMUUTTUJAT */
var modal_is_active = false;

/* PIKAFUNKTIOT */
function confrm(confirm_text,confirm_function,cancel_function) {
	conf_function = (confirm_function == null) ? "" : confirm_function;
	canc_function = (cancel_function == null) ? "" : cancel_function;
	modal_dialogbox(confirm_text,"OK",conf_function,"Cancel",canc_function,"","",1);
}
function confrm_ync(confirm_text,yes_function,no_function,cancel_function) {
	yes_func = (yes_function == null) ? "" : yes_function;
	no_func = (no_function == null) ? "" : no_function;
	cancel_func = (cancel_function == null) ? "" : cancel_function;
	modal_dialogbox(confirm_text,"Yes",yes_func,"Cancel",cancel_func,"No",no_func,1);
}
function msgbox(alert_text,alert_function) {
	al_function = (alert_function == null) ? "" : alert_function;
	modal_dialogbox(alert_text,"OK",al_function,"","","","",2);
}

function hide_modal_dialogbox() {
	// tuhoa canvas
	temp1 = document.getElementById('modal_canvas');
	try {
		document.body.removeChild(temp1);
		temp1 = null;
	} catch(e) {}
	
	// tuhoa confirm div
	temp2 = document.getElementById('modal_container');
	try {
		document.body.removeChild(temp2);
		temp2 = null;
	} catch(e) {}
	modal_is_active = false;
}

/*
content_text = selitys-/kysymysteksti
okname = vasemman napin teksti
okfunc = vasemman napin onclick
cancelname = oikean napin teksti
cancelfunc = oikean napin onclick (null näyttäisi kelpaavan oletuscanceliksi) TAI TYHJÄ STRING!!
abname = keskimmäisen napin teksti
abfunc = keskimmäisen napin onclick
tyyppi = 1: kysymys, 2: huomio, oletuksena kysymys
*/
function modal_dialogbox(content_text,okname,okfunc,cancelname,cancelfunc,abname,abfunc,tyyppi) {
	//msgbox(document.body + document.readyState);
	// Pikakorjaus, heureka kaatuili IE:llä. 070410, eai
	selfcall = 'modal_dialogbox("' + content_text.replace("'", "\\\'") + '","' + okname.replace("'", "\\\'") + '","' + okfunc.replace("'", "\\\'") + '","' + cancelname.replace("'", "\\\'") + '","' + cancelfunc.replace("'", "\\\'") + '","' + abname.replace("'", "\\\'") + '","' + abfunc.replace("'", "\\\'") + '",' + tyyppi + ');';

	if (modal_is_active == true || (ie && document.readyState != 'complete') || (op && document.readyState && (document.readyState != 'loaded' && document.readyState != 'complete'))) {
		// Jos modal ikkuna on jo auki niin kokeillaan 0.3 sekunnin päästä uudestaan
		setTimeout(selfcall,300);
	} else {
		// Asetetaan modaali aktiiviseksi, jotta toinen modaali ei pääse aukeamaan
		modal_is_active = true;
		
		var closingfunction = "hide_modal_dialogbox();";
		
		//add closing function to all of onclick functions
		//okfunc = closingfunction + okfunc.replace(/\'/g,"\\'") + "');";
		okfunction = closingfunction + okfunc;
		abfunction = closingfunction + abfunc;
		cancelfunction = closingfunction + cancelfunc;
		
		// ruudun pimentävä divi
		var canvas = document.createElement('div');
		if (ie && browser_ver < 7) {
			canvas.className = 'modal_canvas_ie6';
		} else {
			canvas.className = 'modal_canvas';
			//canvas.style.top = document.documentElement.scrollTop + "px";
			//canvas.style.left = document.documentElement.scrollLeft + "px";
		}
		canvas.id = 'modal_canvas';
		document.body.appendChild(canvas);
		
		// Luo dynaamisesti iframen IE6:sta varten, jotta selectit eivät paista läpi
		
		// uloin containeri "popupille"
		var container = document.createElement('div');
		if (ie && browser_ver < 7)
			container.className = 'modal_container_ie6';
		else
			container.className = 'modal_container';
		container.id = 'modal_container';
		document.body.appendChild(container);

		// toinen containeri ulkoasun takia
		var bgcontent = document.createElement('div');
		bgcontent.id = 'modal_bgcontent';
		bgcontent.className = 'modal_bgcontent';
		container.appendChild(bgcontent);
		
		// sisältö, jossa teksti
		var content = document.createElement('div');
		switch(tyyppi) {
			case 1:
				content.className = 'modal_confirm';
				break
			case 2:
				content.className = 'modal_alert';
				break
			default:
				content.className = 'modal_confirm';
		}
		content.id = 'modal_content';
		bgcontent.appendChild(content);
		content.innerHTML = content_text.replace(/\n/gi,"<br>");
	
		// sisältö, jossa napit
		var btns = document.createElement('div');
		btns.id = 'modal_btns';
		btns.className = 'modal_buttons';
	
		// tulostetaan napit vain jos ne on määritelty.
		// Confirm ikkunalle (tyyppi = 1) tulostetaan aina OK ja Cancel napit
		// Alert ikkunalle (tyyppi = 2) tulostetaan aina OK nappi
		if (ie) {
			// IE kadotti globaalin variable scopen kun onclick funktio tulostettiin suoraan
			// .onclick=function(){} metodi toimii
			if (okfunc != "" || tyyppi==1 || tyyppi==2) {
				var okbtn = document.createElement('input');
				okbtn.type = 'button';okbtn.value = okname;okbtn.name = "okbtn";okbtn.id = "okbtn";okbtn.className = "btn";
				okstyle=okbtn.style;okstyle.marginLeft = "5px";okstyle.width = "100px";okstyle.height = "26px";
				okbtn.onclick=function(){eval(okfunction);}
				btns.appendChild(okbtn);
			}
			if (abfunc != "" || abname != "") {
				var abbtn = document.createElement('input');
				abbtn.type = 'button';abbtn.value = abname;abbtn.name = "abbtn";abbtn.id = "abbtn";abbtn.className = "btn";
				abstyle=abbtn.style;abstyle.marginLeft = "5px";abstyle.width = "100px";abstyle.height = "26px";
				abbtn.onclick=function(){eval(abfunction);}
				btns.appendChild(abbtn);
			}
			if (cancelfunc != "" || tyyppi==1) {
				var cancelbtn = document.createElement('input');
				cancelbtn.type = 'button';cancelbtn.value = cancelname;cancelbtn.name = "cancelbtn";cancelbtn.id = "cancelbtn";cancelbtn.className = "btn";
				cancelstyle = cancelbtn.style;cancelstyle.marginLeft = "5px";cancelstyle.width = "100px";cancelstyle.height = "26px";
				cancelbtn.onclick=function(){eval(cancelfunction);}
				btns.appendChild(cancelbtn);
			}
			bgcontent.appendChild(btns);
		} else {
			// Opera, Fox ja muut
			btnsHTML = "";
			if (okfunc != "" || tyyppi==1 || tyyppi==2)	btnsHTML += '<input id="okbtn" onclick="' + okfunction + '" class="btn" type="button" value="' + okname + '" style="margin-left:5px;width:100px;height:26px;">';
			if (abfunc != "" || abname != "")		btnsHTML += '<input id="abbtn" onclick="' + abfunction + '" class="btn" type="button" value="' + abname + '" style="margin-left:5px;width:100px;height:26px;">';
			if (cancelfunc != "" || tyyppi==1)		btnsHTML += '<input id="cancelbtn" onclick="' + cancelfunction + '" class="btn" type="button" value="' + cancelname + '" style="margin-left:5px;width:100px;height:26px;">';
			bgcontent.appendChild(btns);
			btns.innerHTML = btnsHTML;
		}

		
		// show divs
		// keskitys ennen näyttämistä, jottei välähtele
		if (ie && browser_ver < 7) 
			keskita(container,false);
		else 
			keskita(container,true);
		content.style.visibility = 'visible';
		container.style.visibility = 'visible';
		canvas.style.visibility = 'visible';
			
		// alertille ok napin focus
		if (tyyppi==2) document.getElementById("okbtn").focus(true);
		// confirmille aina cancel:lle focus
		if (tyyppi==1) document.getElementById("cancelbtn").focus(true);
	}
}

function modal_link(link_href,link_text) {
	return '<div class="modal_link"><a href="' + link_href + '">' + link_text + '</a></div>';
}



function keskita(kesk_ob,positioning) {
	ikkunakorkeus = ie ? document.documentElement.clientHeight : window.innerHeight;
	ikkunaleveys = ie ? document.documentElement.clientWidth : window.innerWidth;

	omaleveys = kesk_ob.clientWidth;
	omakorkeus = kesk_ob.clientHeight;

	//msgbox("omaleveys:" + omaleveys + "\n omakorkeus:" + omakorkeus + "\nReadyState:");

	offset_height = (ikkunakorkeus / 2) - (omakorkeus / 2)
	if (offset_height < 0) offset_height = 0;
	offset_width = (ikkunaleveys / 2) - (omaleveys / 2)
	if (offset_width < 0) offset_width = 0;
				
	position_fixed = positioning ? true : false;
	if (kesk_ob.style && kesk_ob.style.position == 'fixed')
		position_fixed = true;
	else {
	}
		
	if (position_fixed) {
		kesk_ob.style.top = offset_height + "px";
		kesk_ob.style.left = offset_width + "px";
	} else {
		kesk_ob.style.top = (document.documentElement.scrollTop + offset_height) + "px";
		kesk_ob.style.left = (document.documentElement.scrollLeft + offset_width) + "px";
	}
}
function keskita2(kesk_ob,positioning,ownwidth,ownheight) {
	ikkunakorkeus = ie ? document.documentElement.clientHeight : window.innerHeight;
	ikkunaleveys = ie ? document.documentElement.clientWidth : window.innerWidth;

	omaleveys = ownwidth;
	omakorkeus = ownheight;

	offset_height = (ikkunakorkeus / 2) - (omakorkeus / 2)
	if (offset_height < 0) offset_height = 0;
	offset_width = (ikkunaleveys / 2) - (omaleveys / 2)
	if (offset_width < 0) offset_width = 0;
				
	position_fixed = positioning ? true : false;
	if (kesk_ob.style && kesk_ob.style.position == 'fixed')
		position_fixed = true;
	else {
	}
		
	if (position_fixed) {
		kesk_ob.style.top = offset_height + "px";
		kesk_ob.style.left = offset_width + "px";
	} else {
		kesk_ob.style.top = (document.documentElement.scrollTop + offset_height) + "px";
		kesk_ob.style.left = (document.documentElement.scrollLeft + offset_width) + "px";
	}
}

