function trim(s)
{
    var l = 0;
    var r = s.length - 1;
    while (l < s.length && s[l] == ' ')
    {
        l++;
    }
    while (r > l && s[r] == ' ')
    {
        r -= 1;
    }
    return s.substring(l, r + 1);
}

function isDigitOnly(s)
{
	for (i=0; i<s.length; i++)
	{
		if (s.charAt(i) < "0" || s.charAt(i) > "9")
			return false;
	}
	return true;
}

function maritalStatus_errors()
{
	return (document.forms[0].maritalStatus.selectedIndex==0);
}

function dependants_errors()
{
	var dependants = trim(document.forms[0].dependants.value);
	
	if (dependants.length == 0)
		return true;
		
	return (!isDigitOnly(dependants));
}

function validate_phonenumber(v)
{
	// phone nos must be 9 chars or more
	if (v.length < 9)
        return false;
	
	// prefix (optional) can contain the following chars '(' '+' ')'
	if ((v.indexOf("(") != -1) && (v.indexOf(")") != -1))
	{
		var prefix = trim(v.substr(v.indexOf("("), v.lastIndexOf(")")-v.indexOf("(")+1));
		for (i=0; i<prefix.length; i++)
		{
			phoneChar = prefix.charAt(i);
			if (!(!isNaN(phoneChar) || phoneChar == ' ' || phoneChar == '(' || phoneChar == ')' || phoneChar == "+" ))
			{
				return false;
			}
		}
	}

	// tel no must contain only digits
	var telNo = trim(v.substr(v.lastIndexOf(")")+1));
	for (i=0; i<telNo.length; i++)
	{
		phoneChar = telNo.charAt(i);
		if (isNaN(phoneChar))
		{
			return false;
		}
	}

	return true;
}

function validate_landline(v)
{
	if (!validate_phonenumber(v))
		return false;
		
	var telNo = trim(v.substr(v.lastIndexOf(")")+1));
	if (telNo.indexOf("07") == 0)
		return false;
		
	return true;
}

function phone_errors() 
{
    var completedPhones = 0;

	var homePhone = trim(document.forms[0].homePhone.value);
	var workPhone = trim(document.forms[0].workPhone.value);
	var mobilePhone = trim(document.forms[0].mobilePhone.value);

	if ((homePhone != null) && (homePhone.length > 0))
	{
		if (validate_landline(homePhone)) 
			completedPhones++;
		else
			completedPhones--;
	}
	
	if ((workPhone != null) && (workPhone.length > 0))
	{	
		if (validate_landline(workPhone)) 
			completedPhones++;
		else
			completedPhones--;
	}
		
	if ((mobilePhone != null) && (mobilePhone.length > 0))
	{
		if (validate_phonenumber(mobilePhone)) 
			completedPhones++;
		else
			completedPhones--;
	}
	
	if (homePhone == workPhone)
		completedPhones--;
	if (workPhone == mobilePhone)
		completedPhones--;
	if (homePhone == mobilePhone)
		completedPhones--;

    return (completedPhones < 2);
}

// http://www.cabinetoffice.gov.uk/govtalk/schemasstandards/e-gif/datastandards/address/postcode.aspx
function postCode_errors()
{
	var postcodeValue = trim(document.forms[0].postCode.value).toUpperCase();
	if (postcodeValue.length < 5 || postcodeValue.length > 8)
		return true;

	// if no space is present - add one before the last 3 characters
	if (postcodeValue.indexOf(" ") == -1)
	{
		postcodeValue1 = postcodeValue.substring(0, postcodeValue.length-3);
		postcodeValue2 = postcodeValue.substring(postcodeValue.length-3, postcodeValue.length);
		postcodeValue = postcodeValue1 + " " + postcodeValue2;
	}
	
	var postcodeRegExp = /[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}/;	
	var validPostcode = postcodeValue.match(postcodeRegExp);
	
	return postcodeValue != validPostcode;	
}
