var test = true;
var Mver = "";
var SelFieldLength=20;

function show(item){
// util
if (test) {
 var a = "";
 var m = arguments.length;
 for(var i = 0; i < m; i++) a += arguments[i] + "\n"; 
 alert(a);
 }
}

function getLangCode(){
// for VERTI 2.3
    var r = "";
    if (typeof mb.mLangcode=="string" ) {
        r = mb.mLangcode;
    }
return r;
}

function getLang(){
// for VERTI 2.3
    var r = "";
    if (typeof mb.mLangkey != "undefined" ) {
        r = mb.mLangkey;
    }
return r;
}

function setLang(code){
// deprecated
if (typeof(ds.document.SESSION)=="object" ) {
 ds.document.SESSION.lang.value=code;
 }
}

function getNext(){
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 r = ds.document.SESSION.next.value;
 if (isMenu() && r.substring(0,1) != "/") r = getMenudir() + r;
 r=r.replace("!",getLang());
 }
return r;
}

function setNext(next){
if (typeof(ds.document.SESSION)=="object" ) {
 ds.document.SESSION.next.value=next;
 }
}

function getQname(){
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 r = ds.document.SESSION.qname.value;
 }
return r;
}

function setQname(code){
if (typeof(ds.document.SESSION)=="object" ) {
//show("setQname", ds.document.SESSION.qname.value,code); 
 ds.document.SESSION.qname.value=code;
 }
}

function getDblist(){
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 r = ds.document.STATFILE.dblist.value;
 }
return r;
}

function getQtlist(){
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 r = ds.document.STATFILE.qtlist.value;
 }
return r;
}

function getCss(){
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 r = ds.document.SESSION.css.value;
 }
return r;
}

function getQlist(){
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 r = ds.document.SESSION.qlist.value;
 }
return r;
}

function setQlist(list){
if (typeof(ds.document.SESSION)=="object" ) {
 ds.document.SESSION.qlist.value=list;
 }
}

function getQlistText(){
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 r = ds.document.SESSION.qlisttext.value;
 }
return (r + ";;").split(";");  
}

function getQlistName(){
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 r = ds.document.SESSION.qlistname.value;
 }
return r;
}

function toFrontpage(sw){
// frontpage is either px or pxadmin frontpage
if (!sw) sw = 1;
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 openFrontpage();
 if (sw) {
		if (sw==1 && isMenu()) {
             if (typeof showMenu != "undefined") showMenu();
             var msel = getHttpItem(mb.location,"mpar");
			 if (msel.length>0) msel = " " + msel;
			 (msel.length>0) ? mb.m_Do(msel) : sel("");
			 }
		// sw=0
		return;
		}
 if (isMenu()) {
        if (typeof showMenu != "undefined") showMenu();
 		sel("");}
 }
}
function openFrontpage() {
//toFrontpage subfn; called directly by mb.setSch()
if (typeof(ds.document.SESSION)!="object" ) return "";
/*
show("openFrontpage","isInlogged=" + isInlogged(),
		"SESSION.frontpage=" + ds.document.SESSION.frontpage.value,
		"SESSION.next=" + ds.document.SESSION.next.value);
 */
r =(isInlogged()) ? ds.document.SESSION.frontpage.value : ds.document.SESSION.next.value;
 if (r.substring(0,1) != "/") r = getMenudir() + r;
 r = r.replace("!",getLang());
 r = setVirtualPath(r);
 if (isInlog()) {
 		var sep = (r.indexOf("?")>-1) ? "&" : "?";
		r += sep + getParmtext1().substring(1);
		}
px.location.href=r;
}

function getListItem(list,key,eq){
// key:ssä ensimmäisenä erotinmerkki
// key:n loppuun "="
var r = "";
var sep = key.substring(0,1);
if (!eq) eq = "=";
if (list.length > 0) {
 list = sep + list;
 var i = list.toUpperCase().indexOf(key.toUpperCase() + eq);
 if (i > -1) {
 		list = list.substring(i);
		r = list.split(sep)[1];
 		}
 }
return r;
}

function dropListItem(list,key) {
// key:ssä ensimmäisenä erotinmerkki
// key:n loppuun "="
var r = list;
if (r.length == 0) return r;
var sep = key.substring(0,1);
list = sep + list + sep;
var i = list.toUpperCase().indexOf(key.toUpperCase() + "=");
if ( i == -1) return r;
r = list.substring(0,i);
list = list.substring(i+1);
if (list.indexOf(sep) == 0) list = list.substring(1);
list = list.substring(list.indexOf(sep));
list = (list.length > 1) ? list.substring(0,list.length-1) : "";
return (r + list).substring(1);
}

function setListItem(list,key,valu) {
// key:ssä ensimmäisenä itemien välinen erotinmerkki
// key-value erotumerkki joko keyn lopussa tai valun alussa
var sep;
list = dropListItem(list,key);
sep  = (list.length > 0) ? key.substring(0,1): ""; 
return key.substring(1) + "=" + valu + sep + list;
}

function isListItem(list,key){
// key:ssä ensimmäisenä erotinmerkki
var r = false;
if (list.length >= key.length -1) {
 list = key.substring(0,1) + list;
 r = list.toUpperCase().indexOf(key.toUpperCase()) > -1;
 }
return r
}

function getListItemKeys(list,sep) {
list = list.split(sep);
if (list.length == 0) return "";
var k,i,l;
k = "";
for (i = 0; i < list.length; i++) {
		l = list[i];
		k += l.substring(0,l.indexOf("=")) + sep;
		}
return k.substr(0,k.length-1);
}

function getNewListItemKey(list,sep, prefix) {
var keys = sep + getListItemKeys(list,sep) + sep;
if (!sep) {alert("getNewListItemKey: separator missing");return;}
if (!prefix) prefix = "";
var seek = true;
var k,i;
for (i = 0; seek; i++) {
		k = sep + prefix + i;
		seek = keys.indexOf(k) > -1;
		}
return k;
}

function setListCol(list,nr,valu,sep) {
valu = valu.substring(1).split(valu.substring(0,1));
var sep1 = sep.substring(0,1);
var sep2 = sep.substring(1,2);
list = list.split(sep1);
var i,j,k,v;
for (i=0 ; i<list.length; i++) {
		k = getItemKey(list[i]);
		v = getItemValue(list[i]).split(sep2);
		v[nr] = (valu.length == 1) ?  valu[0] : valu[i];
		list[i] = k + "=" + v.join(sep2);
		}
return list.join(sep1);		
}


function getItemKey(itm) {
return itm.substring(0,itm.indexOf("="));
}

function getItemValue(itm) {
return itm.substring(1+itm.indexOf("="));
}

function getDbname() {
// Tietokannan näyttönimi
var r = "";
if (typeof(ds.document.STATFILE)=="object" ) {
 r = ds.document.STATFILE.dbnames.value.split(",");
 r = r[getLang()-1];
 }
return r
}

function getQtname() {
// Pikatietokannan näyttönimi
var r = "";
if (typeof(ds.document.STATFILE)=="object" ) {
 r = ds.document.STATFILE.dbquicknames.value;
 r = r.split(",")[getLang()-1];
 }
return r
}

function getDbdir() {
var r = "";
if (typeof(ds.document.STATFILE)=="object" ) {
 r = ds.document.STATFILE.dbdir.value;
 }
return r
}

function getQtdir() {
var r = "";
if (typeof(ds.document.STATFILE)=="object" ) {
 r = ds.document.STATFILE.dbquick.value;
 }
return r
}
function getParmtext1() {
return getParmtext() + getParmtext2();
}

function getParmtext() {
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 r = "?lang=" + ds.document.SESSION.lang.value;
 }
return r
}

function getParmtext2() {
var r = getVarParm();
if (r.length > 0) return r;
if (typeof(ds.document.SESSION)=="object" ) {
 r = "&xu=" + ds.document.SESSION.user.value
	 + "&yp=" + ds.document.SESSION.password.value;
 }
return r
}


function setUser(user) {
if (typeof(ds.document.SESSION)=="object" ) {
 ds.document.SESSION.user.value=user;
 }
}

function setPassword(psw) {
if (typeof(ds.document.SESSION)=="object" ) {
 ds.document.SESSION.password.value=psw;
 }
}

function getCurrstate(){
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 r = ds.document.SESSION.currstate.value;
 }
return r
}

function setCurrstate(code){
if (typeof(ds.document.SESSION)=="object" ) {
 ds.document.SESSION.currstate.value=code;
 }
}


function writeStatFileHidden(doc)
{
if (typeof(ds.document.SESSION)!="object" ) return;
var lang = getLangCode();
var d 	 = getDblist().split(";"); 
var h 	 = d.length;
var i 	 = new Array(0,0);
var k;
for (var j = 0; j < h-1 ; j++) { 
		k = d[j].split(",");
		if (k.length > 2) { 
			 i[0]++; 
			 i[1] += (k[2].indexOf(lang) > -1) ? (k.length>3 && k[3] == "-") ? 0 : 1 : 0 ;	
			 }
		}
		h = (i[0] == 0) ? h : i[1] ;
	 doc.write('<input type="hidden" name="nodb" value="' + h + '">');
}

