
/* Kalender-Skript */
// var EVENTS_CALENDAR = true;
var CALENDAR_ACTIVE = false;
var CALENDAR_LOCKED = false;
var CALENDAR_DAT_MIN = null;
var CALENDAR_DAT_MAX = null;

var CAL_ARV_MONTH = null;
var CAL_DEP_MONTH = null;

var CAL_AR_MONTHS = new Array("Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember");
var CAL_AR_WKDAYS = new Array("Mo", "Di", "Mi", "Do", "Fr", "Sa", "So");

var CAL_FORM = new Object();

CAL_FORM['ARV'] = new Object();
CAL_FORM['DEP'] = new Object();

CAL_FORM['ARV']['cal_div'] 		= "CAL_MONTH_ARV";
CAL_FORM['ARV']['m_select'] 	= "cal_sel_month_arv";
CAL_FORM['ARV']['txt_input'] 	= "srh_arv_date";

CAL_FORM['DEP']['cal_div'] 		= "CAL_MONTH_DEP";
CAL_FORM['DEP']['m_select'] 	= "cal_sel_month_dep";
CAL_FORM['DEP']['txt_input'] 	= "srh_dep_date";

function cal_setup(min_date, max_date)
{
	CALENDAR_DAT_MIN = min_date;
	CALENDAR_DAT_MAX = max_date;
	
	// Monate intialisieren!
	// dhtml_addToList('f_srh', 'cal_sel_month_arv', "112009", "xx 09", false);
	
	var now_month = cal_subdate(CALENDAR_DAT_MIN, "m", true);
	var now_year  = cal_subdate(CALENDAR_DAT_MIN, "y", true);
	
	var max_month = cal_subdate(CALENDAR_DAT_MAX, "m", true);
	var max_year  = cal_subdate(CALENDAR_DAT_MAX, "y", true);
	
	var i = 0;
	
	do {
		// alert(i);
		// i++;
		
		var opt_value = (now_month + "/" + now_year);
		
		dhtml_addToList('cal_sel_month_arv', opt_value, CAL_AR_MONTHS[now_month - 1] + " " + now_year, false);
		dhtml_addToList('cal_sel_month_dep', opt_value, CAL_AR_MONTHS[now_month - 1] + " " + now_year, false);
		
		now_month++;
		
		if (now_month >= 13)
		{
			now_month = 1;
			now_year++;
		}
		
	} while ((now_year * 100 + now_month) <= (max_year * 100 + max_month));
	
	
	var html = new String();
	
	html += "<table cellspacing=\"2\" cellpadding=\"0\" style=\"font-family: Verdana; font-size: 12px; margin: 1px 0px 1px 0px;\">";
	
	/* Wochentage */
	html += "<tr>";
	
	for (w = 0; w < CAL_AR_WKDAYS.length; w++)
	{
		html += "<td style=\"\" class=\"cal-cell " + (w < 5 ? "cal-weekday" : "cal-weekend") + "\">" + CAL_AR_WKDAYS[w] + "</td>";
	}
	
	html += "</tr>";
	
	/* Tages des Monats einfügen: 7 Spalten x 6 Zeilen = MAX 42 Einträge... */
	
	for (i = 0; i < 6; i++)
	{
		html += "<tr>";
		
		for (j = 0; j < 7; j++)
		{
			// html += "<td id=\"CAL_MONTH_ARV_" + (i * 7 + j) + "\" onClick=\"alert('CAL_MONTH_ARV_" + (i * 7 + j) + "');\">" + (i * 7 + j) + "</td>";
			html += "<td id=\"CAL_MONTH_ARV_" + (i * 7 + j) + "\" class=\"cal-cell\"></td>";
		}
		
		html += "</tr>";
	}
	html += "</table>";
	
	
	document.getElementById('CAL_MONTH_ARV').innerHTML = html;
	
	document.getElementById('CAL_MONTH_DEP').innerHTML = html.replace(/CAL_MONTH_ARV/g, "CAL_MONTH_DEP");
	
	// cal_init();
}

/* Hauptfunktion des Kalenders ... */

