EVENT_HANDLING['CALENDAR'] = true;
var CAL_ADJUST_OFFSET = 7;
var DIV_MAIN_CALENDAR = null;

/* Sprachabhängige Daten */
txt_months	= new Object();
txt_wkdays	= new Object();

ar_months 	= new Array("Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember");
ar_wkdays 	= new Array("Mo", "Di", "Mi", "Do", "Fr", "Sa", "So");

txt_months["de"] = ar_months;
txt_wkdays["de"] = ar_wkdays;

ar_months 	= new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
ar_wkdays 	= new Array("Mo", "Tu", "We", "Th", "Fr", "Sa", "Su");

txt_months["en"] = ar_months;
txt_wkdays["en"] = ar_wkdays;

ar_months 	= new Array("Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Otobre", "Novembre", "Décembre");
ar_wkdays 	= new Array("Lu", "Ma", "Me", "Je", "Ve", "Sa", "Di");

txt_months["fr"] = ar_months;
txt_wkdays["fr"] = ar_wkdays;

var imw_cal_min = null;
var imw_cal_max = null;
var imw_cal_dst = null;
var imw_cal_div = null;
var imw_cal_dat = null;
var imw_cal_lng = null;
var imw_cal_cci = null; /* Current Cell Index */
var imw_cal_ccc = null; /* Current Cell Content */
var imw_cal_locked = false;
var imw_cal_adj = null; /* Adjust Date */
var imw_cal_i_date = null; /* Adjust Date */

function imw_subdate(strDate, type, toInt)
{
	var return_value = null;
	
	temp = strDate.split(".");
	
	if (type == "d") {
		return_value = temp[0];
	} else 
	if (type == "m") {
		return_value = temp[1];
	} else 
	if (type == "y") {
		return_value = temp[2];
		
		if (return_value.length == 2)
			return_value = "20" + return_value;
	}
	
	if (toInt && return_value)
	{
		if (return_value.substr(0, 1) == "0")
			return parseInt( return_value.substr(1, 1) );
		else
			return parseInt( return_value );
	}
	
	return return_value;
}

function imw_cal_lock()
{
	imw_cal_locked = true;
}

function imw_cal_unlock()
{
	imw_cal_locked = false;
}

function imw_cal_validateFmtDate(v_date)
{
	var temp = v_date.split(".");
	
	if (temp.length == 3 && temp[0] > 0 && temp[0] < 32 && temp[1] > 0 && temp[1] < 13 && temp[2].length < 5)
	{
		if (temp[0].length == 1)
			temp[0] = "0" + temp[0];
		
		if (temp[1].length == 1)
			temp[1] = "0" + temp[1];
		
		if (temp[2].length == 0)
			temp[2] = imw_cal_dst.getFullYear();
		else
		if (temp[2].length == 2)
			temp[2] = "20" + temp[2];
		
		return temp[0]+"."+temp[1]+"."+temp[2];
	} else {
		return "";
	}
}

function imw_cal_init_static(p_date, d_date, d_container, lang)
{
	c_date = new Date();
	c_date.setTime(c_date.getTime() + (24 * 60 * 60 * 1000));
	
	f_month = new Date(c_date.getFullYear(), c_date.getMonth(), 1, 0, 0, 0);
	l_month = new Date(c_date.getFullYear(), c_date.getMonth() + 15, 0, 0, 0, 0);
	
	var d_min = imw_cal_validateFmtDate(f_month.getDate()+"."+(f_month.getMonth() + 1)+"."+f_month.getFullYear());
	var d_max = imw_cal_validateFmtDate(l_month.getDate()+"."+(l_month.getMonth() + 1)+"."+l_month.getFullYear()); // + CAL_ADJUST_OFFSET * 
	var m_day = imw_cal_validateFmtDate(c_date.getDate()+"."+(c_date.getMonth() + 1)+"."+c_date.getFullYear());
	
	c_date.setTime(c_date.getTime() + ( (10 - 1) * 24 * 60 * 60 * 1000));
	document.getElementById(p_date).value = imw_cal_validateFmtDate(c_date.getDate()+"."+(c_date.getMonth() + 1)+"."+c_date.getFullYear());
	
	c_date.setTime(c_date.getTime() + ( CAL_ADJUST_OFFSET * 24 * 60 * 60 * 1000));
	document.getElementById(d_date).value = imw_cal_validateFmtDate(c_date.getDate()+"."+(c_date.getMonth() + 1)+"."+c_date.getFullYear());

	imw_cal_init(d_min, d_max, m_day, d_container, lang);
}