function writeStatFileList(doc, dir)
{
var r 	 = "";
var lc = getLangCode();
var lang = getLang();
var parmtext = getParmtext();
var dbcase = dir == "db";
var db = (dbcase) ? getDbdir() : getQtdir();
var d = (dbcase) ? getDblist() :getQtlist();
var	dname = (dbcase) ? "" : ""; //getQtname() + '/';
d = d.split(";"); 
var a1 	 = '<li><b><a href="../' + db + '/';
var a2   = '<li><b><a href="" onclick="javascript:top.loginTo(\'Login' + lang;
var b1 	 = '.asp' + parmtext + '">' + dname ;
var b3   = '\');return false;">' + dname ;
var c 	 = ' </a></b></li>\n';
var i,k,w,q;
for (i = 0; i < d.length-1; i++) { 
 k = d[i].split(",");
 w = false;
 if (k.length < 3) { w = true; }
 else { if (k[2].indexOf(lc) > -1 && (k[3].length == 0 || k[3].charAt(0) != '-')) w = true; }
 q = (k.length < 4) ? true : k[3].toUpperCase() != "TRUE";
 // ei kirjautumista 
 if (w && q) doc.write(a1 + k[0] + '/' +  k[0] + b1 + k[1] + c); 
 // kirjautuminen pääryhmään
 if (w && !q) {
	  r = (isMbchecked(k[0])) ? getMbparmtext(k[0]) : getParmtext2();
		r = a2 + '.asp' + parmtext + r + '&xs=mb&key=' + k[0] + '&next=';
		r += '../' + db + '/' + k[0] + '/' + k[0] + '.asp' 
		+ '?key=' + k[0] + '&case=mb&header=' + k[1] + b3 + k[1] + c;
		doc.write(r);}
 }
} 

function getLoginHref(path,url,d, h,lang) {
// virtual path of px application 
// tietokanta db "Database" "Quicktables"
// kirjautumista vaativa päähakemisto d
// hakemiston näyttönimi: h
var parmtext = getParmtext();
var a = '\');return false;"';
var b = (isMbchecked(d)) ? getMbparmtext(d) : getParmtext2();
return'javascript:top.loginTo(\'' + path + '/Dialog/Login' + lang
		  + '.asp' + parmtext + b + '&xs=mb&key=' + d + '&next='
			+ escape(url)	+ '?key=' + d + '&case=mb&header=' + h + '\');';
}

function isLogged(key) {
 return isMbchecked(key) || isInlog();
}

function loginTo(h){
// Statfile-aliohjelma. 
// writeStatFileList kirjoittaa tämän kirjautumista vaativan päähaaran linkkiin 
// asettaa next-kentän arvoksi paluuosoitteen, jota kirjautumissivu käyttää
var i = h.indexOf("&next=");
setNext(h.substring(i + 6));
px.location.href=h;
}

function checkLogin(){
// IncHeader body onload-funktio
// tarkistaa loginin tarpeen, kun tullaan searchpx2- tai view-sivuille
var ok,lh,j,key,item,r,l,s,h,dbl,a2;
h = px.location.href;
if (h.indexOf("searchpx2.asp") == -1 && h.indexOf("view.asp") == -1) return;
l = px.document.links;
s  = "path=../" + getDbdir() + "/";
a2 = "javascript:top.loginTo('Login" + getLang() + ".asp" + getParmtext();
dbl = getDblist();
for (var i = 0; i < l.length; i++) {
	lh=l[i].href;
	ok = lh.indexOf("varval.asp?") > -1 && lh.indexOf(s) > -1;
	if (ok) {	
		 j = lh.indexOf(s);
		 key = lh.substring(j + s.length);
		 key = key.substring(0,key.indexOf("/"));
		 item = getListItem(dbl,";" + key, ",")
		 item = item.split(",");
		 if (item.length > 3){
		 		if (item[3].toUpperCase() == "TRUE") {
					 if (lh.indexOf("&xu=") > -1) lh = lh.substring(0,lh.indexOf("&xu="));
					 if (lh.indexOf("&case=mb") == -1) lh += "&case=mb";  	 
					 if (lh.indexOf("&key=") == -1) lh += "&key=" + key;
					 r = (isMbchecked(key)) ? getMbparmtext(key) : getParmtext2();
					 lh = a2 + r + "&xs=mb" + "&header=" + item[1] + "&next=" + lh + "')";			 
					 }
				l[i].href = lh;		 	 
	 			 //show("checkLogin:key",key,"item",item,"result href:",l[i].href);
				} 
		 } 
	} 
}

function toStatfile(l) {
// Statfile-sivun haku (kielen vaihto, oletuskielisen lataus) 
if (arguments.length > 0) setLang(l);
startDs(getLang());
}

function startDs(lang) {
var x="";
var y ="";
if (typeof(ds.document.SESSION)== "object") {
	 x = ds.document.SESSION.user.value;
	 y = ds.document.SESSION.password.value;
}
var s = getHttpItem(top.location.search,"Msel");
if (s.length>0) s = "&Msel=" + s;
var p = getHttpItem(top.location.search,"Mpar");
if (p.length>0) s += "&Mpar=" + p;
ds.location=ds.location.pathname + "?Lang=" + lang + "&xu=" + x + "&yp=" + y + 
		"&frontfile=" + getFrontfile() + "&isinlogged=" + isInlogged() + s;
}


function setMbcheck(v) {
if (typeof(ds.document.SESSION)=="object" ) {
	if (!v) v = "";
	ds.document.SESSION.mbcheck.value = v;
	}
}

function isMbcheck() {
var h = px.location.href.toString();
if (h.toUpperCase().indexOf("SAVESHOW.ASP") > -1) {h = getSearch();}
var r = h.indexOf("&case=mb") > -1;
return r;
}

function isInlog() {
// vaaditaanko koko kantaan kirjautuminen
var r = false;
if (typeof(ds.document.SESSION)=="object" ) {
  var d = ds.document.SESSION;
	r = d.dbinlog.value.toUpperCase() == "TRUE"; // || isMbcheck();
	}
return r;
}

function isInlogged(){
if (!isInlog()) return true;
var f= ds.document.forms["SESSION"];
return f.isinlogged.value=="true" && f.user.value.length>0;
}


function getVarParm(doc) {
var r,x,y,c,k,v;
r = "";
if (!doc)doc = px.document;
if (doc.forms[0] && doc.forms[0].varparm) {
	 v = doc.forms[0].varparm.value;
	 x = getHttpItem(v,"xu");
	 y = getHttpItem(v,"yp");
	 c = getHttpItem(v,"case");
	 k = getHttpItem(v,"key");
	 if (x.length>0) { r += "&xu=" + x;}
	 if (y.length>0) { r += "&yp=" + y;}
	 if (c.length>0) { r += "&case=" + c;}
	 if (k.length>0) { r += "&key=" + k;}
	 }
return r;
}

function isGraph() {
return typeof(gdoc) == "string";
}

function toGraph() {
// PX-Web graphics (no Dinosoft graph application)
return true;
}

function chkInlog(doc) {
if (isInlog()) {
;	var loc = doc.location.toString();
	if ((!isInlogged()) && 
		 (loc.indexOf("Statfile")>-1 || isMenu() && (loc.indexOf("Frontpage")>-1 || loc.indexOf("PageQtUpd")>-1))) {
	 	 			var f = doc.forms[0].elements;
					if (!(f.length==1 && f[0].onclick.toString().indexOf("history.go(-1)")>-1)) {
 						 ds.document.SESSION.isinlogged.value="true";
						 if (isMenu()) mb.startMenu("");
	  		     }
      } 
  }
}

function setHrefs(hrefs) {
if (typeof(ds.document.SESSION)=="object" ) {
	 ds.document.SESSION.hrefs.value=hrefs.join("!");
	 }
}

function getHrefs() {
var r ="";
if (typeof(ds.document.SESSION)=="object" ) {
	 r = ds.document.SESSION.hrefs.value;
	 }
return r;
}

function Link(ind) {
	var hrefs = getHrefs().split("!");
	if (isGedit() && hrefs[ind].indexOf("Gedit=true") >-1){ 
		 top.mb.saveMsel();
	}
    top.px.location.href = hrefs[ind];
	window.status = ""; 
}

function showLinks(fr) {
// util
// list hrefs of links of document fr
var p,i,max;
if (typeof fr == "undefined")  p = getHrefs().split("!");
else {
    p = "";
    var l = fr.document.links;
    max = l.length;
    for(i = 0;i<max;i++) {
        p += "!" + l[i].href; 
    }
    if (p.length>0) p = p.substring(1).split("!");    
}
var r = ""
max = p.length;
for ( i=0; i<max; i++) r += i + ":" + p[i] + "\n";
	show(r);
}

function setLinkToStatpage(href,form) {
// varparm-kenttä koodataan saveVarval()-metodissa skandien takia
return unescape(href);
}

function toSearchpx() {
// Etsintäsivulle Statfsivulta 
var p = setVirtualPath("/Dialog/Searchpx.asp");
px.location.href = p + getParmtext1();
}

function setInlog(f) {
// Kirjautumistietojen tallennus
var s = px.location.href.toString();
var xu,yp,i,key,l;
if (arguments.length > 0) {
	xu = f.xu.value;
	yp = f.yp.value;
} else {
	xu = getHttpItem(s,"xu");
	yp = getHttpItem(s,"yp");
}
if (s.indexOf("&case=mb") == -1) {
	 // true =  kirjautuminen koko kantaan
	setUser(xu);
	setPassword(yp);
	} else {
	key = getHttpItem(s,"key");
	updMblist(key,xu,yp);
	}
}
function updMblist(key,xy,yp) {
// Password sivun setInlog -apufunktio
var list, l;
l = ";" + key + "=" + xy + "," + yp;
list = getMblist();
l = l + dropListItem(list,";" + key);
setMblist(l);
}

function getMblist() {
var r ="";
if (typeof(ds.document.SESSION)=="object" ) {
	 r = ds.document.SESSION.mblist.value;
	 }
return r;
}

function setMblist(list){
if (typeof(ds.document.SESSION)=="object" ) {
 ds.document.SESSION.mblist.value=list;
 }
}

function isMbchecked(key) {
var list = getMblist();
return isListItem(list,";" + key);
}

function getMblistItem(key) {
var list =getMblist();
return getListItem(list,";" + key);
}

