// JavaScript Document
<!--
var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;
var at="@";
var dot=".";
function isInteger(s)
{   var i;
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	return true;
}
function stripCharsInBag(s, bag)
{   var i;
	var returnString = "";
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}
function checkInternationalPhone(strPhone){
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
function echeck(str) {
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
		alert("The Email address is invalid.\n\nPlease try again.");
		return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		alert("The Email address is invalid.\n\nPlease try again.");
		return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("The Email address is invalid.\n\nPlease try again.");
		return false;
	}
	if (str.indexOf(at,(lat+1))!=-1){
		alert("The Email address is invalid.\n\nPlease try again.");
		return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("The Email address is invalid.\n\nPlease try again.");
		return false;
	}
	if (str.indexOf(dot,(lat+2))==-1){
		alert("The Email address is invalid.\n\nPlease try again.");
		return false;
	}
	if (str.indexOf(" ")!=-1){
		alert("The Email address is invalid.\n\nPlease try again.");
		return false;
	}
	return true;
}

function ctcFm() {

	if(document.getElementById('fmName').value == "")
	{
		alert("The Name field is incomplete.\n\nPlease try again.");
		return false;
	}
	
	if( document.getElementById('fmBusiness').value == "")
	{
		alert("The 'Business Name' field is incomplete.\n\nPlease try again.");
		return false;
	}
	
	var Phone=document.getElementById('fmFone')
	
	if ( Phone.value == null || Phone.value == "" )
	{
		alert("The Telephone Number field is incomplete.\n\nPlease try again.");
		Phone.focus();
		return false;
	}
	if (checkInternationalPhone(Phone.value)==false)
	{
		alert("The Telephone Number is invalid.\n\nPlease try again.");
		Phone.value=""
		Phone.focus();
		return false;
	}
	
	var emailID=document.getElementById('fmEmail')
	if ( emailID.value==null || emailID.value=="" )
	{
		alert("The Email field is incomplete.\n\nPlease try again.");
		emailID.focus();
		return false;
	}
	
	if (echeck(emailID.value)==false)
	{
		emailID.value="";
		emailID.focus();
		return false;
	}
	return true;
}

