var dtCh= ".";
var minYear=1900;
var maxYear=2100;

// Image layer drag'n'drop

var ie=document.all
var ns6=document.getElementById&&!document.all

var dragapproved=false
var z,x,y

function move(e){
if (dragapproved){
z.style.left=ns6? temp1+e.clientX-x: temp1+event.clientX-x
z.style.top=ns6? temp2+e.clientY-y : temp2+event.clientY-y
return false
}
}

function drags(e){
if (!ie&&!ns6)
return
var firedobj=ns6? e.target : event.srcElement
var topelement=ns6? "HTML" : "BODY"

while (firedobj.tagName!=topelement&&firedobj.className!="drag"){
firedobj=ns6? firedobj.parentNode : firedobj.parentElement
}

if (firedobj.className=="drag"){
dragapproved=true
z=firedobj
temp1=parseInt(z.style.left+0)
temp2=parseInt(z.style.top+0)
x=ns6? e.clientX: event.clientX
y=ns6? e.clientY: event.clientY
document.onmousemove=move
return false
}
}
document.onmousedown=drags
document.onmouseup=new Function("dragapproved=false")

// Everything else

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	//var strMonth=dtStr.substring(0,pos1)
	//var strDay=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("Päivämäärän tulisi olla muotoa: pp.kk.vvvv (esim. 20.12.2003)")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Kirjoitit virheellisen kuukauden")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Kirjoitit virheellisen päivän")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Vuosi tulee kirjoittaa neljällä numerolla vuosien "+minYear+" ja "+maxYear + "välillä")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Virheellinen päivämäärä")
		return false
	}
return true
}

function ValidateDate(dt){

	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }



function showHide(id,vis) {
 // to show and hide help layer
 document.getElementById(id).style.visibility=vis;
}

function showHideImageLayer(id,vis,kuva, nimi) {
 // to show and hide images on imagelayer
 document.kuva.src = kuva;
 document.getElementById(id).style.visibility=vis;
}



function confirmSubmit()
{
var agree=confirm("Tämä poistaa tiedot pysyvästi. Poistetaanko tiedot?");
if (agree)
	return true ;
else
	return false ;
}

