function frmValidate(){
	// Check if a name has been entered
	if (!chkValidStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-1234567890&'-_ ",contactusform.Name.value, true, "Text", 0, 0)){
		alert("You have entered a Name which which has invalid characters");
		contactusform.Name.focus();
		return false;
		}
	// Check if a company name has been entered
	if (!chkValidStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-1234567890&'-_ ",contactusform.Company.value, false, "Text", 0, 0)){
		alert("You have entered a Company Name which which has invalid characters");
		contactusform.Company.focus();
		return false;
		}
	// Check if an address has been entered
	if (!chkValidStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-1234567890&'-_ ",contactusform.Address1.value, false, "Text", 0, 0)){
		alert("You have entered an address which which has invalid characters.");
		contactusform.Address1.focus();
		return false;
		}
	// Check if an address has been entered
	if (!chkValidStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-1234567890&'-_ ",contactusform.Address2.value, false, "Text", 0, 0)){
		alert("You have entered an address which which has invalid characters.");
		contactusform.Address2.focus();
		return false;
		}
	// Check if an address has been entered
	if (!chkValidStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-1234567890&'-_ ",contactusform.Address3.value, false, "Text", 0, 0)){
		alert("You have entered an address which which has invalid characters.");
		contactusform.Address3.focus();
		return false;
		}
	// Check if an address has been entered
	if (!chkValidStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-1234567890&'-_ ",contactusform.Address4.value, false, "Text", 0, 0)){
		alert("You have entered an address which which has invalid characters.");
		contactusform.Address4.focus();
		return false;
		}
	if (!postit(contactusform.Postcode)){
		alert("The post code is incorrect.");
		contactusform.Postcode.focus();
		return false;
	}
	// Check email address	
	if (contactusform.Emailaddress.value == ""){
		alert("You must enter an email address");
		contactusform.Emailaddress.focus();
		return false;
		}
	if (!isValidEmail(contactusform.Emailaddress)){
		return false;
		}
	if (contactusform.message.value == ""){
		alert("You must state what the enquiry is about");
		contactusform.message.focus();
		return false;
		}
	// Check message content
	if (!chkValidStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-'&()._, ",contactusform.message.value, false, "Text", 0, 0)){
		alert("The message must consist of only letters and numbers.");
		contactusform.message.focus();
		return false;
		}
}

function chkValidStr(AllowedChars, StrToCheck, RequiredField, CheckType, MinRange, MaxRange){
// If the field is not required and is empty then don't bother checking any further
if (StrToCheck == "" && RequiredField == false){
	return (true)
	}
// If the field is required/compulsary, check to see if anything has been entered
if (StrToCheck == "" && RequiredField == true){
	return (false)
	}
// Now check the string to see if all characters entered are allowed
for (i = 0;  i < StrToCheck.length;  i++){
	ch = StrToCheck.charAt(i)
	for (j = 0;  j < AllowedChars.length;  j++){
		if (ch == AllowedChars.charAt(j)){
			break
			}
	}
	if (j == AllowedChars.length){
		return (false)
		}
}
// Now if it is a number then check to see if it falls within the allowed range 
if (CheckType == "Number"){
	if (StrToCheck < MinRange || StrToCheck > MaxRange){
		return (false)
		}
	}
// Now if it is a date then check to see if it falls within the allowed range 
if (CheckType == "Date"){
	var CheckDate = new Date(StrToCheck.substr(6,4),Number(StrToCheck.substr(3,2))-1,StrToCheck.substr(0,2))
	if (CheckDate < MinRange || CheckDate > MaxRange){
		return (false)
		}
	}

return (true)
}

function ErrMsgDisplay(ErrMsg){
if (ErrMsg != ""){
	alert(ErrMsg)
	}
}

function postit(pcode){ //check postcode format is valid
 test = pcode.value; size = test.length
 test = test.toUpperCase(); //Change to uppercase
 while (test.slice(0,1) == " ") //Strip leading spaces
  {test = test.substr(1,size-1);size = test.length
  }
 while(test.slice(size-1,size)== " ") //Strip trailing spaces
  {test = test.substr(0,size-1);size = test.length
  }
 pcode.value = test; //write back to form field
 if (size == 0){
 return true;
 }
 if (size < 6 || size > 8){ //Code length rule
  alert(test + " is not a valid postcode - wrong length");
  pcode.focus();
  return false;
  }
 if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
   alert(test + " is not a valid postcode - cannot start with a number");
   pcode.focus();
   return false;
  }
 if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
   alert(test + " is not a valid postcode - alpha character in wrong position");
   pcode.focus();
   return false;
  }
 if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
   alert(test + " is not a valid postcode - number in wrong position");
   pcode.focus();
   return false;
  }
 if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
   alert(test + " is not a valid postcode - number in wrong position");
   pcode.focus();
   return false;
  }
 if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
   alert(test + " is not a valid postcode - no space or space in wrong position");
   pcode.focus();
   return false;
   }
 count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
 if (count1 != count2){//only one space rule
   alert(test + " is not a valid postcode - only one space allowed");
   pcode.focus();
   return false;
  }
