function kk_hotel_openCheckinDateCalendar() {
if (CIcal==null) {
CIcal=new kk_CalendarPopup();
CIcal.setReturnFunction("kk_hotel_setCIDate");
CIcal.setMonthNames(monthNames[0],monthNames[1],monthNames[2],monthNames[3],monthNames[4],monthNames[5],monthNames[6],monthNames[7],monthNames[8],monthNames[9],monthNames[10],monthNames[11]);
CIcal.setDayHeaders(day1,day2,day3,day4,day5,day6,day7);
CIcal.setWeekStartDay(1);
CIcal.setTodayLabel(todayLabel);
}
month=document.searchForm.marrival.options[document.searchForm.marrival.selectedIndex].value;
year=document.searchForm.yarrival.options[document.searchForm.yarrival.selectedIndex].value;
CIcal.showCalendar('checkin',month,year);
return false;
}

function kk_hotel_openCheckoutDateCalendar() {
if (COcal==null) {
COcal=new kk_CalendarPopup();
COcal.setReturnFunction("kk_hotel_setCODate");
COcal.setMonthNames(monthNames[0],monthNames[1],monthNames[2],monthNames[3],monthNames[4],monthNames[5],monthNames[6],monthNames[7],monthNames[8],monthNames[9],monthNames[10],monthNames[11]);
COcal.setDayHeaders(day1,day2,day3,day4,day5,day6,day7);
COcal.setWeekStartDay(1);
COcal.setTodayLabel(todayLabel);
}
month=document.searchForm.mreturn.options[document.searchForm.mreturn.selectedIndex].value;
year=document.searchForm.yreturn.options[document.searchForm.yreturn.selectedIndex].value;
COcal.showCalendar('checkout',month,year);
return false;
}

function kk_hotel_cleanTown(fromField) {
if (fromField=="townSEL"&&(typeof(document.searchForm.townTEXT)!="undefined")){document.searchForm.townTEXT.value="";}
if (fromField=="townTEXT"&&(typeof(document.searchForm.townSEL)!="undefined")){document.searchForm.townSEL.selectedIndex=0;}
}

function kk_hotel_currentYear() {
var now=new Date();
var year=now.getYear();
if(year<999) year+=1900;
return year;
}

function kk_hotel_setCODate(y,m,d) {
var year=kk_hotel_currentYear();
document.searchForm.dreturn.selectedIndex=d-1;
document.searchForm.mreturn.selectedIndex=m-1;
if (y==year) document.searchForm.yreturn.selectedIndex=0;
if (y==year+1) document.searchForm.yreturn.selectedIndex=1;
kk_hotel_getRetWeekDay(d,m,y);
}

function kk_hotel_setCIDate(y,m,d) {
var year=kk_hotel_currentYear();
document.searchForm.darrival.selectedIndex=d-1;
document.searchForm.marrival.selectedIndex=m-1;
if (y==year) document.searchForm.yarrival.selectedIndex=0;
if (y==year+1) document.searchForm.yarrival.selectedIndex=1;
kk_hotel_setMonth();
kk_hotel_setYear();
kk_hotel_getDepWeekDay(d,m,y);
}

function kk_hotel_changed_adults() {
var Max=roomTypeMaxTotal[roomTypeCurr]-(document.forms.searchForm.nbadults.selectedIndex-1);
var idx=document.forms.searchForm.nbchildren.selectedIndex;
if (idx>=Max) document.forms.searchForm.nbchildren.selectedIndex=Max-1;
}

function kk_hotel_changed_children() {
var Max=roomTypeMaxTotal[roomTypeCurr] - (document.forms.searchForm.nbchildren.selectedIndex-1);
var idx=document.forms.searchForm.nbadults.selectedIndex;
if (idx>=Max) document.forms.searchForm.nbadults.selectedIndex=Max-1;
}