function imw_cal_init(d_min, d_max, m_day, d_container, lang)
{
	imw_cal_div = document.getElementById(d_container);
	
	if (!imw_cal_div)
		return false;
	
	imw_cal_min = new Date(imw_subdate(d_min, "y"), (imw_subdate(d_min, "m", true) - 1), imw_subdate(d_min, "d", true), 12, 0, 0);
	imw_cal_max = new Date(imw_subdate(d_max, "y"), (imw_subdate(d_max, "m", true) - 1), imw_subdate(d_max, "d", true), 12, 0, 0);
	
	imw_cal_lng = lang;
	imw_cal_dst = new Date(imw_subdate(m_day, "y"), (imw_subdate(m_day, "m", true) - 1), imw_subdate(m_day, "d", true), 12, 0, 0);; /* start day */
	
	var cal_table = '';
	
	cal_table += '<table border=0 width=210 cellspacing=0 cellpadding=0>';
	cal_table += '<tr><td height=5></td></tr>';
	cal_table += '<tr>';
	cal_table += '<td align=center>';
	cal_table += '<table class="tcal_head" cellspacing=1 cellpadding=0>';
	cal_table += '<tr>';
	cal_table += '<td width=195>';
	cal_table += '<a href="" onClick="imw_inc_month(-1); return false;" id="imw_cal_BtnLft"><img src="/images/calendar/btn_l.png" border=0 align="top" style="padding-right: 5px;"></a>';
	cal_table += '<select id="imw_cal_month" style="width: 80px; height: 20px;" onMouseDown="imw_cal_lock();" onChange="imw_chg_month(this.value);"></select>';
	cal_table += '&nbsp;';
	cal_table += '<select id="imw_cal_year" style="width: 52px; height: 20px;" onMouseDown="imw_cal_lock();" onChange="imw_chg_year(this.value);"></select>';
	cal_table += '<a href="" onClick="imw_inc_month(1); return false;" id="imw_cal_BtnRgt"><img src="/images/calendar/btn_r.png" border=0 align="top" style="padding-left: 5px;"></a>';
	cal_table += '</td>';
	cal_table += '</tr>';
	cal_table += '</table>';
	cal_table += '</td>';
	cal_table += '</tr>';
	cal_table += '<tr><td height=5></td></tr>';
	cal_table += '<tr>';
	cal_table += '<td align=center>';
	cal_table += '<table class="tcal_body" border=0 cellspacing=1 cellpadding=0>';
	cal_table += '<thead>';
	cal_table += '<tr>';
	
	for (w = 0; w < 7; w++)
	{
		cal_table += '<td id="cal_cwk_' + w + '"></td>';
	}
	
	cal_table += '</tr>';
	cal_table += '</thead>';
	cal_table += '<tbody>';
	
	for (r = 0; r < 6; r++)
	{
		cal_table += '<tr>';
		for (c = 0; c < 7; c++)
		{
			cal_table += '<td id="cal_cmd_' + (c + r * 7) + '"></td>';
		}
		cal_table += '</tr>';
	}
						
	cal_table += '</tbody>';
	cal_table += '</table>';
	cal_table += '</td>';
	cal_table += '</tr>';
	cal_table += '</table>';
	
	imw_cal_div.innerHTML = cal_table;
	
	/* Tabelle erstellen und im Container 'imw_cal_div' schreiben! */
	var d_temp = document.getElementById("imw_cal_month");
	for (i = 0; i < 12; i++)
	{
		d_temp.options[d_temp.length] = new Option( txt_months[imw_cal_lng][i], (i + 1) < 10 ? "0" + (i + 1) : (i + 1) );
	}
	
	var d_temp = document.getElementById("imw_cal_year");
	for (i = imw_subdate(d_min, "y"); i <= imw_subdate(d_max, "y"); i++)
	{
		d_temp.options[d_temp.length] = new Option( i, i );
	}
	
	for (i = 0; i < 7; i++)
	{
		if (i < 5)
			document.getElementById("cal_cwk_" + i).innerHTML = txt_wkdays[imw_cal_lng][i];
		else
			document.getElementById("cal_cwk_" + i).innerHTML = "<b>" + txt_wkdays[imw_cal_lng][i] + "</b>";
	}
}

function imw_cal_EventOnClick()
{
	if (imw_cal_dat && !imw_cal_locked)
	{
		imw_cal_close();
	}
}

