// JavaScript Document
function compruebaTarjeta() {
	valor = document.formu.tarjeta.value;
	longitud = valor.length;
	clave = document.formu.clave.value;
	cadena = 'Han ocurrido los siguientes errores:\n';
	error=0;
	if (!esNumero(valor)) {
		error=1;
		cadena += 'Debe escribir un formato numérico para la tarjeta\n';
	}
	if (!noVacio(clave)) {
		error=1;
		cadena += 'Debe indicar una clave';
	}
	dc = obtenerDigitoControl(valor);
	// comprobar digito control
	/*if (dc!=parseInt(valor.charAt(15))) {
		error=1;
		cadena += 'El dígito de control no coincide';
	}*/
	
	if (error>0) {
		//alert(cadena);
		window.location.href = 'errores.asp?err=tarj_err'
	}
	else {
		document.formu.action = 'comprobarAcceso.asp?id='+valor;
		document.formu.submit();
	}
}
function compruebaTarjetaReg() {
	valor = document.reg.tarjeta.value;
	longitud = valor.length;
	error=0;
	if (!esNumero(valor)) {
		error=1;
	}
	dc = obtenerDigitoControl(valor);
	// comprobar digito control
	/*if (dc!=parseInt(valor.charAt(15))) {
		error=2;
		//cadena += 'El dígito de control no coincide';
	}*/
	
	if (error==1) {
		//alert(cadena);
		window.location.href = 'errores.asp?err=tarj_err'
	}
	else if (error==2) {
		window.location.href = 'errores.asp?err=dc'
	}
	else {
		document.reg.action = 'registro2.asp?id='+valor;
		document.reg.submit();
	}
}


