var maxDuration=21;if(maxDuration==0)maxDuration=30;

function init(){var d=new Date(); var y=d.getFullYear(); var lm=document.getElementById('AVP_list_months'); var ld=document.getElementById('AVP_list_days'); var ldm=document.getElementById('AVP_list_dmonths'); var ldd=document.getElementById('AVP_list_ddays'); var ln=document.getElementById('AVP_list_nights'); for(i=0;i<23;i++){ var m=(d.getMonth()+i)%12; if(m==0&&i!=0)y++; appendOption(lm,months[m]+' '+y,y+'-'+(m+1)); if (ldm) appendOption(ldm,months[m]+' '+y,y+'-'+(m+1));}	for(i=1;i<=31;i++){appendOption(ld,i,i); if (ldd) appendOption(ldd,i,i); if(ln && i<=maxDuration) appendOption(ln,i,i);} ld.selectedIndex=d.getDate()-1; if(ldd) ldd.selectedIndex=d.getDate(); setDays(); setDDays();}
function initGuests(){var a=document.getElementById('selectedAdultCount');var c=document.getElementById('selectedChildCount');var i=document.getElementById('selectedInfantCount');if(!a||!c||!i)return;for(var index=1;index<10;index++){appendOption(a,index,index);}for(var index=0;index<10;index++){appendOption(c,index,index);appendOption(i,index,index);}}
function setDays(){var ld=document.getElementById('AVP_list_days'); var lm=document.getElementById('AVP_list_months'); var ym=lm.options[lm.selectedIndex].value; var y=eval(ym.substring(0,4)); var m=eval(ym.substring(5,ym.length)); var d=getDaysInMonth(y,m); if(d>ld.length){ for(i=ld.length;i<d;i++)appendOption(ld,i+1,i+1); } if(d<ld.length){ if(ld.selectedIndex>d-1)ld.selectedIndex=d-1; ld.length=d; updateDDay(); }}
function setDDays(){var ld=document.getElementById('AVP_list_ddays'); var lm=document.getElementById('AVP_list_dmonths'); if (ld && lm){var ym=lm.options[lm.selectedIndex].value; var y=eval(ym.substring(0,4)); var m=eval(ym.substring(5,ym.length)); var d=getDaysInMonth(y,m); if(d>ld.length){for(i=ld.length;i<d;i++)appendOption(ld,i+1,i+1); }	if(d<ld.length){if(ld.selectedIndex>d-1)ld.selectedIndex=d-1; ld.length=d; }}}
function updateDDay(){
var ld=document.getElementById('AVP_list_days');var lm=document.getElementById('AVP_list_months');
var ym=lm.options[lm.selectedIndex].value;var y=eval(ym.substring(0,4));var m=eval(ym.substring(5,ym.length));var d=ld.options[ld.selectedIndex].value;
var ldd=document.getElementById('AVP_list_ddays');var lmd=document.getElementById('AVP_list_dmonths');
if(ldd==null||lmd==null)return;
var ymd=lmd.options[lmd.selectedIndex].value;var yd=eval(ymd.substring(0,4));var md=eval(ymd.substring(5,ymd.length));var dd=ldd.options[ldd.selectedIndex].value;
var aDate=new Date(y,m-1,d);var dDate=new Date(yd,md-1,dd);
if(aDate>dDate){
  dDate=new Date(aDate.getFullYear(),aDate.getMonth(),aDate.getDate()+1);
  ymd=dDate.getFullYear()+'-'+(dDate.getMonth()+1);
  dd=dDate.getDate();
  for(var i=0;i<lmd.options.length;i++){
    if(lmd.options[i].value == ymd){lmd.selectedIndex=i;break;}
  }
  for(var i=0;i<ldd.options.length;i++){
    if(ldd.options[i].value == dd){ldd.selectedIndex=i;break;}
  }
}
}
function appendOption(l,t,v){var e=document.createElement('option');e.text=t;e.value=v;try{l.add(e,null);}catch(ex){l.add(e);}}
function getDaysInMonth(y,m){var days=[31,28,31,30,31,30,31,31,30,31,30,31];var d=days[m-1];if(d==28){if(isLeapYear(y))d=29;}return d/1;}
function isLeapYear(y){if(y%400==0||(y%4==0&&y%100!=0))return true;return false;}
function validate(){var lm=document.getElementById('AVP_list_months'); var ld=document.getElementById('AVP_list_days'); var ym=lm.options[lm.selectedIndex].value; var y=eval(ym.substring(0,4)); var m=eval(ym.substring(5,ym.length)); var d=new Date(); var t=new Date(d.getFullYear(),d.getMonth(),d.getDate()); d=new Date(y,m-1,eval(ld.options[ld.selectedIndex].value)); var nights = 1; var ln=document.getElementById('AVP_list_nights'); var ldm=document.getElementById('AVP_list_dmonths'); var ldd=document.getElementById('AVP_list_ddays'); if (ldm && ldd){var ydm=lm.options[ldm.selectedIndex].value; var yd=eval(ydm.substring(0,4)); var md=eval(ydm.substring(5,ydm.length)); var dd=new Date(); var td=new Date(dd.getFullYear(),dd.getMonth(),dd.getDate()); dd=new Date(yd,md-1,eval(ldd.options[ldd.selectedIndex].value)); var one_day=1000*60*60*24; nights = Math.ceil((dd.getTime()-d.getTime())/one_day); if(d<t || dd <= d || nights > maxDuration){ alert(message); return false; }} else if (ln) { nights = ln.options[ln.selectedIndex].value; if(d<t){alert(message); return false; }} document.getElementById('AVP_arrivalDate').value = lm.options[lm.selectedIndex].value+'-'+ld.options[ld.selectedIndex].value; document.getElementById('AVP_nights').value = nights; document.getElementById('AVP').submit(); }

init();
initGuests();