function getMbparmtext(key) {
	var r = getMblistItem(key).split(",");
	return "&xu=" + r[0].substring(1+r[0].indexOf("=")) + "&yp=" + r[1]; 
}


function addLang(h) {
// lisää kieliparametrih href-stringiin
if (h.toUpperCase().indexOf("LANG=") == -1) {
	 var sep = (h.indexOf("?") > -1) ? "&" : "?";
	 h = h + sep + "lang=" + getLang();
	 }
return h;	 
}

function fromLoginpage() {
// Password?.asp ja Login?.asp -sivujen action-funktio
// varmistetaan kieliparametri
var s,parmtext,xu,yp,h;
s="";
if (px.document.form1) {
	f = px.document.form1;
	xu = f.xu.value;
	yp = f.yp.value;
} else {
	s = px.location.href.toString();
	xu = getHttpItem(s,"xu");
	yp = getHttpItem(s,"yp");
}
	 parmtext = "&xu=" + xu + "&yp=" + yp;
	 h = (isMenu()) ? "/Dialog/Frompsw.asp" : getNext();
	 h = addLang(h) + parmtext;
if (h.indexOf("http") == -1) h = setVirtualPath(h);
//show("fromLoginpage",h,(isMenu() && mb.mSel.length == 0)? "hide":"px");
if (isMenu() && mb.mSel.length == 0){
	 px.location.href = h;
	 } 
else {
	 px.location.href = h;
	 }
}

function getMenupars() {
var r = "";
var f = (typeof(mb.document.MENU)=="object" ) ? mb.document.MENU : ds.document.MENU;
if (typeof(f) == "object") {
 r = f.menupars.value;
 }
return r; 
}

function getMenuPage(lst,vers) {
var r = "";
if (!isInlogged()) lst = "";
var f = (typeof(mb.document.MENU)=="object" ) ? mb.document.MENU : ds.document.MENU; 
if (typeof(f) == "object") {
 r = f.menu.value;
 r = r.replace("!",lst);
 if (!vers) vers = "1.15";
 r = r.replace("!",vers);
 }
return r
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "; path=/") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}


function getCookieNames(prefix,ds) {
	var r="";
	var j = prefix.length;
	var k;
  if (arguments.length < 2) {ds = document.cookie;}
	if (ds.charAt(0) != ";") { ds = ";" + ds;}
	while (ds.indexOf("; ") > -1) { 
			ds = ds.replace("; ",";");
			}			
	ds = ds.substring(1).split(";");
	for (var i = 0; i < ds.length; i++) {
			if ( ds[i].length >= j) {
				 if (ds[i].substring(0,j) == prefix) {
				 		k = ds[i].indexOf("=");
						if (k > -1) { 
							  r += (r.length >0) ? ";" + ds[i].substring(0,k) : ds[i].substring(0,k);
						}
				 }  
			}			
	}
	return r;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = dc.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


function setCookie(name, value, expires, path, domain, secure) {
  var r;
	var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "; path=/") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
		if (isOkToSave(curCookie)) {	
			 document.cookie = curCookie;
			 r = true;
			 }
		else {
			 alert("This query is too long to be saved into cookie.");
			 r = false;
			 }
return r;
}

function getNewCookieName(prefix) {
var seek = true;
var i = 0;
var c = document.cookie;
var n = "item" + i;
if (c.length > 0) {
	for (i = 0; seek; i++) {
		n = prefix + i;
		seek = c.indexOf(n + "=") > -1;
	  }
	}	
return n;
}
 
function isOkToSave(cook) {
var r = true;
if (navigator.appName.indexOf("Microsoft") > -1) {
	 if (!(px.location.search.indexOf("ACTION=EDIT")) > -1) {
	 		var l = document.cookie.length;
	 		if ((l + cook.length) > 4000) {
				 r = false;
				 }
	 		}
	 }
return r;	 
} 
 
function saveSearch(doc) {
// doc = statpage; save statpage's action for later use
// save it to ds.data.action
if (isVarval()) setSearch(doc.location.toString()); // isVarval() is always false bacause we don't use Varval.asp anymore
else setSearch(doc.location.protocol + "//" + doc.location.host + 
    doc.location.pathname + "?" + (doc.forms[0].varparm.value.replace("?","")));
}

function getSearch() {
// return ds.data.action
r = "";
if (typeof(ds.document.DATA)=="object" ) {
	 r = ds.document.DATA.action;
	 }
return r;
}

function setSearch(r) {
// save location to ds.data.action
//show("setSearch",r);
if (typeof(ds.document.DATA)=="object" ) {
	 ds.document.DATA.action = r;
	 }
}

function isSearch(doc) {
// Is the current location the same as the previous one?
// statfile check
var r = false;
if (typeof(ds.document.DATA)=="object" ) {
	 var a = ds.document.DATA.action.toUpperCase();
	 if( a.length > 0) {
	 		 a = a.substring(a.indexOf("?"));
	 		 a=a.replace(/ /g,"+");
	 		 a=a.replace(/\%20/g,"+");
			 var b = doc.location.toString();
	 		 b = b.substring(b.indexOf("?"));
	 		 b=b.replace(/ /g,"+");
			 b=b.replace(/\%20/g,"+");
			 b=b.substring(0,a.length).toUpperCase();
			 r = a == b;
			 //show("statfile.js.isSearch()","ds.DATA: " + a,"doc.location: " + b,r);
	     }
	 }
return r;
}

function isVarval() {
// default.asp: mb exists
if ((!isMenu()) && typeof(mb)=="undefined") return true; 
if (px.location.toString().toLowerCase().indexOf("varval.asp")>-1) return true;
if (getSearch().toLowerCase().indexOf("varval")>-1) return true;
return false;
}

function updateLinks(doc) {
// incHeader-skripti: linkkitietojen muokkaus
// gdoc,gappl: graph.js
if (isMenu() && top.scrollTop) top.scrollTop(); // for iframe version
  var max = doc.links.length;
	if (max > 0 ) {
		 var hrefs = new Array(max);
		 var p2 = getParmtext2();
  	     var p1 = getParmtext1();
		 var i,r;
		 var m = isMenu();
		 var isinfo = isInfo();
		 //var gappl = "/graph";
		 var loc = px.location.toString().toLowerCase();
		 var sch = loc.indexOf("searchpx2.asp") > -1;
		 chkInlog(doc);
		 for ( i=0; i<max; i++) {
		 		var pg = gappl + "/Statpage.aspx";
 		 		doc.links[i].href=doc.links[i].href.replace("/Dialog/varval.asp",pg);											 
		 		 r = doc.links[i].href;
				 if (r.toLowerCase().indexOf("statpage.aspx")>-1) {
				 			 if (getPage(doc.location.pathname,5) == "SAVES") {
									r = setVirtualPath(pg) +  "?" + unescape(hide.document.forms[0].varparm.value);
							 		if (r.toUpperCase().indexOf("ACTION")== -1) r = replaceHttpItem(r,"ACTION","ACTION=RETURN");
									if (px.sortVars) px.sortVars();
							 }
				 			 if (getPage(doc.location.pathname,6) == "GRAPHP") {									
							 		if (r.toUpperCase().indexOf("ACTION")== -1) r = replaceHttpItem(r,"ACTION","ACTION=RETURN");
							 } 
							 if (r.indexOf("RETURN")>-1) doc.links[i].href = setLinkToStatpage(r,doc.forms[0]);
				 }	 
				 if (m && isTableDir(doc.links[i])) {
				 			 if (getAction(doc) == "UPDATEPIC") hrefs[i]="javascript:top.toFrontpage(1);";
							 else hrefs[i] = (m) ? "javascript:top.mb.m_Do()" : doc.links[i].href;		
				 }			 							 
		 		 else {
				 			if (m && isSa(doc.links[i])) {
				 			 	 hrefs[i]="javascript:top.mb.m_Back();"; 
				 			}
		 		 			else {
				 					 if (sch) {
									    if (doc.links[i].href.indexOf(getQtdir())>-1 ){
								 				 r = (isGraph() && isQtPicture()) ? gappl + gdoc : "/Dialog/view.asp";
						 				  	 doc.links[i].href=doc.links[i].href.replace("/Dialog/varval.asp",r);
											}																						 
									 }
				 					 hrefs[i] = doc.links[i].href;
									 // isGedit-parametrin asetus
									 if (isGedit()) {
				 				 		 if ((isGraph() && hrefs[i].indexOf(gappl + gdoc) > -1) 
								 		 || (m && hrefs[i].indexOf("/Dialog/varval")>-1))
								  	 		{ hrefs[i] = replaceHttpItem(hrefs[i],"Gedit","Gedit=true");}
									 }
							 		if (hrefs[i].indexOf("&ACTION=SUBMIT") > -1) { hrefs[i]=hrefs[i].replace("&ACTION=SUBMIT","");}
		 		 			 		if (hrefs[i].indexOf("#") < 0 && hrefs[i].indexOf("javascript:")< 0 && hrefs[i].indexOf("top.") < 0) {
			 		 				 	 if (-1 < hrefs[i].indexOf("?")) {
								 				//if (-1 == hrefs[i].indexOf("xu=")) hrefs[i] += p2;
							       }
		 		  		    //else { hrefs[i] += p1; }
						  }
					 }		
				}
		if (doc.links[i].target != null) {
			 var t = doc.links[i].target;		
 	 	 	 if(t.length == 0 || t == "px") {
		 	 doc.links[i].href="javascript:top.Link(" + i + ");";}
		 	 }			
		 }
		 setHrefs(hrefs);
	}
}

function isInfo() {
// true if Dinosoft Info tools are in use
// info tools: Details, Footnote, StatInfo
return isGraph() && !isVarval();
}