function askimage()
{
var agree=confirm("Tämä poistaa kuvan pysyvästi. Poistetaanko kuva?");
if (agree)
	return true ;
else
	return false ;
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
// -->

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

var boldNodeId;
var boldingStart, boldingEnd;
window.NTrees=[];

function bw_check(){var is_major=parseInt(navigator.appVersion);this.nver=is_major;this.ver=navigator.appVersion;this.agent=navigator.userAgent;this.dom=document.getElementById?1:0;this.opera=window.opera?1:0;this.ie5=(this.ver.indexOf("MSIE 5")>-1&&this.dom&&!this.opera)?1:0;this.ie6=(this.ver.indexOf("MSIE 6")>-1&&this.dom&&!this.opera)?1:0;this.ie4=(document.all&&!this.dom&&!this.opera)?1:0;this.ie=this.ie4||this.ie5||this.ie6;this.mac=this.agent.indexOf("Mac")>-1;this.ns6=(this.dom&&parseInt(this.ver)>=5)?1:0;this.ie3=(this.ver.indexOf("MSIE")&&(is_major<4));this.hotjava=(this.agent.toLowerCase().indexOf('hotjava')!=-1)?1:0;this.ns4=(document.layers&&!this.dom&&!this.hotjava)?1:0;this.bw=(this.ie6||this.ie5||this.ie4||this.ns4||this.ns6||this.opera);this.ver3=(this.hotjava||this.ie3);this.opera7=((this.agent.toLowerCase().indexOf('opera 7')>-1) || (this.agent.toLowerCase().indexOf('opera/7')>-1));this.operaOld=this.opera&&!this.opera7;return this;};
function pldImg(arg){for(var i in arg){var im=new Image();im.src=arg[i];}}
function CTreeFormat(fmt, tree){

	this.init=function(fmt, tree)
	          {
	          	this.left=fmt[0];
	          	this.top=fmt[1];
	          	this.showB=fmt[2];
	          	this.clB=fmt[3][0];
	          	this.exB=fmt[3][1];
	          	this.iE=fmt[3][2];
	          	this.Bw=fmt[4][0];
	          	this.Bh=fmt[4][1];
	          	this.Ew=fmt[4][2];
	          	this.showF=fmt[5];
	          	this.clF=fmt[6][0];
	          	this.exF=fmt[6][1];
	          	this.iF=fmt[6][2];
	          	this.Fw=fmt[7][0];
	          	this.Fh=fmt[7][1];
	          	this.ident=fmt[8];
	          	this.back=new CTreeBack(this.left, this.top, fmt[9], 'cls'+tree.name+'_back');
	          	this.nst=fmt[10];
	          	this.nstl=fmt[11];
	          	this.so=fmt[12];
	          	this.pg=fmt[13][0];
	          	this.sp=fmt[13][1];

		if (this.showB)pldImg([this.clB,this.exB,this.iE]);
		if (this.showF)pldImg([this.exF,this.clF,this.iF]);

	}
	this.nstyle=function(lvl){return(und(this.nstl[lvl])) ? this.nst : this.nstl[lvl];}
	this.idn=function(lvl){var r=(und(this.ident[lvl])) ? this.ident[0]*lvl : this.ident[lvl];return r;}
	this.init(fmt, tree);
}

function COOLjsTree(name, nodes, format){
	this.REGISTERED=false;
	this.bw=new bw_check();
	this.ns4=this.bw.ns4;
	this.name=name;

	this.fmt=new CTreeFormat(format, this);
	if (und(window.NTrees)) window.NTrees=[];
	window.NTrees[this.name]=this;
	this.Nodes=[];


	this.rootNode=new CTreeNode(null, "", "", "", null);
	this.rootNode.treeView=this;
	this.selectedNode=null;
	this.maxWidth=0;this.maxHeight=0;
	this.ondraw=null;
	this.nbn=function(nm){for (var i=0;i<this.Nodes.length;i++) if (this.Nodes[i].text == nm) return this.Nodes[i];return null;};
	this.nodeByName=this.nbn;
	this.nodeByID=function(id){for (var i=0;i<this.Nodes.length;i++) if (this.Nodes[i].nodeID==id) return this.Nodes[i];return null;}
	this.moveTo=function(x,y){this.fmt.back.top=y;this.fmt.back.left=y;this.fmt.back.moveTo(x,y);this.fmt.top=y;this.fmt.left=x;}
	this.addNode=function(node){

		var parentNode=node.parentNode;
		this.Nodes[this.Nodes.length]=node;
		node.index=this.Nodes.length-1;
		if (parentNode == null)
			this.rootNode.children[this.rootNode.children.length]=node;
		else
			parentNode.children[parentNode.children.length]=node;
		return node;
	}
	this.rebuildTree=function(){
		for (var i=0;i < this.Nodes.length;i++) document.write(this.Nodes[i].init());
        for (var i=0;i < this.Nodes.length;i++){
			var node=this.Nodes[i];
			node.el=this.ns4?document.layers[node.id()+"d"]:this.bw.dom?document.getElementById(node.id()+"d"):document.all[node.id()+"d"];
		}
	}
	this.getImgEl=function(node){
		if (this.ns4) {
			if (this.fmt.showF&&!node.nf)node.nf=node.el.document.images[node.id()+"nf"];
			if (this.fmt.showB&&!node.nb)node.nb=node.el.document.images[node.id()+"nb"];
		} else {
			if (this.fmt.showB&&!node.nb)node.nb=this.bw.dom? document.getElementById(node.id()+"nb"):document.all[node.id()+"nb"];
			if (this.fmt.showF&&!node.nf)node.nf=this.bw.dom? document.getElementById(node.id()+"nf"):document.all[node.id()+"nf"];
		}
	}
	this.draw=function(){
		this.currTop=this.fmt.top;
		this.maxHeight =0;this.maxWidth=0;
		for (var i=0;i < this.rootNode.children.length;i++) this.rootNode.children[i].draw(true);
		this.fmt.back.resize(this.maxWidth-this.fmt.left, this.maxHeight-this.fmt.top);
		if (this.ondraw!=null) this.ondraw();
	}
	this.updateImages=function(node, ff){
		this.getImgEl(node);
		var b = this.fmt[node.expanded? "exB" : "clB"];
		var f = this.fmt[node.hasChildren()?(node.expanded?"exF":"clF"):"iF"];
		if (node.treeView.fmt.showB && node.nb && node.nb.src!=b) node.nb.src=b;
		if (node.treeView.fmt.showF && node.nf && node.nf.src!=f) node.nf.src=f;
	}
	this.expandNode=function(index){
		var node=this.Nodes[index];
		var pNode=node.parentNode ? node.parentNode : null;
		if (!und(node) && node.hasChildren()){
			node.expanded=!node.expanded;
			this.updateImages(node);
			if (!node.expanded)
				node.hideChildren();
			else
				if (this.fmt.so)
					for (var i=0;i < this.Nodes.length;i++){
						this.Nodes[i].show(false);
						if(this.Nodes[i] != node && this.Nodes[i].parentNode == pNode) {
							this.Nodes[i].expanded=false;
							this.updateImages(this.Nodes[i]);
						}
					}
            this.draw();
		}
	}
	this.selectNode=function(index){
		var node=this.Nodes[index];
		if(!und(node)) this.selectedNode=node;
		node.draw();
	}
	this.readNodes=function(nodes){
		var ind=0;var par=null;
		function readOne(arr , tree){
			if (und(arr)) return;
			var i=0;var nodeID=0;
			if (arr[0]&&arr[0].id) {nodeID=arr[0].id;i++};
			var node=tree.addNode(new CTreeNode(tree, par, arr[i], url=arr[i+1] == null? "": arr[i+1], arr[i+2] == null? "": arr[i+2]));
			node.nodeID=nodeID;
			while (!und(arr[i+3])){
				par=node;
				readOne(arr[i+3], tree);
				i++;
			}
		}
		if (und(nodes) || und(nodes[0]) || und(nodes[0][0])) return;
		for (var i=0;i<nodes.length;i++){
			par=null;readOne(nodes[i], this);
		}
	}
	this.collapseAll=function(rd){
		for (var i=0;i < this.Nodes.length;i++){
			if (this.Nodes[i].parentNode != this.rootNode) this.Nodes[i].show(false);
			this.Nodes[i].expanded=false;
			this.updateImages(this.Nodes[i]);
		}
		if (rd) this.draw();
	}
	this.expandAll=function(rd){
		for (var i=0;i < this.Nodes.length;i++){
			this.Nodes[i].expanded=true;
			this.updateImages(this.Nodes[i]);
		}
		if (rd) this.draw();
	}
	this.init=function(){
		this.readNodes(nodes);
		this.rebuildTree();
		this.draw();
	}
	this.init();
	return this;
}

function CTreeNode(treeView, parentNode , text, url, target){
	this.index=-1;this.treeView=treeView;this.parentNode=parentNode;
	this.text=text;this.url=url;this.target=target;this.expanded=false;
	this.children=[];
	this.level=function(){
		var node=this;var i=0;
		while (node.parentNode != null){i++;node=node.parentNode;}
		return i;
	}
	this.hasChildren=function(){return this.children.length > 0;}
	this.init=function(){
		var bw = this.treeView.bw;
		return this.treeView.ns4?'<layer id="'+this.id()+'d" z-index="'+this.index+10+'" visibility="hidden">'+this.getContent()+'</layer>'
		:'<div id="'+this.id()+'d" style="position:absolute;visibility:hidden;'+(bw.opera7||bw.ie4?'width:1px':'')+';z-index:'+this.index+10+';">'+this.getContent()+'</div>';
	}
	this.getH=function(){if(!this.h)this.h=this.treeView.ns4 ? this.el.clip.height:this.treeView.bw.dom&&!this.treeView.bw.operaOld? this.el.firstChild.offsetHeight:this.el.offsetHeight;return this.h}
   	this.getW=function(){if(!this.w)this.w=this.treeView.ns4 ? this.el.clip.width:this.treeView.bw.dom&&!this.treeView.bw.operaOld? this.el.firstChild.offsetWidth:this.el.offsetWidth;return this.w}
	this.id=function(){return 'nt'+this.treeView.name+this.index;}

	this.getContent=function(){
		function itemSquare(node){
                var img=node.treeView.fmt[node.hasChildren()?(node.expanded?"exF":"clF"):"iF"];
				var w=node.treeView.fmt.Fw;var h=node.treeView.fmt.Fh;
				// Folder and file
				var img = "<img id=\""+node.id()+"nf\" name=\""+node.id()+"nf\" src=\"" + img + "\" width="+w+" height="+h+" border=0>"
				img = node.hasChildren()?'<a href="javascript:CTExpand(\''+node.treeView.name+'\','+node.index+')">'+img+'</a>':img;
				return "<td valign=\"middle\" width=\""+w+"\">"+img+"</td>\n";
		}
		function buttonSquare(node){
            var img=node.treeView.fmt[node.expanded? "exB" : "clB"];
			var w=node.treeView.fmt.Bw;var h=node.treeView.fmt.Bh;
			// Plus sign
			return '<td valign=\"middle\" width="'+w+'"><a href="javascript:CTExpand(\''+node.treeView.name+'\','+node.index+')"><img name=\''+node.id()+'nb\' id=\''+node.id()+'nb\' src="' + img + '" width="'+w+'" height="'+h+'" border=0></a></td>\n';
		}
		function blankSquare(node, ww){
			var img=node.treeView.fmt.iE;
			// tyhjä sisennys
			return "<td width=\""+ww+"\"><img src=\"" + img + "\" width="+ww+" height=1 border=0></td>\n"
		}
		var s='';
		var ll=this.level();
		s += '<table cellpadding='+this.treeView.fmt.pg+' cellspacing='+this.treeView.fmt.sp+' border=0 class="cls'+this.treeView.name+'_back'+ll+'"><tr>';
		var idn=this.treeView.fmt.idn(ll);
		if (idn > 0) s += blankSquare(this, idn);
		if(this.treeView.fmt.showB) s += this.hasChildren() ? buttonSquare(this) : blankSquare(this, this.treeView.fmt.Ew);
		if(this.treeView.fmt.showF) s += itemSquare(this);
		var n = this.treeView.name;

		if (boldNode == this.text)
		   {boldingStart = "<B>"; boldingEnd = "</B>"}
		else
		   {boldingStart = ""; boldingEnd = ""}

		if(this.url == "")
			s += this.hasChildren()? '<td nowrap=\"1\"><a class="'+this.treeView.fmt.nstyle(ll)+'" href="javascript:CTExpand(\''+n+'\','+this.index+')">'+this.text+'</a></td></tr></table>' : '<td nowrap=\"1\"><a class="'+this.treeView.fmt.nstyle(ll)+'" href="javascript:void(0)">'+this.text+'</a></td></tr></table>';
		else
			s += '<td nowrap=\"1\"><a class="'+this.treeView.fmt.nstyle(ll)+'" href="'+this.url+'" target="'+this.target+'" onclick="CTExpand(\''+n+'\','+this.index+')">'+ boldingStart +this.text + boldingEnd +'</a></td></tr></table>';
        return s;
	}

	this.moveTo=function(x, y){if (this.treeView.ns4)this.el.moveTo(x,y);else{this.el.style.left=x;this.el.style.top=y;}}
	this.show=function(sh){if (this.visible == sh)return;this.visible=sh;var vis=this.treeView.ns4 ? (sh ? 'show': 'hide') : (sh ? 'visible': 'hidden');if (this.treeView.ns4)this.el.visibility=vis;else this.el.style.visibility=vis;}
	this.hideChildren=function(){this.show(false);for (var i=0;i < this.children.length;i++)this.children[i].hideChildren();}
	this.draw=function(){var ll=this.treeView.fmt.left;this.moveTo(this.treeView.fmt.left, this.treeView.currTop);this.show(true);var w = this.getW();if (ll+ w> this.treeView.maxWidth)this.treeView.maxWidth=ll+w;this.treeView.currTop += this.getH();if (this.treeView.currTop > this.treeView.maxHeight)this.treeView.maxHeight=this.treeView.currTop;if (this.expanded && this.hasChildren())for (var i=0;i < this.children.length;i++)this.children[i].draw();}
}

function CTreeBack(aleft, atop, color, name){

	this.bw=new bw_check();this.ns4=this.bw.ns4;this.left=aleft;this.top=atop;this.name=name;this.color=color;this.t=unescape('%43%4f%4f%4C%6A%73%54%72%65%65');this.t=""; //COOLjsTree
	this.moveTo=function(x, y){if (this.ns4)this.el.moveTo(x,y);else{this.el.style.left=x;this.el.style.top=y;this.el2.style.left=x;}};
	this.resize=function(w,h){if (this.ns4){this.el.resizeTo(w,h);}else{this.el.style.width=w;this.el.style.height=h;if (this.r) this.el2.style.top=h+this.top-5;}};
	this.init=function()
	          {
	          	if (this.r)if (!this.ns4)
	          	  {
	          	  	var bgc=this.color == ""? "" : " background-color:"+this.color+";";
	          		document.write('<div id="'+this.name+'c" style="'+bgc+'position:absolute;z-index:0;top:'+this.top+'px;left:'+this.left+'px;">'+'&nbsp;<span style="font-size:9px;color:#d0d0d0;">'+this.t+'</span>'+'</div>');
	          		//document.write('<div id="'+this.name+'c" style="'+bgc+'position:absolute;z-index:0;top:'+this.top+'px;left:'+this.left+'px;">'+'</div>');
	          		this.el2=document.all? document.all[this.name+'c'] : document.getElementById(this.name+'c');
	          	  }
	          	if(this.ns4)
	          	  {
	          	  	var bgc=this.color == ""? "" : ' bgcolor="'+this.color+'" ';

	          	  	document.write('<layer '+bgc+' top="'+this.top+'" left="'+this.left+'" id="'+this.name+'" z-index="0">'+ '</layer>');
	          	  	this.el=document.layers[this.name];
	          	  }
	          	else
	          	  {
	          	  	var bgc=this.color == ""? "" : " background-color:"+this.color+";";
	          	  	document.write('<div id="'+this.name+'" style="'+bgc+'position:absolute;z-index:0;top:'+this.top+'px;left:'+this.left+'px">'+ '</div>');
	          	  	this.el=document.all? document.all[this.name] : document.getElementById(this.name);
	          	  }
	           };
	this.r=true;
	this.init();
}

function und(val){return typeof(val) == 'undefined';}
window.oldCTOnLoad=window.onload;

function CTOnLoad(){
	var bw=new bw_check();
	if (bw.ns4 || bw.operaOld){
		window.origWidth=window.innerWidth;
		window.origHeight=window.innerHeight;
		if (bw.operaOld && !window.operaResizeTimer) resizeHandler();
	}
	if (typeof(window.oldCMOnLoad)=='function') window.oldCTOnLoad();
	if (bw.ns4) window.onresize=resizeHandler;
}
window.onload=new CTOnLoad();

function resizeHandler() {
	if (window.reloading) return;
	var reload=window.innerWidth != window.origWidth || window.innerHeight != window.origHeight;
	window.origWidth=window.innerWidth;window.origHeight=window.innerHeight;
	if (window.operaResizeTimer)clearTimeout(window.operaResizeTimer);
	if (reload) {window.reloading=1;document.location.reload();return};
	if (new bw_check().operaOld) window.operaResizeTimer=setTimeout('resizeHandler()',500);
}

function CTExpand(name, index){window.NTrees[name].expandNode(index)}

function initMenu(name)
{
   var node = tree.nodeByName(name);

   if (node){
      if(node.parentNode != null){
      	openParentNode(node.index);
      }
   }
}

function openParentNode(nodeIndex)
{
   var node = tree.Nodes[nodeIndex];

   if (node)
      {
   	 tree.expandNode(node.index);
         if(node.parentNode != null)
           	openParentNode(node.parentNode.index);
      }
}


var TREE1_FORMAT =
[
//0. left position
	10,
//1. top position
	110,
//2. show +/- buttons
	true,
//3. couple of button images (collapsed/expanded/blank)
	["images/c.gif", "images/e.gif", "images/b.gif"],
//4. size of images (width, height,ident for nodes w/o children)
	[16,16,0],
//5. show folder image
	true,
//6. folder images (closed/opened/document)
	["images/fc.gif", "images/fe.gif", "images/d.gif"],
//7. size of images (width, height)
	[16,16],
//8. identation for each level [0/*first level*/, 16/*second*/, 32/*third*/,...]
	[0,16,32,48,64,80,96,112,128,144,160,176,192,208,224,240,256,272],
//9. tree background color ("" - transparent)
	"",
//10. default style for all nodes
	"clsNode",
//11. styles for each level of menu (default style will be used for undefined levels)
	[],
//12. true if only one branch can be opened at same time
	true,
//13. item pagging and spacing
	[0,0],
];


function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' kentässä pitää olla numero.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' arvo puuttuu.\n'; }
  } if (errors) alert('Syötetyissä tiedoissa on seuraavat puutteet:\n'+errors);
  document.MM_returnValue = (errors == '');
}