return true;
}

function ShowRegisterDlg(_h, txt){
if (_h=='s'){
	RegPanel.style["display"] =""
	if (txt=='1'){
		document.getElementById("lblHelpTitle").innerText = "Not sure about Buildings cover?";
		document.getElementById("txtHelp").innerHTML = "<B>Buildings Cover:</B><BR>Buildings sum insured up to a maximum of "+unescape("%A3")+"500,000.";
		if (eval("typeof QuoteForm.rdodtaBuild_Acc_Damage") != "undefined"){
			document.getElementById("txtHelp").innerHTML = "<B>Buildings Cover:</B><BR>Buildings sum insured up to a maximum of "+unescape("%A3")+"500,000.<BR><B>Accidental Damage:</B><BR>Cover extended to include damage caused by violent external means e.g. putting your foot through the ceiling<BR><B>Voluntary Excess:</B><BR>Standard policy excess is " + unescape("%A3")+ "75 each and every loss (other than subsidence landslip and heave " + unescape("%A3")+ "1000). You can reduce your premium by choosing a voluntary excess. Please note that any voluntary excesses will apply in addition to the standard policy excess.";
			}
		}
	if (txt=='2'){
		document.getElementById("lblHelpTitle").innerText = "Not sure about Contents cover?";
		document.getElementById("txtHelp").innerHTML = "<B>Contents Cover:</B><BR>Contents covers household goods and personal property.";
		if (eval("typeof QuoteForm.rdodtaCont_Acc_Damage") != "undefined"){
			document.getElementById("txtHelp").innerHTML = "<B>Contents Cover:</B><BR>Contents covers household goods and all other personal property including tenants fixtures and fittings.<BR><B>Accidental Damage:</B><BR>Cover extended to include damage caused by violent external means e.g. dropping an ornament.<BR><B>Voluntary Excess:</B><BR>Standard policy excess is " + unescape("%A3")+ "75. Voluntary excesses will apply in addition to the standard excess.";
			}
		}
	if (txt=='8'){
		document.getElementById("lblHelpTitle").innerText = "Not sure of your construction date?";
		document.getElementById("txtHelp").innerHTML = "In the event that<BR>the exact year of construction is not known, please select the nearest estimated year, taking into account the following bandings:-<BR>Pre(1850),<BR>1850-1919,<BR>1920-1945,<BR>1946-1979,<BR>1980-1989,<BR>1990: onwards.";
		}
	if (txt=='3'){
		document.getElementById("lblHelpTitle").innerText = "Not sure where your security ID is?";
		document.getElementById("txtHelp").innerHTML = 'The card security<BR>code is a fraud prevention measure that consists of a unique three or four digit number printed on the signature strip on the back of your debit/credit card.'
		document.getElementById("imgSecurityCode").style["display"] = ""
		}
	}
if (_h=='h'){
	RegPanel.style["display"] ="none";
	document.getElementById("imgSecurityCode").style["display"] = "none";
	}
}

// autotabbing to next field
function autoTab(input,len, e) {
	var keyCode = e.keyCode; 
	var filter = [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
	input.value = input.value.slice(0, len);
	input.form[(getIndex(input)+1) % input.form.length].focus();
}

function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
	if(arr[index] == ele)
	found = true;
	else
	index++;
	return found;
}

	function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
	if (input.form[i] == input)index = i;
	else i++;
	return index;
	}
	return true;
}

// Convert first letter of each word to uppercase
function upperlowercase(txt){
	if (txt.value == ""){
		return true
	}
    var pattern = /(\w)([\w'-]*)/; // a letter, and then one, none or more letters 

    var a = txt.value.split(/\s+/g); // split the sentence into an array of words

    for (i = 0; i < a.length; i ++ ) {
        var parts = a[i].match(pattern); // just a temp variable to store the fragments in.
        var firstLetter = parts[1].toUpperCase();
        var restOfWord = parts[2].toLowerCase();

        a[i] = firstLetter + restOfWord; // re-assign it back to the array and move on
    }
    
    txt.value = a.join(' '); // join it back together
    return true;
}

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function isValidEmail(formField)
{
	var result = true;
	if (formField.value == "")
		return true;
		
	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;

}
