// If at least one database definition
// contains language code as third element,
// only databases with current language code
// are selected to the database list in statfile?.asp.
// Otherwise, all databases are selected

var test = true;

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(){
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 var l = ds.document.STATFILE.languages.value.split(",");
 var i = ds.document.SESSION.lang.value;
 r = l[i];
 }
return r;
}

function getLang(){
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 r = ds.document.SESSION.lang.value;
 }
return r;
}

function setLang(code){
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;
 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" ) {
 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(){
// frontpage is either px or pxadmin frontpage
var r = "";
if (typeof(ds.document.SESSION)=="object" ) {
 r = ds.document.SESSION.frontpage.value;
 r = r.replace("!",getLang());
 if (isInlog()) {
		r += "?" + getParmtext2().substring(1);
		}
 px.location.href=r;
 }
}

function getListItem(list,key){
// key:ssä ensimmäisenä erotinmerkki
// key:n loppuun "="
var r = "";
var sep = key.substring(0,1);
if (list.length > 0) {
 list = sep + list;
 var i = list.toUpperCase().indexOf(key.toUpperCase() + "=");
 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) ? 1 : 0 ;	
			 }
		}
		h = (i[0] == 0) ? h : i[1] ;
	 doc.write('<input type="hidden" name="nodb" value="' + h + '">');
}

function writeStatFileList(doc, dir)
{
if (typeof(ds.document.SESSION)!="object" ) return;
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;
var r 	 = "";
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) 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 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);
var p = "" + px.location.href;
p = p.substring(0,7 + p.toUpperCase().indexOf("/DIALOG"));
p = p + "/Statfile" + getLang() + ".asp"	+ getParmtext1();
px.location.href = p;
}

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 = 3();}
var r = h.indexOf("&case=mb") > -1;
return r;
}

function isInlog() {
// vaaditaanko koko kantaan kirjautuminen
r = false;
if (typeof(ds.document.SESSION)=="object" ) {
  var d = ds.document.SESSION;
	r = d.dbinlog.value.toUpperCase() == "TRUE"; // || isMbcheck();
	}
return r;
}

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 updateLinks(doc) {
// incHeader-skripti: lisää oletuskirjautumistiedot
// jos koko kantaan tai tilastoryhmään vaaditaan kirjautuminen
// if (isInlog() || isMbcheck()) {
  var max = doc.links.length;
	if (max > 0 ) {
		 var hrefs = new Array(max);
		 var p2 = getParmtext2();
  	 var p1 = getParmtext1();
		 var i;
		 for ( i=0; i<max; i++) {
		 		 hrefs[i] = doc.links[i].href;
				 if (hrefs[i].indexOf("ACTION=SUBMIT") > -1) { hrefs[i]=hrefs[i].replace("ACTION=SUBMIT","");}
		 		 if (hrefs[i].indexOf("top.") == -1 && hrefs[i].indexOf("javascript:" == -1)) {
 		 		 			if (-1 < hrefs[i].indexOf("?")) {
								 if (-1 == hrefs[i].indexOf("xu=")) hrefs[i] += p2;
							 }
		 		  		else { hrefs[i] += p1; }
				  }
					if (doc.links[i].target != null) {		
				 		  if(doc.links[i].target.length == 0) {
									doc.links[i].href="javascript:top.launchLink(" + i + ");";}
					}					  
		 }
		 setHrefs(hrefs);
		 }
}

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 launchLink(ind) {
	var hrefs = getHrefs().split("!");
	px.location.href = hrefs[ind]; 
}

function showLinks() {
// util
// listaa sivun linkkien osoitteet
  var p = getHrefs().split("!");
	var r = ""
	var max = p.length;
  var i;
	for ( i=0; i<max; i++) r += i + ":" + p[i] + "\n";
	show(r);
}

function toSearchpx() {
// Etsintäsivulle Statfile-sivulta 
var p = "" + px.location.href;
p = p.substring(0,p.toUpperCase().indexOf("/STATFILE"));
p = p + "/Searchpx.asp"	+ getParmtext1();
px.location.href = p;
}

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 getHttpItem(query,key) {
// query = http-query
var r = "";
query = query + "&";
var i = query.indexOf("&" + key + "=");
if (i == -1) i = query.indexOf("?" + key + "=");
if (i == -1) return r;
r = query.substring(i + 2 + key.length);
r = r.substring(0,r.indexOf("&"));
return r;
}

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[1] + "&yp=" + r[2]; 
}