function imw_cal_EventOnKeyUp()
{
	if (imw_cal_dat)
	{
		var vld_date = imw_cal_validateFmtDate(imw_cal_dat.value);
		
		if (!vld_date)
		{
			window.status = "Invalid: " + imw_cal_dat.value;
			
			/* Überschreibe letzte Auswahl wenn ungültige Eingabe */
			
			if (imw_cal_cci != -1)
			{
				document.getElementById("cal_cmd_" + imw_cal_cci).innerHTML = "<a href=\"\" onClick=\"imw_cal_set('"+(imw_cal_ccc < 10 ? "0"+imw_cal_ccc : imw_cal_ccc)+"."+(document.getElementById("imw_cal_month").selectedIndex + 1 < 10 ? "0"+(document.getElementById("imw_cal_month").selectedIndex + 1) : (document.getElementById("imw_cal_month").selectedIndex + 1))+"."+document.getElementById("imw_cal_year").options[document.getElementById("imw_cal_year").selectedIndex].value+"'); return false;\">" + imw_cal_ccc + "</a>";
			}
		} else {
			window.status = "VALID! " + imw_cal_dat.value + " ("+vld_date+")";
			
			cObjDate = new Date(imw_subdate(imw_cal_dat.value, "y"), (imw_subdate(imw_cal_dat.value, "m", true) - 1), 1, 12, 0, 0);
			
			imw_cal_crt(cObjDate);
			
			/* Dropdownboxes noch anpassen */
	
			/* ### Selected Index MONTH ### */
			var m_index = (imw_subdate(vld_date, "m", true) - 1);
			
			if (m_index >= 0 && m_index <= 11)
			{
				document.getElementById("imw_cal_month").selectedIndex = cObjDate.getMonth();
			}
			
			/* ### Selected Index YEAR ### */
			document.getElementById("imw_cal_year").selectedIndex = cObjDate.getFullYear() - parseInt(document.getElementById("imw_cal_year").options[0].value);
	
		}
	}
}

function imw_sel_mmyy()
{
	/* ### Selected Index MONTH ### */
	var m_index = (imw_subdate(current_date, "m", true) - 1);
	
	if (m_index >= 0 && m_index <= 11)
	{
		document.getElementById("imw_cal_month").selectedIndex = cObjDate.getMonth();
	} else {
		alert("Ungültiger Monat!");
	}
	
	/* ### Selected Index YEAR ### */
	document.getElementById("imw_cal_year").selectedIndex = cObjDate.getFullYear() - parseInt(document.getElementById("imw_cal_year").options[0].value);
}

function imw_cal_open(i_date, e_container, px, py, adjust_date)
{
	imw_cal_i_date = i_date;
	
	/* ### Kalender initialisieren und zeigen ### */
	// alert(document.getElementById(i_date).value);
	
	if (DIV_MAIN_CALENDAR)
	{
		imw_cal_close();
	}
	
	if (document.getElementById(e_container))
	{
		DIV_MAIN_CALENDAR = document.getElementById(e_container);
	}
	
	if (imw_cal_dat == document.getElementById(i_date))
	{	/* Kalender schon geöffnet! */
		return false;
	}
	
	imw_cal_dat = document.getElementById(i_date);
	imw_cal_adj = document.getElementById(adjust_date);
	
	current_date = imw_cal_dat.value;
	// current_date = "20.07.2009";
	
	if (!imw_cal_validateFmtDate(current_date))
	{
		imw_cal_dat = null;
		imw_cal_adj = null;
		
		imw_cal_unlock();
		
		return false;
	}
	
	cObjDate = new Date(imw_subdate(current_date, "y"), (imw_subdate(current_date, "m", true) - 1), 1, 0, 0, 0);
	
	imw_sel_mmyy();
	
	imw_cal_crt(cObjDate);
	
	if (!BROWSER_CURRENT['msie'])
	{
		if (imw_div_pg_type == 'R')
		{
			if (BROWSER_CURRENT['firefox'])
			{
				py = parseInt(py) + 1;
			}
		} else {
			py -= 3;
		}
	} else {
		
	}
	
	// imw_cal_div.style.left = ( parseInt(px) + parseInt(imw_div_offset_x) ) + "px";
	// imw_cal_div.style.top  = ( parseInt(py) + parseInt(imw_div_offset_y) ) + "px";
	
	// imw_cal_div.style.visibility 	= 'visible';
	// imw_cal_div.style.display 		= 'block';
	
	DIV_MAIN_CALENDAR.innerHTML = imw_cal_div.innerHTML;
	
	DIV_MAIN_CALENDAR.style.visibility 	= 'visible';
	DIV_MAIN_CALENDAR.style.display 	= 'block';
	
	imw_cal_div.innerHTML = "";
	
	imw_sel_mmyy(); // Muss nochmal aufgerufen werden, sonst funktioniert es nicht in Chrome, Firefox und Opera
	
	/*
	DIV_MAIN_CALENDAR.innerHTML = DIV_MAIN_CALENDAR.innerHTML.replace(/imw_cal_/g, i_date + "_imw_cal_");
	
	*/
}

