/**
*	Validate
*	Return boolean to indicate if the Buildstore Registration form is valid
*
*	@param	Form			HTML Page being validated
**/

var form=document.getElementById('financeform');

function Validate(form)
{
	errorText = "The following fields must be completed. Please correct and re-submit :\n";
	valid = true;

	// Title must be selected
	if (form.titleID.value == "-")
	{
		errorText = errorText + "\n\t- Title";
		if (valid) form.titleID.focus();
		valid = false;
	}

	// Forename must be entered
	if (form.forename.value == "")
	{
		errorText = errorText + "\n\t- Forename";
		if (valid) form.forename.focus();
		valid = false;
	}

	// Surname must be entered
	if (form.surname.value == "")
	{
		errorText = errorText + "\n\t- Surname";
		if (valid) form.surname.focus();
		valid = false;
	}
	
	// DOB must be entered
	if (form.DOB.value == "")
	{
		errorText = errorText + "\n\t- Date of birth";
		if (valid) form.DOB.focus();
		valid = false;
	}
	
	// Marital Status must be entered
	if (form.MaritalStatus.value == "-")
	{
		errorText = errorText + "\n\t- Marital Status must be entered";
		if (valid) form.MaritalStatus.focus();
		valid = false;
	}

	// Address must be entered
	if (form.AppAdd1.value == "")
	{
		errorText = errorText + "\n\t- Address Line 1";
		if (valid) form.AppAdd1.focus();
		valid = false;
	}
	
	// City must be entered
	if (form.AppCity.value == "")
	{
		errorText = errorText + "\n\t- City";
		if (valid) form.AppCity.focus();
		valid = false;
	}
	
	// County must be entered
	if (form.AppCounty.value == "")
	{
		errorText = errorText + "\n\t- County";
		if (valid) form.AppCounty.focus();
		valid = false;
	}
	
	// Postcode must be entered
	if (form.postcode.value == "")
	{
		errorText = errorText + "\n\t- Postcode";
		if (valid) form.postcode.focus();
		valid = false;
	}

	// Trim the telephone numbers
	form.telDay.value = RightTrim(form.telDay.value);
	form.telEve.value = RightTrim(form.telEve.value);
	form.telMob.value = RightTrim(form.telMob.value);

	// At least one telephone number must be entered
	if ((form.telDay.value == "") && (form.telEve.value == "") && (form.telMob.value == ""))
	{
		errorText = errorText + "\n\t- You must enter at least one telephone number";
		if (valid) form.telDay.focus();
		valid = false;
	}
	
	if(form.telDay.value != "")
	{
		if(!isTelNo(form.telDay))
		{
			errorText = errorText + "\n\t- Daytime telephone number is invalid (only 0-9,+,-,(,) allowed)\n\t and must be at least 6 characters";
			valid = false;
		}
	}
	
	if(form.telEve.value != "")
	{
		if(!isTelNo(form.telEve))
		{
			errorText = errorText + "\n\t- Evening telephone number is invalid (only 0-9,+,-,(,) allowed)\n\t and must be at least 6 characters";
			valid = false;
		}
	}
	
	if(form.telMob.value != "")
	{
		if(!isTelNo(form.telMob))
		{
			errorText = errorText + "\n\t- Mobile telephone number is invalid (only 0-9,+,-,(,) allowed)\n\t and must be at least 6 characters";
			valid = false;
		}
	}
	
	// Email address must be entered, and must be valid
	if ((form.email.value == "") || (! isEmail(form.email)))
	{
		errorText = errorText + "\n\t- Email address must be in the format user@company.co.uk";
		if (valid) form.email.focus();
		valid = false;
	}
	
		// Hard about BuildStore Finance
	if (form.HeardAboutUs.value == "-")
	{
		errorText = errorText + "\n\t- How did you hear about BuildStore Finance must be selected";
		if (valid) form.HeardAboutUs.focus();
		valid = false;
	}
	
	// Terms and conditions need to be agreed
	if (!form.terms.checked)
	{
		errorText = errorText + "\n\t- Terms and Conditions need to be agreed";
		if (valid) form.terms.focus();
		valid = false;
	}
	

	
	// Build type must be entered
	if (form.BuildType.value == "-")
	{
		errorText = errorText + "\n\t- Build type must be selected";
		if (valid) form.BuildType.focus();
		valid = false;
	}
	
	// Land Status must be entered
	if (form.LandStatus.value == "-")
	{
		errorText = errorText + "\n\t- Land status must be selected";
		if (valid) form.LandStatus.focus();
		valid = false;
	}
	
	// Planning Status must be entered
	if (form.PlanningPermission.value == "-")
	{
		errorText = errorText + "\n\t- Planning status must be selected";
		if (valid) form.PlanningPermission.focus();
		valid = false;
	}
	
	// Reason for project must be entered
	if (form.BuildReason.value == "-")
	{
		errorText = errorText + "\n\t- Reason for project must be selected";
		if (valid) form.BuildReason.focus();
		valid = false;
	}


	// Any errors?
	if (valid == false)
		alert (errorText);

	// Inform the web page
	return valid;
}

/**
*	RightTrim
*	Trim trailing spaces from a string
*
**/

function RightTrim(textString)
{
	textString += "";
	TrimChar = " ";
	if (textString.length == 0)
		return (textString);
	var Count = 0;
	for(Count = textString.length -1;Count >= 0;Count--)
	{
		if (!(textString.charAt(Count) == TrimChar))
			return (textString.substring(0, Count + 1));
	}
	return ("");
}

/**
*	isEmail
*	Ensure that an email address is valid
*
*	Email address must be of form a@b.c -- in other words:
*	there must be at least one character before the @
*	there must be at least one character before and after the .
*	the characters @ and . are both required
**/

function isEmail (inField)
{
	var checkEmail = inField.value;

	// there must be >= 1 character before @, so we
	// start looking at character position 1
	// (i.e. second character)
	var i = 1;
	var checkEmailLength = checkEmail.length;

	// look for @
	while ((i < checkEmailLength) && (checkEmail.charAt(i) != "@"))
		i++;

	if ((i >= checkEmailLength) || (checkEmail.charAt(i) != "@"))
	{
		inField.focus();
		return false;
	}
	else
		i += 2;

	// look for .
	while ((i < checkEmailLength) && (checkEmail.charAt(i) != "."))
		i++;

	// there must be at least one character after the .
	if ((i >= checkEmailLength - 1) || (checkEmail.charAt(i) != "."))
	{
		inField.focus();
		return false;
	}
	else
		return true;
}

function isTelNo (inField)
{
	var checkTelNo = inField.value;
	
	var validchars = "0123456789-+() ";
	
	var i = 6;
	var checkTelNoLength = checkTelNo.length;

	if (i > checkTelNoLength)
	{
		inField.focus();
		//alert('Please Enter a valid Telephone Number');
		return false;
	}
	
	i = 0;
	
	// look for invalid characters
	while ((i < checkTelNoLength) && (validchars.indexOf(checkTelNo.charAt(i))!= -1))
		i++;

	// there must be at least one character after the .
	if (i != checkTelNoLength)
	{
		inField.focus();
		return false;
	}
	else
		return true;
}