// JavaScript Document

function DateDiff(interval, start, end)
{
  var iOut = 0, rounding=true;
  var bufferA = Date.parse(start);
  var bufferB = Date.parse(end );

  // check that the start parameter is a valid Date.
  if ( isNaN (bufferA) || isNaN (bufferB) )
  {
      return 0;
  }
  // check that an interval parameter was not numeric.
  if ( interval.charAt == 'undefined' )
  {
    // the user specified an incorrect interval, handle the error.
    return 0;
  }
  var number = bufferB-bufferA;

  // what kind of add to do?
  switch (interval.charAt(0))
  {
	case 'd': case 'D':
      iOut = parseInt(number / 86400000)+parseInt((number %
86400000)/43200001);
      break ;
    case 'h': case 'H':
      iOut = parseInt(number / 3600000 )+parseInt((number %
3600000)/1800001);
      break ;
    case 'm': case 'M':
      iOut = parseInt(number / 60000 )+parseInt((number % 60000)/30001);
      break ;
    case 's': case 'S':
      iOut = parseInt(number / 1000 )+parseInt((number % 1000)/501);
      break ;
    default:
    // If we get to here then the interval parameter
    // didn't meet the d,h,m,s criteria.  Handle
    // the error.
    return 0;
  } return iOut ;
}


function checkDate(obj, field) {

	strVal = obj.value;
	
	if (strVal.length != 10) {
		alert(field + " is an invalid date.  It must be in the format dd/mm/yyyy.");		
		return false;
	}

	var strValid = "0123456789/";
	
	for (i = 0;  i < strVal.length;  i++)
	{
		ch = strVal.charAt(i);
		
		if (strValid.indexOf(ch) == -1) {
			alert(field + " is an invalid date.  It must be in the format dd/mm/yyyy.");
			obj.focus();
			return false;
		}
	}

	if (strVal.charAt(2) != "/") {
		alert(field + " is an invalid date.  It must be in the format dd/mm/yyyy.");
		return false;
	}

	if (strVal.charAt(5) != "/") {
		alert(field + " is an invalid date.  It must be in the format dd/mm/yyyy.");
		return false;
	}
	
	var noOfDays = 0;
	
	if (field == 'Departure Date') {
		noOfDays = 30;
	}
	
	if (!isFutureDate(strVal, noOfDays, field)) {
		return false;
	}
	
	return true;

}

function checkBothDates(){
	
	var frm = document.quote;
	
	if (!frm.departure_date.value == ""  && !frm.return_date.value == ""){
		if (checkDate(frm.departure_date, 'Departure Date') && checkDate(frm.return_date, 'Return Date')){
			
			//create the two dates as objects and do compare: Math.floor((return_date .getTime() - departure_date.getTime()) / (24*60*60*1000)) < 0
			
			//if (frm.departure_date.value >= frm.return_date.value){
			//	alert("Return Date must be greater than the Departure Date");
			//}else{
				return true;
			//}
		}
	}

	return false;
}

