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 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 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;
	}
/*
	var Pcode=document.getElementById('fmPcode')
	if( Pcode.value == "")
	{
		alert("Postcode is invalid .\n\nPlease try again.");
		Pcode.focus();
		return false;
	}

  if (checkPostCode (Pcode.value)) {    
  } 
  else {alert ("Postcode has invalid format");Pcode.focus();return false;};




	var Supplier=document.getElementById('fmSupplier')
	if( Supplier.value == "")
	{
		alert("Please select a supplier.\n\nPlease try again.");		
		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;
	// }

    //    Removed "document.fmForm.submit();" - otherwise form will submit twice
   //     document.fmForm.submit();
return true;
}




function check_using_regex(strtotest, regexstr) 
{
  	var re = new RegExp(regexstr);
  	if (strtotest.match(re)) 
	{
    		return true;
  	} else {
    		return false;
  	}
}




function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  // 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 (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/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 the types of 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();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // 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;
}