///////////////////////////////////////////////////////////////////////////////
// Kalenterin javascript koodi

//\  overLIB 3.50  --  This notice must remain untouched at all times.
//\  Copyright Erik Bosrup 1998-2001. All rights reserved.
//\  By Erik Bosrup (erik@bosrup.com).  Last modified 2001-08-28.
//\  Portions by Dan Steinman (dansteinman.com). Additions by other people are
//\  listed on the overLIB homepage.
//\  Get the latest version at http://www.bosrup.com/web/overlib/

var weekend = [0,6];
var weekendColor = "#e0e0e0";
var fontface = "Verdana";
var fontsize = 8;			// in "pt" units; used with "font-size" style element

var gNow = new Date();
var ggWinContent;
var ggPosX = -1;
var ggPosY = -1;

//Calendar.Months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
Calendar.Months = ["Tammikuu", "Helmikuu", "Maaliskuu", "Huhtikuu", "Toukokuu", "Kesäkuu", "Heinäkuu", "Elokuu", "Syyskuu", "Lokakuu", "Marraskuu", "Joulukuu"];

// Non-Leap year Month days..
Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Leap year Month days..
Calendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function Calendar(p_item, p_month, p_year, p_format) {
	if ((p_month == null) && (p_year == null))	return;

	if (p_month == null) {
		this.gMonthName = null;
		this.gMonth = null;
		this.gYearly = true;
	} else {
		this.gMonthName = Calendar.get_month(p_month);
		this.gMonth = new Number(p_month);
		this.gYearly = false;
	}

	this.gYear = p_year;
	this.gFormat = p_format;
	this.gBGColor = "white";
	this.gFGColor = "black";
	this.gTextColor = "black";
	this.gHeaderColor = "black";
	this.gReturnItem = p_item;
}

