function CalendarObj(){
	this.currentday = null;
	this.selectedday = new Array();

	this.SelectDay = function(fieldid,dayid,date){
		if(document.getElementById(fieldid+dayid).className=="current"){
			this.currentday = dayid;
		}	
		if(this.selectedday[fieldid]!=null && this.selectedday[fieldid]!="" && document.getElementById(fieldid+this.selectedday[fieldid])){
			document.getElementById(fieldid+this.selectedday[fieldid]).className = (this.selectedday[fieldid]==this.currentday)?"current":"";
		}
		document.getElementById(fieldid+dayid).className = "selected";
		this.selectedday[fieldid] = dayid;
		document.getElementById("date_"+fieldid).value = date;
		this.SetEpoch(fieldid);
	};

	this.NextHour = function(fieldid){
		var hour = Number(document.getElementById("hour_"+fieldid).value);
			hour = (hour==23)?0:hour+1;	
		document.getElementById("hour_"+fieldid).value = (hour<10)?"0"+hour:hour;
		this.SetEpoch(fieldid);
	};

	this.PrevHour = function(fieldid){
		var hour = Number(document.getElementById("hour_"+fieldid).value);
		hour = (hour==0)?23:hour-1;
		document.getElementById("hour_"+fieldid).value = (hour<10)?"0"+hour:hour;
		this.SetEpoch(fieldid);
	};

	this.NextMin = function(fieldid){
		var min = Number(document.getElementById("min_"+fieldid).value);
			min = (min==59)?0:min+1;	
		document.getElementById("min_"+fieldid).value = (min<10)?"0"+min:min;
		this.SetEpoch(fieldid);
	};

	this.PrevMin = function(fieldid){
		var min = Number(document.getElementById("min_"+fieldid).value);
		min = (min==0)?59:min-1;
		document.getElementById("min_"+fieldid).value = (min<10)?"0"+min:min;
		this.SetEpoch(fieldid);		
	};
	
	this.SetEpoch = function(fieldid){
		document.getElementById("hidden_"+fieldid).value = Number(this.selectedday[fieldid])+3600000*Number(document.getElementById("hour_"+fieldid).value)+60000*Number(document.getElementById("min_"+fieldid).value);
	};	

	this.SetTime = function(fieldid,closeafterset){
		if(this.selectedday[fieldid]){
			document.getElementById(fieldid).value = document.getElementById("hidden_"+fieldid).value;
			document.getElementById("displayarea_"+fieldid).innerHTML = document.getElementById("date_"+fieldid).value+" "+document.getElementById("hour_"+fieldid).value+":"+document.getElementById("min_"+fieldid).value;
			if(closeafterset==true){
				this.Close("container_"+fieldid);
			}
		}else{
			alert("Valitse p\u00e4iv\u00e4 kalenterista.");
		}
	};	

	this.SetTimeFromInput = function(fieldid,value,calendarpath){
		if(value.length==0){
			document.getElementById("hidden_"+fieldid).value = "";
			document.getElementById(fieldid).value = "";	
		}
		var aika = value.match(/^\d{1,2}\.\d{1,2}\.\d{4}$/);
		if(aika!=null){
			document.getElementById("date_"+fieldid).style.border = "";
			paiva = Number(value.replace(/^(\d{1,2})\.(\d{1,2})\.(\d{4})$/,"$1"));
			kuukausi = Number(value.replace(/^(\d{1,2})\.(\d{1,2})\.(\d{4})$/,"$2"))-1;
			vuosi = Number(value.replace(/^(\d{1,2})\.(\d{1,2})\.(\d{4})$/,"$3"));
			epochtime = new Date(vuosi,kuukausi,paiva).getTime();
						
			if(this.selectedday[fieldid]!=epochtime){
				var params =  "?path=/Calendar";
					params += "&Calendar="+fieldid;
					params += "&SelectedTime="+epochtime;
					params += "&Mode=Timer";
					params += "&Container=calendar_"+ fieldid;
					params += "&kk="+(kuukausi+1);
					params += "&vuosi="+vuosi;
					params += "&SetTimeOnClick=true";		
					Stato.loadContent('calendar_'+fieldid,calendarpath+params);
			}

			document.getElementById(fieldid).value = epochtime;			
		} else {
			document.getElementById("date_"+fieldid).style.border = "solid 1px red";
		}
	};
	

	this.SetTimeOnClick = function(fieldid,dayid,date){
		this.SelectDay(fieldid,dayid,date);	
		if(this.selectedday[fieldid]){
			document.getElementById(fieldid).value =  dayid;
			this.Close("container_"+fieldid);
		}else{
			alert("Valitse p\u00e4iv\u00e4 kalenterista.");
		}
	};	

	this.Open = function(container){
		document.getElementById(container).style.display="";
	};

	this.Close = function(container){
		document.getElementById(container).style.display="none";
	};
	
	this.ResetTime = function(fieldid){
		document.getElementById("hidden_"+fieldid).value = "";
		document.getElementById(fieldid).value = "";
		document.getElementById("displayarea_"+fieldid).innerHTML = "--.--.---- --:--";
		
	};	
	
	this.GetTime = function(fieldid){
		if(document.getElementById(fieldid)){
			return document.getElementById(fieldid).value;
		}else{
			return "";
		}
	};	
	
}

var Calendar = new CalendarObj();