function kk_hotel_fill_guests() {
roomType=document.forms.searchForm.roomtype.options[document.forms.searchForm.roomtype.selectedIndex].value;
for (i=0;i<roomTypeValue.length;i++)
if (roomType==roomTypeValue[i]) roomTypeCurr=i;
var adultSel=document.forms.searchForm.nbadults;
var childSel=document.forms.searchForm.nbchildren;
var a=document.forms.searchForm.nbadults.selectedIndex;
var c=document.forms.searchForm.nbchildren.selectedIndex;
for (i=0;i<=roomTypeGuestOption[roomTypeCurr];i++) {
adultSel.options[i]=new Option (i,i,false,false)
childSel.options[i]=new Option (i,i,false,false)
}
childSel.length=roomTypeGuestOption[roomTypeCurr]+1;
adultSel.length=roomTypeGuestOption[roomTypeCurr]+1;
var url=window.location.href;
var reg=new RegExp("\/do\/","g");
if (url.match(reg)&&roomtypeBool=="true") { 
document.forms.searchForm.nbadults.selectedIndex=a;
kk_hotel_changed_adults();
document.forms.searchForm.nbchildren.selectedIndex=c;
kk_hotel_changed_children();
roomtypeBool="false";
}
else if (!url.match(reg)&&roomtypeBool=="true") {
document.forms.searchForm.nbadults.selectedIndex=defaultAdultIndex;
document.forms.searchForm.nbchildren.selectedIndex=0;
roomtypeBool="false";
}
else {
document.forms.searchForm.nbadults.selectedIndex=adultSel.length-1;
document.forms.searchForm.nbchildren.selectedIndex=0;
}
}

function kk_hotel_setMonth() {
document.searchForm.mreturn.selectedIndex=document.searchForm.marrival.selectedIndex;
kk_hotel_updateWeekDay();
}

function kk_hotel_setYear() {
document.searchForm.yreturn.selectedIndex=document.searchForm.yarrival.selectedIndex;
kk_hotel_updateWeekDay();
}

function kk_hotel_updateWeekDay() {
var dd=document.searchForm.darrival.value;
var dm=document.searchForm.marrival.value;
var dy=document.searchForm.yarrival.value;
if (dy%4==0) var DMonthDays=new Array(31,29,31,30,31,30,31,31,30,31,30,31);
else var DMonthDays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
if (dd>DMonthDays[dm-1]) {
dd=DMonthDays[dm-1];
document.searchForm.darrival.value=dd;
}
var rd=document.searchForm.dreturn.value;
var rm=document.searchForm.mreturn.value;
var ry=document.searchForm.yreturn.value;
if (ry%4==0) var RMonthDays=new Array(31,29,31,30,31,30,31,31,30,31,30,31);
else var RMonthDays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
if (rd>RMonthDays[rm-1]) {
rd=RMonthDays[rm-1];
document.searchForm.dreturn.value=rd;
}
kk_hotel_getDepWeekDay(dd,dm,dy);
kk_hotel_getRetWeekDay(rd,rm,ry);
}

function kk_hotel_getDepWeekDay(d,m,y) {
var daysinmonth=[31,28,31,30,31,30,31,31,30,31,30,31];
var day=document.searchForm.darrival.options[document.searchForm.darrival.selectedIndex].value-1;
var month=document.searchForm.marrival.options[document.searchForm.marrival.selectedIndex].value-1;
var year=document.searchForm.yarrival.options[document.searchForm.yarrival.selectedIndex].value;
if (year%4!=0) daysinmonth[1]=29;
if (typeof(document.searchForm.wdday)!="undefined"){
if (daysinmonth[month]>=day) {
var testdate=new Date(y,m-1,d);
var weekday=testdate.getDay();
if (weekday==0) document.searchForm.wdday.value=Sun;
if (weekday==1) document.searchForm.wdday.value=Mon;
if (weekday==2) document.searchForm.wdday.value=Tue;
if (weekday==3) document.searchForm.wdday.value=Wed;
if (weekday==4) document.searchForm.wdday.value=Thu;
if (weekday==5) document.searchForm.wdday.value=Fri;
if (weekday==6) document.searchForm.wdday.value=Sat;
}
}      
}

