var FB_nb_day_delay = 0;

// Form: show arrival date
// this function to replace the traditional start function
// in this case no update is required every year.
function start() {
	var nbm = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
		
	build_year_select();

	MD = new Date();
		
	nday = MD.getDate();
	aday = MD.getDay();
	amois = MD.getMonth();
	ayear = takeYear(MD);
	cur_year = ayear;
			
	nday += FB_nb_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;

	document.idForm.fromday.selectedIndex = indexDay;
	document.idForm.frommonth.selectedIndex = indexMois;
	document.idForm.fromyear.selectedIndex = indexYear;

	update_departure();
}
//
// build year select
function build_year_select() {
		
var cur_date = new Date();
var cur_year = takeYear(cur_date);

cur_y = new Option(cur_year, cur_year, true, true);
	
document.idForm.fromyear.options[0] = cur_y;
if(document.idForm.toyear != null) {
	cur_yb = new Option(cur_year, cur_year, true, true);
	document.idForm.toyear.options[0] = cur_yb;
}
	
next_y = new Option(cur_year + 1, cur_year + 1, false, false);
document.idForm.fromyear.options[1] = next_y;
next_yb = new Option(cur_year + 1, cur_year + 1, false, false);
if(document.idForm.toyear != null) {
	document.idForm.toyear.options[1] = next_yb;
	}
}
	
// check departure date to arrival date + 1 day (every time the arrival date is changed)
function check_departure() {
	
	if(document.idForm.today != null) {
		var nbm = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
		
		var ar_day = parseInt(document.idForm.fromday.value) + 1;
		var ar_month = parseInt(document.idForm.frommonth.value);
		var ar_year = parseInt(document.idForm.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);
		
		document.idForm.today.selectedIndex = ar_day - 1;
		document.idForm.tomonth.selectedIndex = ar_month - 1;
		document.idForm.toyear.selectedIndex = ar_year - cur_year;
	}
}

// update departure : check 
function update_departure() {
	
	if(document.idForm.today != null) {
		
		var ar_day = parseInt(document.idForm.fromday.value) + 1;
		var ar_month = parseInt(document.idForm.frommonth.value);
		var ar_year = parseInt(document.idForm.fromyear.value);
				
		var de_day = parseInt(document.idForm.today.value) + 1;
		var de_month = parseInt(document.idForm.tomonth.value);
		var de_year = parseInt(document.idForm.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;
}

function adjustDate( monthObj, dayObj, yearObj ) {
var value = 0;
var mthIdx = monthObj.options.selectedIndex;
var Dt = dayObj;
var theYear = yearObj.options[yearObj.options.selectedIndex].value;
var numDays = getDaysInMonth( mthIdx, theYear );
if( mthIdx == 1 ) {
  if( Dt.options.selectedIndex + 2 < numDays ) {
    return 0;
  } else {
    if( Dt.options.selectedIndex + 1 > numDays) {
      Dt.options.selectedIndex=numDays - 1;
    }
    if( (Dt.options.selectedIndex + 1) == numDays ) {
      return 1;
    } else {
      return 4;
    }
  }
}
if( Dt.options.selectedIndex + 2 < numDays ) {
  value = 0;
} else {
  if ( Dt.options.selectedIndex + 1 > numDays ) {
    Dt.options.selectedIndex--;
    value = 3;
  } else if ( Dt.options.selectedIndex + 1 == numDays ) {
    value = 2;
  } else {
    value = 4;
  }
}
return value;
}

function getDaysInMonth( mthIdx, YrStr ) {
var maxDays = 31
if( mthIdx == 1 ) {
  if( isLeapYear( YrStr ) ) {
    maxDays=29;
  } else {
    maxDays=28;
  }
}

if( mthIdx == 3 || mthIdx == 5 || mthIdx == 8 || mthIdx == 10 ) {
  maxDays=30;
}
return maxDays;
}

function isLeapYear( yrStr ) {
var leapYear = false;
var year = parseInt( yrStr, 10 );
if ( year % 4 == 0 ) {
  leapYear = true;
  if( year % 100 == 0 ) {
    leapYear = false;
    if( year % 400 == 0 ) {
      leapYear=true;
    }
  }
}
return leapYear;
}


