
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function IsPrice(sText)
{
   var ValidChars = "0123456789.,";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}


function IsInteger(sText)
{
   var ValidChars = "0123456789";
   var IsInteger=true;
   var Char;
   for (i = 0; i < sText.length && IsInteger == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsInteger = false;
         }
      }
   return IsInteger;
}


function validateCustomerData(form) 
{
  //alert('Checking form...');
  var errorMsg = '';
  
  // Test that all the required fields are filled out ok
  
   if (form.Customer.value == "")
   {
    errorMsg += 'Please enter the name of the policy holder\n';
	form.Customer.focus();
	form.Customer.select();
   }
   
  if (form.mobile.value == "") 
  {
    errorMsg += 'Please give a Mobile number in case we need to contact you\n';
	form.mobile.focus();
	form.mobile.select();
  }
  

  if (form.occupation.value == "") 
    errorMsg += 'Please enter the policy holders occupation\n';
  else if (IsNumeric(form.occupation.value))
    errorMsg += 'The occupation field should contain only letters\n';


      if (form.policyholderAdd.value == "")
   {
    errorMsg += 'Please enter the policy holders address\n';
	form.policyholderAdd.focus();
	form.policyholderAdd.select();
  }  
        if (form.policyholderPostcode.value == "")
   {
    errorMsg += 'Please enter the policy holders Postcode\n';
	form.policyholderPostcode.focus();
	form.policyholderPostcode.select();
  }  
  // Check radio button questions are answered...
  if (!form.prevloss[0].checked && !form.prevloss[1].checked)
	errorMsg += 'Please answer question 1\n';
  
  if (!form.insDeclined[0].checked && !form.insDeclined[1].checked)
	errorMsg += 'Please answer question 2\n';
		
  if (!form.criminal[0].checked && !form.criminal[1].checked)
	errorMsg += 'Please answer question 3\n';
		
  if (!form.bankrupt[0].checked && !form.bankrupt[1].checked)
	errorMsg += 'Please answer question 4\n';
	
  // Ensure form does not allow any YES answers to the questions	
  if (form.prevloss[0].checked || form.insDeclined[0].checked || form.criminal[0].checked || form.bankrupt[0].checked)
  	errorMsg += "Please contact our office on 08000 280380\n";
  
  
  var radio_choice = false;
  // Loop from zero to the one minus the number of radio button selections
  for (counter = 0; counter < form.Amount.length; counter++)
  {
    // If a radio button has been selected it will return true (If not it will return false)
	if (form.Amount[counter].checked)
		radio_choice = true; 
  }
  if (!radio_choice)
	errorMsg += 'Please select the sum to be insured\n';
	
	var radio_choice2 = false;
  // Loop from zero to the one minus the number of radio button selections
  for (counter = 0; counter < form.heard_about_us.length; counter++)
  {
    // If a radio button has been selected it will return true (If not it will return false)
	if (form.heard_about_us[counter].checked)
		radio_choice2 = true; 
  }
  if (!radio_choice2)
	errorMsg += 'Please select How you heard about us\n';
//email check
  if (form.email.value == "" )
  {
    errorMsg += 'Your Email Address is required\n';
	form.email.focus();
  }
  else
  {
	x = form.email.value.indexOf('@');
	y = form.email.value.indexOf('.');

    if (x < 1 || x == (form.email.value.length-1) || y < 1 ) 
	{
	  errorMsg += 'You must give a valid Email Address.\n';
	  form.email.focus();
	  form.email.select();
	}
	  
    s = form.email.value.indexOf(' ');
    if (s != -1)
	{
	  errorMsg += 'Please check your Email Address. It should not contain spaces\n';
	  form.email.focus();
	  form.email.select();
	}
  }
//end email check
	 
   if (errorMsg) 
	alert(errorMsg);
		
  document.returnValue = (errorMsg == '');
}


