// 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 validateForm(theform)
{
	if (checkdate(theform.policy_start_date)) {
		if(theform.policy_type) { 
		
			var no_adults = theform.no_adults.value;
			var no_children = theform.no_children.value;
		
			if(theform.policy_type.value == "Individual")
				{
					if(no_adults < 1)
						{
							alert("Minimum of one adult allowed on this type of policy");
							theform.no_adults.focus();
							return (false);
						}
						
					if(no_adults > 1)
						{
							alert("Maximum of one adult allowed on this type of policy");
							theform.no_adults.focus();
							return (false);
						}
						
					if(no_children > 0)
						{
							alert("No children allowed on this type of policy");
							theform.no_adults.focus();
							return (false);
						}
				}
			
			if(theform.policy_type.value == "Couple")
				{
					if(no_adults < 2)
						{
							alert("Minimum of two adults allowed on this type of policy");
							theform.no_adults.focus();
							return (false);
						}
						
					if(no_adults > 2)
						{
							alert("Maximum of two adults allowed on this type of policy");
							theform.no_adults.focus();
							return (false);
						}
						
					if(no_children > 0)
						{
							alert("No children allowed on this type of policy");
							theform.no_adults.focus();
							return (false);
						}
				}
			
			if(theform.policy_type.value == "Family")
				{
					if(no_adults < 1)
						{
							alert("Minimum of one adult on this type of policy");
							theform.no_adults.focus();
							return (false);
						}
				
					if(no_adults > 2)
						{
							alert("Maximum of two adults on this type of policy");
							theform.no_adults.focus();
							return (false);
						}
						
					if(no_children < 1)
						{
							alert("Minimum of one child on this type of policy");
							theform.no_children.focus();
							return (false);
						}
					
					if(no_children > 5)
						{
							alert("Maximum of five children allowed on this type of policy");
							theform.no_children.focus();
							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 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.policy_start_date) {
		
		var Today = new Date();
		var diff = DateDiff('d', Today, theform.policy_start_date.value) ;
		
		if(theform.policy_start_date.value == "")
		{
			alert("Please enter your policy start date");
			theform.policy_start_date.focus();
			return (false);
		}
		
		if (diff < 0)
		{
			alert("Policy start date must be in the future");
			theform.policy_start_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.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 > 22)
		{
			alert("The maximum age for a child is 22");
			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 1 date of birth");
			theform.child_2_dob.focus();
			return (false);
		}	
		
		if (diff > 22)
		{
			alert("The maximum age for a child is 22");
			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 > 22)
		{
			alert("The maximum age for a child is 22");
			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 1 date of birth");
			theform.child_4_dob.focus();
			return (false);
		}	
		
		if (diff > 22)
		{
			alert("The maximum age for a child is 22");
			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 > 22)
		{
			alert("The maximum age for a child is 22");
			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);
	} else {
		return (false);
	}
	
}