function toNextpage(h){
// onload-funktio: ds.asp
// form action-funktion: Password?.asp
// jos kieliparametri puuttuu, se asetetaan
if(arguments.length == 0)  h = getNext();										
	 px.location.href=addLang(h);
}

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 -sivun 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 = addLang(getNext()) + parmtext;
//show("fromLoginpage:",s,h);
px.location.href=h;
}

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("Valitettavasti haku ei mahdu talletustilaan.");
			 r = false;
			 }
return r;
}

function setQuery(loc,keys,values,dt,desc) {
var name = (px.location.search.indexOf("ACTION=EDIT") > -1) ? getQname() : getNewQueryName("ss");
var value = loc + "!" + keys + "!" + values + "!" + dt + "!" + desc;
var today = new Date();
var expires = new Date(today.getTime() + 10 * 365 * 24 * 60 * 60 * 1000);
var r =(isDs()) ? setDsItem("qrs." + name,value) : setCookie(name,value,expires); 
return (r) ? name : "";
}	

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 saveSelection(doc) {
// tallettaa varval-valinnat cookiehen
var loc = doc.location.toString();
var i = loc.indexOf("&ACTION");
if (i > -1) { loc = loc.substring(0,i);}
saveVarval(doc);
saveSelection2(doc,loc);
}

function timeStamp() {
var a = new Date();
var y = a.getYear();
y = (y < 1000) ? y + 1900 : y;
return  y + "-" + (rjust(a.getMonth() + 1,2,"0")) + "-" + rjust(a.getDate(),2,"0") 
	 		+ " " + (rjust(a.getHours(),2,"0")) + ":" + (rjust(a.getMinutes(),2,"0")) + ":" + (rjust(a.getSeconds(),2,"0")); 
}

function saveSelection2(doc,loc) {
var dt;
getKeys();
getValues();
var q = "Haku taulukosta " + doc.forms[0].ti.value + "\nAnna haulle nimi:";
dt = timeStamp(); 
var desc = getDesc(q);
var name = setQuery(loc,takeKeys(),takeValues(), dt, desc);
setQname(name);
px.location = "../Dialog/Selections.asp?Lang=" + getLang();
}

function saveHtmltableSelection(doc) {
// tallettaa varval-valinnat cookiehen
// käynnistetään Htmltable.asp:n cookie-buttonista
var loc = top.getSearch();
var i = loc.indexOf("&ACTION");
if (i > -1) { loc = loc.substring(0,i);}
saveSelection2(doc,loc);
}

function rjust(s,len,chr) {
// 
if (!chr) chr = " ";
s = "" + s;
if (s.length >= len) { return s; }
else {
		 var r = "";
		 var j = len - s.length;
		 for (var i = 0;i < len; i++) {
		 			 r += (i < j) ? chr : s.charAt(i-j);
					 }
		 }
return r;
}

function getDesc(q) {
var desc ="";
if (px.location.search.indexOf("ACTION=") > -1) {
		var cook = getQuery(getQname()).split("!"); // loc,keys,values,date,desc
		desc = cook[4];
	 }
var d = prompt(q,desc);
desc = (d == null) ? desc : d;
while(desc.indexOf("!") > -1) { desc.replace("!",".");}
return desc;
}

function saveSearch(doc) {
// varval.asp: read px.location
// and save it to ds.data.action
setSearch(doc.location.toString());
}

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
if (typeof(ds.document.DATA)=="object" ) {
	 ds.document.DATA.action = r;
	 }
}


function isSearch(doc) {
// Is the current location the same as the previous one?
// varval.asp, varvalagg.asp check
var r = false;
if (typeof(ds.document.DATA)=="object" ) {
	 var a = ds.document.DATA.action.toUpperCase();
	 if( a.length > 0) {
	 		 while(a.indexOf(" ") >-1) a=a.replace(" ","+");
			 var b = doc.location.toString().substring(0,a.length).toUpperCase();
	 		 while(b.indexOf(" ") >-1) b=b.replace(" ","+");
			 r = a == b;
//			 show(a,b,r);
	     }
	 }
return r;
}

