// javaScript Document

// <![CDATA[
			
function validate() {
  if(!document.getElementById) return;

  // get form variables
  var title = document.getElementById("title").value;
  var initials = document.getElementById("initials").value;
  var surname = document.getElementById("surname").value;
  var street = document.getElementById("street").value;
  var town = document.getElementById("town").value;
  var city = document.getElementById("city").value;
  var county = document.getElementById("county").value;
  var postcode = document.getElementById("postcode").value;
  var phone = document.getElementById("phone").value;
  var fax = document.getElementById("fax").value;
  var incorrect = new Array();
  var no = 0;
  var regExp = /[A-Za-z]{2,6}/;

  if(regExp.test(title)) {
   	title = title.charAt(0).toUpperCase() + title.substring(1,title.length).toLowerCase();
  } else {
   	incorrect[no] = "1";
   	no++;
   	title = "";
  }

  regExp = /[A-Za-z]{1,}/;
  if(regExp.test(initials)) {
   	initials = initials.toUpperCase();
  } else {
   	incorrect[no] = "2";
  	 no++;
  	 initials = "";
  }

  regExp = /[A-Za-z]{3,}-?[A-Za-z]?/;
  if(regExp.test(surname)) {
   	surname = surname.charAt(0).toUpperCase() + surname.substring(1,surname.length).toLowerCase();
  } else {
   	incorrect[no] = "3";
  	 no++;
   	surname = "";
  }

  if(street.length < 5) {
   	incorrect[no] = "4";
   	no++;
   	street = "";
  }

  if(town.length < 3) {
   	incorrect[no] = "5";
   	no++;
   	town = "";
  }
  if(city.length < 3) {
   	incorrect[no] = "6";
   	no++;
   	city = "";
  }
  if(county.length < 5) {
   	incorrect[no] = "7";
   	no++;
   	county = "";
  }

  postcode = postcode.toUpperCase();

  regExp = /[A-Z]{2}\d{1}\d?\s?\d{1}[A-Z]{2}/;
  if(regExp.test(postcode)) {
   	if(postcode.indexOf(" ") < 0) {
    		postcode = postcode.substring(0,postcode.length-3) + " " + postcode.substring(postcode.length-3,postcode.length);
   	}
  } else {
    incorrect[no] = "8";
    no++;
    postcode = "";
  }

  regExp = /\(?\d{5}\)?\s?\d{6}/;
  if(regExp.test(phone)) {
   	if(phone.indexOf("(") < 0) {
  	  	phone = "(" + phone.substring(0,5) + ") " + phone.substring(phone.length-6,phone.length);
    } else if(phone.indexOf(" ") < 0) {
   	 	phone = phone.substring(0,7) + " " + phone.substring(phone.length-6,phone.length);
   	}
  } else {
    incorrect[no] = "9";
    no++;
    phone = "";
  }
  if(regExp.test(fax)) {
   	if(fax.indexOf("(") < 0) {
  	  	fax = "(" + fax.substring(0,5) + ") " + fax.substring(fax.length-6,fax.length);
   	} else if(fax.indexOf(" ") < 0) {
   		fax = fax.substring(0,7) + " " + fax.substring(fax.length-6,fax.length);
  	 }
  } else {
    incorrect[no] = "10";
    no++;
    fax = "";
  }

  for(i=1;i<11;i++) {
  		document.getElementById(i).style.color="#000000";
  }

  for(j=0;j<no;j++) {
  		document.getElementById(incorrect[j]).style.color="#FF0000";
  }

  if(no > 0) {
   	document.getElementById("errors").innerHTML = "<span class=\"error\">Veuillez corriger le formulaire. Merci</span><br />";
  }

  document.getElementById("title").value = title;
  document.getElementById("initials").value = initials;
  document.getElementById("surname").value = surname;
  document.getElementById("street").value = street;
  document.getElementById("town").value = town;
  document.getElementById("city").value = city;
  document.getElementById("county").value = county;
  document.getElementById("postcode").value = postcode;
  document.getElementById("phone").value = phone;
  document.getElementById("fax").value = fax;
}

// ]]>