function onIncheaderLoad(doc) {
var act;
//checkLogin();
setQueryTimeout();
setIncFooterWidth(doc);
//show("onIncheaderLoad."+getPage(doc.location.pathname,5),"isSearch():" + isSearch(doc),"GetAction(doc):" + getAction(doc),"isVarval():" + isVarval());
switch (getPage(doc.location.pathname,5)) {
	case "PASSW":
	case "LOGIN":
		  doc.forms[0].xu.focus();return;
	case "SEARC": //SEARCHPX2
	case "VIEW":
	case "SAVES": //SAVESHOW
	        if (top.stopWait) top.stopWait();
	        var a = getHttpItem(getSearch(),"ACTION");
			if (top.hideMenu && a.indexOf("SUBMIT")>-1) hideMenu();
	 	  status = "Done";
	 	  updateLinks(doc);
	 	  return;
	case "VARVA": //VARVAL VARVALAGG
	case "STATP": //STATPAGE
	case "KSTAT": //KSTATPAGE
	case "KOHTI": //KSTATPAGE
	 updateLinks(doc);
	 act = getAction(doc);
	 if (act.length == 0 || "UPDATE,UPDATEPIC,SUBMIT,RETURN,EDIT".indexOf(act)==-1) clearSelAndSelcmd();
	 //if (act.length > 0  && "SUBMIT,EDIT".indexOf(act)>-1) restoreForms(doc.forms,"VARVAL");
     if (act !="SUBMIT" && top.stopWait) top.stopWait();
   if (isSearch(doc)) {
	 		switch (act) {
				case "SUBMIT":
						 saveSel(doc);
						 if (isVarval()) varvalSubmit(doc);
						 else {
						    selectDefs(doc,"QUE");
						    if (typeof hide.getQuery == "function") hide.getQuery(hide,"hide");
						 }
						 return;
				/*		 
				case "REMOVE":		 		 
						 if (confirm("Poistetaanko tämä haku?")) {
								 px.location="../Dialog/Selections.asp?Lang=" + getLang() ;
					 			 deleteQuery(getQname());
								 setQname("");
								 px.location.href='../Dialog/Selections.asp?Lang=' + getLang();
								 alert("Haku poistettu");
								 }
							return;
				*/			
				case "UPDATE":
				case "UPDATEPIC":
						 if (isVarval()) doc.main.target="hide";
						 selectDefs(doc,"CMD");
						 return;
				case "RETURN":		 				 
				default:
					 //show("onIncHeaderLoad.selectDefs.QUE." + getAction(doc),"sel=" + getSel(),"isSearch()=" + isSearch(doc));			
					 selectDefs(doc,"QUE");
					 setSearch(replaceHttpItem(getSearch(),"vars",""));
					 setSearch(replaceHttpItem(getSearch(),"ACTION",""));
					 doc.forms[0].action = replaceHttpItem(doc.forms[0].action,"vars","");
					 doc.forms[0].action = replaceHttpItem(doc.forms[0].action,"ACTION","");
					 return;
			}
	  }		
	 else {
	 		switch(getAction(doc)) {	
	 		  case "SUBMIT":
   					 saveSel(doc);
						 if (isVarval()) varvalSubmit(doc);
						 else hide.getQuery(hide,"hide");
						 return;
				case "UPDATE":
				case "UPDATEPIC": 
			  		 if (isVarval()) doc.main.target="hide";
			   		 selectDefs(doc,"QUE");
						 return;
				case "RETURN":		  	
			  default:
					 //show("onIncHeaderLoad.selectDefs.CLS" + getAction(doc),"sel=" +getSel(),"isSearch()=" + isSearch(doc));
					 selectDefs(doc,"QUE");
					 setSearch(replaceHttpItem(getSearch(),"vars",""));
					 setSearch(replaceHttpItem(getSearch(),"ACTION",""));
					 doc.forms[0].action = replaceHttpItem(doc.forms[0].action,"vars","");
					 doc.forms[0].action = replaceHttpItem(doc.forms[0].action,"ACTION","");
						 //selectDefs(doc,"CLS");
					 return;
			}	
	 }		
	 return;
 case "SSMON":
 	 makeAsp(px.location);return;		
 }
 status = "Done"
} 

function setIncFooterWidth(doc) {
    if (doc.getElementById("incfooter") == null) return;
    if (typeof currentBlkWidth != "function") return;
    doc.getElementById("incfooter").style.width = currentBlkWidth();
}

function getAction(doc) {
return getHttpItem(doc.location.search,"ACTION");
}

function clearSelAndSelcmd() {
setSel("");
setSel("",true);
if (isGedit()) mb.setSelcmd("");
}

function getPage(s,l) {
// s = doc.location.pathname
var p = s.toString().toUpperCase();
p = p.substring(1+p.lastIndexOf("/"));
p = p.substring(0,p.indexOf("."));
if (l) p = p.substring(0,l);
//alert(p);
return p;
}

function varvalSubmit(doc){
  doc.main.target="px";
	if (isSearch(doc)) doc.main.varparm.value = getSearch().substring(1+getSearch().indexOf("?"));
  //show("onIncheaderLoad.varvalSubmit","main.target:" + doc.main.target,"main.varparm.value:" + doc.main.varparm.value, "main.action:" + doc.main.action);
	if (doc.main.action.toUpperCase().indexOf("STATPAGE.ASPX")>-1) doc.main.action = "../Dialog/Saveshow.asp?IsStatPage=true";
	doc.main.submit();return;
}

function selectDefs(doc, sw) {
var f = doc.forms[0];
var nr = parseInt(f.noofvar.value);
var name,list,res,sel,a,addequal;
res = "";
for (var i=0; i<nr; i++) {
	 name=f.elements["var" + (1+i)].value;
	 sel = getSel(name);
	 if (sel.length == 0 && name.indexOf("+")>-1) sel = getSel(name.replace(/\+/g," "));
	 //if (sel.length == 0 && getSel() != "") top.show("statfile.selectDefs",name,getSel()); ///
	 if (sel.length == 0 && isGedit()) sel = mb.getVarcmd(name);
     if (sel.length == 0 && mb.isQlist()) {
        sel = mb.getQcmd(name); 
        // addequal; set true to add "=" to sel to direct own selections processing to exeCommand below
        // the "=" char is removed from sel before setQcmd() command
        addequal = sel.length>0 && sel.charAt(0) != "=";
        if (addequal) sel = "=" + sel;
      }
   //show("selectDefs",sw,name,"isQlist="+mb.isQlist(),"sel="+sel,"isCommand(sel)=" +isCommand(sel));
	 if (sw == "QUE") { 
			 setStubnr(f.numberstub.value);
			 }
		//if (i == 0) show("selectDefs",sw,name,"sel=" + sel);
	 if (sel.charAt(0) == "=") { 
	 	 			exeCommand(f["values" + (i+1)],sel.substring(1));
	 				//remove code mark @ from querylist
	 				if (sel.charAt(1)=="@") sel = sel.replace("@","");
	 				if (addequal) sel = sel.substring(1);
	 				mb.setQcmd(name,sel);
	 				if (isGedit()) {mb.setVarcmd(name,sel);}
	 }
	 else {
	 	 			if (sel.length>0) {
						 rindex(f["values" + (i+1)],sel);
				    }
     }
	 if (px.howmany) px.howmany(1+i, getStubnr());
	 if (hide.howmany) hide.howmany(1+i, getStubnr());
 }
if (res.length > 0) { window.status=res.substring(1); }
}

function getVarFromQue(doc,valname) {
var a =getHttpItem(doc.location.search,"selidx");
while(a.indexOf(":")>-1) a=a.replace(":","=");
while(a.indexOf("¤")>-1) a=a.replace("¤","&");
return getItemValue(getListItem(a,"&" + valname));
}

function setSel(valu) {
    if (valu.indexOf("#") >- 1) {
        valu = valu.substring(0,valu.indexOf("#"));
    }
    if (typeof(ds.document.SESSION) == "object") { 
        ds.document.SESSION.selidx.value = valu;
    }
}

function getSel(valname) {
r = "";
if (typeof(ds.document.SESSION) == "object") {
	 r= ds.document.SESSION.selidx.value;
	 if (r.length == 0) return "";
	 if (typeof(valname) == "string") {
	 		return getItemValue(getListItem(r,"&" + valname));
	 }
     if (typeof(valname) == "number"){
	 		return getItemValue(r.split("&")[valname]);
	 }
}	 
return r;
}

function isSel() {
	 return getSel().length>0;
}

function getVarSel(valname) {
	 return getItemValue(getListItem(getSel(),"&" + valname));
}

function saveSel(doc) {
    //top.show("saveSel",saveSel.caller,readSearch(doc));
	 setSel(readSearch(doc));
}

function readSearch(doc,num) {
// top.saveVarval() subfunction
var f = doc.forms[0];
var nr = parseInt(f.noofvar.value);
var name,s;
var r = "";
for (var i=0; i<nr; i++) {
		if (typeof(f.elements["aggfile(" + (1+i) + ")"])=="object") {
			 name=f.elements["aggfile(" + (1+i) + ")"].value;
		}
		else {
			 name=f.elements["var" + (1+i)].value;
		}
		s = (isGedit())? mb.getVarcmd(name) : "";
		if (s.length == 0) s = rSelected(f.elements["values" + (1+i)]);
		if (s.length>0) {
	  	 r += "&" + name + "=" + s;
		}	
  }
return r.substring(1);
}

function rindex(ctr,idx) {
if (idx.substr(idx.length-1,1)==",") idx=idx.substr(0,idx.length-1);
idx = idx.split(",");
var il = idx.length;
 if (ctr.type) {
		if (ctr.type.indexOf("select-") > -1) {
		 	 var cl = ctr.options.length;
	 	 	 for(var i = 0; i < il; i++) {
	 	 			if (idx[i]<cl) ctr.options[idx[i]].selected = true;
	 		 }
	  }
 }
}

