// Configuration Olive House
// -------------------------
options = false;	// show options
minimumStay = 3;	// days
// -------------------------


today = new Date();
yearValue = today.getFullYear();
monthIndex = today.getMonth();
dayIndex = today.getDate() - 1;


function setIndices() {
	document.frmBooking.arrivalYear.value = yearValue;
	document.frmBooking.arrivalMonth.selectedIndex = monthIndex;
	document.frmBooking.arrivalDay.selectedIndex = dayIndex;

	document.frmBooking.departureYear.value = yearValue;
	document.frmBooking.departureMonth.selectedIndex = monthIndex;
	document.frmBooking.departureDay.selectedIndex = dayIndex + minimumStay;
	
	if( !options ) { document.getElementById("divOptions").style.display = "none"; }
	
	updateDates();
}


// Not working perfectly: eg when departure set to 28 feb and arrival changes to 26 feb.
function updateDates() {
	daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

	arrivalDay = document.frmBooking.arrivalDay.selectedIndex;
	departureDay = document.frmBooking.departureDay.selectedIndex;

	arrivalMonth = document.frmBooking.arrivalMonth.selectedIndex;
	departureMonth = document.frmBooking.departureMonth.selectedIndex;	

	arrivalYear = parseInt(document.frmBooking.arrivalYear.value);
	departureYear = parseInt(document.frmBooking.departureYear.value);
	
	if(departureYear < arrivalYear) {
		document.frmBooking.departureYear.value = arrivalYear;
	}

	if(departureYear == arrivalYear && departureMonth < arrivalMonth) {
		document.frmBooking.departureMonth.selectedIndex = arrivalMonth;
	}
	
	if( departureDay < (arrivalDay + minimumStay)
	  && (departureMonth <= arrivalMonth)
	  && (departureYear == arrivalYear) ) {
	  	
	  	// Change day. Month wraparound?
	  	daysLeft = (document.frmBooking.arrivalDay.length - 1) - arrivalDay;
	  	if( daysLeft < 3 ) {

			// Yes. Change month. Year wraparound?	  		
	  		document.frmBooking.departureDay.selectedIndex = 2 - daysLeft;
			
			if( departureMonth < 11 ) {
	  			document.frmBooking.departureMonth.selectedIndex = document.frmBooking.departureMonth.selectedIndex + 1;
	  		} else {
				// Yes, do year wraparound.
				document.frmBooking.departureYear.value = parseInt(document.frmBooking.arrivalYear.value) + 1;
	  			document.frmBooking.departureMonth.selectedIndex = 0;
	  		}
	  	} else {
	  		document.frmBooking.departureDay.selectedIndex = arrivalDay + minimumStay;
	  	}
	}
	
	
	// Check if day exceeds no of days in selected month (check leap year first)
	setLeapYear( parseInt(document.frmBooking.arrivalYear.value) ); 
	if(document.frmBooking.arrivalDay.selectedIndex > daysInMonths[document.frmBooking.arrivalMonth.selectedIndex] - 1) {
		document.frmBooking.arrivalDay.selectedIndex = daysInMonths[document.frmBooking.arrivalMonth.selectedIndex] - 1;
	}	

	setLeapYear( parseInt(document.frmBooking.departureYear.value) ); 
	if(document.frmBooking.departureDay.selectedIndex > daysInMonths[document.frmBooking.departureMonth.selectedIndex] - 1) {
		document.frmBooking.departureDay.selectedIndex = daysInMonths[document.frmBooking.departureMonth.selectedIndex] - 1;
	}
}

function setLeapYear(year)
{
    if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400) == 0)  {
	daysInMonths[1] = 29;
    } else {
    	daysInMonths[1] = 28;
    }
}

function validate() {
  ok = true;
  message = "Please correct the following errors in the form:\n";
  if ( document.frmBooking.name.value.length == 0  ) {
    message = message + ("You have not filled out the 'name' field.\n"); 
    ok = false;
  }
  if ( isValidAddress( document.frmBooking.email.value ) == false) {
    message += ("You have not supplied a correct e-mail address.\n"); 
    ok = false;
  }
  if ( document.frmBooking.telephone.value.length == 0  ) {
    message = message + ("You have not filled out the 'telephone' field.\n"); 
    ok = false;
  }
  if (!ok) {
	alert(message);
  }
  return ok;
}

function isValidAddress( address ) {
	// define search pattern
	var pattern = /^[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\.[A-Za-z0-9._%-]{2,4}$/; 
	
	// search and return result
	if ( address.match(pattern) ) {
		return true;
	} else {
		return false;
	}
}
