//VALUES TO CONFIGURE THE QUICKFINDER - START
//define whether to reset the arrival date to current date after check "date > today" has failed - 0 and 1
var resetArrivalDate = 1; //if set to 0, you should change arrYear, arrMonth and arrDayMessage
//define whether you would like to see the passed query as a message before execute
var showQueryString = 1;
//messages used
var arrYearMessage = ("Searched year smaller than current year. Arrival date will be reset to the current date!");
//var arrMonthMessage = ("Der Anreisemonat liegt in der Vergangenheit. Bitte nehmen Sie eine Korrektur vor!");
var arrMonthMessage = ("Searched month smaller than current month. Arrival date will be reset to the current date!");
var arrDayMessage = ("Searched day smaller than current day. Arrival date will be reset to the current date!");
var arrDayNotExistMessage = ("The given arrival date does not exist (e.g. 31.02.). Please correct the value!") ;
//VALUES TO CONFIGURE THE QUICKFINDER - END
function setArrivalDate(value, type){
var currDay = window.document.frmQuickFind.SrchFromDay.value;
var currMonth = window.document.frmQuickFind.SrchFromMonth.value;
var currYear = window.document.frmQuickFind.SrchFromYear.value;

if(currDay.length==1){
currDay = '0'+currDay;
}
if(currMonth.length==1){
currMonth = '0'+currMonth;
}
if((type==1 || type==2) && value.length==1){
value = '0'+value;
}

if(type==0) { //called if date does not change or on automatic date reset
currDateString = new String(currYear+''+currMonth+''+currDay);
window.document.frmQuickFind.qfdArrivalDate.value = currDateString;
}
else{
if(type==1) { //day to change
currDateString = new String(currYear+''+currMonth+''+value);
}
if(type==2) { //month to change
currDateString = new String(currYear+''+value+''+currDay);
}
if(type==3) { //year to change
currDateString = new String(value+''+currMonth+''+currDay);
}
}
} //setArrivalDate

/*Function to check whether the given arrival date is in the past*/
function checkSearchDate(searchDate) {
var currDate = new Date();
if(searchDate.getYear() < currDate.getYear()){
alert(arrYearMessage);
window.document.frmQuickFind.SrchFromYear.focus();
return false;
}
if(searchDate.getYear() == currDate.getYear() && searchDate.getMonth() < currDate.getMonth()){
alert(arrMonthMessage);
window.document.frmQuickFind.SrchFromMonth.focus();
return false;
}
if(searchDate.getYear() == currDate.getYear() && searchDate.getMonth() == currDate.getMonth() &&
searchDate.getDate() < currDate.getDate()){
alert(arrDayMessage);
window.document.frmQuickFind.SrchFromDay.focus();
return false;
}
return true;
} //checkSearchDate

function displayQueryString(form){
if(showQueryString){
var sfDay = form.SrchFromDay.value;
var sfMonth = form.SrchFromMonth.value;
var sfYear = form.SrchFromYear.value;
var qfdNights = form.qfdNights.value;
var qfdAdults = form.qfdAdults.value;
var qfdArrivalDate = form.qfdArrivalDate.value;
//var qfdCategories = form.qfdCategories.value;
var baseUrl = form.action;
var url = baseUrl+'?'+'qfdArrivalDate='+qfdArrivalDate+
'&SrchFromDay='+sfDay+'&SrchFromMonth='+sfMonth+'&SrchFromYear='+sfYear+
'&qfdNights='+qfdNights+'&qfdAdults='+qfdAdults;
alert(url);
}
} //displayQueryString


function validateForm(form) {
var d = parseInt(form.SrchFromDay.value);
var m = parseInt(form.SrchFromMonth.value);
var j = parseInt(form.SrchFromYear.value);

if ( !checkDate(d,m,j)) {
alert(arrDayNotExistMessage);
form.SrchFromDay.focus();
return false;
}

if ( !checkDate2()) {
//form.SrchFromDay.focus();
return false;
}
} //validateForm

function checkDate(d, m, j) {
var dt = new Date(j, m-1, d);
if (dt.getDate() != d || dt.getMonth()+1 != m) {
return false;
}
return true;
} //checkDate

function checkDate2(){
var currDay = window.document.frmQuickFind.SrchFromDay.value;
var currMonth = window.document.frmQuickFind.SrchFromMonth.value;
var currYear = window.document.frmQuickFind.SrchFromYear.value;
var searchDate = new Date(currYear, currMonth-1, currDay);
var check = checkSearchDate(searchDate);
if(check == false){
if(resetArrivalDate){
setCurrentDay(); //reset arrival date
}
return false;
}
else{
window.document.frmQuickFind.qfdArrivalDate.value = currDateString;
return true;
}
} //checkDate2

function setCurrentDay() {
var today = new Date();
var today_day = today.getDate();
var today_month = today.getMonth();
var today_year = today.getFullYear();
//Set Quickfinder date to current day
var currDateString = new String(today_year+''+(today_month+1)+''+today_day);
window.document.frmQuickFind.qfdArrivalDate.value = currDateString;
window.document.frmQuickFind.SrchFromDay.options[today_day-1].selected = true;
window.document.frmQuickFind.SrchFromMonth.options[today_month].selected = true;
window.document.frmQuickFind.SrchFromYear.options[0].value = today_year;
window.document.frmQuickFind.SrchFromYear.options[0].text = today_year;
setArrivalDate(0,0);
} //setCurrentDay
