function formContattiValidator(sLang)
{
  var sNome      = trimAll(document.getElementById("sNome").value);
  var sEMail     = trimAll(document.getElementById("sEMail").value);
  var sMessaggio = trimAll(document.getElementById("sMessaggio").value);

  if(sNome == null || sNome == "")
  {
	if(sLang == "it")
		alert("Attenzione: specificare la Ragione Sociale");
	else
		alert("Warning: please insert the Company");
    document.getElementById("sNome").focus();
    document.getElementById("sNome").select();
    return false;
  }
  
  if(sEMail == null || sEMail == "")
  {
	if(sLang == "it")
		alert("Attenzione: specificare il E-Mail");
	else
		alert("Warning: please insert the E-Mail");
    document.getElementById("sEMail").focus();
    document.getElementById("sEMail").select();
    return false;
  }
  else if(!isValidEmail(sEMail))
  {
	if(sLang == "it")
		alert("Attenzione: l'E-Mail specificata non &egrave; valida");
	else
		alert("Warning: the E-Mail specifified is not valid");
    document.getElementById("sEMail").focus();
    document.getElementById("sEMail").select();
    return false;
  }

  if(sMessaggio == null || sMessaggio == "")
  {
	if(sLang == "it")
		alert("Attenzione: specificare il Messaggio");
	else
		alert("Attenzione: please insert the Messagge");
    document.getElementById("sMessaggio").focus();
    document.getElementById("sMessaggio").select();
    return false;
  }

  return true;
}

function isValidEmail(str)
{
	var atSym = str.lastIndexOf("@");

	if (atSym < 1) { return false; } // no local-part
	if (atSym == str.length - 1) { return false; } // no domain
	if (atSym > 64) { return false; } // there may only be 64 octets in the local-part
	if (str.length - atSym > 255) { return false; } // there may only be 255 octets in the domain
	
	// Is the domain plausible?
	var lastDot = str.lastIndexOf(".");
	// Check if it is a dot-atom such as example.com
	if (lastDot > atSym + 1 && lastDot < str.length - 1) { return true; }
	//  Check if could be a domain-literal.
	if (str.charAt(atSym + 1) == '[' &&  str.charAt(str.length - 1) == ']') { return true; }

	return false;
}

function trimAll(sString)
{
  while (sString.substring(0,1) == ' ')
  {
    sString = sString.substring(1, sString.length);
  }
  while (sString.substring(sString.length-1, sString.length) == ' ')
  {
    sString = sString.substring(0,sString.length-1);
  }
  return sString;
}