function calculaSuma(cadena) {
	total = 0;
	max_index=14;
	while (max_index>=0) {
		digito = Number(cadena.charAt(max_index));
		if ((max_index%2)==0) {
			digito = digito*2;
			if (digito>9) {
				digito = calculaSumaDigito(String(digito));
			}
		}
		total = total + digito;
		max_index--;
	}
	return total;
}
function calculaSumaDigito(cadena) {
	return Number(cadena.charAt(1))+Number(cadena.charAt(0));
}
function obtenerDigitoControl(cadena) {
	suma = calculaSuma(cadena);
	if (suma==0) {
		control = 0;
	}
	else {
		if (suma.toString().length<3) {
			decena = Number(suma.toString().charAt(0))+1;
			decena = decena * 10;
		}
		else {
			decena = Number(suma.toString().charAt(0))+1;
			decena = 100 + (decena*10);
		}
		control = decena - suma;
	}
	return control;
}
function esNumero(strNumero){
	if (strNumero.search(/^[0-9]+$/) != -1)
		return true;
	else
		return false;
}
function noVacio(strVacio){
	if (strVacio!="" && strVacio!=" ")
		return true;
	else
		return false;
}
function compruebaDNI(dni1,dni2) {
	dni1 = dni1.toUpperCase();
	dni2 = dni2.toUpperCase();
	lon1 = dni1.length;
	lon2 = dni2.length;
	letra1 = dni1.charAt(lon1-1);
	letra2 = dni2.charAt(lon2-1);
	num1 = dni1.substr(0, dni1.length-1);
	num2 = dni2.substr(0, dni2.length-1);
	if ((letra1==letra2) && (num1==num2)) {
		return true;
	}
	else {
		return false;
	}
}
function compruebaTamanyo(cadena,inferior,superior) {
	return ((cadena.length>=inferior) && (cadena.length<=superior));
}
function esEmail(strEmail) {
	if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else
		return false;
}
function compruebaR2(dni) {
	error=0;
	if (!compruebaDNI(document.reg2.dni.value,dni)) {
		error=1;
	}
	if (!compruebaTamanyo(document.reg2.pass.value,6,15)) {
		error=2;
	}
	if (!esEmail(document.reg2.email.value)) {
		error=3;
	}
	if (document.reg2.pass.value!=document.reg2.pass2.value) {
		error=4;
	}
	
	switch (error) {
		case 1: {
			//window.location.href = 'errores.asp?err=no_dni&o=r2&dni='+document.reg2.dni.value+'&email='+document.reg2.email.value;
			window.location.href = 'errores.asp?err=no_dni&o=r2&dni='+document.reg2.dni.value+'&email='+document.reg2.email.value+'&estado='+document.reg2.estado.value;
			break;
		}
		case 2: {
			//window.location.href = 'errores.asp?err=no_clave&o=r2&dni='+document.reg2.dni.value+'&email='+document.reg2.email.value;
			window.location.href = 'errores.asp?err=no_clave&o=r2&dni='+document.reg2.dni.value+'&email='+document.reg2.email.value+'&estado='+document.reg2.estado.value;
			break;
		}
		case 3: {
			//window.location.href = 'errores.asp?err=no_email&o=r2&dni='+document.reg2.dni.value+'&email='+document.reg2.email.value;
			window.location.href = 'errores.asp?err=no_email&o=r2&dni='+document.reg2.dni.value+'&email='+document.reg2.email.value+'&estado='+document.reg2.estado.value;
			break;
		}
		case 4: {
			//window.location.href = 'errores.asp?err=pass_no_igual&o=r2&dni='+document.reg2.dni.value+'&email='+document.reg2.email.value;
			window.location.href = 'errores.asp?err=pass_no_igual&o=r2&dni='+document.reg2.dni.value+'&email='+document.reg2.email.value+'&estado='+document.reg2.estado.value;
			break;
		}
		default: {
			document.reg2.action='procesa_alta.asp';
			document.reg2.submit();
			break;
		}
	}
}
function comparaFechas(fec1,fec2) {
	fec1aux = fec1.split('/');
	fec2aux = fec2.split('/');
	
	fec1 = fec1aux[2]+fec1aux[1]+fec1aux[0];
	fec2 = fec2aux[2]+fec2aux[1]+fec2aux[0];
	
	if (fec1<fec2) return 1;
	if (fec1>fec2) return 2;
	if (fec1==fec2) return 0;
}
function compruebaR3(fecha) {
	error=0;	
	if (comparaFechas(document.reg3.fnac.value,fecha)!=0) {
		error=1;
	}
	if (!compruebaTamanyo(document.reg3.pass.value,6,15)) {
		error=2;
	}
	
	if (!esEmail(document.reg3.email.value)) {
		error=3;
	}
	
	if (document.reg3.pass.value!=document.reg3.pass2.value) {
		error=4;
	}


	switch (error) {
		case 1: {
			//window.location.href = 'errores.asp?err=no_fechas&o=r3&fnac='+document.reg3.fnac.value+'&email='+document.reg3.email.value;
			window.location.href = 'errores.asp?err=no_fechas&o=r3&fnac='+document.reg3.fnac.value+'&email='+document.reg3.email.value+'&estado='+document.reg3.estado.value;
			break;
		}
		case 2: {
			//window.location.href = 'errores.asp?err=no_clave&o=r3&fnac='+document.reg3.fnac.value+'&email='+document.reg3.email.value;
			window.location.href = 'errores.asp?err=no_clave&o=r3&fnac='+document.reg3.fnac.value+'&email='+document.reg3.email.value+'&estado='+document.reg3.estado.value;
			break;
		}
		case 3: {
			//window.location.href = 'errores.asp?err=no_email&o=r3&fnac='+document.reg3.fnac.value+'&email='+document.reg3.email.value;
			window.location.href = 'errores.asp?err=no_email&o=r3&fnac='+document.reg3.fnac.value+'&email='+document.reg3.email.value+'&estado='+document.reg3.estado.value;
			break;
		}
		case 4: {
			//window.location.href = 'errores.asp?err=pass_no_igual&o=r3&fnac='+document.reg3.fnac.value+'&email='+document.reg3.email.value;
			window.location.href = 'errores.asp?err=pass_no_igual&o=r3&fnac='+document.reg3.fnac.value+'&email='+document.reg3.email.value+'&estado='+document.reg3.estado.value;
			break;
		}
		default: {
			document.reg3.action='procesa_alta.asp';
			document.reg3.submit();
			break;
		}
	}
}
function esNumero(strNumero){
	if (strNumero.search(/^[0-9]+$/) != -1)
		return true;
	else
		return false;
}
function compruebaR4(num2){
	num1 = document.reg4.telefono2.value;
	error=0;
	
	if ((esNumero(num1) && esNumero(num2))) {
		if (num1!=num2) {
			error=1;
		}
	}
	else {
		error=2;
	}
	if (!compruebaTamanyo(document.reg4.pass.value,6,15)) {
		error=3;
	}
	if (!esEmail(document.reg4.email.value)) {
		error=4;
	}
	if (document.reg4.pass.value!=document.reg4.pass2.value) {
		error=5;
	}

	
	switch (error) {
		case 1: {
			//window.location.href = 'errores.asp?err=no_movil&o=r4&movil='+document.reg4.telefono2.value+'&email='+document.reg4.email.value;
			window.location.href = 'errores.asp?err=no_movil&o=r4&movil='+document.reg4.telefono2.value+'&email='+document.reg4.email.value+'&estado='+document.reg4.estado.value;
			break;
		}
		case 2: {
			//window.location.href = 'errores.asp?err=no_clave&o=r4&movil='+document.reg4.telefono2.value+'&email='+document.reg4.email.value;
			window.location.href = 'errores.asp?err=no_clave&o=r4&movil='+document.reg4.telefono2.value+'&email='+document.reg4.email.value+'&estado='+document.reg4.estado.value;
			break;
		}
		case 3: {
			//window.location.href = 'errores.asp?err=mal_movil&o=r4&movil='+document.reg4.telefono2.value+'&email='+document.reg4.email.value;
			window.location.href = 'errores.asp?err=mal_movil&o=r4&movil='+document.reg4.telefono2.value+'&email='+document.reg4.email.value+'&estado='+document.reg4.estado.value;
			break;
		}
		case 4: {
			//window.location.href = 'errores.asp?err=no_email&o=r4&movil='+document.reg4.telefono2.value+'&email='+document.reg4.email.value;
			window.location.href = 'errores.asp?err=no_email&o=r4&movil='+document.reg4.telefono2.value+'&email='+document.reg4.email.value+'&estado='+document.reg4.estado.value;
			break;
		}
		case 5: {
			//window.location.href = 'errores.asp?err=pass_no_igual&o=r4&movil='+document.reg4.telefono2.value+'&email='+document.reg4.email.value;
			window.location.href = 'errores.asp?err=pass_no_igual&o=r4&movil='+document.reg4.telefono2.value+'&email='+document.reg4.email.value+'&estado='+document.reg4.estado.value;
			break;
		}
		default: {
			document.reg4.action='procesa_alta.asp';
			document.reg4.submit();
			break;
		}
	}
}