function isNotTooManyColumns(doc)
{
// varval.asp check
// In Excel transfer column number must not exceed 256
var idx = 0;
var r = true;
var p = doc.main.pxkonv;
		if (p.value == "xls") {
			 		var cols = parseInt(doc.main.headceller.value);
			 		r = cols < 256;
	  }
return r;
}

function chkExcelColumnCount(doc,m) {
// m = message, virheen teksti varval-sivulta
// Huom: tallettaa varval-sivun, jos läpäisee tarkistuksen
var r = isNotTooManyColumns(doc);
if (!r) { 
	 var i = m.indexOf(",");
	 var m = m.substring(0,i+1) + "\n" + m.substring(i+1);
	 alert(m);
	}
else { 
		 if (isQueryTimeout()) 
		 { 
		 	 startQueryTimeout();
			 saveVarval(doc); 
		 }
		 else { odota();}
		 }
return r;
}

function saveVarval(doc,win) {
	//var id = "VARVAL";
	//saveForms(doc.forms,id);
	saveSearch(doc);	
	saveSel(doc);
	//saveSel(doc,true);
	var act = getAction(doc);
	if (win != "hide" && act != "VAR " && !isVarval()) {
		 copyPage(doc.forms[0],setVirtualPath("/Dialog/Saveshow.asp"),"px",hide);
		 hide.document.forms[0].varparm.value=escape(hide.document.forms[0].varparm.value)
		 }
} 

function toSelections() {
if (typeof showMenu != "undefined") showMenu();
var p = setVirtualPath("/Dialog/Selections.asp")
		+ "?lang=" + getLang();
px.document.location = p;
}

function toTransfer() {
if (typeof showMenu != "undefined") showMenu();
var p = setVirtualPath("/graph/graphTransfer.aspx")
		+ "?lang=" + getLang();
px.document.location = p;
}

function isCommand(ctl) {
var c;
if (typeof(ctl) == "object") c =ctl.value; 
else c =  ctl;
//if (c.length<4) return false;
if (typeof c != "string") return false; // to be sure
if (c.charAt(0) != "=") return false;
c = c.toUpperCase()+" ";
if (c.indexOf("=ALL ")== 0) return true;
if (c.indexOf("=NONE ")== 0) return true;
if (c.indexOf("=LAST ") == 0) return true;
if (c.indexOf("=FIRST ") == 0) return true;
return false;
}

function exeCommand(ctl0,ctl1) {
var expr,cond,i,n,r, isObj;
isObj = typeof(ctl1) == "object";
if (!isObj) expr = ctl1;
else expr = ctl1.value; 
i = (expr + " ").indexOf(" ");
cond = expr.substring(0,i).toUpperCase();
//top.show("exeCommand","isObj(clt1)="+isObj,expr,i,'"'+cond+'"');
switch (cond){
		   case "ALL":
			 		return rfind(ctl0,"*");
		   case "FIRST":
		   case "LAST":
		 		n = parseInt(expr.substring(i));
			    if (isNaN(n)){
			        n = 1;
			    }
			 	r = cmark(ctl0,cond,n);
				return r;		
		   default:
		 	  return rfind(ctl0,expr,expr.length);
		   }		
}

function cmark(ctr,expr,n) {
var r = "";
var s;
if (ctr.type) {
		if (ctr.type.indexOf("select-multiple") > -1) {
		 var l = ctr.options.length;
	 	 for(var i = 0; i < l; i++) {
		    s = cmark0(expr,i,n,l);
				ctr.options[i].selected = s;
				if (s) {
				 	 if (r.length > 0) r += " ;";
				 	 r += ctr.options[i].text;
				 	 }
	 			}
	 	}
 }
else {
if (ctr.length) {
	var l = ctr.length;
		if (l > 0 && ctr[l-1].type == "checkbox") {
	 	 	 for(var i = 0; i < l; i++) {
			  	if (ctr[i].checked = cmark0(expr,i,n,l)) { 
				 	 	 if (r.length > 0) r += ", ";
				 	 	 r += ctr[i].value;
				 	 	 }
	 			  }
	  	}
  	}
} 
return r;
}

function cmark0(expr,i,n,l) {
var s;
switch (expr) {
	 case "FIRST":
	 		s = i<n;
			break;
	case "LAST":
			s = i>l-n-1;
			break;
	default:
		  s = false;
			break;					 					 
  }		 
return s;
}

function rparse(expr) {
r = expr.split(";");
var l,res,s;
res = "";
for (var i = 0; i<r.length; i++) {
		l = r[i].length;
		if (l > 0) {
	 		 res += (r[i].charAt(0) != "*") ? "^" : "";
			 for (var j = 0; j<l-1; j++) {
			 		 if (r[i].charAt(j) != "*") {
					 		if ("({[]})=:+,".indexOf(r[i].charAt(j)) > -1) { 
								 res += "[\\" + r[i].charAt(j) + "]";}
							else {
								 res += (r[i].charAt(j) != "?") ? r[i].charAt(j) : "[\\S]";}
						} 
			 }
			 s = r[i].charAt(l-1);		 
			 if (s != "*") {
					if ("({[]})=:+,".indexOf(r[i].charAt(j)) > -1) { 
								 res += "[\\" + r[i].charAt(j) + "]";}
					else {
				 		 res += (s != ".") ? (s != "?") ? s : "[\\S]" : " ";}
		 	 }
			 res += (i < r.length-1) ? "|" : "";
			 }
		}
return res;
}

function rfind(ctr,expr) {
// if expr starts with "@", the names have codes in the begin of text
    var begin = false;
    if (typeof expr == "string" && expr.length > 0) {
        begin = expr.charAt(0) == "@";
        if (begin) expr = expr.substring(1);
    }
    expr = rparse(expr);
    var reg = new RegExp(expr,"i")
    var r = "";
    if (ctr.type) {
		if (ctr.type.indexOf("select-") > -1) {
		 var l = ctr.options.length;
	 	 var s,t;
	 	 for(var i = 0; i < l; i++) {
	 	        t = ctr.options[i].text;
	 	 		if (begin) {
	 	 		    // remove & and ; characters from check, adjust size
	 	 		    // SelFieldLength is set in the beginning of statfile.js
	 	 		    t = t.replace(/\&/g,"");
	 	 		    t = t.replace(/\;/g,"");
	 	 		    if (SelFieldLength>0) t = t.substring(0,SelFieldLength);
	 	 		} else {
	 	 		    t = ctr.options[i].text + " ";
	 	 		}
	            s = t.search(reg) != -1;
	 	 		    //top.show("rfind,begin=" + begin, expr,t);
	 	 		    //if (!confirm("jatkatko")) return r;
	 			ctr.options[i].selected = s;
	 			if (s) { 
				 	 if (r.length > 0) r += " ;";
				 	 r += ctr.options[i].text;
					 if (ctr.type == "select-one") return r;
				 	 }
	 			}
	 	}
    }
    else {
        if (ctr.length) {
	        var l = ctr.length;
		    if (l > 0 && ctr[l-1].type == "checkbox") {
		 	    var s;
	 	 	    for(var i = 0; i < l; i++) {
	 	 			s = ctr[i].value + " ";
	 				s = s.search(reg) != -1;
	 				ctr[i].checked = s;
					if (s) { 
				 	 	 if (r.length > 0) r += ", ";
				 	 	 r += ctr[i].value;
				 	}
	 		    }
	  	    }
  	    }
    } 
    return r;
}

function rSelected(ctr,type,iscds) {
// if iscodes = true, read only SelFieldLength first characters of names
var r = "";
var s;
var iscodes = false;
var names = (type && type == "names") ? true : false;
if (names && typeof iscds == "boolean") iscodes = iscds;
var i,l;
if (ctr.type) {
		if (ctr.type.indexOf("select-") > -1) {
		 	 l = ctr.options.length;
	 	 	 for(i = 0; i < l; i++) {
	 			 if (ctr.options[i].selected) {
					if (names) {
					    s = ctr.options[i].text.replace(/\&/g,"").replace(/\;/g,"");
					    if (iscodes) {
					        r += s.substring(0,SelFieldLength);
					    } else {
					        r += s;
					    }
						r +=  ";";
				    } else {
						    r+= i + ",";
				 		}
					if (ctr.type == "select-one") return r;
				 }		
	 		 }
	 	}
 }

else {
if (ctr.length) {
	  l = ctr.length;
		if (l > 0 && ctr[l-1].type == "checkbox") {
	 	 	 for(i = 0; i < l; i++) {
	 				if (ctr[i].checked) r += ctr[i].value + " ;";
	  	}
  	}
 }		
} 
if (r.length>0) r = r.substring(0,r.length-1); 
return r;
}

function onIncheaderUnload() {
//if (px.closevarval) px.closevarval();
//if (px.svgframe && px.svgframe.closevarval)  px.svgframe.closevarval();
//if (hide.closevarval) hide.closevarval();
//closetolfr();
if (document.getElementById("konv")) {
    var f = document.getElementById("konv");
    //top.show("onIncheaderUnload",f.options[f.selectedIndex].value);
}
closeMsgWindow();
}

function getStubnr() {
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
  r = ds.document.SESSION.stubnr.value;
  }
return r;
}

function setStubnr(stubnr){
if (typeof(ds.document.SESSION)=="object" ) {
 ds.document.SESSION.stubnr.value=stubnr;
 }
}

function getValnr() {
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
  r = ds.document.SESSION.valnr.value;
  }
return r;
}

function setValnr(valnr){
if (typeof(ds.document.SESSION)=="object" ) {
 ds.document.SESSION.valnr.value=valnr;
 }
}


