// date.js
// von anderen js gemeinsam genutzte funktionen
// version 1.11

var checkout_changed;

function initialize_date()
{
	var gNow = new Date();
	document.getElementById('ci_day').selectedIndex=gNow.getDate()-1;
	document.getElementById('ci_month').selectedIndex=gNow.getMonth();
	var cur_year=gNow.getYear();
	if( cur_year<2000 )
		cur_year+=1900;
	var i=0;
	for ( index=cur_year,i=0; index<=cur_year+1; index++,i++ )
	{
		document.getElementById('ci_year').options[i].text=index;
		document.getElementById('ci_year').options[i].value=index;
	}
	for ( index=cur_year,i=0; index<=cur_year+1; index++,i++ )
	{
		document.getElementById('co_year').options[i].text=index;
		document.getElementById('co_year').options[i].value=index;
	}
	document.getElementById('ci_year').selectedIndex=0;
	document.getElementById('co_year').selectedIndex=0;
	calc_date();
}


function calc_date(force,minstay)
{
	var ml,year,ci_year,ci_month,ci_day,co_year,co_month,co_day;
    if( !minstay )
    	minstay=1;

 	set_month_length('ci_day','ci_month','ci_year');

	ci_year=document.getElementById('ci_year').selectedIndex;
	year=parseInt(document.getElementById('ci_year').options[ci_year].value);
	ci_month=document.getElementById('ci_month').selectedIndex+1;
	ci_day=document.getElementById('ci_day').selectedIndex+1;

	ml=[31,28,31,30,31,30,31,31,30,31,30,31];
	if( (year%4)==0 )
	{
		if( (year%100)==0 )
		{
			if( ((year/100)%4)==0 ) { ml[1]=29; }
		}
		else { ml[1]=29; }
	}
	co_year=ci_year;
	co_month=ci_month;
	co_day=ci_day+minstay;
	if( co_day>ml[co_month-1] )
	{
		co_day=minstay;
		co_month++;
		if( co_month>12 )
		{
			co_month=1;
			co_year++;
			if( co_year>2 )
			{
				co_year=2;
				co_day=31;
				co_month=12;
			}
		}
	}

	document.getElementById('co_year').selectedIndex=co_year;
	document.getElementById('co_month').selectedIndex=co_month-1;
	document.getElementById('co_day').selectedIndex=co_day-1;
	set_month_length('co_day','co_month','co_year');
}


function set_month_length(day,month,year)
{
	var ml,cur_day,cur_year;

    var day_element=document.getElementById(day);
    var month_element=document.getElementById(month);
    var year_element=document.getElementById(year);

	ml=[31,28,31,30,31,30,31,31,30,31,30,31];
	cur_year=parseInt(year_element.options[year_element.selectedIndex].value);
	if( (cur_year%4)==0 )
	{
		if( (cur_year%100)==0 )
		{
			if( ((cur_year/100)%4)==0 ) { ml[1]=29; }
		}
		else { ml[1]=29; }
	}

	cur_day=day_element.selectedIndex;

    var new_length=ml[month_element.selectedIndex];
	var cur_length=day_element.options.length;
	if( cur_length>new_length )
		day_element.options.length=new_length;
	if( cur_length<new_length )
	{
		for ( var index=cur_length; index<new_length; index++ )
		{
			day_element.options.length++;
			day_element.options[index].value=index+1;
			day_element.options[index].text=index+1;
		}
	}

	if( cur_day>day_element.length-1 )
	{
		cur_day=day_element.length-1;
	}
	day_element.selectedIndex=cur_day;
}


function set_month_length2(day_form,day,month,year)
{
	var ml,cur_day,cur_year;

    var day_element=document.getElementById(day_form);

	ml=[31,28,31,30,31,30,31,31,30,31,30,31];
	cur_year=year;
	if( (cur_year%4)==0 )
	{
		if( (cur_year%100)==0 )
		{
			if( ((cur_year/100)%4)==0 ) { ml[1]=29; }
		}
		else { ml[1]=29; }
	}

	cur_day=day;

    var new_length=ml[month-1];
	var cur_length=day_element.options.length;
	if( cur_length>new_length )
		day_element.options.length=new_length;
	if( cur_length<new_length )
	{
		for ( var index=cur_length; index<new_length; index++ )
		{
			day_element.options.length++;
			day_element.options[index].value=index+1;
			day_element.options[index].text=index+1;
		}
	}

	if( cur_day>day_element.length-1 )
	{
		cur_day=day_element.length-1;
	}
	day_element.selectedIndex=cur_day;
}


function test_checkout()
{
	var ci_year,ci_month,ci_day,co_year,co_month,co_day;

	ci_year=parseInt(document.getElementById('ci_year').options[document.getElementById('ci_year').selectedIndex].value);
	ci_month=document.getElementById('ci_month').selectedIndex+1;
	ci_day=document.getElementById('ci_day').selectedIndex+1;
	co_year=parseInt(document.getElementById('co_year').options[document.getElementById('co_year').selectedIndex].value);
	co_month=document.getElementById('co_month').selectedIndex+1;
	co_day=document.getElementById('co_day').selectedIndex+1;

    //if( co_year<ci_year )
    //{
    //    document.getElementById('co_year').selectedIndex=document.getElementById('ci_year').selectedIndex;
    //    co_year=ci_year;
    //}
    //if( co_year==ci_year && co_month<ci_month )
    //{
    // 	document.getElementById('co_month').selectedIndex=ci_month-1;
    //    co_month=ci_month;
    //}
    //if( co_year==ci_year && co_month==ci_month && co_day<=ci_day )
    //{
    //    calc_date();
    //}

   	set_month_length('co_day','co_month','co_year');

	if( co_year<ci_year || (co_year==ci_year && co_month<ci_month) || (co_year==ci_year && co_month==ci_month && co_day<=ci_day) )
		return 1;
	else
		return 0;
}


function test_checkin()
{
	var ci_year,ci_month,ci_day;

	ci_year=parseInt(document.getElementById('ci_year').options[document.getElementById('ci_year').selectedIndex].value);
	ci_month=document.getElementById('ci_month').selectedIndex+1;
	ci_day=document.getElementById('ci_day').selectedIndex+1;
	var gNow = new Date();
	var cur_day=gNow.getDate();
	var cur_month=gNow.getMonth()+1;
	var cur_year=gNow.getYear();
	if( cur_year<2000 )
		cur_year+=1900;

//	if( ci_year==cur_year && ci_month<cur_month )
//	{
//		document.getElementById('ci_month').selectedIndex=cur_month-1;
//	}
//	if( ci_year==cur_year && ci_month==cur_month && ci_day<cur_day )
//	{
//		document.getElementById('ci_day').selectedIndex=cur_day-1;
//	}
	if( (ci_year==cur_year && ci_month<cur_month) || (ci_year==cur_year && ci_month==cur_month && ci_day<cur_day) )
		return 1;
	else
		return 0;
}