function onIncheaderLoad(doc) {
checkLogin();
setQueryTimeout();
var r = doc.location.pathname.toString().toUpperCase();
// Password?.asp, Login?.asp
if (r.indexOf("PASSWORD") > -1 || r.indexOf("LOGIN") > -1) { 
doc.forms[0].xu.focus();
return;
}
if (r.indexOf("SEARCHPX2.ASP") > -1 || r.indexOf("VIEW.ASP") > -1 || r.indexOf("SAVESHOW.ASP") > -1){
	 updateLinks(doc);
	 return;}
// varval.asp, varvalagg.asp action:
var id = "VARVAL";
if ( r.indexOf(id) > -1 ) {
	 if (isSearch(doc)) {
	 		restoreForms(doc.forms,id)
			r = doc.location.search.toString().toUpperCase();
			if (r.indexOf("ACTION=SUBMIT") > -1) doc.main.submit();		
			else {
					 if (r.indexOf("ACTION=REMOVE") > -1) {
					 		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");
								 }
							}
					 else {
					 			if (r.indexOf("ACTION=EDIT") > -1) {
//									 alert("Muokkaa valintoja ja talleta haku");
									 }
					 			}
					 }
			}
	 else {
	 		selectDefs(doc);	
	 		}		
	 updateLinks(doc);
	 return;
	 }
if (r.indexOf("SSMONITOR") > -1) {
	 makeAsp(px.location);
	 }
}

function selectDefs(doc) {
if (!isDs("cls")) return;
var nr = parseInt(doc.forms[0].noofvar.value);
var name,list,res;
res ="";
for (var i=0; i<nr; i++) {
		if (typeof(doc.forms[0].elements["aggfile(" + (1+i) + ")"])=="object") {
			 name=doc.forms[0].elements["aggfile(" + (1+i) + ")"].value;
			 }
		else {
			 name=doc.forms[0].elements["var" + (1+i)].value;
			 }
	 		 list=getDef(name);
    if (list.length > 0) {
		   list = list.split("!"); 
			 rfind(px.document.forms[0].elements["values" + (1+i)],list[list.length-1]);
    	 px.howmany(1+i, getStubnr());
			 res += ";" + name + list[0].substring(list[0].indexOf("="));
			 }
		}
if (res.length > 0) { window.status=res.substring(1); }
}

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) {
	var id = "VARVAL";
	saveSearch(doc);	
	saveForms(doc.forms,id);
} 

function toSelections() {
var loc = px.document.location;
var p = "Selections.asp"
		+ "?lang=" + getLang();
px.document.location = p;
}

function writeOwnSelections(doc) 
{
var d = getQueryNames("ss");
if (d.length == 0) return;
d = d.split(";"); 
var a0 	 = '<tr><td valign="top"><input type="radio" checked name="ma" value="'; // d[0]
var a1 	 = '<tr><td valign="top"><input type="radio" name="ma" value="'; // d[0]
var a2   = '"></td><td width="40%">'; // tilaston nimi
var b 	 = '</td><td valign="top">';
var c 	 = '</td></tr>';
var i,cook,ti;
var qname = getQname();
qname = (qname.length == 0) ? d[0] : qname;
var r;
for (i = 0; i < d.length; i++) { 
 r = (d[i] == qname) ? a0 : a1;
  cook = getQuery(d[i]).split("!"); // loc,keys,values,date,desc
 ti = unescape(getListItem(cook[0],"&ti")).substring(3); 
 r +=  d[i] + a2 + ti + b + cook[4] + b + cook[3] + c;
 while (r.indexOf("+") > -1) {r = r.replace("+"," ");}
 doc.write(r);
 }
} 

function executeQuery() {
// Etsi haku
if (chkQueryTimeout()) {
	 var cn = "";
	 var ma = px.document.forms[0].ma;
	 if (!ma) {return;}
	 if (ma.length) {
   		for (var i = 0; i < ma.length; i++) {
			 		if (ma[i].checked) {cn = ma[i].value; break;}
			 		} 
			}
	 else {cn = ma.value;}	
	 if (cn.length == 0) {return;}
	 var cook = getQuery(cn).split("!");
	 //show("1",cook[0],"2",cook[1],"3",cook[2],"4",cook[3],"5",cook[4]);
	 setSearch(cook[0]);
	 putKeys(cook[1]);
	 putValues(cook[2]);
	 hide.location = cook[0] + "&ACTION=SUBMIT";
	 }
}

function putKeys(keys) {
if (typeof(ds.document.DATA)=="object" ) {
	 ds.document.DATA.keys.value = keys;
	 }
}

function putValues(v) {
if (typeof(ds.document.DATA)=="object" ) {
	 ds.document.DATA.values.value = v;
	 }
}