function openTol(tolnr,stubnr,varname,html) {
if (typeof(tolfr)=="undefined" || tolfr.closed || typeof(tolfr.rfind)=="undefined") {	
 if (arguments.length < 4) html="";
 html = html + "/Dialog/tolfr.html?" + varname;
 setTolnr(tolnr);
 setStubnr(stubnr);
var props = 'location=yes,toolbar=yes,directories=yes,menubar=yes,status=yes,copyhistory=no,scrollbars=yes,resizable=YES,height=520;width=700';
 tolfr = window.open(html,"tolfr",props);
 }		
tolfr.focus();
}

function getTolnr() {
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
  r = ds.document.SESSION.tolnr.value;
  }
return r;
}

function setTolnr(tolnr){
if (typeof(ds.document.SESSION)=="object" ) {
 ds.document.SESSION.tolnr.value=tolnr;
 }
}

function getChecklist() {
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
  r = ds.document.SESSION.checklist.value;
  }
return r;
}

function setChecklist(lst){
if (typeof(ds.document.SESSION)=="object" ) {
 ds.document.SESSION.checklist.value=lst;
 }
}

function vartol() {
var expr = getChecklist();
if (expr.length > 1) {
	 var nr = getTolnr();
	 var varname = "values" + nr; 
	 void(rfind(px.document.forms[0][varname],expr));
	 px.howmany(nr, getStubnr());
	 }
}

var activeDsn = "te";

function isDs(dsn) {
// ds.DATA.qrs
if (!dsn) dsn = activeDsn;
else activeDsn = dsn;
var r = false;
if (typeof(ds.document.DATA)=="object" && typeof(ds.document.DATA.elements[dsn])=="object" ) {
	 var d = ds.document.DATA.elements[dsn];
	 if (typeof(d) == "object") r = d.value == "true";
	 }
return r;
}

function getDsItemByName(dsn,name) {
var dir = getDsDir(dsn);
if (dir.length == 0) return "";
var l,i;
dir = dir.split(";");
name = name.toUpperCase();
for(i = 0; i < dir.length; i++) {
				l = dir[i].indexOf("=" + name);
				if (l > -1) {
					 key = dir[i].substring(0,l);
					 return  getDsItem(dsn + "." + key); 
				}
	}			
return "";
}		 

function getDsItem(key) {
key = (key + ".").split("."); // ensure two elements
if (typeof(ds.document.DATA) != "object") return "";
var d = ds.document.DATA.elements[key[0]];
if (typeof(d) != "object") return "";
d.load(key[0]);
var r = "";
if (key[1].length > 0) r = d.getAttribute(key[1]);
return (r) ? r : "";
}

function setDsItemByName(dsn,name,valu) {
var dir,key,l;
dir = getDsDir(dsn);
key = "";
if (dir.length > 0)
	 dir = dir.split(";");
	 name = name.toUpperCase();
	 for(i = 0; i < dir.length; i++) {
				l = dir[i].indexOf("=" + name);
				if (l > -1) {
					 key = dir[i].substring(0,l);
				}
	 }
if (key == "") {key=getNewDsItemKey(dsn,"cl");}				
setDsItem(dsn + "." + key + "." + name, valu);
}

function setDsItem(key,valu) {
if (!valu) valu ="";
if (typeof(ds.document.DATA) != "object") return false;
key = (key + "..").split("."); // key =<dsn>.<item>.<name>; ensure three element
var d = ds.document.DATA.elements[key[0]];
if (typeof(d) != "object") return false;
d.load(key[0]);
var dir = updDsDir(d.getAttribute(key[0]),key[1],key[2]);
d.setAttribute(key[1],valu);
d.setAttribute(key[0],dir);
d.save(key[0]);
return true;
}

function dropDsItemByName(dsn,name) {
var dir = getDsDir(dsn);
if (dir.length == 0) return false;
var l,i;
dir = dir.split(";");
name = name.toUpperCase();
for(i = 0; i < dir.length; i++) {
				l = dir[i].indexOf("=" + name);
				if (l > -1) {
					 key = dir[i].substring(0,l);
					 return  dropDsItem(dsn + "." + key); 
				}
	}			
return false;
}		 

function dropDsItem(dsn) {
if (typeof(ds.document.DATA) != "object") return false;
dsn = (dsn + ".").split("."); // ensure two element
var d = ds.document.DATA.elements[dsn[0]];
if (typeof(d) != "object") return false;
d.load(dsn[0]);
d.removeAttribute(dsn[1]);
var dir = d.getAttribute(dsn[0]);
if (dir) {
	 dir = dropListItem(dir,";" + dsn[1]);
	 d.setAttribute(dsn[0],dir);
	 }
d.save(dsn[0]);
return true;
}

function updDsDir(dir,key,name) {
dir =(!dir) ? "" : dir; //dir might be null
name = (!name) ? "" : name.toUpperCase();
var k = ";" + key;
if (isListItem(dir,k)) { 
	 var d = dir;
	 dir = dropListItem(dir,k);
	 }
dir = (dir.length > 0) ? ";" + dir : ""; 
return key + "=" + name + dir;
}

function setDsDir(dsn,dir) {
if (typeof(ds.document.DATA) != "object") return false;
var d = ds.document.DATA.elements[dsn];
if (typeof(d) != "object") return false;
d.load(dsn);
d.setAttribute(dsn,dir);
d.save(dsn);
}

function getDsDir(dsn) {
return getDsKeys(dsn,true);
}

function showDsDir(dsn) {
var dir = getDsDir(dsn);
while (dir.indexOf(";") > -1) dir = dir.replace(";", "\n","g");
show(dir);
}

function getDsKeys(dsn, values) {
if (!values) values = false;
if (typeof(ds.document.DATA) != "object") return "";
d = ds.document.DATA.elements[dsn];
if (typeof(d) != "object") return "";
d.load(dsn);
var dir = d.getAttribute(dsn);
if (!dir) return "";
if (values) return dir;
dir = dir.split(";");
for (var i = 0; i < dir.length; i++) { dir[i] = dir[i].substring(0,dir[i].indexOf("="));}
dir = dir.join(";");
return dir;
}

function getNewDsItemKey(dsn,prefix) {
if (!prefix) prefix = "item";
var dir = ";" + getDsKeys(dsn) + ";";
var r;
var seek = true;
for (i = 0; seek; i++) {
		r = prefix + i;
		if (dir.indexOf(";" + r + ";") == -1) seek = false;
		}
return r;
}

function getDefs() {
// ds.DATA.cls
return getDsItem("cls.defs");
}

function showDefs() {
var list = getDefs();
while (list.indexOf("¤")>-1) { list=list.replace("¤","\n");}
show(list);
}

function setDefs(list) {
setDsItem("cls.defs",list);
}

function getDef(varname, key) {
var itm = getDefItem(varname);
if (itm.length == 0) return "";
var lst = getDsItemByName("cls",varname);
return getListItem(lst,"¤" + itm);
}

function getDefItem(key) {
var list,itm;
list = getDsItem("cls.defs");
if (list.length == 0) return "";
itm = getListItem(list,"¤" + key);
if (itm.length == 0) return "";
return getItemValue(itm);
}

function setDef(key,valu) {
var list = getDsItem("cls.defs");
list = setListItem(list,"¤" + key,valu);
setDsItem("cls.defs",list);
}

function dropDef(varname,key) {
if (key != getDefItem(varname)) return; 
var list = getDsItem("cls.defs");
list = dropListItem(list,"¤" + varname);
setDsItem("cls.defs",list);
}

function qLoad(name){
if (typeof(ds.document.SESSION)=="object" ) {
 ds.document.SESSION.qlist.value=qSortGet("cls",name);
 ds.document.SESSION.qlistname.value=name;
 }
}

function qSortGet(key,name){
	return getDsItemByName(key,name).split("¤").sort(qSortFun).join("¤");
}

function qSortFun(a,b) {
	a = a.substring(1+a.indexOf("="));
	b = b.substring(1+b.indexOf("="));
	if (a>b) return 1;
	if (b>a) return -1;
	return 0; 
}

function qSave() {
if (typeof(ds.document.SESSION)=="object" ) {
 	var name =ds.document.SESSION.qlistname.value;
	if (name.length == 0) {alert("Saving failed, list name missing");return;}
	var v = ds.document.SESSION.qlist.value;
 	if (v.length > 0) { setDsItemByName("cls",name,v);} 
	else {dropDsItemByName("cls",name);}  
  }
}

function qSet(key,valu) {
var list = getQlist();
if (!valu) {
	 valu = key;
	 key  = getNewListItemKey(list,"¤");
	 }
list = setListItem(list,key,valu);
setQlist(list);
qSave();
return list;
}

function qGet(key) {
var list = getQlist();
return getListItem(list,key);
}

function qDrop(key) {
var list = getQlist();
list = dropListItem(list,key);
setQlist(list);
qSave();
return list;
}

function qSize(list) {
if (!list) list = getQlist();
var r =(list.length == 0) ? 220 : list.split("¤").length * 23 + 310;
return r;
}

function qColSet(nr,valu) {
var list = getQlist();
list = setListCol(list,nr,valu,"¤!");
setQlist(list);
qSave();
return list;
}

function getVal(valnr,valname){
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 r = ds.document.SESSION.val.value;
 while (r.indexOf("?") > -1) {
 			 r = r.replace("?",valnr,"g");
 			 }
 while (r.indexOf("!") > -1) {
 			 r = r.replace("!",valname, "g");
 			 }
 }
return updVal(r,valname);
}

function updVal(cd,valname){
cd = cd.split("¤");
var r0 = cd[0];
var r2 = cd[1];
var ds = "";
var isds = isDs("cls");
if (isds) {
	 qLoad(valname);
	 ds = getQlist();
 }
if (ds.length > 0){
	 r0 += valRows(ds, cd[2]);
  }
if (isds) {r0 += valButtons(ds.length>0);}
return r0 + r2;
}