function validateForm(theform)
{
	
	if (!checkNum()) {
		return false;
	}
	
	if(theform.no_days) { 
	
		if(theform.no_days.value == "")
			{
				alert("Please enter the number of days");
				return false;
			}
		
		if(theform.no_days.value > 180)
			{
				alert("The maximum number of days is 180");
				return false;
			}
	}
	
	if(theform.title) { 
	if(theform.title.value == "")
	{
		alert("Please enter your title");
		theform.title.focus();
		return (false);
	}
	}
	
	if(theform.forename) {
	if(theform.forename.value == "")
	{
		alert("Please enter your forename");
		theform.forename.focus();
		return (false);
	}
	}
	
	if(theform.surname) {
	if(theform.surname.value == "")
	{
		alert("Please enter your surname");
		theform.surname.focus();
		return (false);
	}
	}
		
	if(theform.dob) {
		
	var Today = new Date();
	var diff = DateDiff('d', theform.dob.value, Today) ;
	var diff = diff/365
		
	if(theform.dob.value == "")
	{
		alert("Please enter your date of birth");
		theform.dob.focus();
		return (false);
	}
	
	if (diff > 64)
	{
		alert("The maximum age for an adult is 64");
		theform.dob.focus();
		return (false);
	}
	
	if (diff < 0)
	{
		alert("Invalid date of birth");
		theform.dob.focus();
		return (false);
	}
	
	}
		
	if(theform.house_number) {
	if(theform.house_number.value == "")
	{
		alert("Please enter house number/name");
		theform.house_number.focus();
		return (false);
	}
	}
	
	if(theform.address_1) {
	if(theform.address_1.value == "")
	{
		alert("Please enter address line 1");
		theform.address_1.focus();
		return (false);
	}
	}
	
	if(theform.town_city) {
	if(theform.town_city.value == "")
	{
		alert("Please enter your town/city");
		theform.town_city.focus();
		return (false);
	}
	}
			
	if(theform.postcode) {
	if(theform.postcode.value == "")
	{
		alert("Please enter your post code");
		theform.postcode.focus();
		return (false);
	}
	}
	
	if(theform.telephone) {
	if(theform.telephone.value == "")
	{
		alert("Please enter your contact telephone number");
		theform.telephone.focus();
		return (false);
	}
	}
				
	if(theform.activities) {
		if(theform.activities.options.length < 2) {		
//		if(theform.activities.value == "")
//		{
			alert("Please enter details of your activities");
			theform.activities.focus();
			return (false);
		} else {
			strActivities = ""
			for (var i = 1 ; i < theform.activities.options.length ; i++) {
				strActivities = strActivities + "'" + theform.activities.options[i].value + "',";
			}
			strActivities = strActivities + "'nothing'";
			theform.allActivities.value = strActivities;
		}
	}
	
	if(theform.departure_date) {
	
	var Today = new Date();
	var days = theform.no_days.value ;
	//var diff1 = DateDiff('d', theform.departure_date.value, theform.return_date.value) ;
	var diff2 = DateDiff('d', Today, theform.departure_date.value) ;
		
	//if (diff1 > days)
	//{
	//	alert("You may not exceed the cover period");
	//	theform.departure_date.focus();
	//	return (false);
	//}
	
	
	if(!checkBothDates()) {
		return false;
	}

	if (!checkDate(theform.departure_date, "Departure Date")) {
		return false;
	}

	if (!checkDate(theform.return_date, "Return Date")) {
		return false;
	}
	
	
	
	//if (diff2 < 0)
	//{
		//alert("Departure date must be in the future");
		//theform.departure_date.focus();
		//return (false);
	//}
	
	}
						
	if(theform.adult_1_title) {
	if(theform.adult_1_title.value == "")
	{
		alert("Please enter adult 1 title");
		theform.adult_1_title.focus();
		return (false);
	}
	}
	
	if(theform.adult_1_forename) {
	if(theform.adult_1_forename.value == "")
	{
		alert("Please enter adult 1 forename");
		theform.adult_1_forename.focus();
		return (false);
	}
	}
	
	if(theform.adult_1_surname) {
	if(theform.adult_1_surname.value == "")
	{
		alert("Please enter adult 1 surname");
		theform.adult_1_surname.focus();
		return (false);
	}
	}
	
	if(theform.adult_1_dob) {
	
	var Today = new Date();
	var diff = DateDiff('d', theform.adult_1_dob.value, Today) ;
	var diff = diff/365

	
	if(theform.adult_1_dob.value == "")
	{
		alert("Please enter adult 1 date of birth");
		theform.adult_1_dob.focus();
		return (false);
	}	
	
	if (diff > 64)
	{
		alert("The maximum age for an adult is 64");
		theform.adult_1_dob.focus();
		return (false);
	}

	if (diff < 0)
	{
		alert("Invalid date of birth");
		theform.adult_1_dob.focus();
		return (false);
	}
	
	}	
	
	if(theform.adult_2_title) {
	if(theform.adult_2_title.value == "")
	{
		alert("Please enter adult 2 title");
		theform.adult_2_title.focus();
		return (false);
	}
	}
	
	if(theform.adult_2_forename) {
	if(theform.adult_2_forename.value == "")
	{
		alert("Please enter adult 2 forename");
		theform.adult_2_forename.focus();
		return (false);
	}
	}
	
	if(theform.adult_2_surname) {
	if(theform.adult_2_surname.value == "")
	{
		alert("Please enter adult 2 surname");
		theform.adult_2_surname.focus();
		return (false);
	}
	}
	
	if(theform.adult_2_dob) {
	
	var Today = new Date();
	var diff = DateDiff('d', theform.adult_2_dob.value, Today) ;
	var diff = diff/365
	
	if(theform.adult_2_dob.value == "")
	{
		alert("Please enter adult 2 date of birth");
		theform.adult_2_dob.focus();
		return (false);
	}
	
	if (diff > 64)
	{
		alert("The maximum age for an adult is 64");
		theform.adult_2_dob.focus();
		return (false);
	}
	
	if (diff < 0)
	{
		alert("Invalid date of birth");
		theform.adult_2_dob.focus();
		return (false);
	}
	
	}
		
	if(theform.adult_3_title) {
	if(theform.adult_3_title.value == "")
	{
		alert("Please enter adult 3 title");
		theform.adult_3_title.focus();
		return (false);
	}
	}
	
	if(theform.adult_3_forename) {
	if(theform.adult_3_forename.value == "")
	{
		alert("Please enter adult 3 forename");
		theform.adult_3_forename.focus();
		return (false);
	}
	}
	
	if(theform.adult_3_surname) {
	if(theform.adult_3_surname.value == "")
	{
		alert("Please enter adult 3 surname");
		theform.adult_3_surname.focus();
		return (false);
	}
	}
	
	if(theform.adult_3_dob) {
	
	var Today = new Date();
	var diff = DateDiff('d', theform.adult_3_dob.value, Today) ;
	var diff = diff/365
	
	if(theform.adult_3_dob.value == "")
	{
		alert("Please enter adult 3 date of birth");
		theform.adult_3_dob.focus();
		return (false);
	}
	
	if (diff > 64)
	{
		alert("The maximum age for an adult is 64");
		theform.adult_3_dob.focus();
		return (false);
	}	
	
	if (diff < 0)
	{
		alert("Invalid date of birth");
		theform.adult_3_dob.focus();
		return (false);
	}
	
	}
		
	if(theform.adult_4_title) {
	if(theform.adult_4_title.value == "")
	{
		alert("Please enter adult 4 title");
		theform.adult_4_title.focus();
		return (false);
	}
	}
	
	if(theform.adult_4_forename) {
	if(theform.adult_4_forename.value == "")
	{
		alert("Please enter adult 4 forename");
		theform.adult_4_forename.focus();
		return (false);
	}
	}
	
	if(theform.adult_4_surname) {
	if(theform.adult_4_surname.value == "")
	{
		alert("Please enter adult 4 surname");
		theform.adult_4_surname.focus();
		return (false);
	}
	}
	
	if(theform.adult_4_dob) {
	
	var Today = new Date();
	var diff = DateDiff('d', theform.adult_4_dob.value, Today) ;
	var diff = diff/365
	
	if(theform.adult_4_dob.value == "")
	{
		alert("Please enter adult 4 date of birth");
		theform.adult_4_dob.focus();
		return (false);
	}
	
	if (diff > 64)
	{
		alert("The maximum age for an adult is 64");
		theform.adult_4_dob.focus();
		return (false);
	}
	
	if (diff < 0)
	{
		alert("Invalid date of birth");
		theform.adult_4_dob.focus();
		return (false);
	}
	
	}
	
	if(theform.adult_5_title) {
	if(theform.adult_5_title.value == "")
	{
		alert("Please enter adult 5 title");
		theform.adult_5_title.focus();
		return (false);
	}
	}
	
	if(theform.adult_5_forename) {
	if(theform.adult_5_forename.value == "")
	{
		alert("Please enter adult 5 forename");
		theform.adult_5_forename.focus();
		return (false);
	}
	}
	
	if(theform.adult_5_surname) {
	if(theform.adult_5_surname.value == "")
	{
		alert("Please enter adult 5 surname");
		theform.adult_5_surname.focus();
		return (false);
	}
	}
	
	if(theform.adult_5_dob) {
	
	var Today = new Date();
	var diff = DateDiff('d', theform.adult_5_dob.value, Today) ;
	var diff = diff/365

	if(theform.adult_5_dob.value == "")
	{
		alert("Please enter adult 5 date of birth");
		theform.adult_5_dob.focus();
		return (false);
	}
	
	if (diff > 64)
	{
		alert("The maximum age for an adult is 64");
		theform.adult_5_dob.focus();
		return (false);
	}
	
	if (diff < 0)
	{
		alert("Invalid date of birth");
		theform.adult_5_dob.focus();
		return (false);
	}
	
	}
	
	if(theform.child_1_title) {
	if(theform.child_1_title.value == "")
	{
		alert("Please enter child 1 title");
		theform.child_1_title.focus();
		return (false);
	}
	}
	
	if(theform.child_1_forename) {
	if(theform.child_1_forename.value == "")
	{
		alert("Please enter child 1 forename");
		theform.child_1_forename.focus();
		return (false);
	}
	}
	
	if(theform.child_1_surname) {
	if(theform.child_1_surname.value == "")
	{
		alert("Please enter child 1 surname");
		theform.child_1_surname.focus();
		return (false);
	}
	}
	
	if(theform.child_1_dob) {
	
	var Today = new Date();
	var diff = DateDiff('d', theform.child_1_dob.value, Today) ;
	var diff = diff/365

	if(theform.child_1_dob.value == "")
	{
		alert("Please enter child 1 date of birth");
		theform.child_1_dob.focus();
		return (false);
	}	
	
	if (diff > 16)
	{
		alert("The maximum age for a child is 16");
		theform.child_1_dob.focus();
		return (false);
	}
	
	if (diff < 0)
	{
		alert("Invalid date of birth");
		theform.child_1_dob.focus();
		return (false);
	}
	
	}	
	
	if(theform.child_2_title) {
	if(theform.child_2_title.value == "")
	{
		alert("Please enter child 2 title");
		theform.child_2_title.focus();
		return (false);
	}
	}
	
	if(theform.child_2_forename) {
	if(theform.child_2_forename.value == "")
	{
		alert("Please enter child 2 forename");
		theform.child_2_forename.focus();
		return (false);
	}
	}
	
	if(theform.child_2_surname) {
	if(theform.child_2_surname.value == "")
	{
		alert("Please enter child 2 surname");
		theform.child_2_surname.focus();
		return (false);
	}
	}
	
	if(theform.child_2_dob) {
	
	var Today = new Date();
	var diff = DateDiff('d', theform.child_2_dob.value, Today) ;
	var diff = diff/365
	
	if(theform.child_2_dob.value == "")
	{
		alert("Please enter child 2 date of birth");
		theform.child_2_dob.focus();
		return (false);
	}
	
	if (diff > 16)
	{
		alert("The maximum age for a child is 16");
		theform.child_2_dob.focus();
		return (false);
	}
	
	if (diff < 0)
	{
		alert("Invalid date of birth");
		theform.child_2_dob.focus();
		return (false);
	}
	
	}
		
	if(theform.child_3_title) {
	if(theform.child_3_title.value == "")
	{
		alert("Please enter adult 3 title");
		theform.child_3_title.focus();
		return (false);
	}
	}
	
	if(theform.child_3_forename) {
	if(theform.child_3_forename.value == "")
	{
		alert("Please enter child 3 forename");
		theform.child_3_forename.focus();
		return (false);
	}
	}
	
	if(theform.child_3_surname) {
	if(theform.child_3_surname.value == "")
	{
		alert("Please enter child 3 surname");
		theform.child_3_surname.focus();
		return (false);
	}
	}
	
	if(theform.child_3_dob) {
	
	var Today = new Date();
	var diff = DateDiff('d', theform.child_3_dob.value, Today) ;
	var diff = diff/365
	
	if(theform.child_3_dob.value == "")
	{
		alert("Please enter child 3 date of birth");
		theform.child_3_dob.focus();
		return (false);
	}
	
	if (diff > 16)
	{
		alert("The maximum age for a child is 16");
		theform.child_3_dob.focus();
		return (false);
	}
	
	if (diff < 0)
	{
		alert("Invalid date of birth");
		theform.child_3_dob.focus();
		return (false);
	}
	
	}
		
	if(theform.child_4_title) {
	if(theform.child_4_title.value == "")
	{
		alert("Please enter child 4 title");
		theform.child_4_title.focus();
		return (false);
	}
	}
	
	if(theform.child_4_forename) {
	if(theform.child_4_forename.value == "")
	{
	alert("Please enter child 4 forename");
		theform.child_4_forename.focus();
		return (false);
	}
	}
	
	if(theform.child_4_surname) {
	if(theform.child_4_surname.value == "")
	{
	alert("Please enter child 4 surname");
		theform.child_4_surname.focus();
		return (false);
	}
	}
	
	if(theform.child_4_dob) {
	
	var Today = new Date();
	var diff = DateDiff('d', theform.child_4_dob.value, Today) ;
	var diff = diff/365
	
	if(theform.child_4_dob.value == "")
	{
		alert("Please enter child 4 date of birth");
		theform.child_4_dob.focus();
		return (false);
	}
	
	if (diff > 16)
	{
		alert("The maximum age for a child is 16");
		theform.child_4_dob.focus();
		return (false);
	}
	
	if (diff < 0)
	{
		alert("Invalid date of birth");
		theform.child_4_dob.focus();
		return (false);
	}
	
	}
	
	if(theform.child_5_title) {
	if(theform.child_5_title.value == "")
	{
		alert("Please enter child 5 title");
		theform.child_5_title.focus();
		return (false);
	}
	}
	
	if(theform.child_5_forename) {
	if(theform.child_5_forename.value == "")
	{
	alert("Please enter child 5 forename");
		theform.child_5_forename.focus();
		return (false);
	}
	}
	
	if(theform.child_5_surname) {
	if(theform.child_5_surname.value == "")
	{
	alert("Please enter child 5 surname");
		theform.child_5_surname.focus();
		return (false);
	}
	}
	
	if(theform.child_5_dob) {
	
	var Today = new Date();
	var diff = DateDiff('d', theform.child_5_dob.value, Today) ;
	var diff = diff/365
	
	if(theform.child_5_dob.value == "")
	{
		alert("Please enter child 5 date of birth");
		theform.child_5_dob.focus();
		return (false);
	}
	
	if (diff > 16)
	{
		alert("The maximum age for a child is 16");
		theform.child_5_dob.focus();
		return (false);
	}
	
	if (diff < 0)
	{
		alert("Invalid date of birth");
		theform.child_5_dob.focus();
		return (false);
	}

	}
	
	if(theform.declaration) {
	if(!theform.declaration.checked)
	{
	alert("You must agree to the declaration before submitting your application");
		theform.declaration.focus();
		return (false);
	}
	}
	
	if(theform.summary) {
	if(!theform.summary.checked)
	{
	alert("You must accept the summary of cover before submitting your application");
		theform.summary.focus();
		return (false);
	}
	}
	
	return (true);
}