function kk_hotel_getRetWeekDay(d,m,y) {
var daysinmonth=[31,28,31,30,31,30,31,31,30,31,30,31];
var day=document.searchForm.dreturn.options[document.searchForm.dreturn.selectedIndex].value-1;
var month=document.searchForm.mreturn.options[document.searchForm.mreturn.selectedIndex].value-1;
var year=document.searchForm.yreturn.options[document.searchForm.yreturn.selectedIndex].value;
if (year%4!=0) daysinmonth[1]=29;
if (typeof(document.searchForm.wrday)!="undefined"){
if (daysinmonth[month]>=day) {
var testdate=new Date(y,m-1,d);
var weekday=testdate.getDay();
if (weekday==0) document.searchForm.wrday.value=Sun;
if (weekday==1) document.searchForm.wrday.value=Mon;
if (weekday==2) document.searchForm.wrday.value=Tue;
if (weekday==3) document.searchForm.wrday.value=Wed;
if (weekday==4) document.searchForm.wrday.value=Thu;
if (weekday==5) document.searchForm.wrday.value=Fri;
if (weekday==6) document.searchForm.wrday.value=Sat;
}
}
}

function kk_hotel_ctrlForm(form) {
selectedDepDay=form.darrival.options[form.darrival.selectedIndex].value;
selectedDepMonth=form.marrival.options[form.marrival.selectedIndex].value;
selectedDepYear=form.yarrival.options[form.yarrival.selectedIndex].value;
selectedRetDay=form.dreturn.options[form.dreturn.selectedIndex].value;
selectedRetMonth=form.mreturn.options[form.mreturn.selectedIndex].value;
selectedRetYear=form.yreturn.options[form.yreturn.selectedIndex].value;
selectedDepDate=new Date(selectedDepYear,selectedDepMonth-1,selectedDepDay);
selectedRetDate=new Date(selectedRetYear,selectedRetMonth-1,selectedRetDay);
today=new Date();
tableJJ=[31,29,31,30,31,30,31,31,30,31,30,31];
if (topDestValues== null||topDestValues=="I"||topDestValues=="") {
if (form.townTEXT.value==""||form.townTEXT.value==null) {alert(alertNoDestination);return false;}
else {
for (i=0;i<correctDestination.length;i+=2) {
if (form.townTEXT.value.toUpperCase()==correctDestination[i]) {
form.townTEXT.value=correctDestination[i+1];
i=correctDestination.length;
}
}
form.town.value=form.townTEXT.value;
}
}
else {
if (form.townSEL.selectedIndex==0) {
if (form.townTEXT.value==""||form.townTEXT.value==null) {
alert(alertNoDestination);
return false;
}
else {
for (i=0;i<correctDestination.length;i+=2) {
if (form.townTEXT.value.toUpperCase()==correctDestination[i]) {
form.townTEXT.value=correctDestination[i+1];
i=correctDestination.length;
}
}
form.town.value=form.townTEXT.value;
}
}
else {form.town.value=form.townSEL.options[form.townSEL.selectedIndex].value;}
}
if (selectedDepDay>tableJJ[selectedDepMonth-1]||(selectedRetDay>tableJJ[selectedRetMonth-1])) {
alert(alert1);
return false;
}
selectedDepTime=selectedDepDate.getTime()+82800000;
selectedRetTime=selectedRetDate.getTime()+82800000;
if (today.getTime()>selectedDepTime||today.getTime()>selectedRetTime) {
alert(alert2);
return false;
}
if (selectedRetTime<selectedDepTime) {alert(alert3);return false;}
if ((document.forms.searchForm.nbadults.selectedIndex+document.forms.searchForm.nbchildren.selectedIndex)<=0) {
alert(alertNoPerson);
return false;
}
if ((form.yreturn.selectedIndex==form.yarrival.selectedIndex)&&(form.mreturn.selectedIndex==form.marrival.selectedIndex) &&(form.dreturn.selectedIndex==form.darrival.selectedIndex)) {alert(alert4);return false;}
form.nbnights.value=Math.round((selectedRetTime-selectedDepTime)/(1000*3600*24));
if (typeof (form.priceList)!="undefined") {
interval=form.priceInterval.value;
if (interval==interval1) {form.pricemin.value=0; form.price.value=price1;}
if (interval==interval2) {form.pricemin.value=price1; form.price.value=price2;}
if (interval==interval3) {form.pricemin.value=price2; form.price.value=10000;}
}
return true;
}

