// 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.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.occupation) {
		if(theform.occupation.value == "")
			{
				alert("Please enter your occupation");
				theform.occupation.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.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.cover_location) {
		if(theform.cover_location.value == "")
			{
				alert("Please enter your jump/course location");
				theform.cover_location.focus();
				return (false);
			}
	}
	
	if(theform.cover_country) {
		if(theform.cover_country.value == "")
			{
				alert("Please enter your jump/course country");
				theform.cover_country.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);
}