function isValidDate(objElement, strDescription) {
// Checks for the following valid date formats:
// DD/MM/YY   DD/MM/YYYY   DD-MM-YY   DD-MM-YYYY
// Also separates date into month, day, and year variables
var datePat= /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
 while(objElement.value.charAt(0)==' ') objElement.value=objElement.value.substring(1,objElement.value.length);							                   // Trim objElement of leading spaces
 while(objElement.value.charAt(objElement.value.length-1)==' ')objElement.value=objElement.value.substring(0,objElement.value.length-1); // Trim objElement of trailing spaces
 var matchArray = objElement.value.match(datePat);        // is the format ok?
 if (matchArray == null) {
  alert(strDescription + " error.\n\n" + strDescription + " is not in a valid format.");
  return false;
 }
 var month = matchArray[3];                               // parse date into variables
 var day = matchArray[1];
 var year = matchArray[4];
 if (month < 1 || month > 12) {                           // check month range
  alert(strDescription + " error.\n\nMonth must be between 1 and 12.");
  return false;
 }
 if (day < 1 || day > 31) {
  alert(strDescription + " error.\n\nDay must be between 1 and 31.");
  return false;
 }
 if ((month==4 || month==6 || month==9 || month==11) && day==31) {
  alert(strDescription + " error.\n\nMonth "+month+" doesn't have 31 days!")
  return false
 }
 if (month == 2) {                                        // check for february 29th/leap year
  var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
  if (day>29 || (day==29 && !isleap)) {
   alert(strDescription + " error.\n\nFebruary " + year + " doesn't have " + day + " days!");
   return false;
  }
 }
 objElement.value = day + "/" + month + "/" + year;       // Put date into dd/mm/yy or dd/mm/yyyy format
 return true;                                             // date is valid
}


function checkForm(theForm) {

	if (theForm.elements['varContactname'].value == '') {
		alert('You must enter a Contact Name.');
		theForm.elements['varContactname'].focus();
		return false;
	}

	if (theForm.elements['varTelephone'].value == '') {
		alert('You must enter your Telephone Number.');
		theForm.elements['varTelephone'].focus();
		return false;
	}
    theForm.elements['varTelephone'].value=theForm.elements['varTelephone'].value.replace(' ','');
    if (isNaN(theForm.elements['varTelephone'].value)) {
        alert("Please enter a valid telephone number");	
		theForm.elements['varTelephone'].focus();
		return false;
	}
	
	if (theForm.elements['varEmail'].value != '') {
		if (theForm.elements['varEmail'].value.indexOf("@")==-1 || theForm.elements['varEmail'].value.indexOf(".")==-1 || theForm.elements['varEmail'].value.indexOf(" ")!=-1 || theForm.elements['varEmail'].value.length<6) {
		alert("Please enter a valid email address.");
		theForm.elements['varEmail'].focus();
		return false
		}
	}

	if (theForm.elements['varDD'].value == 'noneselected') {
		alert('Please select a Day.');
        theForm.elements['varDD'].focus();
		return false;
	}
	if (theForm.elements['varMON'].value == 'noneselected') {
		alert('Please select a Month.');
        theForm.elements['varMON'].focus();
		return false;
	}
	if (theForm.elements['varYYYY'].value == 'noneselected') {
		alert('Please select a year.');
        theForm.elements['varYYYY'].focus();
		return false;
	}

	theForm.elements['varFulldate'].value = theForm.elements['varDD'].value + '/' + theForm.elements['varMON'].value + '/' + theForm.elements['varYYYY'].value;
//	alert('value is: ' +theForm.elements['varFulldate'].value);
	if (!isValidDate(theForm.elements['varFulldate'], 'Date')) {
        theForm.elements['varDD'].focus();
		return false;
	}		
	
	if (theForm.elements['varSitting'].value == 'noneselected') {
		alert('Please select a Sitting.');
        theForm.elements['varSitting'].focus();
		return false;
	}

	if (theForm.elements['varTime'].value == 'noneselected') {
		alert('Please select a Prefered Time.');
        theForm.elements['varTime'].focus();
		return false;
	}

	if (theForm.elements['varDiners'].value == 'noneselected') {
		alert('Please select number of Diners.');
        theForm.elements['varDiners'].focus();
		return false;
	}

	if (theForm.elements['varSurvey'].value == 'noneselected') {
		alert('Please select how you heard about us.');
        theForm.elements['varSurvey'].focus();
		return false;
	}
		
	// temporary add this code Dec 2007 to stop bookings for 01/01/08 to 07/02/08
	if ((theForm.elements['varMON'].value == '01') && (theForm.elements['varYYYY'].value == '2008')) {
		alert('Sorry, we are closed from News Years Day 2008 to Thursday 7th February 2008.  We re-open Friday 8th February 2008.\n\nPlease choose an alternate date.\n\nWe apologise for any inconvenience this may cause.');
        theForm.elements['varMON'].focus();
		return false;
	}
	if (((theForm.elements['varMON'].value == '02') && (theForm.elements['varDD'].value < '08')) && (theForm.elements['varYYYY'].value == '2008')) {
		alert('Sorry, we are closed from News Years Day 2008 to Thursday 7th February 2008.  We re-open Friday 8th February 2008.\n\nPlease choose an alternate date.\n\nWe apologise for any inconvenience this may cause.');
        theForm.elements['varDD'].focus();
		return false;
	}
	// temporary added above code Dec 2007 to stop bookings for 01/01/08 to 07/02/08


document.reservation.submit();
}
