function MyTrim(camp,upper) {
  var valor = camp.value;
	while (''+valor.charAt(0)==' ') valor=valor.substring(1,valor.length);
  while (''+valor.charAt(valor.length)==' ') valor=valor.substring(0,valor.length-1);
	if (upper) valor=valor.toUpperCase();
  camp.value = valor;
}

function verificarText(formulari,obj) {
 var valor=eval("document.forms['"+formulari+"']."+obj);
 MyTrim(valor,false);
 if(valor.value == '') return false;
 else return true;
}
function verificarCheck(formulari,obj) {
  var valor=eval("document.forms['"+formulari+"']."+obj);
  if(valor.checked) return true;
  else return false;
}


function verificarEmail (emailStr) {
	/* Verificar si el email tiene el formato user@dominio. */
	var emailPat=/^(.+)@(.+)$/ 
	/* Verificar la existencia de caracteres. ( ) < > @ , ; : \ " . [ ] */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]" 
	/* Verifica los caracteres que son válidos en una dirección de email */
	var validChars="\[^\\s" + specialChars + "\]" 
	var quotedUser="(\"[^\"]*\")" 
	/* Verifica si la dirección de email está representada con una dirección IP Válida */ 
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* Verificar caracteres inválidos */ 
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	 //alert("Email erroni (revisa @ i .'s)")
	 return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	// Si el user "user" es valido 
	if (user.match(userPat)==null) {
	// Si no
  	//alert("no usuari no valid.")
  	return false
	}
	/* Si la dirección IP es válida */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
  	for (var i=1;i<=4;i++) {
  	if (IPArray[i]>255) {
    	//alert("IP de desti no valida")
    	return false
	}
	}
	return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	//alert("Domini no valid.")
	return false
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	domArr[domArr.length-1].length>3) { 
	//alert("La direccio no valida")
	return false
	}
	if (len<2) {
	var errStr="La dirección es erronea"
	//alert(errStr)
	return false
	}
	// La dirección de email ingresada es Válida
	return true;
}
function verificar(idioma) {
  msg = new Array();
  msg = msgIdioma(idioma);
  ruta = document.forms.formContacto;
  ruta.action = 'contact.php?mod=1';
  if (!verificarCheck('formContacto','check1') && !verificarCheck('formContacto','check2') && !verificarCheck('formContacto','check3') && !verificarCheck('formContacto','check4')) error = false;
  else error= true;
  if(error) {
    if(!verificarText('formContacto','nombre')) alert(msg[0]);
    else if (!verificarEmail(ruta.email.value)) alert(msg[1]);
    else if (!verificarText('formContacto','comentario')) alert(msg[2]);
    else ruta.submit();
  } else alert(msg[3]);

}
function notaLegal(url) {
	window.open(url,'Nota Legal','width=300,height=400,scrollbars=NO,resizable=NO,Menubar=NO,Status=NO');
}
function msgIdioma(idioma) {
  var msg = new Array();
  switch(idioma) {
    case 'ca': 
      msg[0] = 'Introduexi el vostre nom.';
      msg[1] = 'Email incorrecte.';
      msg[2] = 'Si us plau afegeix un comentari.';
      msg[3] = 'Si us plau selecciona una de les quatre opcions.';
    break;
    case 'es': 
      msg[0] = 'Introduzca su nombre.';
      msg[1] = 'Email incorrecto.';
      msg[2] = 'Por favor añada un comentario.';
      msg[3] = 'Por favor seleccione una de las cuatro opciones.';
    break;
    case 'en': 
      msg[0] = 'Please enter your name.';
      msg[1] = 'Email address not valid.';
      msg[2] = 'Please add your comments.';
      msg[3] = 'Please select one of the four options.';
    break;
  }
 return msg;
}

