//Funcion de envio de mail, llamda al servlet.
function envioservlet(idioma){
	var url = "/jsps/bswebs/MailPayslipNomina.jsp";
	var sendto = "cayellasisaac@bancsabadell.com";
	//var sendto = "imarques@altentic.com";
	var body = "";
	var idi = idioma;
	var from = "info@bancsabadell.com";
	var subject = "";
	if(idi == "cat"){
		subject = "BSNòmina";
	}else if(idi == "cas"){
		subject = "BSNómina";
	}
	
	
	if(validateform(idioma)){
			body = construirbody(idioma);
			//alert(body);
			document.getElementById("body").value = body;
			document.getElementById("idioma").value = idi;
			document.getElementById("from").value = from;
			document.getElementById("subject").value = subject;
			document.getElementById("to").value = sendto;
			//location.href = url + "?to=" + sendto + "&from="+from +"&idioma=" + idi + "&subject="+subject +"&body=" +body;
			document.forms[0].submit();
	}
}

function construirbody(idioma){
	var texto = "";
	 texto += "Nombre: " + document.getElementById("form.nombre").value + "\n";
	 texto += "Apellido 1: " + document.getElementById("form.apellido1").value + "\n";
	 texto += "Apellido 2: " + document.getElementById("form.apellido2").value + "\n";
	 texto += "Telefono: " + document.getElementById("form.telefono").value + "\n";
	 texto += "Nif: " + document.getElementById("form.nif").value + "\n";
	 var index = document.getElementById("form.horario").selectedIndex;
	 texto += "Horario: " + document.getElementById("form.horario")[index].value + "\n";	 
	 texto += "Email: " + document.getElementById("form.email").value + "\n";	 
	 texto += "Mayor Edad: " + document.getElementById("form.mayor18").value + "\n";	
	if(idioma == "cas"){
	 	texto += "Idioma: Castellano" + "\n";	
	}else if(idioma == "cat"){
	 	texto += "Idioma: Catala" + "\n";	
	}  	 	 	 	 
	return texto;
}

//Funcion legal es simplemente el calculo del enlace de legalidad segÃºn el idioma.
function legal(idioma){
	if (idioma == "cas") {
		var winUrl = "https://www.sabadellatlantico.com/cs/Satellite?c=GBS_Texto&cid=1183017542638&language=1178258082822&pagename=SabAtl/GBS_Texto/GBS_popupCondiciones&title=Consulte+las+condiciones+legales+de+esta+solicitud";
	}
	if (idioma == "cat") {
		var winUrl = "https://www.sabadellatlantico.com/cs/Satellite?c=GBS_Texto&cid=1191335758056&language=1183015603985&pagename=SabAtl/GBS_Texto/GBS_popupCondiciones&title=Consulti+les+condicions+legals";
	}
     	var mmConfig="toolbar=no,location=no,directories=0,status=no,scrollbars=no,resizable=no,width=549, height=358,left=20,top=50";
     	window.open(winUrl,'legal',mmConfig);	
}