function validateBuyersData(form) 
// Used with ProTX forms
{
  //alert('Checking form...');
  var errorMsg = '';
  
  // Test that all the required fields are filled out ok
  
  if (form.Customeremail.value == "" )
  {
    errorMsg += 'Your Email Address is required\n';
	form.Customeremail.focus();
  }
  else
  {
	x = form.Customeremail.value.indexOf('@');
	y = form.Customeremail.value.indexOf('.');

    if (x < 1 || x == (form.Customeremail.value.length-1) || y < 1 ) 
	{
	  errorMsg += 'You must give a valid Email Address.\n';
	  form.Customeremail.focus();
	  form.Customeremail.select();
	}
	  
    s = form.Customeremail.value.indexOf(' ');
    if (s != -1)
	{
	  errorMsg += 'Please check your Email Address. It should not contain spaces\n';
	  form.Customeremail.focus();
	  form.Customeremail.select();
	}
  }
  	
  if (form.CustomerName.value == "" )
    errorMsg += 'Your Name is required\n';


  if (form.BillingAddress.value == "" )
  	errorMsg += 'Please enter the Card Holders Billing Address\n';

  
  if ( ! form.termsconditions.checked )
  	errorMsg += 'Please indicate that you agree to our Terms & Conditions\n';
	
   if (errorMsg) 
	alert('Required form fields:\n'+errorMsg);
  document.returnValue = (errorMsg == '');
}


function validatePurchaseForm(form) 
{
  //alert('Checking Purchase form...');
  var errorMsg = '';
  
  // Test that all the required fields are filled out ok
  	
  if (form.name.value == "") 
    errorMsg += 'Please give your name\n';
	
	
  if (form.Email.value == "" )
  {
    errorMsg += 'Your Email Address is required\n';
	form.Email.focus();
  }
  else
  {
	x = form.Email.value.indexOf('@');
	y = form.Email.value.indexOf('.');

    if (x < 1 || x == (form.Email.value.length-1) || y < 1 ) 
	{
	  errorMsg += 'You must give a valid Email Address.\n';
	  form.Email.focus();
	  form.Email.select();
	}
	  
    s = form.Email.value.indexOf(' ');
    if (s != -1)
	{
	  errorMsg += 'Please check your Email Address. It should not contain spaces\n';
	  form.Email.focus();
	  form.Email.select();
	}
  }
	
  if (form.price.value == "")
    errorMsg += 'Please enter the price of the property you intend to purchase\n';
  else if (!IsPrice(form.price.value))
  	{
     errorMsg += 'Please enter the price of the property you intend to purchase - without symbols or spaces';
	 form.price.focus();
	 form.price.select();
	}

   if (errorMsg) 
	alert(errorMsg);
		
  document.returnValue = (errorMsg == '');
}


function validateSaleForm(form) 
{
  //alert('Checking Sale form...');
  var errorMsg = '';
  
  // Test that all the required fields are filled out ok
  	
  if (form.name.value == "") 
    errorMsg += 'Please give your name\n';
	
	
  if (form.Email.value == "" )
  {
    errorMsg += 'Your Email Address is required\n';
	form.Email.focus();
  }
  else
  {
	x = form.Email.value.indexOf('@');
	y = form.Email.value.indexOf('.');

    if (x < 1 || x == (form.Email.value.length-1) || y < 1 ) 
	{
	  errorMsg += 'You must give a valid Email Address.\n';
	  form.Email.focus();
	  form.Email.select();
	}
	  
    s = form.Email.value.indexOf(' ');
    if (s != -1)
	{
	  errorMsg += 'Please check your Email Address. It should not contain spaces\n';
	  form.Email.focus();
	  form.Email.select();
	}
  }
	
  if (form.price.value == "")
    errorMsg += 'Please enter the price of the property you are selling\n';
  else if (!IsPrice(form.price.value))
  	{
     errorMsg += 'Please enter the price of the property you sre selling - without symbols or spaces';
	 form.price.focus();
	 form.price.select();
	}
	
   if (form.salebedrooms.value == "")
    errorMsg += 'Please enter the number of bedrooms the property is being marketed with\n';
  else if (!IsInteger(form.salebedrooms.value))
  	{
     errorMsg += 'Please enter a whole number - without symbols or spaces';
	 form.salebedrooms.focus();
	 form.saleebedrooms.select();
	}	
	 
   if (errorMsg) 
	alert(errorMsg);
		
  document.returnValue = (errorMsg == '');
}