// function cal_draw(cal_id, opt_id, current_month, input_date_arv, input_date_dep)
function cal_draw(TYPE, current_month)
{
	var cal_id = CAL_FORM[TYPE]['cal_div'];
	
	var opt = document.getElementById(CAL_FORM[TYPE]['m_select']);
	
	for (i = 0; i < opt.length; i++)
	{
		if (opt.options[i].value == current_month)
		{
			opt.selectedIndex = i;
		}
	}
	
	var input_date_arv = document.getElementsByName(CAL_FORM['ARV']['txt_input'])[0].value;
	var input_date_dep = document.getElementsByName(CAL_FORM['DEP']['txt_input'])[0].value;
	
	month_init = current_month.split("/");
	
	cObjDate = new Date(month_init[1], month_init[0] - 1, "01", 12, 0, 0);
	
	// alert(cObjDate.getDate()+"."+( (cObjDate.getMonth() + 1) < 10 ? "0" + (cObjDate.getMonth() + 1) : (cObjDate.getMonth() + 1))+"."+cObjDate.getFullYear());
	
	if (cObjDate.getDay() == 0)
		var month_offset = 6;
	else
		var month_offset = cObjDate.getDay() - 1;
	
	// selected date
	sObjDate_from = new Date(cal_subdate(input_date_arv, "y"), (cal_subdate(input_date_arv, "m", true) - 1), cal_subdate(input_date_arv, "d", true), 12, 0, 0);
	sObjDate___to = new Date(cal_subdate(input_date_dep, "y"), (cal_subdate(input_date_dep, "m", true) - 1), cal_subdate(input_date_dep, "d", true), 12, 0, 0);
	
	// min selectable date
	mObjDate = new Date(cal_subdate(CALENDAR_DAT_MIN, "y"), (cal_subdate(CALENDAR_DAT_MIN, "m", true) - 1), cal_subdate(CALENDAR_DAT_MIN, "d", true), 12, 0, 0);
	
	// max selectable date
	nObjDate = new Date(cal_subdate(CALENDAR_DAT_MAX, "y"), (cal_subdate(CALENDAR_DAT_MAX, "m", true) - 1), cal_subdate(CALENDAR_DAT_MAX, "d", true), 12, 0, 0);
	
	
	/* Tages des Monats einfügen: 7 Spalten x 6 Zeilen = MAX 42 Einträge... */
	
	for (i = 0; i < 42; i++)
	{
		c_day = (i - month_offset + 1);
		
		tObjDate = new Date(cObjDate.getFullYear(), cObjDate.getMonth(), c_day, 12, 0, 0);
		
		// document.getElementById(cal_id + "_" + i).innerHTML = tObjDate.getDate();
		
		if (tObjDate.getTime() >= mObjDate.getTime() && tObjDate.getTime() <= nObjDate.getTime())
		{
			if (i >= month_offset && cObjDate.getMonth() == tObjDate.getMonth())
			{
				if (( tObjDate.getTime() >= sObjDate_from.getTime() && tObjDate.getTime() <= sObjDate___to.getTime() ) || (tObjDate.getTime() == sObjDate_from.getTime()) )
				{
					// document.getElementById(cal_id + "_" + i).innerHTML = "<b style=\"color: BLUE;\">" + tObjDate.getDate() + "</b>";
					document.getElementById(cal_id + "_" + i).innerHTML = "<a href=\"\" onClick=\"cal_set('" + TYPE + "', '" + cal_validateFmtDate(tObjDate.getDate() + "." + (tObjDate.getMonth() + 1) + "." + tObjDate.getFullYear()) + "'); return false;\" style=\"\">" + tObjDate.getDate() + "</a>";
					
					if (false)
					{
						if (tObjDate.getTime() == sObjDate_from.getTime())
						{
							document.getElementById(cal_id + "_" + i).className = "cal-cell cal-date-selected-arv";
						} else
						if (tObjDate.getTime() == sObjDate___to.getTime())
						{
							document.getElementById(cal_id + "_" + i).className = "cal-cell cal-date-selected-dep";
						} else {
							document.getElementById(cal_id + "_" + i).className = "cal-cell cal-date-in-selected";
						}
					} else {
						// document.getElementById(cal_id + "_" + i).className = "cal-cell cal-date-selected";
						if (TYPE == "ARV" && tObjDate.getTime() == sObjDate_from.getTime())
						{
							document.getElementById(cal_id + "_" + i).className = "cal-cell cal-date-selected-arv";
						} else
						if (TYPE == "DEP" && tObjDate.getTime() == sObjDate___to.getTime())
						{
							document.getElementById(cal_id + "_" + i).className = "cal-cell cal-date-selected-dep";
						} else {
							/*
							document.getElementById(cal_id + "_" + i).className = "cal-cell cal-date-in-selected";
							*/
							document.getElementById(cal_id + "_" + i).className = "cal-cell cal-date-active";
						}
					}
					
				} else {
					// document.getElementById(cal_id + "_" + i).innerHTML = "<b style=\"color: GREEN;\">" + tObjDate.getDate() + "</b>";
					document.getElementById(cal_id + "_" + i).innerHTML = "<a href=\"\" onClick=\"cal_set('" + TYPE + "', '" + cal_validateFmtDate(tObjDate.getDate() + "." + (tObjDate.getMonth() + 1) + "." + tObjDate.getFullYear()) + "'); return false;\" style=\"\">" + tObjDate.getDate() + "</a>";
					document.getElementById(cal_id + "_" + i).className = "cal-cell cal-date-active";
				}
			} else {
				document.getElementById(cal_id + "_" + i).innerHTML = "<span style=\"\">" + tObjDate.getDate() + "</span>";
				document.getElementById(cal_id + "_" + i).className = "cal-cell cal-date-inactive";
			}
		} else {
			
			if (i >= month_offset && cObjDate.getMonth() == tObjDate.getMonth())
			{
				document.getElementById(cal_id + "_" + i).innerHTML = "<span style=\"\">" + tObjDate.getDate() + "</span>";
				document.getElementById(cal_id + "_" + i).className = "cal-cell cal-date-inactive";
			} else {
				document.getElementById(cal_id + "_" + i).innerHTML = "<span style=\"\">" + tObjDate.getDate() + "</span>";
				document.getElementById(cal_id + "_" + i).className = "cal-cell cal-date-inactive";
			}
			
		}
	}
}