function takeKeys() {
r ="";
if (typeof(ds.document.DATA)=="object" ) {
	 r = ds.document.DATA.keys.value;
	 }
return r;	 
}

function takeValues() {
r ="";
if (typeof(ds.document.DATA)=="object" ) {
	 r = ds.document.DATA.values.value;
	 }
return r;	 
}

function removeQuery() {
// Etsi haku
var cn = "";
var ma = px.document.forms[0].ma;
if (!ma) {return;}
if (ma.length) {
   for (var i = 0; i < ma.length; i++) {
			 if (ma[i].checked) {cn = ma[i].value; break;}
			 } 
	}
else {cn = ma.value;}	
if (cn.length == 0) {return;}
setQname(cn);
var cook = getQuery(cn).split("!");
//show("1",cook[0],"2",cook[1],"3",cook[2],"4",cook[3],"5",cook[4]);
setSearch(cook[0]);
putKeys(cook[1]);
putValues(cook[2]);
px.location = cook[0] + "&ACTION=REMOVE";
}

function editQuery() {
// Etsi haku
var cn = "";
var ma = px.document.forms[0].ma;
if (!ma) {return;}
if (ma.length) {
   for (var i = 0; i < ma.length; i++) {
			 if (ma[i].checked) {cn = ma[i].value; break;}
			 } 
	}
else {cn = ma.value;}	
if (cn.length == 0) {return;}
setQname(cn);
var cook = getQuery(cn).split("!");
//show("1",cook[0],"2",cook[1],"3",cook[2],"4",cook[3],"5",cook[4]);
setSearch(cook[0]);
putKeys(cook[1]);
putValues(cook[2]);
px.location = cook[0] + "&ACTION=EDIT";
}


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 != "*") {
			 		 res += (s != ".") ? (s != "?") ? s : "[\\S]" : " ";
			 		 }
			 res += (i < r.length-1) ? "|" : "";
			 }
		}
return res;
}

function rfind(ctr,expr) {
expr = rparse(expr);
var reg = new RegExp(expr,"i")
var r = "";
if (ctr.type) {
		if (ctr.type.indexOf("select-multiple") > -1) {
		 var l = ctr.options.length;
	 	 var s;
	 	 for(var i = 0; i < l; i++) {
	 	 		s = ctr.options[i].text + " ";
	 			s = s.search(reg) != -1;
	 			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") {
		 	 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) {
var r = "";
var i,l;
if (ctr.type) {
		if (ctr.type.indexOf("select-multiple") > -1) {
		 	 l = ctr.options.length;
	 	 	 for(i = 0; i < l; i++) {
	 						 if (ctr.options[i].selected) r += ctr.options[i].text + " ;";
	 		 }
	 	}
 }

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-2); 
return r;
}


function varval(w, varnr) {
var form = w.document.forms[0];
var expr = form.expr.value;
var varname = "values" + varnr;
form.res.value=rfind(px.document.forms[0][varname],expr);
px.howmany(varnr, getStubnr());
window.status = "";
}

function openVal(valnr,stnr,aggnr) {
var n = (arguments.length > 2) ? "aggfile(" + aggnr +")": "var" + valnr;
var valname = px.document.forms[0][n].value;
var code = getVal(valnr,valname);
var s = qSize();
var props = 'width=600,height=' + s;
var props = 'left=300,top=0,toolbar=no,location=no,directories=no,menubar=no,status=no,copyhistory=no,scrollbars=no,resizable=YES,' + props;
if (code.length > 0) {
 if (!(typeof(val)!="undefined" && !val.closed)) {
 	 val = window.open("","valw",props);
	 clearvarval();
	 } 
 if (getValnr() != valnr) {
 	val.document.write(code);
 	val.document.close();
	val.resizeTo(600,s);
	}
 setValnr(valnr);
 setStubnr(stnr);
// show("openVal, typeof(val)",typeof(val),"typeof(forsm[0])",typeof(val.document.forms[0]));
 val.focus();
 if (val.document.forms[0]) val.document.forms[0].expr.focus();
 }
}

function closetolfr() {
if (typeof(tolfr)=="object") {
	 if (!tolfr.closed) tolfr.close();
	 }
}

function closevarval() {
if (typeof(val)=="object") {
	 if (!val.closed) val.close();
	 }
}

function clearvarval() {
setValnr("");
setStubnr("");
}

function onIncheaderUnload() {
closevarval();
closetolfr();
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 evalVal(nr) {
var lk = px.document.links;
if (lk.length > 0 || lk[0].href.indexOf("launchLink") > 0) {
	 lk = getHrefs().split("!");
	 }
var h;
for (var i = 0; i < lk.length; i++) {
		h = (typeof(lk[i]) == "object") ? lk[i].href : lk[i];
 		if (h.length > 0) {
			 if (h.search("openVal") != -1 && h.search(nr + ",") != -1) {
			 		 h = h.substring(h.indexOf("top"));
					 eval(h);
		 			 return;
		 			 }
	  	  } 
		 } 
}
	
function nextVal(nr) {
evalVal(nr + 1);
}

function prevVal(nr) {
evalVal(nr - 1);
}

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 = 'toolbar=no,directories=no,menubar=no,status=no,copyhistory=no,scrollbars=yes,resizable=YES,height=520;width=700';
 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());
	 }
}