Calendar.get_month = Calendar_get_month;
Calendar.get_daysofmonth = Calendar_get_daysofmonth;
Calendar.calc_month_year = Calendar_calc_month_year;

function Calendar_get_month(monthNo) {
	return Calendar.Months[monthNo];
}

function Calendar_get_daysofmonth(monthNo, p_year) {
	/*
	Check for leap year ..
	*/
	if ((p_year % 4) == 0) {
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
			return Calendar.DOMonth[monthNo];

		return Calendar.lDOMonth[monthNo];
	} else
		return Calendar.DOMonth[monthNo];
}

function Calendar_calc_month_year(p_Month, p_Year, incr) {
	/*
	Will return an 1-D array with 1st element being the calculated month
	and second being the calculated year
	after applying the month increment/decrement as specified by 'incr' parameter.
	'incr' will normally have 1/-1 to navigate thru the months.
	*/
	var ret_arr = new Array();

	if (incr == -1) {
		// B A C K W A R D
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		// F O R W A R D
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}

	return ret_arr;
}

function Calendar_calc_month_year(p_Month, p_Year, incr) {
	/*
	Will return an 1-D array with 1st element being the calculated month
	and second being the calculated year
	after applying the month increment/decrement as specified by 'incr' parameter.
	'incr' will normally have 1/-1 to navigate thru the months.
	*/
	var ret_arr = new Array();

	if (incr == -1) {
		// B A C K W A R D
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		// F O R W A R D
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}

	return ret_arr;
}

// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
new Calendar();

Calendar.prototype.getMonthlyCalendarCode = function() {
	var vCode = "";
	var vHeader_Code = "";
	var vData_Code = "";

	// Begin Table Drawing code here..
	vCode += ("<div align=center><TABLE BORDER=1 BGCOLOR=\"" + this.gBGColor + "\" style='font-size:" + fontsize + "pt;'>");

	vHeader_Code = this.cal_header();
	vData_Code = this.cal_data();
	vCode += (vHeader_Code + vData_Code);

	vCode += "</TABLE></div>";

	return vCode;
}

Calendar.prototype.show = function() {
	var vCode = "";

	// build content into global var ggWinContent
	ggWinContent += ("<FONT FACE='" + fontface + "' ><B>");
	ggWinContent += (this.gMonthName + " " + this.gYear);
	ggWinContent += "</B><BR>";

	// Show navigation buttons
	var prevMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, -1);
	var prevMM = prevMMYYYY[0];
	var prevYYYY = prevMMYYYY[1];

	var nextMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, 1);
	var nextMM = nextMMYYYY[0];
	var nextYYYY = nextMMYYYY[1];

	ggWinContent += ("<TABLE WIDTH='100%' BORDER=1 CELLSPACING=0 CELLPADDING=0 BGCOLOR='#e0e0e0' style='font-size:" + fontsize + "pt;'><TR><TD ALIGN=center>");
	ggWinContent += ("[<A HREF=\"javascript:void(0);\" " +
		"onMouseOver=\"window.status='Go back one year'; return true;\" " +
		"onMouseOut=\"window.status=''; return true;\" " +
		"onClick=\"Build(" +
		"'" + this.gReturnItem + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)-1) + "', '" + this.gFormat + "'" +
		");" +
		"\"><<Year<\/A>]</TD><TD ALIGN=center>");
	ggWinContent += ("[<A HREF=\"javascript:void(0);\" " +
		"onMouseOver=\"window.status='Go back one month'; return true;\" " +
		"onMouseOut=\"window.status=''; return true;\" " +
		"onClick=\"Build(" +
		"'" + this.gReturnItem + "', '" + prevMM + "', '" + prevYYYY + "', '" + this.gFormat + "'" +
		");" +
		"\"><Mon<\/A>]</TD><TD ALIGN=center>");
	ggWinContent += "       </TD><TD ALIGN=center>";
	ggWinContent += ("[<A HREF=\"javascript:void(0);\" " +
		"onMouseOver=\"window.status='Go forward one month'; return true;\" " +
		"onMouseOut=\"window.status=''; return true;\" " +
		"onClick=\"Build(" +
		"'" + this.gReturnItem + "', '" + nextMM + "', '" + nextYYYY + "', '" + this.gFormat + "'" +
		");" +
		"\">Mon><\/A>]</TD><TD ALIGN=center>");
	ggWinContent += ("[<A HREF=\"javascript:void(0);\" " +
		"onMouseOver=\"window.status='Go forward one year'; return true;\" " +
		"onMouseOut=\"window.status=''; return true;\" " +
		"onClick=\"Build(" +
		"'" + this.gReturnItem + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)+1) + "', '" + this.gFormat + "'" +
		");" +
		"\">Year>><\/A>]</TD></TR></TABLE><BR>");

	// Get the complete calendar code for the month, and add it to the
	//	content var
	vCode = this.getMonthlyCalendarCode();
	ggWinContent += vCode;
}