function imw_inc_month(increment)
{
	// inkrementieren: -1 bzw +1
	
	new_index_m = document.getElementById("imw_cal_month").selectedIndex + increment;
	new_index_y = document.getElementById("imw_cal_year").selectedIndex;
	
	if (new_index_m >= 12)
	{
		new_index_m = 0;
		new_index_y++;
		
		if (new_index_y >= document.getElementById("imw_cal_year").length)
		{
			new_index_m = 11;
			new_index_y = document.getElementById("imw_cal_year").length - 1;
		}
	} else
	if (new_index_m <= -1)
	{
		new_index_m = 11;
		new_index_y--;
		
		if (new_index_y <= -1)
		{
			new_index_m = new_index_y = 0;
		}
	}
	
	// alert(new_index_m);
	
	document.getElementById("imw_cal_month").selectedIndex = new_index_m;
	document.getElementById("imw_cal_year").selectedIndex = new_index_y;
	
	var current_date = "01."+(document.getElementById("imw_cal_month").selectedIndex + 1)+"."+document.getElementById("imw_cal_year").options[document.getElementById("imw_cal_year").selectedIndex].value;
	
	cObjDate = new Date(imw_subdate(current_date, "y"), (imw_subdate(current_date, "m", true) - 1), 1, 12, 0, 0);
	
	imw_cal_crt(cObjDate);
}

function imw_chg_month(month)
{
	var current_date = "01."+month+"."+document.getElementById("imw_cal_year").options[document.getElementById("imw_cal_year").selectedIndex].value;
	
	cObjDate = new Date(imw_subdate(current_date, "y"), (imw_subdate(current_date, "m", true) - 1), 1, 12, 0, 0);
	
	imw_cal_crt(cObjDate);
}

function imw_chg_year(year)
{
	var current_date = "01."+( document.getElementById("imw_cal_month").selectedIndex + 1 )+"."+year;
	
	cObjDate = new Date(imw_subdate(current_date, "y"), (imw_subdate(current_date, "m", true) - 1), 1, 12, 0, 0);
	
	imw_cal_crt(cObjDate);
}

function imw_cal_close()
{
	if (!DIV_MAIN_CALENDAR)
		return false;
	
	imw_cal_div.innerHTML = DIV_MAIN_CALENDAR.innerHTML;
	DIV_MAIN_CALENDAR.innerHTML = "";
	
	DIV_MAIN_CALENDAR.style.visibility = 'hidden';
	DIV_MAIN_CALENDAR.style.display = 'none';
	
	if (imw_cal_dat)
	{
		imw_cal_dat.value = imw_cal_validateFmtDate(imw_cal_dat.value);
	}
	
	imw_cal_dat = null;
	imw_cal_adj = null;
	
	DIV_MAIN_CALENDAR = null;
}

