// JavaScript Document

function validate(form){
  var name        = document.getElementById('name').value;
  var email       = document.getElementById('email').value;
  var phone       = document.getElementById('phone').value;
  var address     = document.getElementById('address').value;
  var city        = document.getElementById('city').value;
  var services    = document.getElementById('services').value;
  var message     = document.getElementById('message').value;
  var code        = document.getElementById('security_code').value;
  
  var NameRegex = /^([ \u00c0-\u01ffa-zA-Z'\-\.])+$/; 
  var EmailRegex = /^[a-z\d]+([\.\-_][a-z\d]+)*@[a-z\d]+([\.\-_][a-z\d]+)*\.[a-z]{2,5}/i;                  
  var TelephoneRegex = /(\(\d{3}\))|\d{3}[ \.-]?\d{3}[ \.-]?\d{4}/; 
  var AddressRegex = /^[0-9]+\s+([a-zA-Z\d\.\- ]+|[a-zA-Z]+\s[a-zA-Z]+)$/;
  var CityRegex = /^([ \u00c0-\u01ffa-zA-Z'\-\.])+$/;  
  var MessageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  
  if(name == "") {
    alert("You must enter your name.");
	document.form1.name.focus();
    return false;
  }
  if(!name.match(NameRegex)) {
    alert("You have entered an invalid name.");
	document.form1.name.focus();
    return false;
  }
  if(email == "") {
    alert("You must enter your email.");
	document.form1.email.focus();
    return false;
  }
  if(!email.match(EmailRegex)) {
    alert("You have entered an invalid email");
	document.form1.email.focus();
    return false;
  }
  if(phone == "") {
    alert("You must enter your phone number.");
	document.form1.phone.focus();
    return false;
  }
  if(phone != "" && !phone.match(TelephoneRegex)) {
    alert("You have entered an invalid phone number.");
	document.form1.phone.focus();
    return false;
  }
  if(address != "" && !address.match(AddressRegex)) {
    alert("You have entered an invalid address.");
	document.form1.address.focus();
    return false;
  } 
  if(city != "" && !city.match(CityRegex)) {
    alert("You have entered an invalid city.");
	document.form1.city.focus();
    return false;
  }
  if(services == "1") {
	alert("You must choose the service you need.");
	document.form1.services.focus();
    return false;
  }
  if(message == "") {
    alert("You must enter a message.");
	document.form1.message.focus();
    return false;
  }
  if(message.match(MessageRegex)) {
    alert("You have entered an invalid message.");
	document.form1.message.focus();
    return false;
  }
  if(code == "") {
    alert("You must enter the security code.");
	document.form1.security_code.focus();
    return false;
  }
  return true;
}

function CheckTA()
{
  var max=350, texthalt;

  texthalt=document.getElementById('message').value;

  if (texthalt.length >= max)
	{
	document.getElementById('message').value=texthalt.substring(0,max);
	document.getElementById('message').value.blur();
	}
}