// 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.telephone) {
	if(theform.telephone.value == "")
	{
	alert("Please enter your contact telephone number");
		theform.telephone.focus();
		return (false);
	}
	}
	
	if(theform.house_number_corr) {
	if(theform.house_number_corr.value == "")
	{
	alert("Please enter your correspondence house number");
		theform.house_number_corr.focus();
		return (false);
	}
	}
	
	if(theform.address_1_corr) {
	if(theform.address_1_corr.value == "")
	{
	alert("Please enter your correspondence address line 1");
		theform.address_1_corr.focus();
		return (false);
	}
	}
	
	if(theform.town_city_corr) {
	if(theform.town_city_corr.value == "")
	{
	alert("Please enter the correspondence town/city");
		theform.town_city_corr.focus();
		return (false);
	}
	}
		
	if(theform.post_code_corr) {
	if(theform.post_code_corr.value == "")
	{
	alert("Please enter your correspondence post code");
		theform.post_code_corr.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.buildings_sum) {
		
		if(theform.buildings_sum.value > 0) {
	
			
			var property_type = (theform.property_type.value)
			if(property_type == "House") {
			
			if(theform.buildings_sum.value < 50000)
			{
			alert("Buildings sum must be between £50,000 and £500,000");
				theform.buildings_sum.focus();
				return (false);
			}
			
			if(theform.buildings_sum.value > 500000)
			{
				alert("Buildings sum must be between £50,000 and £500,000");
				theform.buildings_sum.focus();
				return (false);
			}
										}
		
			var property_type = (theform.property_type.value)
			if(property_type == "Apartment") {
			
			if(theform.buildings_sum.value < 10000)
			{
				alert("Buildings sum must be between £10,000 and £500,000");
				theform.buildings_sum.focus();
				return (false);
			}
			
			if(theform.buildings_sum.value > 500000)
			{
				alert("Buildings sum must be between £10,000 and £500,000");
				theform.buildings_sum.focus();
				return (false);
			}
			
			if(theform.contents_sum.value == 0)
			{
				alert("Buildings only policies acceptable for houses only");
				theform.contents_sum.focus();
				return (false);
			}
		
											}
		}
	}
	
	if(theform.listed_yn) {
	if(theform.listed_yn.checked)
	{
		alert("Please call us for a quotation on 0870 1217 590");
		return (false);
	}
	}
	
	if(theform.nonuk_yn) {
	if(theform.nonuk_yn.checked)
	{
		alert("Please call us for a quotation on 0870 1217 590");
		return (false);
	}
	}
	
	if(theform.contents_sum) {
	
		if(theform.contents_sum.value > 0) {
	
			if(theform.contents_sum.value > 50000)
			{
				alert("Contents sum maximum is £50,000");
				theform.contents_sum.focus();
				return (false);
			}
	
			if(theform.buildings_sum.value > 0) {
			
				var property_type = (theform.property_type.value)
				
				if(property_type == "Apartment") {
				var buildings = (theform.buildings_sum.value/10)
				
				if(theform.contents_sum.value < buildings)
				{
					alert("Contents sum must be a minumum of 10% of buildings sum insured");
					theform.contents_sum.focus();
					return (false);
				}
				
				}
												
				if(theform.contents_sum.value < 5000)
				{
					alert("Contents sum minimum is £5,000");
					theform.contents_sum.focus();
					return (false);
				}								
												
												}
												
			if(theform.buildings_sum.value == "") 	{
							
				if(theform.contents_sum.value < 5000)
				{
					alert("Contents sum minimum is £5,000");
					theform.contents_sum.focus();
					return (false);
				}
													}
													
				if(theform.buildings_sum.value < 10000) {
			
				var property_type = (theform.property_type.value)
				if(property_type == "Apartment")
				{
					alert("Apartments must have a minimum of £10,000 buildings sum insured");
					theform.buildings_sum.focus();
					return (false);
				}
														}
													
		}
	}
	
	if(theform.house_number_ins) {
	if(theform.house_number_ins.value == "")
	{
		alert("Please enter the insured address house number");
		theform.house_number_ins.focus();
		return (false);
	}
	}
	
	if(theform.address_1_ins) {
	if(theform.address_1_ins.value == "")
	{
		alert("Please enter the insured address line 1");
		theform.address_1_ins.focus();
		return (false);
	}
	}
	
	if(theform.town_city_ins) {
	if(theform.town_city_ins.value == "")
	{
	alert("Please enter the insured address town/city");
		theform.town_city_ins.focus();
		return (false);
	}
	}
	
	if(theform.year_built_ins) {
	if(theform.year_built_ins.value == "")
	{
		alert("Please enter the insured address year built");
		theform.year_built_ins.focus();
		return (false);
	}
	}

	if(theform.cracks_yn) {
	var e = document.forms['application'].elements['cracks_yn'];
	if(!e[1].checked)
	{
			alert("Please call us on 0870 121 7590 for a quotation");
			return (false);	
	}
	}
	
	if(theform.subs_yn) {
	var e = document.forms['application'].elements['subs_yn'];
	if(!e[1].checked)
	{
			alert("Please call us on 0870 121 7590 for a quotation");
			return (false);	
	}
	}
	
	if(theform.flooding_yn) {
	var e = document.forms['application'].elements['flooding_yn'];
	if(!e[1].checked)
	{
			alert("Please call us on 0870 121 7590 for a quotation");
			return (false);
	}
	}
	
	if(theform.nonstandard_yn) {
	var e = document.forms['application'].elements['nonstandard_yn'];
	if(!e[1].checked)
	{
			alert("Please call us on 0870 121 7590 for a quotation");
			return (false);	
	}
	}
	
	if(theform.claims_yn) {
	var e = document.forms['application'].elements['claims_yn'];
	if(!e[1].checked)
	{
			alert("Please call us on 0870 121 7590 for a quotation");
			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);
	}
	}
	
	if(theform.clauses) {
	if(!theform.clauses.checked)
	{
		alert("You must read the applicable clauses before proceeding");
		theform.clauses.focus();
		return (false);
	}
	}
	
	return (true);
}