function valRows(ds, r0) {
	 var a0 = '<tr><td valign="top" width="10%"><input type="radio" name="sel" onclick="window.opener.top.pickQList(this)"value="'; // d[0] "checked" attribute removed 2004-04-09
	 var a1 = '<tr><td width="10%" valign="top"><input type="radio" name="sel" onclick="window.opener.top.pickQList(this)" value="'; // d[0]
	 var a2 = '"></td><td width="40%">'; // valinnan nimi
	 var b  = '</td><td>';
	 var c  = '</td></tr>';
	 var i,k,sel,ti,r1,t;
	 ds = ds.split("¤"); 
	 for (i = 0; i < ds.length; i++) { 
	  	 r1 = (i==0) ? a0: a1;
  		 k= getItemKey(ds[i]);
			 sel=getItemValue(ds[i]).split("!"); // key,name,date,default,sum,query
 			 r1 += k + a2 + sel[0] + b + sel[1] + b + valDefault(sel[2],k) + c;
 			 r0 += r1;
 			 }
return r0;
}

function valDefault(sel,key) {
var r = '<input type="checkbox" name="' + "def" + key + '" '; 
if (sel =="1") r += 'checked ';
return r += ' onclick="window.opener.top.updDefCheckBox(this)" />';
}

function updDefCheckBox(ctl) {
var nm = ctl.name;
var key = nm.substring(3);
if (ctl.checked == false) {
	 dropDef(ctl.document.title, key);
	 }
else {
	 setDef(ctl.document.title,key);
	 }
var f = ctl.form.elements;
var v = "";
for (var i = 0; i<f.length; i++) {
		if (f[i].type == "checkbox" && f[i].name.indexOf("def") == 0) {
			 if (f[i].name != nm) {f[i].checked = false;}
			 v += ";"; 
			 v += (f[i].checked) ? "1" : "0";
		   }
		}
qColSet(2,v);
}

function valButtons(isList) {
var t,r;
t = getQlistText();
r = '<tr><td></td><td colspan="3">'
if (isList) {
	 r += '<input TYPE="BUTTON" VALUE="' + t[0] + '" name="SUBMIT1" onclick="javascript:window.opener.top.exeQList(this)">'
	 +    '<input TYPE="BUTTON" VALUE="' + t[1] + '" name="SUBMIT2" onclick="javascript:window.opener.top.dropQList(this)">'
	 +    '<input TYPE="BUTTON" VALUE="' + t[5] + '" name="SUBMIT3" onclick="javascript:window.opener.top.updQList(this)">';
	 }
return r;
}

function exeQList(ctl) {
var r = selQList(ctl);
if (r.length > 0) {
 ctl.form.expr.value=r[r.length-1]; 
 ctl.form.submit();
 }
return false;
}

function pickQList(ctl) {
var r = selQList(ctl);
if (r.length > 0) {
 ctl.form.expr.value=r[r.length-1]; 
 }
return false;
}

function selQKey(ctl) {
var f,k;
f = ctl.form.sel;
k = "¤";
if (!f) {alert("Error in selQKey");return "";}
if (f.length) {
	for (var i = 0; i < f.length; i++) {
	 		if (f[i].checked) {k += f[i].value;break;}
	 		} 
	if (k == "¤") {alert(getQlistText()[6]); ctl.focus();return "";}
	}
else {
	if (!f.checked) {alert(getQlistText()[6]); ctl.focus(); return "";}
	k += f.value;
	}
return k;
}

function selQList(ctl) {
var k = selQKey(ctl);
if (k.length == 0) return "";
return getItemValue(qGet(k)).split("!");
}

function dropQList(ctl) {
var r = selQKey(ctl);
if (r.length == 0) return false;
if(!dropConfirm(r,getQlistText()[4])){ val.focus(); return false;}
dropDef(ctl.document.title,r);
var h = qDrop(r)
h = qSize(h);
var w = (h>550) ? 730 : 680;
px.val.document.write(getVal(getValnr(),getQlistName()));
px.val.document.close();
px.val.resizeTo(w,h);
px.val.document.form1.expr.focus();
return false;
}

function updConfirm(r,cl) {
var s = "\n\n";
r = r[0] + ":" + s + getQlistText()[7] + "\n"  +  r[r.length-1] + s + getQlistText()[8] + s + cl;
return confirm(r);
}

function dropConfirm(k,txt) {
var s = "\n\n";
var r = getItemValue(qGet(k)).split("!");
r = txt + ":" + s + r[0]+ ", "  + r[1] + ":" + s + r[r.length-1]; 
return confirm(r);
}

function newQList(ctl) {
var f = ctl.form;
var t = f.expr.value;
var p = f.prompt.value;
var d = "";
window.focus();
d=qPrompt(p,d);
f.expr.focus();
if (!d) return false;
//var list = getQlist();
t =(t.length==0) ?  t + "*" : t; 
var h = qSet(d +  "!" + px.timeStamp() + "!0!0!" + t);
h = qSize(h)
var w = (h>550) ? 730 : 680;
px.val.document.write(getVal(getValnr(),getQlistName()));
px.val.document.close();
px.val.resizeTo(w,h);
px.val.document.form1.expr.focus();
return false;
}

function updQList(ctl) {
var key = selQKey(ctl);
if (key.length == 0) return false;
var r = getItemValue(qGet(key)).split("!")
var t = ctl.form.expr.value;
t =(t.length==0) ?  t + "*" : t; 
r[1] = timeStamp();
if(!updConfirm(r,t)){ val.focus(); return false;}
r[r.length-1] = t;
var h = qSet(key,r.join("!"));
val.document.form1.expr.focus();
return false;
}

function qPrompt(p,d) {
d=prompt(p,d);
if (!d) return d;
while (d.indexOf("!") >-1) d=d.replace("!","");
while (d.indexOf("¤") >-1) d=d.replace("¤","");
if (d.length > 0) return d;
else return null;
}

function getQuery(name) {
return (isDs()) ? getDsItem(activeDsn + "." + name) : getCookie(name);
}

function deleteQuery(name) {
if (isDs()) {dropDsItem(activeDsn + "." + name); }
else {deleteCookie(name); }
}

function getQueryNames(prefix) {
return (isDs()) ? getDsKeys(activeDsn) : getCookieNames(prefix);
}

function getNewQueryName(prefix) {
return (isDs()) ? getNewDsItemKey(activeDsn,prefix) : getNewCookieName(prefix);
}

function deleteAllQueries(prefix) {
var dir = getQueryNames(prefix);
if (dir.length == 0) return;
dir = dir.split(";");
var i = dir.length;
for (i = 0 ; i < dir.length; i++) {
		deleteQuery(dir[i]);
		}
}

function isQueryTimeout() {
var r = false;
if (typeof(ds.document.SESSION)=="object" ) {
	 if (typeof(ds.document.SESSION.qtimeout)=="object" ) {
 	 		r = ds.document.SESSION.qtimeout.value == "TRUE";
	 }		
 }
return r
}

function setQueryTimeout(val){
if (arguments.length == 0) val="TRUE"; 
if (typeof(ds.document.SESSION)=="object" ) {
 	 if (typeof(ds.document.SESSION.qtimeout)=="object" ) {
	 		ds.document.SESSION.qtimeout.value=val;
	 }		
 }
}

function startQueryTimeout() {
setQueryTimeout("FALSE");
setTimeout("setQueryTimeout()",30000);
}				 

function odota() {
openMsgWindow("Haku kesken. Odota...");
}

function openMsgWindow(msg) {
var code = getMsgWindow(msg);
var props = 'left=300,top=100,toolbar=no,location=no,directories=no,menubar=no,status=no,copyhistory=no,scrollbars=no,resizable=YES,width=350,height=100';
if (code.length > 0) {
 if (typeof(msgw)!="object" || (typeof(msgw)=="object" && isProp(msgw,"closed") && msgw.closed)) {
 	 		 msgw = window.open("","msgw",props);
	}
	if (typeof(msgw)=="object" && msgw != null) { 
 		 	 msgw.document.write(code);
 		 	 msgw.document.close();
	} else {
		 while (msg.indexOf("<br>")>-1) msg = msg.replace("<br>","\n");
		 alert(msg);
	}
 }
}

function isProp(obj, prop) {
if (obj != null) {
for (var i in obj) {
		if (i == prop) return true;
		}
}		
return false;
}

function closeMsgWindow() {
if (typeof(msgw)=="object" && msgw != null) {
	 if (!msgw.closed) msgw.close();
	 }
}

function getMsgWindow(msg) {
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 r = ds.document.SESSION.msgwindow.value;
 r = r.replace("!",msg);
 }
return r
}

function closeWindow(w) {
if (typeof(w)=="object") {
	 if (!w.closed) w.close();
	 }
}

function chkQueryTimeout() {
if (isQueryTimeout()) 
	 { 
	 startQueryTimeout();
	 return true;
 	 }
else { odota();
	 return false;
	 }
}

function startDemo() {
px.location=gappl;
}

function isMenu() {
	 return typeof(menu)=="object";
}
		
function isSa(lnk) {
// varval ja varvalagg: kertoo missä aihealuelinkki
var t ="";
if (typeof(lnk.outerText)=="string") t = lnk.outerText;
if (typeof(lnk.text)=="string") t = lnk.text;
return t == getSaName();
}

function getSaName() {
var r ="";
if (typeof(ds.document.MENU)=="object" ) {
 r = ds.document.MENU.saname.value;
 }
return r; 
}

function isTableDir(lnk) {
// varval ja varvalagg: kertoo missä taulukkoluettelon linkki
var t ="";
if (typeof(lnk.outerText)=="string") t = lnk.outerText;
if (typeof(lnk.text)=="string") t = lnk.text;
return t == getTableDirName();
}