function kk_hotel_majDate() {
if (document.searchForm.darrival.value =="01" && document.searchForm.marrival.value =="01" && document.searchForm.dreturn.value =="01" && document.searchForm.mreturn.value=="01") {
var now=new Date();
var tmp=now.getTime()+(86400000*7);
now.setTime(tmp);
var month=now.getMonth()+1;
var year=now.getFullYear();
var day=now.getDate();
document.searchForm.darrival.options[day-1].selected=true;
document.searchForm.marrival.options[month-1].selected=true;
document.searchForm.yarrival.options[year-this_year].selected=true;
var now2=new Date();
tmp=now2.getTime()+(86400000*8);
now2.setTime(tmp);
month=now2.getMonth()+1;
year=now2.getFullYear();
day=now2.getDate();
document.searchForm.dreturn.options[day-1].selected=true;
document.searchForm.mreturn.options[month-1].selected=true;
document.searchForm.yreturn.options[year-this_year].selected=true;
}
}

function kk_hotel_relativeDates() {
var v=new Array();
v["name"]=new Array();
v["val"]=new Array();
var RelativeDay;
var RelativeWeek;
var RelativeMonth;
var RelativeDelay;
var url=location.href;
var urlVars=url.split("?");
var vars=urlVars[1].split("&");
for (var i=0;i<vars.length;i++) {
v["name"][i]=vars[i].split("=")[0];
v["val"][i]=vars[i].split("=")[1];
if (v["name"][i]=="departureRelativeDay") RelativeDay=v["val"][i];
if (v["name"][i]=="departureRelativeWeek") RelativeWeek=v["val"][i];
if (v["name"][i]=="departureRelativeMonth") RelativeMonth=v["val"][i];
if (v["name"][i]=="RelativeDelay") RelativeDelay=v["val"][i];
}
if (RelativeDay==null) RelativeDay=0;
if (RelativeWeek==null) RelativeWeek=0;
if (RelativeMonth==null) RelativeMonth=0;
if (RelativeDelay==null) RelativeDelay=0;
var DateActuelle=new Date();
day=DateActuelle.getDate().toString(10);
month=(DateActuelle.getMonth()+1).toString(10);
this_year=(DateActuelle.getYear()+1900).toString(10);
if (this_year>=3905) {this_year=this_year - 1900;}
if (RelativeDay!=0||RelativeWeek!=0||RelativeMonth!=0) {
var ArriveeHotel=kk_hotel_calculArrival(RelativeDay,RelativeMonth,RelativeWeek);
day=ArriveeHotel.substring(0,2);
month=ArriveeHotel.substring(2,4);
year=ArriveeHotel.substring(4,8);
if (day<10) day=day.substring(1,2);
if (month<10) month=month.substring(1,2);
document.searchForm.darrival.options[day-1].selected=true;
document.searchForm.marrival.options[month].selected=true;
document.searchForm.yarrival.options[year-this_year].selected=true;
}
var Aday=day;
var Amonth=month;
var Ayear=year;
var now2=new Date();
month=now2.getMonth()+1;
this_year=now2.getFullYear();
if (this_year>=3905) {this_year=this_year-1900;}
day=now2.getDate();
if (RelativeDelay!=0) {
var DepartHotel=kk_hotel_calculDeparture(Aday,Amonth,Ayear,RelativeDelay);
day=DepartHotel.substring(0,2);
month=DepartHotel.substring(2,4);
year=DepartHotel.substring(4,8);
if (day<10) day=day.substring(1,2);
if (month<10) month=month.substring(1,2);
document.searchForm.dreturn.options[day-1].selected=true;
document.searchForm.mreturn.options[month].selected=true;
document.searchForm.yreturn.options[year-this_year].selected=true;
}
}