function ponerClase(capa,nponer) {
	elemento = document.getElementById(capa)
	var newClassName = elemento.className;
	newClassName = " ";
	newClassName += nponer;
	elemento.className = newClassName;
}

function mostrar(capa) {
	document.getElementById(capa).style.display='block';
}
function ocultar(capa) {
	document.getElementById(capa).style.display='none';
}
function noVacio(strVacio){//Comprueba que un campo no esté vacio
	if (strVacio!="" && strVacio!=" ")
		return true;
	else
		return false;
}
function noCorto(strCadena,maximo,minimo){//Comprueba la longitud de un campo para ver si está entre max y el minimo
	if (strCadena.length>=minimo && strCadena.length<=maximo)
		return true;
	else
		return false;
}

function comprueba_form_datos() {
	
	fm=document.forms.formu;
	errsec= new Array();
	errmsg = new Array();

	//nombre	
	i=0;
	errsec[i]=1;
	errmsg[i]="nombre";
	if (noVacio(fm.nombre.value)) errsec[i]=0;
	//apellido1
	i++;
	errsec[i]=1;
	errmsg[i]="apellido 1";
	if (noVacio(fm.apellido1.value)) errsec[i]=0;
	//apellido2
	i++;
	errsec[i]=1;
	errmsg[i]="apellido 2";
	if (noVacio(fm.apellido2.value)) errsec[i]=0;
	//DNI
	i++;
	errsec[i]=1;
	errmsg[i]="DNI / Tarjeta Residencia";
	if (noVacio(fm.nif.value)) errsec[i]=0;
	//Pass
	i++;
	errsec[i]=1;
	errmsg[i]="contraseña";
	if (noVacio(fm.pass.value)) errsec[i]=0;
	//Email
	i++;
	errsec[i]=1;
	errmsg[i]="email";
	if (esEmail(fm.email.value)) errsec[i]=0;
	//Pais
	i++;
	errsec[i]=1;
	errmsg[i]="país";
	if (noVacio(fm.pais.value)) errsec[i]=0;
	

	//Busqueda de errores	
	var cadena_errores="El formato ";
	var num_errores=0;
	for (k=0 ;k < errsec.length ; k++){
		if (errsec[k]==1){
			cadena_errores+= errmsg[k]+ ', ';
			num_errores++;
		}	
	}
	cadena_errores = cadena_errores.substring (0,cadena_errores.length-2);
	cadena_errores+=" introducido no es válido."
	if (num_errores!=0) alert (cadena_errores);
	else {
			fm.action='modifica.asp?origen='+fm.origen.value;		
			fm.submit();		
	}

	/*error = 0;
	
	if (document.formu.nombre.value=='') {
		error=1;
	}
	if (document.formu.apellido1.value=='') {
		error=2;
	}
	if (document.formu.apellido2.value=='') {
		error=3;
	}
	if (document.formu.dni.value=='') {
		error=4;
	}
	if (document.formu.pass.value=='') {
		error=5;
	}
	if ((document.formu.email.value=='') || !esEmail(document.formu.email.value)) {
		error=6;
	}
	if (document.formu.pais.value=='') {
		error=7;
	}

	switch (error) {
		case 1: {
			window.location='errores.asp?err=obligatorios&o=datos_m';
			break;
		}
		case 2: {
			window.location='errores.asp?err=obligatorios&o=datos_m';
			break;
		}
		case 3: {
			window.location='errores.asp?err=obligatorios&o=datos_m';
			break;
		}
		case 4: {
			window.location='errores.asp?err=obligatorios&o=datos_m';
			break;
		}
		case 5: {
			window.location='errores.asp?err=pass&o=datos_m';
			break;
		}
		case 6: {
			window.location='errores.asp?err=no_email&o=datos_m';
			break;
		}
		case 7: {
			window.location='errores.asp?err=obligatorios&o=datos_m';
			break;
		}
		default: {
			document.formu.action = 'modifica.asp?origen='+document.formu.origen.value;
			document.formu.submit();
		}
	}*/
}

