// 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 (theform.age) { 
		if(theform.age.value > 64)
			{
				if(theform.units_acc.value > 0) {
					alert("Accident Benefits are only available for those aged 64 and under");
					theform.units_acc.focus();
					return (false);
										  }
			}
	
		if(theform.age.value > 54)
			{
				if(theform.units_ill.value > 0) {
					alert("Illness benefits are only available for those aged 54 and under");
					theform.units_ill.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) {
		if(theform.dob.value == "")
			{
				alert("Please enter your date of birth");
				theform.dob.focus();
				return (false);
			}
	}
	
	if(theform.height) {
		if(theform.height.value == "")
			{
				alert("Please enter your height");
				theform.height.focus();
				return (false);
			}
	}
	
	if(theform.weight) {
		if(theform.weight.value == "")
			{
				alert("Please enter your weight");
				theform.weight.focus();
				return (false);
			}
	}
	
	if(theform.occupation_main) {
		if(theform.occupation_main.value == "")
			{
				alert("Please enter your main occupation");
				theform.occupation_main.focus();
				return (false);
			}
	}
	
	if(theform.telephone) {
		if(theform.telephone.value == "")
			{
				alert("Please enter your contact telephone number");
				theform.telephone.focus();
				return (false);
			}
	}
	
	if(theform.house_number) {
		if(theform.house_number.value == "")
			{
				alert("Please enter your house number/name");
				theform.house_number.focus();
				return (false);
			}
	}
	
	if(theform.address_1) {
		if(theform.address_1.value == "")
			{
				alert("Please enter your 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.post_code) {
		if(theform.post_code.value == "")
			{
				alert("Please enter your post code");
				theform.post_code.focus();
				return (false);
			}
	}
	
	
	if(theform.activites) {
		if(theform.activities.value == "")
			{
				alert("Please list all activities for which cover is required");
				theform.activities.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.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 read the summary of cover before submitting your application");
				theform.summary.focus();
				return (false);
			}
	}
	
	return (true);
}