//Valida que los campos esten completos y que el formato del dni sea correcto.
function validateform(idioma){

	camposvalidar = new Array ("form.nombre", "form.apellido1", "form.telefono", "form.nif", "form.email");
	camposnoms 	= new Array ("Nom", "Primer cognom", "Telèfon", "NIF", "Correu electrònic");
	camposnombres 	= new Array ("Nombre", "Primer apellido", "Teléfono", "NIF", "Correo electrónico");

	var error;
	var alertMsg="";
	
	for (i = 0; i < camposvalidar.length; i++) {
		var campo = document.getElementById(camposvalidar[i]);
		if (isEmpty(campo) == true) {
			
			if(idioma == "cas"){
				//alert("El dni es incorrecto: 99999999L");
				
				if(alertMsg!=""){alertMsg+="\n";}
				alertMsg +="El campo "+camposnombres[i]+" es obligatorio.";
			}else if(idioma == "cat"){
				//alert("El dni es erroni: 99999999L");
				if(alertMsg!=""){alertMsg+="\n";}
				alertMsg +="El camp "+camposnoms[i]+" és obligatori.";
			}
			
			campo.style.backgroundColor = "#f9d1d1";
			campo.focus();
			//return false;
			error = true;
						
			
		}
		else {
			campo.style.backgroundColor = "#ffffff";
		}
		
	}
	if (error == true){
	//	return false;
	}
	
	var dni = document.getElementById("form.nif");
	if(dni_correcto(dni.value) == true){
		dni.style.backgroundColor = "#ffffff";
	}else{
		dni.style.backgroundColor = "#f9d1d1";
		dni.focus();
		if(idioma == "cas"){
			//alert("El dni es incorrecto: 99999999L");
			if(alertMsg!=""){alertMsg+="\n";}
			alertMsg +="El NIF es incorrecto: 99999999L";
		}else if(idioma == "cat"){
			//alert("El dni es erroni: 99999999L");
			if(alertMsg!=""){alertMsg+="\n";}
			alertMsg +="El NIF es erroni: 99999999L";
		}
		//return false;
		
	}
	var telefono = document.getElementById("form.telefono");
	if(telefono.value.length < 9){
		telefono.style.backgroundColor = "#f9d1d1";
		telefono.focus();
		if(alertMsg!=""){alertMsg+="\n";}
			alertMsg +="El teléfono es incorrecto. ";
		//alert("es mas peque que 9");
		// return false;
	}else if (allDigits(telefono.value)){
		//alert("solo hay digitos");
		telefono.style.backgroundColor = "#ffffff";
		
	}else{
		telefono.style.backgroundColor = "#f9d1d1";
		telefono.focus();
		if(alertMsg!=""){alertMsg+="\n";}
			alertMsg +="El teléfono es incorrecto. ";
		
		//alert("hay mas cosas que numeros.");
		//return false;
	}

	
	if(document.getElementById("form.horario").selectedIndex  == 0){
		if(idioma == "cas"){
			//alert("Seleccione un horario.");
			if(alertMsg!=""){alertMsg+="\n";}
			alertMsg +="Seleccione un horario.";
		}else if(idioma=="cat"){
			//alert("Seleccioni un horari.");
			if(alertMsg!=""){alertMsg+="\n";}
			alertMsg +="Seleccioni un horari.";
		}
		//return false;
	}else{
		document.getElementById("form.horario").backgroundColor = "#ffffff";
	}
	if(document.getElementById("form.mayor18").checked != true){
		if(idioma == "cas"){
			//alert("Declaro que soy mayor de 18 a\u00f1os: * es obligatorio.");
			if(alertMsg!=""){alertMsg+="\n";}
			alertMsg +="Declaro que soy mayor de 18 a\u00f1os: * es obligatorio.";
		}else if(idioma=="cat"){
			//alert("Declaro que s\u00f3c major de 18 anys: * es obligatori.");
			if(alertMsg!=""){alertMsg+="\n";}
			alertMsg +="Declaro que s\u00f3c major de 18 anys: * es obligatori.";
		}
		//return false;
	}
	
	if(alertMsg!=""){
		alert(alertMsg);
		return false;
	}
	
	if (error == true){
		return false;
	}
	
	return true;
	
}
function allDigits(str){
		return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset){
		var result = true;
		for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	return result;
}

//Valida si el campo esta vacio o no.
function isEmpty(campo){
	if(campo.value == ""){
		return true;
	}else{
		return false;
	}
}


//Valida el formato del dni.
function dni_correcto(dni) {
//Valida DNI: 00000000L o NIE: L00000000L
var er_dni = /^[A-z]?\d{8}[A-z]$/;
 
if (!er_dni.test(dni)) {
	return false;
	}
	return true;
}
