function validateform(form) 
{
	errors = '';
	if(form.txtTitle.value == 0) errors += 'You must select your title\n';
	if(form.txtFirstName.value == '') errors += 'You must enter your first name\n';
	if(form.txtSurname.value == '') errors += 'You must enter your surname\n';
	if(form.txtContactNumber.value == '') errors += 'You must enter your telephone no.\n';
	if(!form.txtContactNumber.value == '')
	{
		if(!IsNumeric(form.txtContactNumber.value)) errors += 'Please enter your telephone no. in the correct format\n';
	}
	if(form.txtMobileNumber.value == '') errors += 'You must enter your mobile no.\n';
	if(!form.txtMobileNumber.value == '')
	{
		if(!IsNumeric(form.txtMobileNumber.value)) errors += 'Please enter your mobile no. in the correct format\n';
	}
	if(form.txtCallTime.value == '') errors += 'You must enter your preferred time to receive call \n';
	if(form.txtEmailAddress.value == '') errors += 'You must enter your contact email address\n';
	if(!form.txtEmailAddress.value == '')
	{
		if(!isEmail(form.txtEmailAddress)) errors += 'Please enter your contact email address in the correct format\n';
	}
	if(form.txtHouse.value == '') errors += 'You must enter your house no.\n';
	if(form.txtRoad.value == '') errors += 'You must enter your street\n';
	if(form.txtTown.value == '') errors += 'You must enter your town/city\n';
	if(form.txtCounty.value == '') errors += 'You must enter your county\n';			
	if(form.txtPostCode.value == '') errors += 'You must enter your postcode\n';
	if(!form.txtPostCode.value == '')
	{
		if(!checkPostCode(form.txtPostCode.value)) errors += 'Please enter your postcode in the correct format\n';
	}
	if(form.txtAddressYears.value == '') errors += 'You must enter how long you have lived in your council house\n';
	if(!form.rdoSection125[0].checked && !form.rdoSection125[1].checked)
	{
		errors += 'Please indicate if the council has sent you a Section 125 or offer notice? (If unsure select no.)\n';
	}
	if(form.rdoSection125[0].checked)
	{
		if(form.txtCouncil.value == '') errors += 'Please enter the council have you received your section 125 notice from\n';
		if(form.txtPropertyValue.value == '') errors += 'Please enter purchase price\n';
		if(form.txtNoticedate.value == '') errors += 'Please enter date of notice\n';
	}
	if(form.rdoSection125[1].checked)
	{
		if(!form.rdoApply[0].checked && !form.rdoApply[1].checked) errors += 'Please indicate if you applied for your Right to Buy?\n';
		
	}	
					
	if(errors += '') 
	{
		alert(errors);
		return false;
	} 
	else 
	{
		return true;
	}
}


function checkPostCode (toCheck) {
  // This array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (/^([a-z]{1,2}[0-9]{1,2})(\s*)([0-9]{1}[abdefghjlnpqrstuwxyz]{2})$/i);

  // Expression for postcodes: ANA NAA, and AANA  NAA
  pcexp.push (/^([a-z]{1,2}[0-9]{1}[a-z]{1})(\s*)([0-9]{1}[abdefghjlnpqrstuwxyz]{2})$/i);
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against both post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop

      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  //if (valid) {return postCode;} else return false;
  if (valid) {return true;} else return false;
}
 function IsNumeric(strString)
   //  check for valid numeric strings	
   {
	var strValidChars = "0123 456 789";
	var strChar;
	var blnResult = true;

	//if (strString.length == 0) return false;
	if (strString.length <= 10) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
	return blnResult;
	}

	
function isEmail(formInput)
// Check emailaddress structure
{
	var atPosition, dotPosition, lastPosition;
	with (formInput)
	{
		aPosition = value.indexOf("@");
		dotPosition = value.lastIndexOf(".");
		lastPosition = value.length-1;
		if (aPosition < 1 || dotPosition - aPosition < 2 || lastPosition - dotPosition > 3 || lastPosition - dotPosition < 2)
		{
			return(false);
		}
		return(true);
	}
}

