function toggle(el) {
myEl = document.getElementById(el);myEl.style.display = (myEl.style.display == 'none') ? 'block' : 'none';
}

/* dit wordt gebruikt om zoekcriteria te tonen en te verbergen in de zoekformulieren */

function searchToggle(el) {	
myEl = document.getElementById(el);	
myEl.style.display = (myEl.style.display == 'none') ? 'block' : 'none';	
if(myEl.style.display == 'none') {		
	document.cookie = "searchtoggle=0;";	
} else {	
	document.cookie = "searchtoggle=1;";	
}
}


function setFormAndScreenValue(formName, elementName, dateValue) {

	/* Then put it in the form and on the screen */
	document.forms[formName].elements[elementName].value = dateValue;

	SelectDropDown(document.forms[formName].elements[elementName], dateValue, false);
	
	return true;

}


function checkNights(formName) {	
	var arrivaldd = document.forms[formName].elements['res_Arrival_DD'].value;
	var arrivalmm = document.forms[formName].elements['res_Arrival_MM'].value;
	var arrivalyy = document.forms[formName].elements['res_Arrival_YY'].value;
	
	var departdd = document.forms[formName].elements['res_Departure_DD'].value;
	var departmm = document.forms[formName].elements['res_Departure_MM'].value;
	var departyy = document.forms[formName].elements['res_Departure_YY'].value;
	
	var arrival;
	
	var depart;
	
	var defaultNightsStay = 3; 	/* Default number of nights */

	/* If date(s) are not set, default them to today (arrival) and today + defaultNightsStay (depart) in order to continue with search	*/

	if(arrivaldd == -1 || arrivalmm == -1 || arrivalyy == -1) {
		/* If there is no arrival date, set it to tomorrow */
		/* Note: Add 1 to the month since otherwise January = 0, etc. */
		arrival = new Date();
		arrival.setMonth(arrival.getMonth()+1);	/* Do month first so it can automatically adjust if necessary when date is incremented */
		arrival.setDate(arrival.getDate()+1);

		arrivaldd = arrival.getDate();
		arrivalmm = arrival.getMonth();
		arrivalyy = arrival.getFullYear();	
				
		/* Then put it in the form and on the screen */
		setFormAndScreenValue(formName, 'res_Arrival_DD', arrivaldd);
		setFormAndScreenValue(formName, 'res_Arrival_MM', arrivalmm);
		setFormAndScreenValue(formName, 'res_Arrival_YY', arrivalyy);

	}
	else {
		arrival = new Date(arrivalyy, arrivalmm, arrivaldd);
		
		/* Check that the arrival date is not in the past, if so, set it to tomorrow */
		var today = new Date();
		today.setMonth(today.getMonth()+1);
		
		if (arrival < today) {
		
			today.setDate(today.getDate()+1);
			
			arrival.setDate(today.getDate());
			arrival.setMonth(today.getMonth());
			arrival.setFullYear(today.getFullYear());
			
			arrivaldd = arrival.getDate();
			arrivalmm = arrival.getMonth();
			arrivalyy = arrival.getFullYear();
			
					
			/* Then put it in the form and on the screen */
			setFormAndScreenValue(formName, 'res_Arrival_DD', arrivaldd);
			setFormAndScreenValue(formName, 'res_Arrival_MM', arrivalmm);
			setFormAndScreenValue(formName, 'res_Arrival_YY', arrivalyy);
		}
		
	}		


	if (departdd == -1 || departmm == -1 || departyy == -1) {
		/* If there is no departure date, set it to defaultNightsStay days after the arrival date to be safe */

		depart = new Date();
		depart.setDate(arrivaldd);
		depart.setMonth(arrivalmm);
		depart.setFullYear(arrivalyy);
		depart.setDate(depart.getDate() + defaultNightsStay);

		departdd = depart.getDate();
		departmm = depart.getMonth();
		departyy = depart.getFullYear(); 
		
		/* Set it in the form for the search (not sure if this is necessary) and make it visible on the screen */
		setFormAndScreenValue(formName, 'res_Departure_DD', departdd);
		setFormAndScreenValue(formName, 'res_Departure_MM', departmm);
		setFormAndScreenValue(formName, 'res_Departure_YY', departyy);

	}
	else {
		depart = new Date(departyy, departmm, departdd);
		
		/* If the departure date is in the past, set it to defaultNightsStay past the arrival date */
		var today = new Date();
		today.setMonth(today.getMonth()+1);		

		if (depart < today) {
		
			depart.setMonth(arrivalmm);
			depart.setFullYear(arrivalyy);
			depart.setDate(arrivaldd + defaultNightsStay);	/* Set date last so month can automatically adjust if it needs to */


			departdd = depart.getDate();
			departmm = depart.getMonth();
			departyy = depart.getFullYear();  
				
			/* Then put it in the form and on the screen */
			setFormAndScreenValue(formName, 'res_Departure_DD', departdd);
			setFormAndScreenValue(formName, 'res_Departure_MM', departmm);
			setFormAndScreenValue(formName, 'res_Departure_YY', departyy);
	
		}

	}


	/* Check how many nights the stay is for */
	var nights = datediff(arrival, depart);
		
	if(nights > 0) {
		/* If the stay is for a valid number of nights, no problem */
		document.forms[formName].elements['Nights'].value = nights;
		return true;
	} else if (nights == 0) {
		/* Increase the day by defaultNightsStay and change it onscreen */
		depart.setDate(depart.getDate()+defaultNightsStay);
		departdd = depart.getDate();
		departmm = depart.getMonth();
		departyy = depart.getFullYear();

		/* Then put it in the form and on the screen */
		setFormAndScreenValue(formName, 'res_Departure_DD', departdd);
		setFormAndScreenValue(formName, 'res_Departure_MM', departmm);
		setFormAndScreenValue(formName, 'res_Departure_YY', departyy);

		nights = defaultNightsStay;
		document.forms[formName].elements['Nights'].value = nights;

		return true;
	} else {
	
		/* If departure date is set to before arrival date, switch the dates around and show results */
		/* First set it in the form (not sure this is necessary) and then on the screen */
		
		departdd = arrival.getDate();
		departmm = arrival.getMonth();
		departyy = arrival.getFullYear();
		
		arrivaldd = depart.getDate();
		arrivalmm = depart.getMonth();
		arrivalyy = depart.getFullYear();

		setFormAndScreenValue(formName, 'res_Arrival_DD', arrivaldd);
		setFormAndScreenValue(formName, 'res_Arrival_MM', arrivalmm);
		setFormAndScreenValue(formName, 'res_Arrival_YY', arrivalyy);
		
		setFormAndScreenValue(formName, 'res_Departure_DD', departdd);
		setFormAndScreenValue(formName, 'res_Departure_MM', departmm);
		setFormAndScreenValue(formName, 'res_Departure_YY', departyy);
	
	
		/* Recalculate the number of nights and display */
		nights = datediff(depart, arrival);
		document.forms[formName].elements['Nights'].value = nights;

		return true;
	}	


}
