function toForm() {
document.form1.Title.focus();
}
function getFocus() {
  var focusHere = document.getElementById("Title");
  focusHere = focusHere.focus();
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
  getFocus();
});

var ALERT_TITLE = "FACTOR - Contact us";
var ALERT_BUTTON_TEXT = "Close";

if(document.getElementById) {
  window.alert = function(txt) {
    createCustomAlert(txt);
  }
}

function createCustomAlert(txt) {
  d = document;
  if(d.getElementById("modalContainer")) return;
  mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
  mObj.id = "modalContainer";
  mObj.style.height = document.documentElement.scrollHeight + "px";
  alertObj = mObj.appendChild(d.createElement("div"));
  alertObj.id = "alertBox";
  if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
  alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";
  h1 = alertObj.appendChild(d.createElement("h1"));
  h1.appendChild(d.createTextNode(ALERT_TITLE));
  msg = alertObj.appendChild(d.createElement("p"));
  msg.innerHTML = txt;
  btn = alertObj.appendChild(d.createElement("a"));
  btn.id = "closeBtn";
  btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
  btn.href = "#";
  btn.onclick =function() { removeCustomAlert();return false; } 
}







function validateFormOnSubmit(theForm) {
var reason = "";
	reason += validateTitle(theForm.Title);
  	reason += validateFirstName(theForm.FirstName);
	reason += validateLastname(theForm.Surname);
  	reason += validateEmail(theForm.VisitorEmail);
  	reason += validatePhone(theForm.VisitorPhone);
	reason += validatePost(theForm.VisitorPost);
	reason += validateComments(theForm.VisitorComments);
  if (reason != "") {
    alert("<strong>Please correct the following:</strong><br /><br />" + reason);
    return false;
  }
  return true;
}
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Red'; 
        error = "-&nbsp;The required field has not been filled in.<br />"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateFirstName(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Red'; 
        error = "-&nbsp;You didn't enter your first name.<br />";
    } else if ((fld.value.length < 2) || (fld.value.length > 25)) {
        fld.style.background = 'Red'; 
        error = "-&nbsp;Your first name is the wrong length.<br />";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Red'; 
        error = "-&nbsp;Your first name contains illegal characters.<br />";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateLastname(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Red'; 
        error = "-&nbsp;You didn't enter your surname.<br />";
    } else if ((fld.value.length < 2) || (fld.value.length > 25)) {
        fld.style.background = 'Red'; 
        error = "-&nbsp;Your surname is the wrong length.<br />";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Red'; 
        error = "-&nbsp;Your surname contains illegal characters.<br />";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Red';
        error = "-&nbsp;You didn't enter an email address.<br />";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Red';
        error = "-&nbsp;Please enter a valid email address.<br />";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Red';
        error = "-&nbsp;The email address contains illegal characters.<br />";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePost(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "-&nbsp;You didn't enter a postcode.<br />";
        fld.style.background = 'Red';
    } else if (isNaN(parseInt(stripped))) {
        error = "-&nbsp;Your postcode must be numeric eg. 2000.<br />";
        fld.style.background = 'Red';
    } else if ((fld.value.length < 4) || (fld.value.length > 4)) {
        error = "-&nbsp;The postcode  is the wrong length. Please insert exactly 4 digits.\n";
        fld.style.background = 'Red';
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "-&nbsp;You didn't enter a phone number.<br />";
        fld.style.background = 'Red';
    } else if (isNaN(parseInt(stripped))) {
        error = "-&nbsp;Your phone number must be numeric.<br />";
        fld.style.background = 'Red';
    } else if ((fld.value.length < 8) || (fld.value.length > 20)) {
        error = "-&nbsp;The phone number is the wrong length.<br />";
        fld.style.background = 'Red';
    }
    return error;
}

function validateTitle(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = 'Red'; 
        error = "-&nbsp;Please select your title.<br />";
    } 
	else {
        fld.style.background = 'White';
    }
    return error;
}
function validateComments(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = 'Red'; 
        error = "-&nbsp;Please detail your enquiry in the enquiry field.<br />";
    } 
	else {
        fld.style.background = 'White';
    }
    return error;
}
function removeCustomAlert() {
  document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
	document.form1.Title.focus();
}
