var year = "";
var isEntryTypeDay = false;

function initCaltools(entryTypeDay, entryFormVisible)
{
	isEntryTypeDay = entryTypeDay;
	if (!isEntryTypeDay)
	{
		$('div.oc').mouseenter( function(e) { 
			$('#calTT'+e.target.id).css({"top": (e.pageY+5)+"px", "left": (e.pageX+5)+"px" });
			$('#calTT'+e.target.id).stop(true, true).fadeIn();
		} );
		$('div.oc').mouseleave( function(e) { 
			$('#calTT'+e.target.id).stop(true, true).fadeOut();
		} );
		
		if (entryFormVisible)
		{
			$("#fromdate").datepicker();
			$('#fromdate').change( function(event) { checkUpdateToDate(); } );
			$('#fromtime').change( function(event) { checkUpdateToDate(); } );
			$("#todate").datepicker();
			$('.recalc').change( function(event) { calcDuration(); } );
			$('#todate').change( function(event) { resetDuration(); } );
			$('#totime').change( function(event) { resetDuration(); } );
			$('#duration').change( function(event) { resetTodateAndTime(); } );
			calcDuration();
		}		
	}
}
function checkUpdateToDate()
{
	if ($("#duration").val()!="x")
		return;

	var todateVal = $("#todate").val();

	if (todateVal=="")
	{
		$("#todate").val($("#fromdate").val());
	}
	if (todateVal==$("#fromdate").val())
	{
		fromTimeSelIdx = $("#fromtime").attr("selectedIndex");
		if ($("#totime").attr("selectedIndex") < fromTimeSelIdx)
		{
			$("#totime option:eq("+fromTimeSelIdx+")").attr('selected','selected');
		}
	}
}
function resetDuration()
{
	if (isEntryTypeDay)
		return;

	if ($("#duration").val()=="x")
		return;

	$("#duration option").removeAttr("selected");
	window.document.belform.duration.style.backgroundColor = '#f90';
	window.setTimeout("resetbg()",333);
}
function resetTodateAndTime()
{
	if ($('#todate').val()=="" && ( $('#totime').val()==$('#fromtime').val() || $('#totime').val()=="00:00" ) )
		return;

	$('#todate').val("");
	$("#totime option").removeAttr("selected");
	window.document.belform.todate.style.backgroundColor = '#f90';
	window.document.belform.totime.style.backgroundColor = '#f90';
	window.setTimeout("resetbg()",333);
}

function calcDuration()
{
	if (isEntryTypeDay)
		return;

	try
	{
		var duration = "";

		var durationSel = jQuery.trim($("#duration").val());
		if (durationSel!="x")
		{
			duration = " ";
		}

		try
		{
			if (duration=="")
			{
				var from = jQuery.trim($("#fromdate").val() + " " + $("#fromtime").val());
				var to = jQuery.trim($("#todate").val() + " " + $("#totime").val());
				if (jQuery.trim($("#todate").val())=="")
					to = jQuery.trim($("#fromdate").val() + " " + $("#totime").val());
				var fromDate = getDate(from);
				var toDate = getDate(to);
				var mins = (toDate.getTime() - fromDate.getTime()) / 1000 / 60;

				if (!isNaN(mins))
				{
					if (mins<0)
					{
						duration = " Ups... Das Ende liegt vor dem Start.";
					}
					else
					{
						if (mins>43200) // = größer 30 Tage
						{
							duration += "Mehr als 30 Tage";
						}
						else
						{
							var day =  Math.floor( mins / 60 / 24 );
							var hours = Math.floor( mins / 60 - (day*24));
							mins = mins - day*60*24 - hours*60;
							var comma = "";

							if (day==0 && hours==0 && mins==0)
							{
								duration = " Ups, der Termin ist zu kurz.";
							}
							else
							{
								if (day!=0)
								{
									duration += day + " Tag";
									if (day!=1)
										duration += "e";
									comma = ", ";
								}
								if (hours!=0)
								{
									duration += comma + hours + " Stunde";
									if (hours!=1)
										duration += "n";
									comma = ", ";
								}
								if (mins!=0)
								{
									duration += comma + mins + " Minute";
									if (mins!=1)
										duration += "n";
								}
							}
						}
					}
				}
				else
				{
					duration = " Ups... das Datum ist nicht lesbar";
				}
			}
		}
		catch (exc2) {}
		
		if (duration.length>0 && duration!=" ")
		{
			var isError = (duration.substring(0,4)==" Ups");
			if (isError) 	duration = "<font color='red'>" + duration + "</font>";
			else			duration = "= " + duration;
		}
		$("#durationOut").fadeOut('fast', function() {
			$("#durationOut").html(duration);
			$("#durationOut").fadeIn('fast', function() { } );
		});
	}
	catch (exc) {}
}
function getDate(dateTimeStr)
{
	if (dateTimeStr.length<11)
		return "";

	var tokens = dateTimeStr.split(" ");
	if (tokens.length!=2)
		return "";

	var dateStr = tokens[0];
	var timeStr = tokens[1];

	tokens = dateStr.split(".");
	if (tokens.length!=2 && tokens.length!=3)
		return "";
	var d = tokens[0];
	var m = tokens[1]-1;
	var y = new Date().getFullYear();
	if (tokens.length==3)
		y = tokens[2];

	tokens = timeStr.split(":");
	if (tokens.length!=2)
		return "";
	var h = tokens[0];
	var min = tokens[1];
	return new Date(y, m, d, h, min, 0, 0);
}
function setYear(newyear)
{
	year=newyear;
}

function set(daymonth)
{
	var daymonthstr = ""+daymonth;
	var day = "";
	var month = "";
	var useyear = year;
	var entry = "";
	var strlen = daymonthstr.length;
	
	if (strlen>4)	// ttmmjjjj oder tmmjjjj
	{
		useyear = daymonthstr.substring(strlen-4, strlen);
		daymonthstr = daymonthstr.substring(0, strlen-4);
		strlen = strlen-4;
	}
	
	if (strlen==3)
	{
		day   = daymonthstr.substring(0, 1);
		month = daymonthstr.substring(1, 3);
	}
	else
	{
		day   = daymonthstr.substring(0, 2);
		month = daymonthstr.substring(2, 4);
	}

	if (month.substring(0,1)=="0")
		month = month.substring(1,2);
		
	entry = day+"."+month+"."+useyear;

	var target;
	
	fromFilled 	= (window.document.belform.fromdate.value!="");
	toFilled 	= (window.document.belform.todate.value!="");
	
	if (fromFilled && toFilled)
	{
		window.document.belform.fromdate.value="";
		window.document.belform.todate.value="";
		fromFilled = false;
		toFilled = false;
	}
	
	if (!fromFilled)
	{
		target = window.document.belform.fromdate;
	}
	else
	{
		target = window.document.belform.todate;
		resetDuration();
	}
	
	target.value = entry;
	var oldbg = target.style.background;
	var oldbrd = target.style.border;

	target.style.backgroundColor = '#f90';
	if (fromFilled)
	{
		window.document.belform.remarks.focus();
	}
	window.setTimeout("resetbg()",333);
	calcDuration()
}

function resetbg()
{
	window.document.belform.fromdate.style.backgroundColor = window.document.belform.remarks.style.backgroundColor;
	window.document.belform.todate.style.backgroundColor = window.document.belform.remarks.style.backgroundColor;
	window.document.belform.totime.style.backgroundColor = window.document.belform.remarks.style.backgroundColor;
	window.document.belform.duration.style.backgroundColor = window.document.belform.remarks.style.backgroundColor;
}

