function MyTrim(camp) {
  var valor = camp;
	while (''+valor.charAt(0)==' ') valor=valor.substring(1,valor.length);
  while (''+valor.charAt(valor.length)==' ') valor=valor.substring(0,valor.length-1);
	return valor;
}

function validarContacto(idioma){
	var error = false;
	var msgError = '';
	
	if (MyTrim(document.getElementById('ComentarioNombre').value) == '') {
		if(idioma == 'en') msgError += "Name incorrect. \n\r";
		else msgError += "Nombre incorrecto. \n\r";
  	error = 1;
  }
	
	if (MyTrim(document.getElementById('ComentarioEmail').value) == '') {
		if(idioma == 'en') msgError += "E-mail incorrect. \n\r";
		else msgError += "E-mail incorrecto. \n\r";
  	error = 1;
  }else{
		var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
		if (!filter.test(document.getElementById('ComentarioEmail').value)){
			if(idioma == 'en') msgError += "E-mail incorrect. \n\r";
			else msgError += "E-mail incorrecto. \n\r";
			error = 1;
		}
	}

	if (error) alert(msgError);
	return error;
}

function validar(idioma) {
	var msgError='';
	var error=0;
	
	var emails = document.getElementById('MailEmailDestino').value;
	var nuevosEmails = emails.split(";");
	
	//COMPROVEM QUE NO PASSIN DE 3 MAILS
	if (nuevosEmails.length>3) {
		if(idioma == 'en') msgError += "The maximum e-mail addresses are 3. \n\r";
		else msgError += "El máximo de direcciones de e-mail són 3. \n\r";
		error = 1;
	}
		
	if (MyTrim(document.getElementById('MailNombreOrigen').value) == '') {
		if(idioma == 'en') msgError += "You have not entered your name. \n\r";
		else msgError += "No has introducido tu nombre. \n\r";
  	error = 1;
  }
	
	
	if (MyTrim(document.getElementById('MailEmailOrigen').value) == '') {
		if(idioma == 'en') msgError += "You have not entered your e-mail. \n\r";
		else msgError += "No has introducido tu e-mail. \n\r";
  	error = 1;
  }else{
		var filter=/^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
		if (!filter.test(document.getElementById('MailEmailOrigen').value)){
			if(idioma == 'en') msgError += "Your e-mail is incorrect. \n\r";
			else msgError += "Tu e-mail es incorrecto. \n\r";
			error = 1;
		}
	}
	var i=0;
	var errorEmailDestino=0;
	for(i;i<nuevosEmails.length;i++) {
		if (MyTrim(nuevosEmails[i]) == '') {
			errorEmailDestino=1;
	  	error = 1;
	  }else{
			var filter=/^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
			if (!filter.test(nuevosEmails[i])){
				errorEmailDestino=1;
				error = 1;
			}
		}
	}
	if(errorEmailDestino) {
		if(idioma == 'en') msgError += "You did not enter an e-mail correct destination. \n\r";
			else msgError += "No has introducido un e-mail de destino correcto. \n\r";
	}
	if (error) alert(msgError);
	return error;
}

function proyectos(fase,idioma) {
	// declaro array amb les 3 fases dels projectes
	var elements = new Array();
	elements[0] = 1;
	elements[1] = 2;
	elements[2] = 3;
	
	// recorro fases, per aplicar estils i condicions
	for(var i = 0; i<3;i++) {
		var aux = i+1;
		// si la fase coincideix amb el valor auxiliar (contador + 1 per evitar el desfase de numeracio(0,1,2 i 1,2,3)) mostrem info i posem color especial
		if(aux == fase) {
			document.getElementById('info'+aux).style.display = 'block';
			// document.getElementById('link'+aux).style.color = 'red';
			document.getElementById('img'+aux).src = '/img/img_cases/'+aux+'_on_'+idioma+'.png';
		// si no coincideixen amaguem info i apliquem color normal
		} else {
			document.getElementById('info'+aux).style.display = 'none';
			document.getElementById('img'+aux).src = '/img/img_cases/'+aux+'_off_'+idioma+'.png';
		}
	}
}

function validarInforme() {
	var msgError='';
	var error=0;
	
	if (MyTrim(document.getElementById('mail').value) == '') {
		msgError += "No has introducido tu e-mail. \n\r";
  	error = 1;
  }else{
		var filter=/^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
		if (!filter.test(document.getElementById('mail').value)){
			msgError += "Tu e-mail es incorrecto. \n\r";
			error = 1;
		}
	}
	
	if(document.getElementById('privacidad').checked == 0) {
		msgError += "No has aceptado la política de privacidad. \n\r";
  	error = 1;
	}
	if(msgError){
		alert(msgError);
	}
	return error;
}