function getTableDirName() {
var r ="";
if (typeof(ds.document.MENU)=="object" ) {
 r = ds.document.MENU.tdname.value;
 }
return r; 
}

/*
function getMenuRole(){
	 var r = "";
	 var f = ds.document.MENU.role;
	 if (typeof(f) =="object") r = f.value;
	 return r; 
}
*/
function getMenufile(){
	 var f = (typeof(mb.document.MENU) == "object") ? mb.document.MENU : ds.document.MENU;
	 if (typeof(f) =="object") return f.menufile.value;
	 else return ""; 
}
function getMenudir(){
	 var f = (typeof(mb.document.MENU) == "object") ? mb.document.MENU : ds.document.MENU;
	 if (typeof(f) =="object") return f.menudir.value;
	 else return ""; 
}

function getFrontfile(){
	 var r = "";
	 var f = ds.document.SESSION.frontpage;
	 if (typeof(f) =="object") r = f.value;
	 return r; 
}

function toPx(url,path, name) {
 url = url.replace("!",getLang());
 if (px) {
  if (path) { // if path (and name) <==> Subject Area login  
		 var d = url.split("/");
		 url = getLoginHref(path,url,d[2],name,getLang())
		 }
	else {
		 if (isQtPicture() && url.indexOf(getQtdir())>-1) {
		 		var r = url.substring(url.lastIndexOf("/"),4 + url.indexOf(".asp"));
				url = url.replace(r,"");
				mb.toPxdir(url); return;
		    } 
		 url = setVirtualPath(url);
		 }
	px.location = url;
	}
}
/*
sel() is used in menu commands
**/
function sel(a,b,c) {
if (mb.mSel && a == mb.mSel) {
// if the selected row is already active, return one step back
    a = a.substring(0,a.length-1);
    a = a.replace(/ /g ,"");
    //show("sel-back","mb.mSel=" + mb.mSel,"mb.mSelR="+mb.mSelR,"search=sel('" + a + "'");
    for (var i = 0;i<mb.mComm.length;i++) {
        if (mb.mComm[i].indexOf("sel('" + a + "'")>-1) {
            if (mb.mComm[i].indexOf("js:")) eval(mb.mComm[i].substring(3));
            else eval(mb.mComm[i]);
            return;
        }
    }
}
mb.mSel = a;
if (b){
    if (navigator.appName == "Opera") {
        setTimeout('mb.toPxDir("' + b + '","' + c + '")',300);
        }
    else mb.toPxDir(b,c);
    }
mb.setMenu(a);
}

/*
selx() used navigation link commands 
where the backward step is not appropriate
see main.js buildNaviControls()
**/

function selx(a,b,c) {
mb.mSel = a;
if (b){
    if (navigator.appName == "Opera") {
        setTimeout('mb.toPxDir("' + b + '","' + c + '")',300);
        }
    else mb.toPxDir(b,c);
    }
mb.setMenu(a);
}


function startMenuPage(restart) {
	if (isMenu()) {
		 var m = (mb.location != "empty.htm") ? mb.location.pathname: getMenufile();
		 if (!restart) restart = false;
		    show("ds.onload","startMenupage()requests for frame mb:",
				m + getMenupars(),
				"restart=" + restart,
				"will call mb=" + (mb.location == "empty.htm" || restart),
				"mb.location now:",
				mb.location);		 
		 if (mb.location == "empty.htm" || restart) {
		 		mb.location= m + getMenupars();
		 }
	}			
}

function isQtPicture(){
var r = false;
if (typeof(ds.document.STATFILE)=="object" ) {
 r = ds.document.STATFILE.qtype.value == "PICTURE";
 }
return r;
}

function getVirtualPath() {
    var l = top.location.href;
    if (l.indexOf("?") > -1) l = l.substring(0,l.indexOf("?"));
    return l.substring(0,l.lastIndexOf("/"));
}

function setVirtualPath(path) {
if (path.indexOf("..") == 0) path = path.substring(2);
return getVirtualPath() + path;
}

function getInfo(cs,ptr) {
switch (cs) {
  case "DB" :
			 px.location=setVirtualPath(gappl + "/menu/DbInfo.aspx") + "?case=DB";
	 		 return;
	case "QT" :
			 		px.location=setVirtualPath(gappl + "/menu/DbInfo.aspx") + "?case=QT";
			return;
	case "SA" :
			 if (typeof(ptr)=="string") {
  		    px.location=setVirtualPath(gappl + "/menu/SaInfo.aspx") + "?SA=" + ptr;
					}
			 return;
	default :
			return;
	}
}

function writeInfo(topic,cs,doc) {
var f = ds.document.STATFILE;
if (typeof(f) == "object") {
	 		switch (cs) {
  				 case "DB" :
			 		 			doc.write(getDbInfo(topic+"DB",f.dbinfo));
	 		 					return;
					 case "QT" :
			 		 			doc.write(getDbInfo(topic+"QT",f.qtinfo));
			 					return;
					 }
		 }			 
}

function getDbInfo(topic,f) {
	 		switch (topic){
	 		  case "HEADINGDB" :
	 					return getDbname();
	 		  case "HEADINGQT" :
	 					return getQtname();
				case "INFODB" :
				case "INFOQT" :
						return getInfoByLang(f);
				default:
				    return "";								
				}
}

function getInfoByLang(f) {
var lc = getLangCode();
var info = f.value;
var r = "";
var i = info.indexOf(lc + ":");
if (i > -1) {
	 r = info.substring(i+3).split("$")[0];
	 }
return r;
}

function isGedit() {
if (!isMenu()) return false;
return typeof(mb.document.GEDIT)=="object";
}

function setCtype(ctl) {
if (isEnabled()) {
	 document.forms[0].elements["_ctype"].value=ctl.name;
	 return true;
	 }
else return false;	 
}

function copyPage(form,action,target,fr) {
// copy form into html page and put it into fr
// used by savevarval
var f = form.elements;
var ids;
r = '<html lang="fi"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">';
r += '<title></title></head><body><form method="post" action="';
r += action + '" target="' + target + '">\n';
for(var i = 0;i<f.length; i++) {
	 switch (f[i].type){
	 				case "select-multiple":
							 idx = "";
							 for(var j = 0; j<f[i].length; j++) {
							 		if (f[i][j].selected) idx+="," + (j+1);
							 }
							 if (idx.length>0) r += hidden(f[i].name,idx.substring(1));
							 break;
					default:
							 if( f[i].value.length>0) r += hidden(f[i].name,f[i].value);		
							 break;
	 }						 		 		
}
r += "</form></body></html>";
fr.document.write(r);
fr.document.close();
}

function hidden(name,val) {
//subfn for copyPage()
return r = '<input type="hidden" name="' + name + '" value="' + htmlEncode(val) + '" />\n';
}

function htmlEncode(valu) {
// tähän merkit
valu=valu.replace(/ä/g,"&auml;");
valu=valu.replace(/Ä/g,"&Auml;");
valu=valu.replace(/ö/g,"&ouml;");
valu=valu.replace(/Ö/g,"&Ouml;");
return valu;
}

function restoreSaveShow() {
if (!hide.document.forms[0]) return;
	 var f =hide.document.forms[0];
	 for(var i = 0; i<f.elements.length; i++) {
	 f.elements[i].value=f.elements[i].value;
	 }
	 if (f.action.indexOf("Saveshow")>-1) f.submit();
}

function copyHiddenFields(fr1,fr2) {
// copy fields from f2 to f1
// relevant field names in f2 are lowercase
f1 = fr1.document.forms[0];
f2 = fr2.document.forms[0];
var nm;
for (var i = 0; i<f1.elements.length; i++) {
		if (f1.elements[i].type == "hidden") {
				 nm = f1.elements[i].name.toLowerCase();
				 if (typeof(f2.elements[nm]) == "object") f1.elements[i].value = f2.elements[nm].value;
		}
  }
}

function submitForm(fm,path,target) {
var act = fm.action;
var tar = fm.target;
var met = fm.method;
fm.action = path;
fm.target = target;
fm.submit();
fm.action = act;
fm.target = tar;
fm.method = met;
}

function startWait() {
var w =logo.document.getElementById("wait");
if (w) w.style.visibility="visible";
}
function stopWait() {
var w =logo.document.getElementById("wait");
if (w) w.style.visibility="hidden";
}

function trim(value) {
// remove trailing and leading whitespace, \n,\r etc
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   return temp;
}

function showProps(obj, objName) {
   if (!objName) objName="-";
   var result = "";
   for (var i in obj) {
      result += objName + "." + i + " = " + obj[i] + "\n";
   }
   return result;
}

function showForm(fr,form) {
var r,n = "\n";
if (!form) form = 0;
if (fr.document) fr = fr.document;
r = fr.forms[form].name + n;
r += "location:" + fr.location.toString() + n;
r += "action:" + fr.forms[form].action + n;
var f = fr.forms[form].elements;
for(var i = 0;i<f.length; i++) {
	 if (f[i].name.substring(0,2) != "__") {
	 		r +="[" + i + "]" + f[i].name;
	 		if (f[i].type == "radio" || f[i].type == "checkbox") if (f[i].id) r += "." + f[i].id ;
			r += ":" + f[i].type;
			if (f[i].text) r += f[i].text;
			if (f[i].type == "radio" || f[i].type == "checkbox"){
			 	 if (f[i].type == "radio") r += "," + f[i].value;
				 r += "=" + f[i].checked;
				 }
		  else if (f[i].value) r += "=" + f[i].value;
	 		r += n;
	 } 
	 else r +="[" + i + "]" + f[i].name + ":hidden .NET field" + n;
}
show(r);
} 