function validateSalePurchaseForm(form) 
{
  //alert('Checking Sale & Purchase form...');
  var errorMsg = '';
  
  // Test that all the required fields are filled out ok
  	
  if (form.name.value == "") 
    errorMsg += 'Please give your name\n';
	
	
  if (form.Email.value == "" )
  {
    errorMsg += 'Your Email Address is required\n';
	form.Email.focus();
  }
  else
  {
	x = form.Email.value.indexOf('@');
	y = form.Email.value.indexOf('.');

    if (x < 1 || x == (form.Email.value.length-1) || y < 1 ) 
	{
	  errorMsg += 'You must give a valid Email Address.\n';
	  form.Email.focus();
	  form.Email.select();
	}
	  
    s = form.Email.value.indexOf(' ');
    if (s != -1)
	{
	  errorMsg += 'Please check your Email Address. It should not contain spaces\n';
	  form.Email.focus();
	  form.Email.select();
	}
  }
	
	
  if (form.saleprice.value == "")
    errorMsg += 'Please enter the price of the property you are selling\n';
  else if (!IsPrice(form.saleprice.value))
  	{
     errorMsg += 'Please enter the price of the property you sre selling - without symbols or spaces';
	 form.saleprice.focus();
	 form.saleprice.select();
	}
	
	
  if (form.purchaseprice.value == "")
    errorMsg += 'Please enter the price of the property you are buying\n';
  else if (!IsPrice(form.purchaseprice.value))
  	{
     errorMsg += 'Please enter the price of the property you are buying - without symbols or spaces';
	 form.purchaseprice.focus();
	 form.purchaseprice.select();
	}
	
	
	
  if (form.salebedrooms.value == "")
    errorMsg += 'Please enter the number of bedrooms the sale property is being marketed with\n';
  else if (!IsInteger(form.salebedrooms.value))
  	{
     errorMsg += 'Please enter a whole number - without symbols or spaces';
	 form.salebedrooms.focus();
	 form.saleebedrooms.select();
	}	
	
	 
   if (errorMsg) 
	alert(errorMsg);
		
  document.returnValue = (errorMsg == '');
}


function validateRemortgageForm(form) 
{
  //alert('Checking Remortgage form...');
  var errorMsg = '';
  
  // Test that all the required fields are filled out ok
  	
  if (form.name.value == "") 
    errorMsg += 'Please give your name\n';
	
	
  if (form.Email.value == "" )
  {
    errorMsg += 'Your Email Address is required\n';
	form.Email.focus();
  }
  else
  {
	x = form.Email.value.indexOf('@');
	y = form.Email.value.indexOf('.');

    if (x < 1 || x == (form.Email.value.length-1) || y < 1 ) 
	{
	  errorMsg += 'You must give a valid Email Address.\n';
	  form.Email.focus();
	  form.Email.select();
	}
	  
    s = form.Email.value.indexOf(' ');
    if (s != -1)
	{
	  errorMsg += 'Please check your Email Address. It should not contain spaces\n';
	  form.Email.focus();
	  form.Email.select();
	}
  }
	
  if (form.price.value == "")
    errorMsg += 'Please enter the loan amount\n';
  else if (!IsPrice(form.price.value))
  	{
     errorMsg += 'Please enter the loan amount - without symbols or spaces';
	 form.price.focus();
	 form.price.select();
	}
	 
   if (errorMsg) 
	alert(errorMsg);
		
  document.returnValue = (errorMsg == '');
}