function kk_hotel_calculDeparture(darrival,marrival,yarrival,returnDelay) {
var DECEMBER=11;
var JANUARY=0;
var jour=darrival;
var mois=marrival;
var annee=yarrival;
if (annee>3904) {annee=annee-1900;}
var DateActuelle=new Date();
var retour=returnDelay;
while(retour>0) {
if (jour==kk_hotel_DAY_MAX_MONTH(mois,annee)) {
mois++;
if (mois>DECEMBER) {mois=JANUARY;annee++;}
retour--;
jour=1;
DateActuelle.setDate(jour);
DateActuelle.setMonth(mois);
DateActuelle.setYear(annee);
}
else {
jour++;
DateActuelle.setDate(jour);
DateActuelle.setMonth(mois);
DateActuelle.setYear(annee);
retour--;
}
}
var Jour="";
var Mois="";
var Annee="";
Jour=DateActuelle.getDate().toString(10);
Mois=(DateActuelle.getMonth()+0).toString(10);
Annee=(DateActuelle.getYear()+1900).toString(10);
if (Annee>3904) {Annee=Annee-1900;}
if (Mois<10) Mois="0"+Mois;
if (Jour<10) Jour="0"+Jour;
if (jour==31) Jour=31;
return Jour+Mois+Annee;
}

function kk_hotel_calculArrival(RelativeDay,RelativeMonth,RelativeWeek) {
var CodeJour=-1;
var CodeMois=-1;
var JourNumero=0;
var JourSemaine;
var NbreJoursSemaine;
var DECEMBER=11;
var JANUARY=0;
var DateActuelle=new Date();
var jour=DateActuelle.getDate();
var mois=DateActuelle.getMonth();
var annee=DateActuelle.getYear()+1900;
if (annee>3904) {annee=annee-1900;}
CodeMois=kk_hotel_getCodeMois(RelativeMonth);
CodeJour=kk_hotel_getCodeJour(RelativeDay);
if (CodeMois==-1) {
while (RelativeMonth!=0) {
mois++;
if (mois>DECEMBER) {mois=JANUARY;annee++;}
RelativeMonth--;
}
if (jour>kk_hotel_DAY_MAX_MONTH(mois,annee)) {
jour=1;
mois++;
if(mois>DECEMBER) {mois=JANUARY;annee++;}
}
DateActuelle.setDate(jour);
DateActuelle.setMonth(mois);
DateActuelle.setYear(annee);
}
else {
while(mois!=kk_hotel_getCodeMois(RelativeMonth)) {
if (jour>kk_hotel_DAY_MAX_MONTH(mois,annee)) {
jour=1;
mois++;
}
else mois++;
if (mois>DECEMBER) {
mois=JANUARY;
annee++;
}
}
DateActuelle.setDate(jour);
DateActuelle.setMonth(mois);
DateActuelle.setYear(annee);
}
NbreJoursSemaine=7*RelativeWeek;
if (CodeJour==-1) {
JourNumero=1;
while(RelativeDay+NbreJoursSemaine>0)
{
if (jour==kk_hotel_DAY_MAX_MONTH(mois,annee))
{
mois++;
if (mois>DECEMBER) {
mois=JANUARY;
annee++;
}
RelativeDay--;
DateActuelle.setDate(jour);
DateActuelle.setMonth(mois);
DateActuelle.setYear(annee);
jour=1;
}
else {
jour++;
DateActuelle.setDate(jour);;
DateActuelle.setMonth(mois);
DateActuelle.setYear(annee);
RelativeDay--;
}
}
}
else {
while((JourSemaine!=CodeJour|NbreJoursSemaine>0) & JourNumero==0) {
if (jour==kk_hotel_DAY_MAX_MONTH(mois,annee)) {
mois++;
jour=1;
NbreJoursSemaine--;
DateActuelle.setDate(jour);
DateActuelle.setMonth(mois);
DateActuelle.setYear(annee);
JourSemaine=DateActuelle.getDay()
if (mois>DECEMBER) {mois=JANUARY;annee++;}
}
jour++;
NbreJoursSemaine--;
DateActuelle.setDate(jour);
DateActuelle.setMonth(mois);
DateActuelle.setYear(annee);
JourSemaine=DateActuelle.getDay();
}
}
var Jour="";
var Mois="";
var Annee="";
Jour=DateActuelle.getDate().toString(10);
Mois=(DateActuelle.getMonth()+0).toString(10);
Annee=(DateActuelle.getYear()+1900).toString(10);
if (Annee>3904) {Annee=Annee-1900;}
if (Mois<10) Mois="0"+Mois;
if (Jour<10) Jour="0"+Jour;
if (JourSemaine==31) Jour=31;
return Jour+Mois+Annee;
}