function cal_init()
{
	if (!CALENDAR_ACTIVE)
	{
		// Initialisierungen nur vornehmen, wenn Kalender davor inaktiv
		// alert("MIN: " + CALENDAR_DAT_MIN + "\nMAX: " + CALENDAR_DAT_MAX + "\n" + document.f_srh.srh_arv_date.value + " - " + document.f_srh.srh_dep_date.value);
		
		CAL_ARV_MONTH = cal_subdate(document.f_srh.srh_arv_date.value, "m", true) + "/" + cal_subdate(document.f_srh.srh_arv_date.value, "y", true);
		CAL_DEP_MONTH = cal_subdate(document.f_srh.srh_dep_date.value, "m", true) + "/" + cal_subdate(document.f_srh.srh_dep_date.value, "y", true);
		
		// cal_draw('CAL_MONTH_ARV', 'cal_sel_month_arv', CAL_ARV_MONTH, document.f_srh.srh_arv_date, document.f_srh.srh_dep_date);
		// cal_draw('CAL_MONTH_DEP', 'cal_sel_month_dep', CAL_DEP_MONTH, document.f_srh.srh_arv_date, document.f_srh.srh_dep_date);
		
		cal_draw('ARV', CAL_ARV_MONTH);
		cal_draw('DEP', CAL_DEP_MONTH);
		
		cal_update_travel_info();
	}
	
	if (document.getElementById('CAL_MAIN'))
	{
		CALENDAR_ACTIVE = true;
	} else {
		CALENDAR_ACTIVE = false;
	}
	
	cal_open();
}

function cal_deinit()
{
	CALENDAR_ACTIVE = false;
}

function cal_open()
{
	if (CALENDAR_ACTIVE)
	{
		document.getElementById('CAL_MAIN').style.display = 'block';
		document.getElementById('CAL_MAIN').style.visibility = 'visible';
	}
}

function cal_close()
{
	if (CALENDAR_ACTIVE)
	{
		document.getElementById('CAL_MAIN').style.display = 'none';
		document.getElementById('CAL_MAIN').style.visibility = 'hidden';
	}
}

function cal_set(TYPE, date)
{
	if (!CALENDAR_ACTIVE)
		return false;
	
	var date_arv = document.getElementById('srh_arv_date').value;
	var date_dep = document.getElementById('srh_dep_date').value;
	
	var arv_obj_date = new Date(cal_subdate(date_arv, "y"), (cal_subdate(date_arv, "m", true) - 1), cal_subdate(date_arv, "d", true), 12, 0, 0);
	var dep_obj_date = new Date(cal_subdate(date_dep, "y"), (cal_subdate(date_dep, "m", true) - 1), cal_subdate(date_dep, "d", true), 12, 0, 0);
	
	var travel_duration = (dep_obj_date.getTime() - arv_obj_date.getTime()) / (24*60*60*1000);
	
	// Neues Datum setzen
	document.getElementById('srh_' + TYPE.toLowerCase() + '_date').value = date;
	
	if (TYPE == "ARV")
	{
		current_date = new Date(cal_subdate(date, "y"), (cal_subdate(date, "m", true) - 1), cal_subdate(date, "d", true), 12, 0, 0);
		
		dep_obj_date.setTime(current_date.getTime() + (travel_duration *  (24*60*60*1000)));
		
		document.getElementById('srh_dep_date').value = cal_validateFmtDate(dep_obj_date.getDate() + "." + (dep_obj_date.getMonth() + 1) + "." + dep_obj_date.getFullYear());
		
		// Select vorselektieren!
		for (i = 0; i < document.getElementById('cal_sel_month_dep').length; i++)
		{
			if (document.getElementById('cal_sel_month_dep').options[i].value == (dep_obj_date.getMonth() + 1) + "/" + dep_obj_date.getFullYear())
			{
				document.getElementById('cal_sel_month_dep').selectedIndex = i;
				i = document.getElementById('cal_sel_month_dep').length;
			}
		}
	}
	
	// Beide Kalenderteile neu zeichnen
	cal_draw('ARV', document.getElementById('cal_sel_month_arv').value);
	cal_draw('DEP', document.getElementById('cal_sel_month_dep').value);
	
	cal_update_travel_info();
}