function imw_cal_crt(cObjDate)
{
	/* Tabellen füllen */
	
	/* ### Set Days ### */
	// alert(document.getElementById("imw_cal_year").options[0].value);
	
	if (cObjDate.getDay() == 0)
		var month_offset = 6;
	else
		var month_offset = cObjDate.getDay() - 1;
	
	// var month_dmax = cObjDate;
	
	// selected date
	input_date = imw_cal_dat.value;
	sObjDate = new Date(imw_subdate(input_date, "y"), (imw_subdate(input_date, "m", true) - 1), imw_subdate(input_date, "d", true), 12, 0, 0); // new Date(imw_subdate(imw_cal_dst, "y"), (imw_subdate(imw_cal_dst, "m", true) - 1), (imw_subdate(imw_cal_dst, "d", true) - 1), 12, 0, 0);
	
	// min selectable date
	mObjDate = new Date(imw_cal_dst.getFullYear(), imw_cal_dst.getMonth(), imw_cal_dst.getDate(), 12, 0, 0); // new Date(imw_subdate(imw_cal_dst, "y"), (imw_subdate(imw_cal_dst, "m", true) - 1), (imw_subdate(imw_cal_dst, "d", true) - 1), 12, 0, 0);
	
	// max selectable ate
	nObjDate = new Date(imw_cal_max.getFullYear(), imw_cal_max.getMonth(), imw_cal_max.getDate(), 12, 0, 0);
	
	// alert(mObjDate.getDate()+"."+( (mObjDate.getMonth() + 1) < 10 ? "0" + (mObjDate.getMonth() + 1) : (mObjDate.getMonth() + 1))+"."+mObjDate.getFullYear());
	
	imw_cal_cci = -1;
	imw_cal_ccc = "";
	
	for (i = 0; i < 42; i++)
	{
		c_day = (i - month_offset + 1);
		
		tObjDate = new Date(cObjDate.getFullYear(), cObjDate.getMonth(), c_day, 12, 0, 0);
		
		// if (i >= month_offset && cObjDate.getMonth() == tObjDate.getMonth())
		
		if (true)
		{
			if (tObjDate.getTime() >= mObjDate.getTime() && tObjDate.getTime() <= nObjDate.getTime())
			{
				input_date = ((tObjDate.getDate() < 10 ? "0"+tObjDate.getDate() : tObjDate.getDate())+"."+( (tObjDate.getMonth() + 1) < 10 ? "0" + (tObjDate.getMonth() + 1) : (tObjDate.getMonth() + 1))+"."+tObjDate.getFullYear());
				if (i >= month_offset && cObjDate.getMonth() == tObjDate.getMonth())
				{
					if (tObjDate.getTime() == sObjDate.getTime())
					{
						document.getElementById("cal_cmd_" + i).innerHTML = "<a href=\"\" onClick=\"imw_cal_set('"+input_date+"'); return false;\" style=\"border: 2px solid #b70909; height: 15px; padding-top: 1px; width: 23px;\">" + tObjDate.getDate() + "</a>";
						
						imw_cal_cci = i;
						imw_cal_ccc = tObjDate.getDate();
					} else {
						document.getElementById("cal_cmd_" + i).innerHTML = "<a href=\"\" onClick=\"imw_cal_set('"+input_date+"'); return false;\">" + tObjDate.getDate() + "</a>";
					}
				} else {
					document.getElementById("cal_cmd_" + i).innerHTML = "";
				}
			} else {
				if (i >= month_offset && cObjDate.getMonth() == tObjDate.getMonth())
				{
					document.getElementById("cal_cmd_" + i).innerHTML = tObjDate.getDate();
				} else {
					document.getElementById("cal_cmd_" + i).innerHTML = "";
				}
			}
		}
	}
	
	/* ### Buttons ausblenden!? ### */
	if (document.getElementById("imw_cal_month").selectedIndex == 0 && document.getElementById("imw_cal_year").selectedIndex == 0)
	{
		document.getElementById("imw_cal_BtnLft").style.visibility = 'hidden';
		document.getElementById("imw_cal_BtnRgt").style.visibility = 'visible';
	} else
	if (document.getElementById("imw_cal_month").selectedIndex == 11 && document.getElementById("imw_cal_year").selectedIndex == (document.getElementById("imw_cal_year").length - 1) ) {
		document.getElementById("imw_cal_BtnLft").style.visibility = 'visible';
		document.getElementById("imw_cal_BtnRgt").style.visibility = 'hidden';
	} else {
		document.getElementById("imw_cal_BtnLft").style.visibility = 'visible';
		document.getElementById("imw_cal_BtnRgt").style.visibility = 'visible';
	}
}

function imw_cal_set(v_date)
{
	if (imw_cal_dat)
	{
		imw_cal_dat.value = v_date;
	}
	
	if (imw_cal_adj)
	{
		tObjDate = new Date(imw_subdate(v_date, "y"), (imw_subdate(v_date, "m", true) - 1), imw_subdate(v_date, "d", true), 12, 0, 0);
		
		tObjDate.setTime(tObjDate.getTime() + (CAL_ADJUST_OFFSET * 24 * 60 * 60 * 1000));
		
		imw_cal_adj.value = (tObjDate.getDate() < 10 ? "0"+tObjDate.getDate() : tObjDate.getDate())+"."+(tObjDate.getMonth()+1 < 10 ? "0" + (tObjDate.getMonth()+1) : tObjDate.getMonth()+1)+"."+tObjDate.getFullYear();
	}
	
	imw_cal_close();
}