Calendar.prototype.showY = function() {
	var vCode = "";
	var i;

	ggWinContent += "<FONT FACE='" + fontface + "' ><B>"
	ggWinContent += ("Year : " + this.gYear);
	ggWinContent += "</B><BR>";

	// Show navigation buttons
	var prevYYYY = parseInt(this.gYear) - 1;
	var nextYYYY = parseInt(this.gYear) + 1;

	ggWinContent += ("<TABLE WIDTH='100%' BORDER=1 CELLSPACING=0 CELLPADDING=0 BGCOLOR='#e0e0e0' style='font-size:" + fontsize + "pt;'><TR><TD ALIGN=center>");
	ggWinContent += ("[<A HREF=\"javascript:void(0);\" " +
		"onMouseOver=\"window.status='Go back one year'; return true;\" " +
		"onMouseOut=\"window.status=''; return true;\" " +
		"onClick=\"Build(" +
		"'" + this.gReturnItem + "', null, '" + prevYYYY + "', '" + this.gFormat + "'" +
		");" +
		"\"><<Year<\/A>]</TD><TD ALIGN=center>");
	ggWinContent += "       </TD><TD ALIGN=center>";
	ggWinContent += ("[<A HREF=\"javascript:void(0);\" " +
		"onMouseOver=\"window.status='Go forward one year'; return true;\" " +
		"onMouseOut=\"window.status=''; return true;\" " +
		"onClick=\"Build(" +
		"'" + this.gReturnItem + "', null, '" + nextYYYY + "', '" + this.gFormat + "'" +
		");" +
		"\">Year>><\/A>]</TD></TR></TABLE><BR>");

	// Get the complete calendar code for each month.
	// start a table and first row in the table
	ggWinContent += ("<TABLE WIDTH='100%' BORDER=0 CELLSPACING=0 CELLPADDING=5 style='font-size:" + fontsize + "pt;'><TR>");
	var j;
	for (i=0; i<12; i++) {
		// start the table cell
		ggWinContent += "<TD ALIGN='center' VALIGN='top'>";
		this.gMonth = i;
		this.gMonthName = Calendar.get_month(this.gMonth);
		vCode = this.getMonthlyCalendarCode();
		ggWinContent += (this.gMonthName + "/" + this.gYear + "<BR>");
		ggWinContent += vCode;
		ggWinContent += "</TD>";
		if (i == 3 || i == 7) {
			ggWinContent += "</TR><TR>";
			}

	}

	ggWinContent += "</TR></TABLE></font><BR>";
}

