
var monthHeaderArray=["January","February","March","April","May","June","July","August","September","October","November","December"]
function getMonthName(ccmnth){return monthHeaderArray[ccmnth];}
function nextMonth(myD){var currMnth=getDateMonth(myD);var currYr=getDateYear(myD);if(currMnth=="11"){nextMnth=0;nextYr=currYr;nextYr++;}
else{nextMnth=currMnth;nextMnth++;nextYr=currYr;}
str=(nextMnth+1)+"/1/"+nextYr;dt=new Date(str);return dt;}
function getNoOfDaysInMnth(ccmnth,yr){return 32-new Date(yr,ccmnth-1,32).getDate();}
function Left(str,n){if(n<=0)
return"";else if(n>String(str).length)
return str;else
return String(str).substring(0,n);}
function Right(str,n){if(n<=0)
return"";else if(n>String(str).length)
return str;else{var iLen=String(str).length;return String(str).substring(iLen,iLen-n);}}
function ToInt(value){if(Left(value,1)=="0")
return parseInt(Right(value,1));else
return parseInt(value);}
function ToString(value){if(value<10)
return"0"+value;else
return value.toString();}
function Trim(str){return str.replace(/^\s*|\s*$/g,"");}
function getDateYear(myDt){return myDt.getFullYear();}
function getDateMonth(myDt){return myDt.getMonth();}
function getDateDay(myDt){return myDt.getDate();}
function GetDateDiff(value1,value2){return(Math.round((value1.getTime()-value2.getTime())/(24*60*60*1000)));}
function genArriveMonthArray(){var monthArray=new Array()
var myDt=new Date();for(var i=0;i<18;i++){var tmpDt=myDt;var otext=getMonthName(getDateMonth(tmpDt))+" "+Right(getDateYear(tmpDt),2);var ovalue=getDateYear(tmpDt)+ToString(getDateMonth(tmpDt)+1);monthArray[i]=new Array(otext,ovalue);myDt=nextMonth(tmpDt);}
return monthArray;}
function genDepartMonthArray(){var monthArray=new Array()
var myDt=GetDateFromString(document.getElementById("selectedarrivemonth").value+document.getElementById("selectedarrivedate").value,0);for(var i=0;i<2;i++){var tmpDt=myDt;var otext=getMonthName(getDateMonth(tmpDt))+" "+Right(getDateYear(tmpDt),2);var ovalue=getDateYear(tmpDt)+ToString(getDateMonth(tmpDt)+1);monthArray[i]=new Array(otext,ovalue);myDt=nextMonth(tmpDt);}
return monthArray;}
function getDateArray(lbound,ubound){var dateArray=new Array();for(var i=0;i<=ubound-lbound;i++){dateArray[i]=i+lbound;}
return dateArray;}
function loadArriveMonth(){var select=document.getElementById("arriveMonth");var monthArray=genArriveMonthArray();for(var i=0;i<monthArray.length;i++){var option=document.createElement("option");option.text=monthArray[i][0];option.value=monthArray[i][1];try{select.add(option,null);}
catch(ex){select.add(option);}}}
function loadDepartMonth(){var select=document.getElementById("departMonth");var optionArray=select.getElementsByTagName("option");var monthArray=genDepartMonthArray();for(var i=optionArray.length-1;i>=0;i--){select.remove(i);}
for(var i=0;i<monthArray.length;i++){var option=document.createElement("option");option.text=monthArray[i][0];option.value=monthArray[i][1];try{select.add(option,null);}
catch(ex){select.add(option);}}}
function loadDate(daycontrol,monthcontrol){var monthselect=document.getElementById(monthcontrol);var sIndex=monthselect.selectedIndex;var option=monthselect.getElementsByTagName("option");var y=Left(option[sIndex].value,4);var m=parseInt(option[sIndex].value.replace(y,""))+1;var ubound=getNoOfDaysInMnth(m-1,y);var lbound=1;var dayselect=document.getElementById(daycontrol);var option=dayselect.getElementsByTagName("option");var dateArray=getDateArray(lbound,ubound);for(var i=option.length-1;i>=0;i--){dayselect.remove(i);}
for(var i=0;i<dateArray.length;i++){var option=document.createElement("option");option.text=dateArray[i];option.value=dateArray[i];if(daycontrol=="arriveDate"&&dateArray[i]==document.getElementById("selectedarrivedate").value)
option.selected=true;if(daycontrol=="departDate"&&dateArray[i]==document.getElementById("selecteddepartdate").value)
option.selected=true;try{dayselect.add(option,null);}
catch(ex){dayselect.add(option);}}}
function getSelectedDate(control){var select=document.getElementById(control);var sIndex=select.selectedIndex;var option=select.getElementsByTagName("option");var y=Left(option[sIndex].value,4);var m=parseInt(option[sIndex].value.replace(y,""));return getNoOfDaysInMnth(m,y);}
function dayOfWeek(){var dt=new Date();return getDateDay(dt);}
function CheckinOnSelect(yyyymm,dd){var curDate=new Date();var checkIndate=GetDPCheckDate(yyyymm,dd);if(GetDateDiff(curDate,checkIndate)>0){checkIndate=curDate;}
InitialArriveDateMonth(checkIndate);loadDepartMonth();if(checkIndate!=null){var checkOutDate=GetDateFromString(GetStringFromDate(checkIndate),24);InitialDepartDateMonth(checkOutDate);SetDPValue(GetStringFromDate(checkIndate),GetStringFromDate(checkOutDate));}
loadDateNights();}
function CheckoutOnSelect(yyyymm,dd){var checkOutdate=GetDPCheckDate(yyyymm,dd);var checkIndate=GetDPCheckDate("arriveMonth","arriveDate");if(GetDateDiff(checkIndate,checkOutdate)>0){checkOutdate=GetDateFromString(GetStringFromDate(checkIndate),24);SetDPValue(GetStringFromDate(checkIndate),GetStringFromDate(checkOutdate));}
if(GetDateDiff(checkOutdate,checkIndate)>30){checkOutdate=GetDateFromString(GetStringFromDate(checkIndate),30*24);SetDPValue(GetStringFromDate(checkIndate),GetStringFromDate(checkOutdate));}
InitialDepartDateMonth(checkOutdate);loadDateNights();}
function GetDPCheckDate(yyyymm,dd){var dpCheckInDate;var dpCheckInString=GetDPControlValue(yyyymm)+ToString(GetDPControlValue(dd));if(dpCheckInString!="")
dpCheckInDate=GetDateFromString(dpCheckInString,0);else
dpCheckInDate=null;return dpCheckInDate;}
function GetDPControlValue(controlname){var controlSelect=document.getElementById(controlname);var controlOption=controlSelect.getElementsByTagName("option");return controlOption[controlSelect.selectedIndex].value;}
function GetDateFromString(datestring,hours){var yr=Left(datestring,4);var mth=Left(datestring.replace(yr,""),2);var dt=Right(datestring,2);var myDate=new Date(ToInt(yr),ToInt(mth)-1,ToInt(dt),hours);return myDate;}
function GetStringFromDate(mydate){var yr=ToString(mydate.getFullYear());var mth=ToString(mydate.getMonth()+1);var dt=ToString(mydate.getDate());return yr+mth+dt;}
function GetDPSelectedIndex(dpid,selectedvalue){var dp=document.getElementById(dpid);var op=dp.getElementsByTagName("option");var checkInIndex;for(var i=0;i<op.length;i++){if(op[i].value==selectedvalue)
checkInIndex=i;}
return checkInIndex;}
function SetDPValue(checkinString,checkOutString){var dpCheckInYM=document.getElementById("arriveMonth");var dpCheckInDT=document.getElementById("arriveDate");dpCheckInYM.selectedIndex=GetDPSelectedIndex("arriveMonth",Left(checkinString,6));dpCheckInDT.selectedIndex=GetDPSelectedIndex("arriveDate",ToInt(Right(checkinString,2)));var dpCheckOutYM=document.getElementById("departMonth");var dpCheckOurDT=document.getElementById("departDate");dpCheckOutYM.selectedIndex=GetDPSelectedIndex("departMonth",Left(checkOutString,6));dpCheckOurDT.selectedIndex=GetDPSelectedIndex("departDate",ToInt(Right(checkOutString,2)));}
function loadDpSelectEvent(controlid){var control=document.getElementById(controlid);control.onchange=function(){controlOnSelect(controlid);}}
function controlOnSelect(controlid){switch(controlid){case"arriveDate":CheckinOnSelect("arriveMonth","arriveDate");case"arriveMonth":CheckinOnSelect("arriveMonth","arriveDate");loadDate("arriveDate","arriveMonth");case"departMonth":CheckoutOnSelect("departMonth","departDate");loadDate("departDate","departMonth");case"departDate":CheckoutOnSelect("departMonth","departDate");}}
function InitialDateMonth(){var myDate=new Date();var myDdate1=GetDateFromString(GetStringFromDate(myDate),0);var myDdate2=GetDateFromString(GetStringFromDate(myDate),24);InitialArriveDateMonth(myDdate1);InitialDepartDateMonth(myDdate2);}
function InitialArriveDateMonth(arriveDate){var sad=document.getElementById("selectedarrivedate");var sam=document.getElementById("selectedarrivemonth");sad.value=ToString(getDateDay(arriveDate));sam.value=ToString(getDateYear(arriveDate))+ToString(getDateMonth(arriveDate)+1);}
function InitialDepartDateMonth(departDate){var spd=document.getElementById("selecteddepartdate");var spm=document.getElementById("selecteddepartmonth");spd.value=ToString(getDateDay(departDate));spm.value=ToString(getDateYear(departDate))+ToString(getDateMonth(departDate)+1);}
function loadDateNights(){var aD=GetDateFromString(document.getElementById("selectedarrivemonth").value+document.getElementById("selectedarrivedate").value,0);var dD=GetDateFromString(document.getElementById("selecteddepartmonth").value+document.getElementById("selecteddepartdate").value,0);var aDay=document.getElementById("arriveDay");while(aDay.lastChild){aDay.removeChild(aDay.lastChild);}
aDay.appendChild(document.createTextNode(toWordDay(aD.getDay())));var dDay=document.getElementById("departDay");while(dDay.lastChild){dDay.removeChild(dDay.lastChild);}
dDay.appendChild(document.createTextNode(toWordDay(dD.getDay())));var num=document.getElementById("numberOfNights");while(num.lastChild){num.removeChild(num.lastChild);}
num.appendChild(document.createTextNode(GetDateDiff(dD,aD)));if(GetDateDiff(dD,aD)==1)
document.getElementById('labelForNumberOfNights').innerHTML='Night';else
document.getElementById('labelForNumberOfNights').innerHTML='Nights';}
function loadPage(){document.getElementById('arriveMonth').innerHTML='';InitialDateMonth();loadArriveMonth();loadDepartMonth();loadDate("arriveDate","arriveMonth");loadDate("departDate","departMonth");loadDpSelectEvent("arriveDate");loadDpSelectEvent("arriveMonth");loadDpSelectEvent("departDate");loadDpSelectEvent("departMonth");loadDateNights();}
function toWordDay(n){switch(n){case 0:return('Sunday');case 1:return('Monday');case 2:return('Tuesday');case 3:return('Wednesday');case 4:return('Thursday');case 5:return('Friday');case 6:return('Saturday');}}
function addLoadEvent(func){var oldonload=window.onload;if(typeof window.onload!='function'){window.onload=func;}else{window.onload=function(){if(oldonload){oldonload();}
func();};}}
addLoadEvent(loadPage);$(document).ready(function(){$('#begin_date').datePicker({createButton:false,displayClose:true,startDate:'01/01/2008',endDate:'31/12/2011'}).bind('click',function()
{$("#teaserform input, #teaserform select").attr("disabled","disabled");$(this).dpDisplay();return false;}).bind('dateSelected',function(e,selectedDate,$td,state)
{updateSelects(selectedDate);}).bind('dpClosed',function(e,selectedDates)
{var d=selectedDates[0];if(d){d=new Date(d);$('#end_date').dpSetStartDate(d.addDays(1).asString());}
$("#teaserform input, #teaserform select").attr("disabled","");}).dpSetPosition($.dpConst.POS_TOP,$.dpConst.POS_RIGHT);var updateSelects=function(selectedDate)
{selectedDate=new Date(selectedDate);var d=selectedDate.getDate();var m=selectedDate.getMonth()+1;var y=selectedDate.getFullYear();var padCharD='';var padCharM='';if(m<10){padCharM='0';}
if(d<10){padCharD='0';}
var formattedYearMonth=y.toString()+padCharM+m.toString();var formattedDate=padCharD+d.toString();$('#selectedarrivedate').attr({'value':formattedDate});$('#selectedarrivemonth').attr({'value':formattedYearMonth});var selectedIndex=0;var arriveMonth=document.getElementById('arriveMonth');for(var counter=0;counter<$("#arriveMonth option").size();counter++)
{if(arriveMonth[counter].value==formattedYearMonth)
{var selectedIndex=counter;}}
document.getElementById('arriveMonth').selectedIndex=selectedIndex;loadDate("arriveDate","arriveMonth");loadDepartMonth("departDate","departMonth");CheckoutOnSelect("departMonth","departDate");loadDate("departDate","departMonth");}
$('#end_date').datePicker({createButton:false,displayClose:true,startDate:'01/01/2008',endDate:'31/12/2011'}).bind('click',function()
{$(this).queue(function(){$("#teaserform input, #teaserform select").attr("disabled","disabled");$(this).dequeue();});$(this).dpDisplay();return false;}).bind('dateSelected',function(e,selectedDate,$td,state)
{updateSelects2(selectedDate);}).bind('dpClosed',function(e,selectedDates)
{$("#teaserform input, #teaserform select").attr("disabled","");}).dpSetPosition($.dpConst.POS_TOP,$.dpConst.POS_RIGHT);var updateSelects2=function(selectedDate)
{selectedDate=new Date(selectedDate);var d=selectedDate.getDate();var m=selectedDate.getMonth()+1;var y=selectedDate.getFullYear();var padCharD='';var padCharM='';if(m<10){padCharM='0';}
if(d<10){padCharD='0';}
var formattedYearMonth=y.toString()+padCharM+m.toString();var formattedDate=padCharD+d.toString();$('#selecteddepartdate').attr({'value':formattedDate});$('#selecteddepartmonth').attr({'value':formattedYearMonth});var selectedIndex=0;var departMonth=document.getElementById('departMonth');for(var counter=0;counter<$("#departMonth option").size();counter++)
{if(departMonth[counter].value==formattedYearMonth)
{var selectedIndex=counter;}}
document.getElementById('departMonth').selectedIndex=selectedIndex;loadDate("departDate","departMonth");}});