function isDs(dsn) {
if (!dsn) dsn = "qrs";
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() {
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=getDsItemByName("cls",name);
 ds.document.SESSION.qlistname.value=name;
 }
}

function qSave() {
if (typeof(ds.document.SESSION)=="object" ) {
 	var name =ds.document.SESSION.qlistname.value;
	if (name.length == 0) {alert("No saving, qlistname 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,\'' + t[4] + '\')">';
	 }
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 == "¤") return "";
	}
else {
	if (!f.checked) 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,txt) {
var r = selQKey(ctl);
if (r.length == 0) return false;
if(!dropConfirm(r,txt)){ val.focus(); return false;}
dropDef(ctl.document.title,r);
s = qDrop(r);
val.document.write(getVal(getValnr(),getQlistName()));
val.document.close();
val.resizeTo(600,qSize(s));
val.focus();
return false;
}

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 pickSelected(ctr,i) {
var f = px.document.forms[0];
var r = rSelected(f.elements["values" + i]);
ctr.form.expr.value = r; 
}

function newQList(ctl) {
var f = ctl.form;
var t = f.expr.value;
var p = f.prompt.value;
var d = "";
var cont = true;
window.focus();
d=qPrompt(p,d);
val.focus();
if (!d) return false;
var list = getQlist();
t =(t.length==0) ?  t + "*" : t; 
var s = qSet(d +  "!" + timeStamp() + "!0!0!" + t);
val.document.write(getVal(getValnr(),getQlistName()));
val.document.close();
val.resizeTo(600,qSize(s));
val.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("qrs." + name) : getCookie(name);
}

function deleteQuery(name) {
if (isDs()) {dropDsItem("qrs." + name); }
else {deleteCookie(name); }
}

function getQueryNames(prefix) {
return (isDs()) ? getDsKeys("qrs") : getCookieNames(prefix);
}

function getNewQueryName(prefix) {
return (isDs()) ? getNewDsItemKey("qrs",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 monitor(){
if (px.location.toString().indexOf("varval") == -1) {
	show("Ohjelma käynnistyy Varval.asp-sivulta!");
	return;
	}
var f = px.document.forms[0];
f.method="get";
f.action="SSMonitor.asp";
f.submit();
}

function makeAsp(s) {
s = unescape(unescape(s));
if( s.indexOf("timeid=") == -1) {show("Tämä haku ei kelpaa (timeid puuttuu)",s);return;}
var ptr,sch;
var i = s.indexOf("?");
ptr = s.substring(0,i+1);
ptr=ptr.replace("SSMonitor","Monitor");
s=s.substring(i);
while(s.indexOf("+") > -1) { s=s.replace("+"," ");} 
s=s.substring(1).split("&");
sch="";
var b,c;
for (i = 0; i < s.length; i++) {
		b=c=false; 
 		if (s[i].indexOf("var")==0 && s[i].charAt(4)== "="){c=true;	}
 		if ((!c) && s[i].indexOf("values")==0 && s[i].charAt(7)== "="){c=true;	}
		if ((!c) && s[i].indexOf("Valdavarden")==0 && s[i].charAt(12)== "="){c=true;	}
 		if ((!c) && s[i].indexOf("xu=")==0){c=true;	}
 		if ((!c) && s[i].indexOf("yp=")==0){c=true;	}
		if (c) {
		     if (ptr.indexOf(s[i]) == -1) {ptr += s[i] + "&"; }
				 }	 
		else {
				 if (s[i].indexOf("pxkonv=")==0) { 
			 	 		s[i]=s[i].replace("pxkonv=","konv=\"") + "\"";
			 			}
			   if ((!b) && s[i].indexOf("lang=")==0) {
			 			 s[i]=s[i].replace("lang=","strLanguage=\"")+ "\"";
			 			 b=true;}
				 if ((!b) && s[i].indexOf("root=")==0) {
			 	 		s[i]=s[i].replace("root=","strRoot=\"")+ "\"";
			 			b=true;}			 
				 if ((!b) && s[i].indexOf("aggselnr=")==0) {
			 	 		s[i]=s[i].replace("aggselnr=","aggreg1=\"")+ "\"";
			 			b=true;}			 		 
				 if ((!b) && s[i].indexOf("prevagg=")==0) {
			 	 		s[i]=s[i].replace("prevagg=","allagg=\"")+ "\"";
			 			b=true;}			 
				 if ((!b) && s[i].indexOf("matrix=")==0) {
			 	 		s[i]=s[i].replace("=","=\"") + "\"";
			 			b=true;}			 		
				 if ((!b) && s[i].indexOf("elim=")==0) {
			 	 		s[i]=s[i].replace("=","=\"") + "\"";
			 			b=true;}			 			  
				 if ((!b) && s[i].indexOf("varparm=")==0) {
			 	 		s[i]=s[i].replace("=","=\"") + "\"";
			 			b=true;}			  
				if ((!b) && s[i].indexOf("ti=")==0) {
			 		  s[i]=s[i].replace("=","=\"") + "\"";
			 		  b=true;}			 			  
				if ((!b) && s[i].indexOf("path=")==0) {
			 		  s[i]="";}
				if ((!b) && s[i].indexOf("infofile=")==0) {
			 		  s[i]=s[i].replace("=","=\"") + "\"";
			 		  b=true;}			 			  
				if ((!b) && s[i].indexOf("stubceller=")==0) {
			 		  s[i]=s[i].replace("stubceller=","noofselStub=\"") + "\"";
			 		  b=true;}			 			  
				if ((!b) && s[i].indexOf("headceller=")==0) {
			 		  s[i]=s[i].replace("headceller=","noofselHead=\"") + "\"";
			 		  b=true;}			 			  
				if ((!b) && s[i].indexOf("case")==0) {
			 		  s[i]="";}			 			  
			 		 sch += s[i];
			 		 sch += (s[i].length >0) ? "\n" : "";
			 }
		 }
ptr=ptr.substring(0,ptr.length-1);		 
var r = '<html><head><title>Monitor</title></head><body>'
+ '<form name="asp"><table summary=""><caption><h3>Liitäntä A1Monitoriin</h3></caption>'
+ '<tr><td><h4>Kopioi A1Monitorin Edit/Main/Web Page URL -kenttään:</h4></td></tr><tr><td>'
+ '<textarea rows="3" cols="70" name="url" onclick="this.select();">' + ptr  
+ '</textarea></td></tr><tr><td><h4>Kopioi ..\\px\\Dialog\\SSInput.asp-tiedostoksi:</h4></td></tr><tr><td>'
+ '<textarea rows="20" cols="70" name="asp" onclick="this.select();">\<%\n' + sch
+ '%\></textarea></td></tr></table></form></body></html>';
var props = 'left=300,top=0,toolbar=no,location=no,directories=no,menubar=no,status=no,copyhistory=no,scrollbars=no,resizable=YES,width=600,height=550';
if (!(typeof(mon)!="undefined" && !mon.closed)) {
 	 mon = window.open("","monw",props);
	 } 
 	mon.document.write(r);
	mon.document.asp.url.select();
 	mon.document.close();
	mon.focus();
}

function isQueryTimeout() {
var r = false;
if (typeof(ds.document.SESSION)=="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" ) {
 ds.document.SESSION.qtimeout.value=val;
 }
}

function startQueryTimeout() {
setQueryTimeout("FALSE");
setTimeout("setQueryTimeout()",60000);
}				 

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=250,height=100';
if (code.length > 0) {
 if (!(typeof(msgw)!="undefined" && !msgw.closed)) {
 	 msgw = window.open("","msgw",props);
	 } 
 	msgw.document.write(code);
 	msgw.document.close();
	}
}

function closeMsgWindow() {
if (typeof(msgw)=="object") {
	 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="/gdemo";
}

function showProps(obj, objName) {
   var result = "";
   for (var i in obj) {
      result += objName + "." + i + " = " + obj[i] + "\n";
   }
   return result;
} 