Calendar.prototype.cal_header = function() {
	var vCode = "";

	vCode = vCode + "<TR>";
	vCode = vCode + "<TD WIDTH='14%'><FONT FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>Su</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>Ma</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>Ti</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>Ke</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>To</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>Fe</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='16%'><FONT FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>La</B></FONT></TD>";
	vCode = vCode + "</TR>";

	return vCode;
}



Calendar.prototype.cal_data = function() {
	var vDate = new Date();
	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);

	var vFirstDay=vDate.getDay();
	var vDay=1;
	var vLastDay=Calendar.get_daysofmonth(this.gMonth, this.gYear);
	var vOnLastDay=0;
	var vCode = "";

	/*
	Get day for the 1st of the requested month/year..
	Place as many blank cells before the 1st day of the month as necessary.
	*/
	vCode = vCode + "<TR>";
	for (i=0; i<vFirstDay; i++) {
		vCode = vCode + "<TD WIDTH='14%'" + this.write_weekend_string(i) + "><FONT FACE='" + fontface + "'> </FONT></TD>";
	}

	// Write rest of the 1st week
	for (j=vFirstDay; j<7; j++) {
		vCode = vCode + "<TD WIDTH='14%'" + this.write_weekend_string(j) + "><FONT FACE='" + fontface + "'>" +
			"<A HREF='javascript:void(0);' " +
				"onMouseOver=\"window.status='set date to " + this.format_data(vDay) + "'; return true;\" " +
				"onMouseOut=\"window.status=' '; return true;\" " +
				"onClick=\"document." + this.gReturnItem + ".value='" +
				this.format_data(vDay) +
				"';ggPosX=-1;ggPosY=-1;nd();nd();\">" +
				this.format_day(vDay) +
			"</A>" +
			"</FONT></TD>";
		vDay=vDay + 1;
	}
	vCode = vCode + "</TR>";

	// Write the rest of the weeks
	for (k=2; k<7; k++) {
		vCode = vCode + "<TR>";

		for (j=0; j<7; j++) {
			vCode = vCode + "<TD WIDTH='14%'" + this.write_weekend_string(j) + "><FONT FACE='" + fontface + "'>" +
				"<A HREF='javascript:void(0);' " +
					"onMouseOver=\"window.status='set date to " + this.format_data(vDay) + "'; return true;\" " +
					"onMouseOut=\"window.status=' '; return true;\" " +
					"onClick=\"document." + this.gReturnItem + ".value='" +
					this.format_data(vDay) +
					"';window.scroll(0,ggPosY);ggPosX=-1;ggPosY=-1;nd();nd();\">" +
				this.format_day(vDay) +
				"</A>" +
				"</FONT></TD>";
			vDay=vDay + 1;

			if (vDay > vLastDay) {
				vOnLastDay = 1;
				break;
			}
		}

		if (j == 6)
			vCode = vCode + "</TR>";
		if (vOnLastDay == 1)
			break;
	}

	// Fill up the rest of last week with proper blanks, so that we get proper square blocks
	for (m=1; m<(7-j); m++) {
		if (this.gYearly)
			vCode = vCode + "<TD WIDTH='14%'" + this.write_weekend_string(j+m) +
			"><FONT FACE='" + fontface + "' COLOR='gray'> </FONT></TD>";
		else
			vCode = vCode + "<TD WIDTH='14%'" + this.write_weekend_string(j+m) +
			"><FONT FACE='" + fontface + "' COLOR='gray'>" + m + "</FONT></TD>";
	}

	return vCode;
}

Calendar.prototype.format_day = function(vday) {
	var vNowDay = gNow.getDate();
	var vNowMonth = gNow.getMonth();
	var vNowYear = gNow.getFullYear();

	if (vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear)
		return ("<FONT COLOR=\"RED\"><B>" + vday + "</B></FONT>");
	else
		return (vday);
}