function paFm() {

	// Check Name	
	var Fname=document.getElementById('fmName')
	var FnameRegex=/(\d+)/; //No numerics
	if( Fname.value == "")
	{
		alert("The Name field is incomplete.\n\nPlease try again.");
		Fname.focus();
		return false;
	}
	if (check_using_regex(Fname.value,FnameRegex)==true)
	{
		alert("The Name field is invalid - it can't contain numbers.\n\nPlease try again.");
		Fname.value="";
		Fname.focus();
		return false;
	}

	// Check Business Name	
	var Bname=document.getElementById('fmBusiness')
	if( Bname.value == "")
	{
		alert("The 'Business Name' field is incomplete.\n\nPlease try again.");
		Bname.focus();
		return false;
	}

	// Check Postcode
	var Postcode=document.getElementById('Postcode')
	if( Postcode.value == "")
	{
		alert("The 'Postcode' field is incomplete.\n\nPlease try again.");
		Postcode.focus();
		return false;
	}


	// Check PhoneNO
	var Phone=document.getElementById('fmFone')
	var PhoneRegex=/(^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?#(\d{4}|\d{3}))?$)/;
	if ( Phone.value == null || Phone.value == "" )
	{
		alert("The Telephone Number field is incomplete.\n\nPlease try again.");
		Phone.focus();
		return false;
	}
	
	if (check_using_regex(Phone.value,PhoneRegex)==false)
	{
		alert("The Telephone Number is invalid - must be a UK number with no international codes.\n\nPlease try again.");
		Phone.value="";
		Phone.focus();
		return false;
	}
	
	// Check Email
	var Emailaddr=document.getElementById('fmEmail')
	var EmailRegex=/(^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$)/;
	if ( Emailaddr.value==null || Emailaddr.value=="" )
	{
		alert("The Email field is incomplete.\n\nPlease try again.");
		Emailaddr.focus();
		return false;
	}
	if (check_using_regex(Emailaddr.value,EmailRegex)==false)
	{
		alert("The Email address is invalid.\n\nPlease try again.");
		Emailaddr.value="";
		Emailaddr.focus();
		return false;
	}
	if (email_final_checks(Emailaddr.value)==false)
	{
		alert("The Email address is invalid.\n\nPlease try again.");
		Emailaddr.value="";
		Emailaddr.focus();
		return false;
	}

	// Check Check boxes - at least 1 should be selected
	if(getchboxValue(document.fmForm.fmElec) == false && 
	   getchboxValue(document.fmForm.fmGas) == false &&
	   getchboxValue(document.fmForm.fmWater) == false && 
	   getchboxValue(document.fmForm.fmMobile) == false && 
	   getchboxValue(document.fmForm.fmLandline) == false && 
	   getchboxValue(document.fmForm.fmBband) == false && 
	   getchboxValue(document.fmForm.fmInsu) == false ) 
	{
		alert("No utility was selected.\n\nPlease select at least one utility.");
		return false;
	}

	document.fmForm.validationcode.value='1';
	return true;
}

function validate_form() {

	// Check Business Name	
	var Bname=document.getElementById('fmBusiness')
	if( Bname.value == "")
	{
		alert("The 'Business Name' field is incomplete.\n\nPlease try again.");
		Bname.focus();
		return false;
	}

	// Check Postcode
	var Postcode=document.getElementById('Postcode')
	if( Postcode.value == "")
	{
		alert("The 'Postcode' field is incomplete.\n\nPlease try again.");
		Postcode.focus();
		return false;
	}

	// Check First Name	
	var Fname=document.getElementById('fmFname')
	var FnameRegex=/(\d+)/; //No numerics
	if( Fname.value == "")
	{
		alert("The First Name field is incomplete.\n\nPlease try again.");
		Fname.focus();
		return false;
	}
	if (check_using_regex(Fname.value,FnameRegex)==true)
	{
		alert("The First Name field is invalid - it can't contain numbers.\n\nPlease try again.");
		Fname.value="";
		Fname.focus();
		return false;
	}

	// Check Surname
	var Lname=document.getElementById('fmLname')
	var LnameRegex=/(\d+)/; //No numerics
	if( Lname.value == "")
	{
		alert("The Surname field is incomplete.\n\nPlease try again.");
		Lname.focus();
		return false;
	}
	if (check_using_regex(Lname.value,LnameRegex)==true){
		alert("The Surname field is invalid - it can't contain numbers.\n\nPlease try again.");
		Lname.value="";
		Lname.focus();
		return false;
	}

	// Check PhoneNO
	var Phone=document.getElementById('fmFone')
	var PhoneRegex=/(^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?#(\d{4}|\d{3}))?$)/;
	if ( Phone.value == null || Phone.value == "" )
	{
		alert("The Telephone Number field is incomplete.\n\nPlease try again.");
		Phone.focus();
		return false;
	}
	
	if (check_using_regex(Phone.value,PhoneRegex)==false)
	{
		alert("The Telephone Number is invalid - must be a UK number with no international codes.\n\nPlease try again.");
		Phone.value="";
		Phone.focus();
		return false;
	}
	
	// Check Email
	var Emailaddr=document.getElementById('fmEmail')
	var EmailRegex=/(^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$)/;
	if ( Emailaddr.value==null || Emailaddr.value=="" )
	{
		alert("The Email field is incomplete.\n\nPlease try again.");
		Emailaddr.focus();
		return false;
	}
	if (check_using_regex(Emailaddr.value,EmailRegex)==false)
	{
		alert("The Email address is invalid.\n\nPlease try again.");
		Emailaddr.value="";
		Emailaddr.focus();
		return false;
	}
	if (email_final_checks(Emailaddr.value)==false)
	{
		alert("The Email address is invalid.\n\nPlease try again.");
		Emailaddr.value="";
		Emailaddr.focus();
		return false;
	}	

	// Check Check boxes - at least 1 should be selected
	if(getchboxValue(document.fmForm.fmElec) == false && 
	   getchboxValue(document.fmForm.fmGas) == false &&
	   getchboxValue(document.fmForm.fmWater) == false && 
	   getchboxValue(document.fmForm.fmMobile) == false && 
	   getchboxValue(document.fmForm.fmLandline) == false && 
	   getchboxValue(document.fmForm.fmBband) == false && 
	   getchboxValue(document.fmForm.fmInsu) == false ) 
	{
		alert("No utility was selected.\n\nPlease select at least one utility.");
		return false;
	}

	document.fmForm.validationcode.value='1';
        document.fmForm.submit();
}

function getchboxValue(chbox)
{
	if(typeof chbox != "undefined")
	{
		return chbox.checked;
	
  	} else {
		return false;
  	}
}

function check_using_regex(strtotest, regexstr) 
{
  	var re = new RegExp(regexstr);
  	if (strtotest.match(re)) 
	{
    		return true;
  	} else {
    		return false;
  	}
}

function email_final_checks(strToCheck)
{
            // Get number of chars that occur after @
            var AtPosition = strToCheck.indexOf("@");
            var Position = strToCheck.length - AtPosition;

            // Check position of @ is at least 5 chars before end of string
            if (Position < 6)
            {
                return false;
            }

            // Check for at least 1 stop after @ + 2 
            if (strToCheck.indexOf(".", AtPosition + 2) == -1)
            {
                return false;
            }

            // Check for at least two chars before @
            if (AtPosition < 2)
            {
                return false;
            }

            // email format should be OK
            return true;
}

//-->