// 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.postcode_1) { 
	if (theform.postcode_1.value == "")
	{
	alert("Please enter your post code");
		theform.postcode_1.focus();
		return (false);
	}
	}
	
	if (theform.postcode_2) { 
	if (theform.postcode_2.value == "")
	{
	alert("Please enter your post code");
		theform.postcode_2.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.telephone) {
	if(theform.telephone.value == "")
	{
	alert("Please enter your contact telephone number");
		theform.telephone.focus();
		return (false);
	}
	}
	
	if(theform.occupation) {
	if(theform.occupation.value == "")
	{
	alert("Please enter your occupation");
		theform.occupation.focus();
		return (false);
	}
	}
	
	if(theform.house_number_risk) {
	if(theform.house_number_risk.value == "")
	{
	alert("Please enter risk address house number/name");
		theform.house_number_risk.focus();
		return (false);
	}
	}
	
	if(theform.address_1_risk) {
	if(theform.address_1_risk.value == "")
	{
	alert("Please enter your risk address line 1");
		theform.address_1_risk.focus();
		return (false);
	}
	}
	
	if(theform.town_city_risk) {
	if(theform.town_city_risk.value == "")
	{
	alert("Please enter your risk address town/city");
		theform.town_city_risk.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.computer_value) {
		
		if(theform.computer_value.value > 5000)
			{
				alert("Maximum total cover £5,000");
				theform.computer_value.focus();
				return (false);
			}
							   }
	
	if(theform.computer_1_desc) {
	if(theform.computer_1_desc.value == "")
	{
	alert("Please enter a description for the first computer item");
		theform.computer_1_desc.focus();
		return (false);
	}
	}
	
	if(theform.computer_1_value) {
	
		var cover = theform.computer_1_value.value + theform.computer_2_value.value + theform.computer_3_value.value + theform.computer_4_value.value;
		var limit = theform.computer_value.value;
		
		if(theform.computer_1_value.value == "")
			{
			alert("Please enter the value of the first computer item");
				theform.computer_1_value.focus();
				return (false);
			}
		if(theform.computer_1_value.value > 2500)
			{
			alert("Single item limit £2,500");
				theform.computer_1_value.focus();
				return (false);
			}
		if(cover > limit)
			{
			alert("You may not exceed total computer cover");
				theform.computer_1_value.focus();
				return (false);
			}
	
	}
	
	if(theform.computer_2_value) {
	
		var cover = theform.computer_1_value.value + theform.computer_2_value.value + theform.computer_3_value.value + theform.computer_4_value.value;
		var limit = theform.computer_value.value;
		
		if(theform.computer_2_value.value > 2500)
			{
				alert("Single item limit £2,500");
				theform.computer_2_value.focus();
				return (false);
			}
			
		if(cover > limit)
			{
			alert("You may not exceed total computer cover");
				theform.computer_2_value.focus();
				return (false);
			}
	}
	
	if(theform.computer_3_value) {
	
		var cover = theform.computer_1_value.value + theform.computer_2_value.value + theform.computer_3_value.value + theform.computer_4_value.value;
		var limit = theform.computer_value.value;

		if(theform.computer_3_value.value > 2500)
			{
				alert("Single item limit £2,500");
				theform.computer_3_value.focus();
				return (false);
			}
			
		if(cover > limit)
			{
			alert("You may not exceed total computer cover");
				theform.computer_3_value.focus();
				return (false);
			}
	}
	
	if(theform.computer_4_value) {
	
		var cover = theform.computer_1_value.value + theform.computer_2_value.value + theform.computer_3_value.value + theform.computer_4_value.value;
		var limit = theform.computer_value.value;

		if(theform.computer_4_value.value > 2500)
			{
				alert("Single item limit £2,500");
				theform.computer_4_value.focus();
				return (false);
			}
			
		if(cover > limit)
			{
			alert("You may not exceed total computer cover");
				theform.computer_4_value.focus();
				return (false);
			}
	}
	
	if(theform.cycle_make) {
	if(theform.cycle_make.value == "")
	{
	alert("Please enter your cycle make");
		theform.cycle_make.focus();
		return (false);
	}
	}
	
	if(theform.cycle_present_value) {
		
		var limit = theform.cycle_value.value;

		if(theform.cycle_present_value.value == "")
			{
			alert("Please enter cycle present value");
				theform.cycle_present_value.focus();
				return (false);
			}
			
		if(theform.cycle_present_value.value > limit)
			{
			alert("You may not exceed total cycle cover");
				theform.cycle_present_value.focus();
				return (false);
			}
	
	}
	
	if(theform.possessions_value) {
	
		if(theform.possessions_value.value > 3000)
			{
				alert("Maximum total cover £3,000");
				theform.possessions_value.focus();
				return (false);
			}
	}
								  
	if(theform.valuable_1_desc) {
	if(theform.valuable_1_desc.value == "")
	{
	alert("Please enter a description for the first valuable");
		theform.valuable_1_desc.focus();
		return (false);
	}
	}
	
	if(theform.valuable_1_value) {
	
		var cover = theform.valuable_1_value.value + theform.valuable_2_value.value + theform.valuable_3_value.value + theform.valuable_4_value.value + theform.valuable_5_value.value + theform.valuable_6_value.value;
		var limit = theform.possessions_value.value;

		if(theform.valuable_1_value.value > 1000)
			{
				alert("Single item limit £1,000");
				theform.valuable_1_value.focus();
				return (false);
			}
			
		if(cover > limit)
			{
			alert("You may not exceed total valuables cover");
				theform.valuable_1_value.focus();
				return (false);
			}
	}
	
	if(theform.valuable_2_value) {
	
		var cover = theform.valuable_1_value.value + theform.valuable_2_value.value + theform.valuable_3_value.value + theform.valuable_4_value.value + theform.valuable_5_value.value + theform.valuable_6_value.value;
		var limit = theform.possessions_value.value;

		if(theform.valuable_2_value.value > 1000)
			{
				alert("Single item limit £1,000");
				theform.valuable_2_value.focus();
				return (false);
			}
			
		if(cover > limit)
			{
			alert("You may not exceed total valuables cover");
				theform.valuable_2_value.focus();
				return (false);
			}
	}
	
	if(theform.valuable_3_value) {
	
		var cover = theform.valuable_1_value.value + theform.valuable_2_value.value + theform.valuable_3_value.value + theform.valuable_4_value.value + theform.valuable_5_value.value + theform.valuable_6_value.value;
		var limit = theform.possessions_value.value;

		if(theform.valuable_3_value.value > 1000)
			{
				alert("Single item limit £1,000");
				theform.valuable_3_value.focus();
				return (false);
			}
			
		if(cover > limit)
			{
			alert("You may not exceed total valuables cover");
				theform.valuable_3_value.focus();
				return (false);
			}
	}
	
	if(theform.valuable_4_value) {
	
		var cover = theform.valuable_1_value.value + theform.valuable_2_value.value + theform.valuable_3_value.value + theform.valuable_4_value.value + theform.valuable_5_value.value + theform.valuable_6_value.value;
		var limit = theform.possessions_value.value;

		if(theform.valuable_4_value.value > 1000)
			{
				alert("Single item limit £1,000");
				theform.valuable_4_value.focus();
				return (false);
			}
			
		if(cover > limit)
			{
			alert("You may not exceed total valuables cover");
				theform.valuable_4_value.focus();
				return (false);
			}
	}
	
	if(theform.valuable_5_value) {
	
		var cover = theform.valuable_1_value.value + theform.valuable_2_value.value + theform.valuable_3_value.value + theform.valuable_4_value.value + theform.valuable_5_value.value + theform.valuable_6_value.value;
		var limit = theform.possessions_value.value;

		if(theform.valuable_5_value.value > 1000)
			{
				alert("Single item limit £1,000");
				theform.valuable_5_value.focus();
				return (false);
			}
			
		if(cover > limit)
			{
			alert("You may not exceed total valuables cover");
				theform.valuables_5_value.focus();
				return (false);
			}
	}
	
	if(theform.valuable_6_value) {
	
		var cover = theform.valuable_1_value.value + theform.valuable_2_value.value + theform.valuable_3_value.value + theform.valuable_4_value.value + theform.valuable_5_value.value + theform.valuable_6_value.value;
		var limit = theform.possessions_value.value;

		if(theform.valuable_6_value.value > 1000)
			{
				alert("Single item limit £1,000");
				theform.valuable_6_value.focus();
				return (false);
			}
			
		if(cover > limit)
			{
			alert("You may not exceed total valuables cover");
				theform.valuables_6_value.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);
}