function imw_cal_EventOnKeyUp()
{
	if (!CALENDAR_ACTIVE)
		return false;
	
	// cal_set('ARV', document.getElementById('srh_arv_date').value, false);
	// Select vorselektieren!
	var date_arv = document.getElementById('srh_arv_date').value;
	var date_dep = document.getElementById('srh_dep_date').value;
	
	var arv_obj_date = new Date(cal_subdate(date_arv, "y"), (cal_subdate(date_arv, "m", true) - 1), cal_subdate(date_arv, "d", true), 12, 0, 0);
	var dep_obj_date = new Date(cal_subdate(date_dep, "y"), (cal_subdate(date_dep, "m", true) - 1), cal_subdate(date_dep, "d", true), 12, 0, 0);
	
	for (i = 0; i < document.getElementById('cal_sel_month_arv').length; i++)
	{
		if (document.getElementById('cal_sel_month_arv').options[i].value == (arv_obj_date.getMonth() + 1) + "/" + arv_obj_date.getFullYear())
			document.getElementById('cal_sel_month_arv').selectedIndex = i;
		if (document.getElementById('cal_sel_month_dep').options[i].value == (dep_obj_date.getMonth() + 1) + "/" + dep_obj_date.getFullYear())
			document.getElementById('cal_sel_month_dep').selectedIndex = i;
	}
	
	// Beide Kalenderteile neu zeichnen
	cal_draw('ARV', document.getElementById('cal_sel_month_arv').value);
	cal_draw('DEP', document.getElementById('cal_sel_month_dep').value);
	
	cal_update_travel_info();
}

function cal_lock(status)
{
	CALENDAR_LOCKED = status;
}

function cal_locked()
{
	return (CALENDAR_LOCKED == true);
}

function cal_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 cal_validateFmtDate(v_date, year_short)
{
	var temp = v_date.split(".");
	
	if (!year_short)
		year_short = false;
	
	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 == 2)
			temp[2] = "20" + temp[2];
		
		if (year_short)
		{
			temp[2] = temp[2].substr(2, 2);
		}
		
		return temp[0]+"."+temp[1]+"."+temp[2];
	} else {
		return "";
	}
}

function cal_update_travel_info()
{
	// cal_travel_info
	// alert("UPD Info");
	
	var date_arv = document.getElementById('srh_arv_date').value;
	var date_dep = document.getElementById('srh_dep_date').value;
	
	var arv_obj_date = new Date(cal_subdate(date_arv, "y"), (cal_subdate(date_arv, "m", true) - 1), cal_subdate(date_arv, "d", true), 12, 0, 0);
	var dep_obj_date = new Date(cal_subdate(date_dep, "y"), (cal_subdate(date_dep, "m", true) - 1), cal_subdate(date_dep, "d", true), 12, 0, 0);
	
	var travel_duration = Math.round( (dep_obj_date.getTime() - arv_obj_date.getTime()) / (24*60*60*1000) );
	
	// xx.xx.xx - yy.yy.yy
	
	if (travel_duration < 0)
	{
		travel_duration = 0;
	}
	
	var info_txt = "";
	
	info_txt += cal_validateFmtDate( cal_subdate(date_arv, "d", true) + "." + cal_subdate(date_arv, "m", true) + "." + cal_subdate(date_arv, "y"), true );
	info_txt += " &ndash; ";
	info_txt += cal_validateFmtDate( cal_subdate(date_dep, "d", true) + "." + cal_subdate(date_dep, "m", true) + "." + cal_subdate(date_dep, "y"), true );
	info_txt += ", ";
	info_txt += " " + travel_duration + " " + (travel_duration == 1 ? "Nacht" : "N&auml;chte");
	
	if (travel_duration < 1)
	{
		document.getElementById('cal_travel_info').style.color = 'RED';
	} else {
		document.getElementById('cal_travel_info').style.color = 'BLACK';
	}
	
	document.getElementById('cal_travel_info').innerHTML = info_txt;
}

function cal_dec(div_id, cal_type)
{
	var div = document.getElementById(div_id);
	
	if (div.selectedIndex > 0)
		div.selectedIndex--;
	
	cal_draw(cal_type, document.getElementById(div_id).value);
}

function cal_inc(div_id, cal_type)
{
	var div = document.getElementById(div_id);
	
	if (div.selectedIndex < div.length - 1)
		div.selectedIndex++;
	
	cal_draw(cal_type, document.getElementById(div_id).value);
}