function compruebaMail() {
	cadena = 'Han ocurrido los siguientes errores:\n';
	error=0;
	
	if ((document.olv.email.value=='') || (!esEmail(document.olv.email.value))) {
			error=1;
			cadena += 'El email no es valido';
	}
	if (error==1) {
		alert(cadena);
	}
	else {
		document.olv.submit();
	}
}

function comprueba_contacto() {
	cadena = "";
	error=0;
	if ((document.formu.email.value=='') || (!esEmail(document.formu.email.value))) {
		error=1;
		cadena += "Debe indicar su email o escribir una dirección correcta\n";
    }
	if (document.formu.nombre.value=='') {
		error=1;
		cadena += "Debe indicar su nombre\n";
	}
	if (document.formu.apellidos.value=='') {
		error=1;
		cadena += "Debe indicar sus apellidos\n";
	}
	if (document.formu.tema.value=='') {
		error=1;
		cadena += "Debe indicar un tema\n";
	}
	if (document.formu.mensaje.value=='') {
		error=1;
		cadena += "Debe escribir un mensaje\n";
	}
	if (document.formu.bases.checked==false) {
		error=1;
		cadena += "Debe aceptar las condiciones legales para continuar\n";
	}
	if (error==1) {
		alert(cadena);
	}
	else {
		document.formu.action = 'envia_contacto.asp';
		document.formu.submit();
	}
}

function ventanaSecundaria(url,ancho,alto,nombre){
	scr=1;
	if (nombre=='undefined') {
		nombre='SPF';
	}
	var x=(screen.width-ancho)/2;
	var y=(screen.height-alto)/2; 
	if (scr=="") scr=0;
	else scr=scr;  
	var caracteristicas = "width="+ancho+",height="+alto+",menubar=no,toolbar=no,resizable=no,scrollbars="+scr+",left=" + x + ",top=" + y + ",alwaysRaised=true,status=no";
	poplimon=window.open(url,nombre, caracteristicas);
}

