var m_day_delay = 1;

function start() {
	var nbm = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
		
	thisform = document.idForm;

	build_year_select(thisform);

	MD = new Date();
		
	nday = MD.getDate();
	aday = MD.getDay();
	amois = MD.getMonth();
	ayear = takeYear(MD);
	cur_year = ayear;
			
	nday += m_day_delay;

	if(nday > nbm[amois])
	{
		nday -= nbm[amois];
		amois++;
		if(amois > 11) {
			ayear++;
			amois = 0;
		}
	}

	indexDay = nday - 1;
	indexMois = amois;
	indexYear = ayear - cur_year;
		
	if(indexDay < 0 || indexDay > 30)
		indexDay = 0;
	if(indexMois < 0 || indexMois > 11)
		indexMois = 0;
	if(indexYear < 0 || indexYear > 1)
		indexDay = 0;

	thisform.fromday.selectedIndex = indexDay;
	thisform.frommonth.selectedIndex = indexMois;
	thisform.fromyear.selectedIndex = indexYear;

	update_departure();
}

function generateSession()
{
	var time = new Date();
	var sec  = time.getSeconds();
	f = Math.floor(Math.random() * 1000000000) + '';
	var sess = '' + sec + f;
	return sess;
}


// date manager ////////////////////////////////////////////
// build year select
function build_year_select(thisform) {
	var cur_date = new Date();
	var cur_year = takeYear(cur_date);
		
	cur_y = new Option(cur_year, cur_year, true, true);
	thisform.fromyear.options[0] = cur_y;
	if(thisform.toyear != null) {
		cur_yb = new Option(cur_year, cur_year, true, true);
		thisform.toyear.options[0] = cur_yb;
	}
		
	next_y = new Option(cur_year + 1, cur_year + 1, false, false);
	thisform.fromyear.options[1] = next_y;
	next_yb = new Option(cur_year + 1, cur_year + 1, false, false);
	if(thisform.toyear != null) {
		thisform.toyear.options[1] = next_yb;
	}
}
	
// check departure date to arrival date + 1 day (every time the arrival date is changed)
function check_departure() {
	thisform = document.idForm;

	if(thisform.today == null)
		return;

	var nbm = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
	
	var ar_day = parseInt(thisform.fromday.value) + 1;
	var ar_month = parseInt(thisform.frommonth.value);
	var ar_year = parseInt(thisform.fromyear.value);
			
	if(ar_day > nbm[ar_month - 1]) {
		ar_day = 1;
		ar_month += 1;
		if(ar_month > 12) {
			ar_month = 1;
			ar_year += 1;
		}
	}
			
	var cur_date = new Date();
	var cur_year = takeYear(cur_date);
	
	thisform.today.selectedIndex = ar_day - 1;
	thisform.tomonth.selectedIndex = ar_month - 1;
	thisform.toyear.selectedIndex = ar_year - cur_year;
}

// update departure : check 
function update_departure() {
	thisform = document.idForm;

	if(thisform.today == null)
		return;
		
	var ar_day = parseInt(thisform.fromday.value) + 1;
	var ar_month = parseInt(thisform.frommonth.value);
	var ar_year = parseInt(thisform.fromyear.value);
			
	var de_day = parseInt(thisform.today.value) + 1;
	var de_month = parseInt(thisform.tomonth.value);
	var de_year = parseInt(thisform.toyear.value);
	
	if(de_year < ar_year) {
		check_departure();
	} else {
		if(de_year == ar_year) {
			if(de_month < ar_month) {
				check_departure();
			} else {
				if(de_month == ar_month) {
					if(de_day <= ar_day) {
						check_departure();
					}
				}
			}
		}
	}
}

function takeYear(theDate)
{
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}