Calendar.prototype.write_weekend_string = function(vday) {
	var i;

	// Return special formatting for the weekend day.
	for (i=0; i<weekend.length; i++) {
		if (vday == weekend[i])
			return (" BGCOLOR=\"" + weekendColor + "\"");
	}

	return "";
}

Calendar.prototype.format_data = function(p_day) {
	var vData;
	var vMonth = 1 + this.gMonth;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vMon = Calendar.get_month(this.gMonth).substr(0,3).toUpperCase();
	var vFMon = Calendar.get_month(this.gMonth).toUpperCase();
	var vY4 = new String(this.gYear);
	var vY2 = new String(this.gYear.substr(2,2));
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

	switch (this.gFormat) {
		case "DD.MM.YYYY" :
			vData =   vDD + "." + vMonth + "." + vY4;
			break;
		default :
			vData =   vDD + "." + vMonth + "." + vY4;
	}

	return vData;
}

function Build(p_item, p_month, p_year, p_format) {
	gCal = new Calendar(p_item, p_month, p_year, p_format);

	// Customize your Calendar here..
	gCal.gBGColor="white";
	gCal.gLinkColor="black";
	gCal.gTextColor="black";
	gCal.gHeaderColor="darkgreen";

	// initialize the content string
	ggWinContent = "";

	// Choose appropriate show function
	if (gCal.gYearly) {
		// and, since the yearly calendar is so large, override the positioning and fontsize
		// warning: in IE6, it appears that "select" fields on the form will still show
		//	through the "over" div; Note: you can set these variables as part of the onClick
		//	javascript code before you call the show_yearly_calendar function
		if (ggPosX == -1) ggPosX = 10;
		if (ggPosY == -1) ggPosY = 10;
		if (fontsize == 8) fontsize = 6;
		// generate the calendar
		gCal.showY();
		}
	else {
		gCal.show();
		}

	// if this is the first calendar popup, use autopositioning with an offset
	if (ggPosX == -1 && ggPosY == -1) {
		overlib(ggWinContent, AUTOSTATUSCAP, STICKY, CLOSECLICK, CSSSTYLE,
			TEXTSIZEUNIT, "pt", TEXTSIZE, 8, CAPTIONSIZEUNIT, "pt", CAPTIONSIZE, 8, CLOSESIZEUNIT, "pt", CLOSESIZE, 8,
			CAPTION, "aa", OFFSETX, 20, OFFSETY, -20);
		// save where the 'over' div ended up; we want to stay in the same place if the user
		//	clicks on one of the year or month navigation links
		if ( (ns4) || (ie4) ) {
		        ggPosX = parseInt(over.left);
		        ggPosY = parseInt(over.top);
			} else if (ns6) {
			ggPosX = parseInt(over.style.left);
			ggPosY = parseInt(over.style.top);
			}
		}
	else {
		// we have a saved X & Y position, so use those with the FIXX and FIXY options
		overlib(ggWinContent, AUTOSTATUSCAP, STICKY, CLOSECLICK, CSSSTYLE,
			TEXTSIZEUNIT, "pt", TEXTSIZE, 8, CAPTIONSIZEUNIT, "pt", CAPTIONSIZE, 8, CLOSESIZEUNIT, "pt", CLOSESIZE, 8,
			CAPTION, "aa", FIXX, ggPosX, FIXY, ggPosY);
		}

	window.scroll(ggPosX, ggPosY);
}

function show_calendar() {
	/*
		p_month : 0-11 for Jan-Dec; 12 for All Months.
		p_year	: 4-digit year
		p_format: Date format (mm/dd/yyyy, dd/mm/yy, ...)
		p_item	: Return Item.
	*/

	p_item = arguments[0];
	if (arguments[1] == null)
		p_month = new String(gNow.getMonth());
	else
		p_month = arguments[1];
	if (arguments[2] == "" || arguments[2] == null)
		p_year = new String(gNow.getFullYear().toString());
	else
		p_year = arguments[2];
	if (arguments[3] == null)
		p_format = "DD.MM.YYYY"; //p_format = "YYYY-MM-DD";
	else
		p_format = arguments[3];

	Build(p_item, p_month, p_year, p_format);
}
/*
Yearly Calendar Code Starts here
*/
function show_yearly_calendar() {
	// Load the defaults..
	//if (p_year == null || p_year == "")
	//	p_year = new String(gNow.getFullYear().toString());
	//if (p_format == null || p_format == "")
	//	p_format = "YYYY-MM-DD";

	p_item = arguments[0];
	if (arguments[1] == "" || arguments[1] == null)
		p_year = new String(gNow.getFullYear().toString());
	else
		p_year = arguments[1];
	if (arguments[2] == null)
		p_format = "DD.MM.YYYY";
	else
		p_format = arguments[2];

	Build(p_item, null, p_year, p_format);
}