function kk_hotel_isBissextile(annee) {
if (annee%400==0) {return true;}
else if ((annee%4==0)||(annee % 100!=0)) {return true;}
else {return false;}
}

function kk_hotel_DAY_MAX_MONTH(mois,annee) {
if(mois==kk_hotel_getCodeMois("January")||mois==0) return 31;
if(mois==kk_hotel_getCodeMois("March")||mois==2) return 31;
if(mois==kk_hotel_getCodeMois("April")||mois==3)return 30;
if(mois==kk_hotel_getCodeMois("May")||mois==4) return 31;
if(mois==kk_hotel_getCodeMois("June")||mois==5) return 30;
if(mois==kk_hotel_getCodeMois("July")||mois==6) return 31;
if(mois==kk_hotel_getCodeMois("August")||mois==7) return 31;
if(mois==kk_hotel_getCodeMois("September")||mois==8) return 30;
if(mois==kk_hotel_getCodeMois("October")||mois==9) return 1;
if(mois==kk_hotel_getCodeMois("November")||mois==10) return 30;
if(mois==kk_hotel_getCodeMois("December")||mois==11) return 31;
if(mois==kk_hotel_getCodeMois("February")||mois==1) {if (kk_hotel_isBissextile(annee)) return 29; else return 28;}
}

function kk_hotel_getCodeMois(RelativeMonth) {
if (RelativeMonth=="January") return 0;
if (RelativeMonth=="February") return 1;
if (RelativeMonth=="March") return 2;
if (RelativeMonth=="April") return 3;
if (RelativeMonth=="May") return 4;
if (RelativeMonth=="June") return 5;
if (RelativeMonth=="July") return 6;
if (RelativeMonth=="August") return 7;
if (RelativeMonth=="September") return 8;
if (RelativeMonth=="October") return 9;
if (RelativeMonth=="November") return 10;
if (RelativeMonth=="December") return 11;
else return -1;
}

function kk_hotel_getCodeJour(RelativeDay) {
if (RelativeDay=="Monday") return 1;
if (RelativeDay=="Thuesday") return 2;
if (RelativeDay=="Wednesday") return 3;
if (RelativeDay=="Thursday") return 4;
if (RelativeDay=="Friday") return 5;
if (RelativeDay=="Saturday") return 6;
if (RelativeDay=="Sunday") return 0;
else return -1;
}

function kk_hotel_checkRoomtype(form) {
var nbadults=parseInt(form.nbadults.options[form.nbadults.selectedIndex].value);
var nbchilds=0;
if (form.nbchildren.options) {
nbchilds=parseInt(form.nbchildren.options[form.nbchildren.selectedIndex].value);
if (isNaN(nbchilds)) {nbchilds=0;}
}
var nbPeople=nbadults+nbchilds;
var kkRoomType=form.roomtype.options[form.roomtype.selectedIndex].value.toUpperCase();
if (nbPeople==2) {
if (FORBIDEN_ROOMTYPE_BY_PEOPLE[2].indexOf(kkRoomType)!=-1) {return confirm(CONFIRM_ROOMTYPE);}		
}
else if (nbPeople==3) {
if (FORBIDEN_ROOMTYPE_BY_PEOPLE[3].indexOf(kkRoomType)!=-1) {return confirm(CONFIRM_ROOMTYPE);}
}
else if (nbPeople==4) {
if (FORBIDEN_ROOMTYPE_BY_PEOPLE[4].indexOf(kkRoomType)!=-1) {return confirm(CONFIRM_ROOMTYPE);}		
}
else if (nbPeople>=5) {
if (FORBIDEN_ROOMTYPE_BY_PEOPLE[5].indexOf(kkRoomType)!=-1) {return confirm(CONFIRM_ROOMTYPE);}
return true;
}
}