function validateAcceptForm(form) 
{
  //alert('Checking Acceptance form...');
  var errorMsg = '';
  
  // Test that all the required fields are filled out ok
  	
  if (form.name.value == "") 
    errorMsg += 'Please give your full name\n';
	
	
  if (form.Email.value == "" )
  {
    errorMsg += 'Your Email Address is required\n';
	form.Email.focus();
  }
  else
  {
	x = form.Email.value.indexOf('@');
	y = form.Email.value.indexOf('.');

    if (x < 1 || x == (form.Email.value.length-1) || y < 1 ) 
	{
	  errorMsg += 'You must give a valid Email Address.\n';
	  form.Email.focus();
	  form.Email.select();
	}
	  
    s = form.Email.value.indexOf(' ');
    if (s != -1)
	{
	  errorMsg += 'Please check your Email Address. It should not contain spaces\n';
	  form.Email.focus();
	  form.Email.select();
	}
  }
	
  if (form.tel.value == "")
    errorMsg += 'Please enter a telephone number\n';

	 
   if (errorMsg) 
	alert(errorMsg);
		
  document.returnValue = (errorMsg == '');
}


function validateRetrieveQuote(form) 
{
  //alert('Checking form...');
  var errorMsg = '';
  
  // Test that all the required fields are filled out ok
  	
  if (form.Reference.value == "") 
    errorMsg += 'Please enter the reference number from the original quote\n';
  else if (!IsNumeric(form.Reference.value))
    errorMsg += 'The reference number from the original quote should contain only digits\n';
	
  if (form.Email.value == "" )
  {
    errorMsg += 'Your Email Address is required\n';
	form.Email.focus();
  }
  else
  {
	x = form.Email.value.indexOf('@');
	y = form.Email.value.indexOf('.');

    if (x < 1 || x == (form.Email.value.length-1) || y < 1 ) 
	{
	  errorMsg += 'You must give a valid Email Address.\n';
	  form.Email.focus();
	  form.Email.select();
	}
	  
    s = form.Email.value.indexOf(' ');
    if (s != -1)
	{
	  errorMsg += 'Please check your Email Address. It should not contain spaces\n';
	  form.Email.focus();
	  form.Email.select();
	}
  }
	
	 
   if (errorMsg) 
	alert(errorMsg);
		
  document.returnValue = (errorMsg == '');
}

function validateConfirmation(form) 
{
  //alert('Checking form...');
  var errorMsg = '';
  
  // Test that all the required fields are filled out ok
  	
  if (form.ClientAddress.value == "") 
    errorMsg += 'Please enter your current address\n';
  
  if (!form.Confirm.checked) 
    errorMsg += 'Please confirm that your lender will act for you in this conveyancing transaction\n';
	 
   if (errorMsg) 
	alert(errorMsg);
		
  document.returnValue = (errorMsg == '');
}

function validateHipConfirmation(form) 
{
  //alert('Checking Hip form...');
  var errorMsg = '';
  
  if (form.name.value == "") 
    errorMsg += 'Please enter your full name\n';
  
  // Test that all the required fields are filled out ok
    if (form.Email.value == "" )
  {
    errorMsg += 'Your email address is required\n';
	form.Email.focus();
  }
  else
  {
	x = form.Email.value.indexOf('@');
	y = form.Email.value.indexOf('.');

    if (x < 1 || x == (form.Email.value.length-1) || y < 1 ) 
	{
	  errorMsg += 'You must give a valid email address.\n';
	  form.Email.focus();
	  form.Email.select();
	}
	  
    s = form.Email.value.indexOf(' ');
    if (s != -1)
	{
	  errorMsg += 'Please check your email address. It should not contain spaces\n';
	  form.Email.focus();
	  form.Email.select();
	}
  }
  	
  if (form.ClientAddress.value == "") 
    errorMsg += 'Please enter your current address\n';
  
  if (!form.Confirm.checked) 
    errorMsg += 'Please confirm that Layard Horsfall can act for you in this HIP preparation.\n';
	 
   if (errorMsg) 
	alert(errorMsg);
		
  document.returnValue = (errorMsg == '');
}