/*
\u00e1 -> á 
\u00e9 -> é 
\u00ed -> í 
\u00f3 -> ó 
\u00fa -> ú 
\u00c1 -> Á 
\u00c9 -> É 
\u00cd -> Í 
\u00d3 -> Ó 
\u00da -> Ú 
\u00f1 -> ñ 
\u00d1 -> Ñ 
*/

color_form_ko="#ddd";
color_form_ok="#fff";

var ok_nombre;
var ok_apellidos;
var ok_dni;
var ok_telefono;
var ok_email;
var ok_pass;
var ok_passpass;
var ok_direccion;
var ok_cp;
var ok_poblacion;
var ok_provincia;


function comprueba_nombre(valor)
{
	if (valor=="d41d8cd98f00b204e9800998ecf8427e" || valor=="undefined" || valor=="") {
		document.registro.nombre.style.backgroundColor=color_form_ko; 
		document.registro.nombre_text.value="   Escribe tu nombre";
		document.registro.nombre_text.style.backgroundImage="url('images/ko.png')";
		document.registro.nombre_text.style.backgroundRepeat="no-repeat";
		ok_nombre=0;
	} else {
		document.registro.nombre.style.backgroundColor=color_form_ok; 
		document.registro.nombre_text.value="";
		document.registro.nombre_text.style.backgroundImage="url('images/ok.png')";
		document.registro.nombre_text.style.backgroundRepeat="no-repeat";
		ok_nombre=1;
	}
	boton_envio();
}
function comprueba_nombre_direccion(valor)
{
	if (valor=="d41d8cd98f00b204e9800998ecf8427e" || valor=="undefined" || valor=="") {
		document.registro.nombre_direccion.style.backgroundColor=color_form_ko; 
		document.registro.nombre_direccion_text.value="   Nombre de la direcci\u00f3n";
		document.registro.nombre_direccion_text.style.backgroundImage="url('images/ko.png')";
		document.registro.nombre_direccion_text.style.backgroundRepeat="no-repeat";
		ok_nombre_direccion=0;
	} else {
		document.registro.nombre_direccion.style.backgroundColor=color_form_ok; 
		document.registro.nombre_direccion_text.value="";
		document.registro.nombre_direccion_text.style.backgroundImage="url('images/ok.png')";
		document.registro.nombre_direccion_text.style.backgroundRepeat="no-repeat";
		ok_nombre_direccion=1;
	}
	boton_envio();
}
function comprueba_apellidos(valor)
{
	if (valor=="" || valor=="undefined") {
		document.registro.apellidos.style.backgroundColor=color_form_ko; 
		document.registro.apellidos_text.value="   Escribe tus apellidos";
		document.registro.apellidos_text.style.backgroundImage="url('images/ko.png')";
		document.registro.apellidos_text.style.backgroundRepeat="no-repeat";
		ok_apellidos=0;
	} else {
		document.registro.apellidos.style.backgroundColor=color_form_ok; 
		document.registro.apellidos_text.value="";
		document.registro.apellidos_text.style.backgroundImage="url('images/ok.png')";
		document.registro.apellidos_text.style.backgroundRepeat="no-repeat";
		ok_apellidos=1;
	}
	boton_envio();
}
function comprueba_dnicif(valor)
{
	if (valor=="d41d8cd98f00b204e9800998ecf8427e" || valor=="undefined" || valor=="") {
		document.registro.dnicif.style.backgroundColor=color_form_ko; 
		document.registro.dnicif_text.value="   Escribe tu DNI/NIF/CIF";
		document.registro.dnicif_text.style.backgroundImage="url('images/ko.png')";
		document.registro.dnicif_text.style.backgroundRepeat="no-repeat";
		ok_nombre=0;
	} else {
		document.registro.dnicif.style.backgroundColor=color_form_ok; 
		document.registro.dnicif_text.value="";
		document.registro.dnicif_text.style.backgroundImage="url('images/ok.png')";
		document.registro.dnicif_text.style.backgroundRepeat="no-repeat";
		ok_nombre=1;
	}
	boton_envio();
}
function comprueba_direccion(valor)
{
	if (valor=="" || valor=="undefined") {
		document.registro.direccion.style.backgroundColor=color_form_ko; 
		document.registro.direccion_text.value="   Escribe tu direcci\u00f3n";
		document.registro.direccion_text.style.backgroundImage="url('images/ko.png')";
		document.registro.direccion_text.style.backgroundRepeat="no-repeat";
		ok_direccion=0;
	} else {
		document.registro.direccion.style.backgroundColor=color_form_ok; 
		document.registro.direccion_text.value="";
		document.registro.direccion_text.style.backgroundImage="url('images/ok.png')";
		document.registro.direccion_text.style.backgroundRepeat="no-repeat";
		ok_direccion=1;
	}
	boton_envio();
}
function comprueba_provincia(valor)
{
	if (valor=="" || valor=="undefined") {
		document.registro.provincia.style.backgroundColor=color_form_ko; 
		document.registro.provincia_text.value="   Selecciona tu provincia";
		document.registro.provincia_text.style.backgroundImage="url('images/ko.png')";
		document.registro.provincia_text.style.backgroundRepeat="no-repeat";
		ok_provincia=0;
	} else {
		document.registro.provincia.style.backgroundColor=color_form_ok; 
		document.registro.provincia_text.value="";
		document.registro.provincia_text.style.backgroundImage="url('images/ok.png')";
		document.registro.provincia_text.style.backgroundRepeat="no-repeat";
		ok_provincia=1;
	}
	boton_envio();
}

function comprueba_poblacion(valor)
{
	if (valor=="" || valor=="undefined") {
		document.registro.poblacion.style.backgroundColor=color_form_ko; 
		document.registro.poblacion_text.value="   Escribe tu poblaci\u00f3n";
		document.registro.poblacion_text.style.backgroundImage="url('images/ko.png')";
		document.registro.poblacion_text.style.backgroundRepeat="no-repeat";
		ok_poblacion=0;
	} else {
		document.registro.poblacion.style.backgroundColor=color_form_ok; 
		document.registro.poblacion_text.value="";
		document.registro.poblacion_text.style.backgroundImage="url('images/ok.png')";
		document.registro.poblacion_text.style.backgroundRepeat="no-repeat";
		ok_poblacion=1;
	}
	boton_envio();
	
}

function comprueba_dni(valor, array)
{	
	var pag = self.location.href.match( /\/([^/]+)$/ )[1];
	var pagina=pag.split("?");
	var pagina=pagina[0];
	
	if (pagina=="personal.php")
	{
		ok_dni=1;
	}
	else
	{
		var esnie=0;
		if (valor=="" || valor=="undefined") {
			document.registro.dni.style.backgroundColor=color_form_ko; 
			document.registro.dni_text.value="   Escribe tu DNI/NIE (sin guiones)";
			document.registro.dni_text.style.backgroundImage="url('images/ko.png')";
			document.registro.dni_text.style.backgroundRepeat="no-repeat";
			ok_dni=0;
		} else {
			
			var usuarios=array.split(",");
			for (usuario in usuarios)
			{
				numero1 = valor.substr(0,valor.length-1); 
				numero2 = usuarios[usuario].substr(0,usuarios[usuario].length-1); 
				
				valor2=valor.toUpperCase();
				
				if ((/^[XxTtYyZz]{1}[0-9]{7}[a-zA-Z]{1}$/.test(valor)) || (/^[XxTtYyZz]{1}[0-9]{7}[a-zA-Z]{1}$/.test(valor2))){
					var esnie=1;
				}
				
				if (numero1==numero2)
				{
					//alert (usuarios[usuario] + " - " + valor + "igual");
					document.registro.dni.style.backgroundColor=color_form_ko; 
					document.registro.dni_text.value="   DNI/NIE ya registrado";
					document.registro.dni_text.style.backgroundImage="url('images/ko.png')";
					document.registro.dni_text.style.backgroundRepeat="no-repeat";
					ok_dni=0;
					exit;
				}
				else
				{
					numero = valor.substr(0,valor.length-1); 
					let = valor.substr(valor.length-1,1); 
					numero = numero % 23; 
					letra='TRWAGMYFPDXBNJZSQVHLCKET'; 
					letra=letra.substring(numero,numero+1); 
					letra2='trwagmyfpdxbnjzsqvhlcket'; 
					letra2=letra2.substring(numero,numero+1); 
					
					if (letra!=let && letra2!=let && esnie==0){
						if (letra2!=let) document.registro.dni.style.backgroundColor=color_form_ko;
						document.registro.dni_text.value="   Escribe tu DNI/NIE (sin guiones)";
						document.registro.dni_text.style.backgroundImage="url('images/ko.png')";
						document.registro.dni_text.style.backgroundRepeat="no-repeat";
						ok_dni=0;
					} 
					else {
						document.registro.dni.style.backgroundColor=color_form_ok;
						document.registro.dni_text.value="";
						document.registro.dni_text.style.backgroundImage="url('images/ok.png')";
						document.registro.dni_text.style.backgroundRepeat="no-repeat";
						ok_dni=1;
					}
					
				}
			}
		}
	}	
	boton_envio();
}

function comprueba_email(valor)
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(valor)){
		document.registro.email.style.backgroundColor=color_form_ok;
		document.registro.email_text.value="";
		document.registro.email_text.style.backgroundImage="url('images/ok.png')";
		document.registro.email_text.style.backgroundRepeat="no-repeat";
		ok_email=1;
	} else {
		document.registro.email.style.backgroundColor=color_form_ko;
		document.registro.email_text.value="   Escribe un email v\u00e1lido";
		document.registro.email_text.style.backgroundImage="url('images/ko.png')";
		document.registro.email_text.style.backgroundRepeat="no-repeat";
		ok_email=0;
	}
	boton_envio();
}

function comprueba_cp(valor)
{
	if (/^[0-9]{5}$/.test(valor)){
		document.registro.cp.style.backgroundColor=color_form_ok;
		document.registro.cp_text.value="";
		document.registro.cp_text.style.backgroundImage="url('images/ok.png')";
		document.registro.cp_text.style.backgroundRepeat="no-repeat";
		ok_cp=1
	} else {
		document.registro.cp.style.backgroundColor=color_form_ko;
		document.registro.cp_text.value="   Escribe tu C.P.";
		document.registro.cp_text.style.backgroundImage="url('images/ko.png')";
		document.registro.cp_text.style.backgroundRepeat="no-repeat";
		ok_cp=0;
	}
	boton_envio();
}
function comprueba_telefono(valor)
{
	if (/^\d{9}$/.test(valor)){
		document.registro.telefono.style.backgroundColor=color_form_ok;
		document.registro.telefono_text.value="";
		document.registro.telefono_text.style.backgroundImage="url('images/ok.png')";
		document.registro.telefono_text.style.backgroundRepeat="no-repeat";
		ok_telefono=1;
	} else {
		document.registro.telefono.style.backgroundColor=color_form_ko;
		document.registro.telefono_text.value="   Escribe tu n\u00famero de tel\u00e9fono";
		document.registro.telefono_text.style.backgroundImage="url('images/ko.png')";
		document.registro.telefono_text.style.backgroundRepeat="no-repeat";
		ok_telefono=0;
	}
	boton_envio();
}

function comprueba_pass(valor)
{
	if (/(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$/.test(valor)){
		document.registro.pass.style.backgroundColor=color_form_ok;
		document.registro.pass_text.value="";
		document.registro.pass_text.style.backgroundImage="url('images/ok.png')";
		document.registro.pass_text.style.backgroundRepeat="no-repeat";
		ok_pass=1;
	} else {
		document.registro.pass.style.backgroundColor=color_form_ko;
		document.registro.pass_text.value="   8-10 car\u00e1cteres alfanum\u00e9ricos";
		document.registro.pass_text.style.backgroundImage="url('images/ko.png')";
		document.registro.pass_text.style.backgroundRepeat="no-repeat";
		ok_pass=0;
	}
	boton_envio();
}
function comprueba_passpass(valor)
{
	if (valor=="" || valor=="undefined" || valor!=document.registro.pass.value) {
		document.registro.passpass.style.backgroundColor=color_form_ko;
		document.registro.passpass_text.value="   Repite tu contrase\u00f1a";
		document.registro.passpass_text.style.backgroundImage="url('images/ko.png')";
		document.registro.passpass_text.style.backgroundRepeat="no-repeat";
		ok_passpass=0;
	} else {
		document.registro.passpass.style.backgroundColor=color_form_ok;
		document.registro.passpass_text.value="";
		document.registro.passpass_text.style.backgroundImage="url('images/ok.png')";
		document.registro.passpass_text.style.backgroundRepeat="no-repeat";
		ok_passpass=1;
	}
	boton_envio();
}

function comprueba_pass2(valor)
{
	if (/(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$/.test(valor)){
		document.cambiapass.pass2.style.backgroundColor=color_form_ok;
		document.cambiapass.pass2_text.value="";
		document.cambiapass.pass2_text.style.backgroundImage="url('images/ok.png')";
		document.cambiapass.pass2_text.style.backgroundRepeat="no-repeat";
		ok_pass2=1;
	} else {
		document.cambiapass.pass2.style.backgroundColor=color_form_ko;
		document.cambiapass.pass2_text.value="   8-10 car\u00e1cteres alfanum\u00e9ricos";
		document.cambiapass.pass2_text.style.backgroundImage="url('images/ko.png')";
		document.cambiapass.pass2_text.style.backgroundRepeat="no-repeat";
		ok_pass2=0;
	}
	boton_envio2();
}
function comprueba_passpass2(valor)
{
	if (valor=="" || valor=="undefined" || valor!=document.cambiapass.pass2.value) {
		document.cambiapass.passpass2.style.backgroundColor=color_form_ko;
		document.cambiapass.passpass2_text.value="   Repite tu contrase\u00f1a";
		document.cambiapass.passpass2_text.style.backgroundImage="url('images/ko.png')";
		document.cambiapass.passpass2_text.style.backgroundRepeat="no-repeat";
		ok_passpass2=0;
	} else {
		document.cambiapass.passpass2.style.backgroundColor=color_form_ok;
		document.cambiapass.passpass2_text.value="";
		document.cambiapass.passpass2_text.style.backgroundImage="url('images/ok.png')";
		document.cambiapass.passpass2_text.style.backgroundRepeat="no-repeat";
		ok_passpass2=1;
	}
	boton_envio2();
}

function comprueba_datos_actualizar(){
	comprueba_nombre(document.registro.nombre.value); 
	comprueba_apellidos(document.registro.apellidos.value); 
	comprueba_dni(document.registro.dni.value, '');
	comprueba_telefono(document.registro.telefono.value); 
	comprueba_email(document.registro.email.value);
	/*comprueba_email(document.registro.email.value);*/
	comprueba_pass(document.registro.pass.value);
	comprueba_passpass(document.registro.passpass.value);
	
	//comprueba_nombre_direccion(document.registro.nombre_direccion.value); 
	comprueba_direccion(document.registro.direccion.value);   
	comprueba_cp(document.registro.cp.value);  
	comprueba_poblacion(document.registro.poblacion.value);
	comprueba_provincia(document.registro.provincia.value);  
}

function comprueba_datos_actualizar_registro(){
	comprueba_nombre(document.registro.nombre.value);
	comprueba_apellidos(document.registro.apellidos.value); 
	comprueba_dni(document.registro.dni.value, '');
	comprueba_telefono(document.registro.telefono.value); 
	/*comprueba_email(document.registro.email.value);*/
	comprueba_pass(document.registro.pass.value);
	comprueba_passpass(document.registro.passpass.value);
	//comprueba_nombre_direccion(document.registro.nombre_direccion.value); 
	comprueba_direccion(document.registro.direccion.value);   
	comprueba_cp(document.registro.cp.value);  
	comprueba_poblacion(document.registro.poblacion.value);
	comprueba_provincia(document.registro.provincia.value);  
}
	
function vererrores(x)
{
	comprueba_datos_actualizar_registro();
	if (x=='1')
	{
	}
	else
	{
		if (ok_nombre==0) error1="Escribe tu nombre.\n"; else error1="";
		if (ok_apellidos==0) error2="Escribe tus apellidos.\n"; else error2="";
		if (ok_dni==0) error3="Escribe un dni valido.\n"; else error3="";
		if (ok_telefono==0) error4="Escribe tu telefono (9 cifras).\n"; else error4="";
		/*if (ok_email==0) error5="Escribe un email valido.\n"; else error5="";*/
		if (ok_pass==0) error6="Escribe el password (8 cifras alfanumerico).\n"; else error6="";
		if (ok_passpass==0) error7="Repite el password que has escrito correctamente.\n"; else error7="";
		if (ok_direccion==0) error8="Escribe tu direccion.\n"; else error8="";
		if (ok_cp==0) error9="Escribe tu codigo postal (5 cifras).\n"; else error9="";
		if (ok_poblacion==0) error10="Escribe tu poblacion.\n"; else error10="";
		if (ok_provincia==0) error11="Selecciona tu provincia."; else error11="";
		alert (error1+error2+error3+error4+error6+error7+error8+error9+error10+error11);//
	}
}
function vererroresActualiza(x)
{
	//comprueba_datos_actualizar_registro();
	
	if (x=='1')
	{
	 	comprueba_datos_actualizar()
	}
	else
	{
		
		if (ok_nombre==0) error1="Escribe tu nombre.\n"; else error1="";
		if (ok_apellidos==0) error2="Escribe tus apellidos.\n"; else error2="";
		if (ok_dni==0) error3="Escribe un dni valido.\n"; else error3="";
		if (ok_telefono==0) error4="Escribe tu telefono (9 cifras).\n"; else error4="";
		/*if (ok_email==0) error5="Escribe un email valido.\n"; else error5="";*/
		if (ok_pass==0) error6="Escribe el password (8 cifras alfanumerico).\n"; else error6="";
		if (ok_passpass==0) error7="Repite el password que has escrito correctamente.\n"; else error7="";
		if (ok_direccion==0) error8="Escribe tu direccion.\n"; else error8="";
		if (ok_cp==0) error9="Escribe tu codigo postal (5 cifras).\n"; else error9="";
		if (ok_poblacion==0) error10="Escribe tu poblacion.\n"; else error10="";
		if (ok_provincia==0) error11="Selecciona tu provincia."; else error11="";
		alert (error1+error2+error3+error4+error6+error7+error8+error9+error10+error11);//
	}
}

function boton_envio()
{	
	var pag = self.location.href.match( /\/([^/]+)$/ )[1];
	var pagina=pag.split("?");
	var pagina=pagina[0];
		
	if (pagina=='registro.php')
	{
		//alert ("ok_nombre"+ok_nombre+"ok_apellidos"+ok_apellidos+"ok_dni"+ok_dni+"ok_telefono"+ok_telefono);
		//alert ("ok_email"+ok_email+"ok_pass"+ok_pass+"ok_passpass"+ok_passpass);
		//alert ("ok_direccion"+ok_direccion+"ok_cp"+ok_cp+"ok_poblacion"+ok_poblacion+"ok_provincia"+ok_provincia);
		
		if (ok_nombre==1 && ok_apellidos==1 && ok_dni==1 && ok_telefono==1 && /*ok_email==1 &&*/ ok_pass==1 && ok_passpass==1 && ok_direccion==1 && ok_cp==1 && ok_poblacion==1 && ok_provincia==1 ) 
		{
			document.getElementById('errores').style.display='none';
			document.registro.submit.disabled=false; 
			//document.registro.submit.style.backgroundImage="url(images/enviar.gif)";
		}
		else {
			document.registro.submit.disabled=true; 
			//document.registro.submit.style.backgroundImage="url(images/enviarno.gif)";
		}
	}
	
	if (pagina=='personal.php')
	{
		//alert ("ok_nombre"+ok_nombre+"ok_apellidos"+ok_apellidos+"ok_dni"+ok_dni+"ok_telefono"+ok_telefono);
		//alert ("ok_email"+ok_email+"ok_pass"+ok_pass+"ok_passpass"+ok_passpass);
		//alert ("ok_direccion"+ok_direccion+"ok_cp"+ok_cp+"ok_poblacion"+ok_poblacion+"ok_provincia"+ok_provincia);
		
		if (ok_nombre==1 && ok_apellidos==1 && ok_telefono==1 &&  ok_pass==1 && ok_passpass==1 && ok_direccion==1 && ok_cp==1 && ok_poblacion==1 && ok_provincia==1 ) 
		{
			document.getElementById('errores').style.display='none';
			document.registro.submit.disabled=false; 
			//document.registro.submit.style.backgroundImage="url(images/enviar.gif)";
		}
		else {
			document.registro.submit.disabled=true; 
			//document.registro.submit.style.backgroundImage="url(images/enviarno.gif)";
		}
	}
	
	
	if (pagina=='direccionfacturacion.php')
	{	

		if (ok_nombre_direccion==1 && ok_nombre==1 && ok_apellidos==1 && ok_direccion==1 && ok_telefono==1 && ok_cp==1 && ok_poblacion==1 && ok_provincia==1)
		{
			document.registro.submit.disabled=false; 
			//document.registro.submit.style.backgroundImage="url(images/enviar.gif)";
		}
		else {
			document.registro.submit.disabled=true; 
			//document.registro.submit.style.backgroundImage="url(images/enviarno.gif)";
		}
	}
	
}

function boton_envio2()
{
	if (ok_pass2==1 && ok_passpass2==1 ) 
	{
		document.cambiapass.submit.disabled=false; 
		//document.registro.submit.style.backgroundImage="url(images/enviar.gif)";
	}
	else {
		document.cambiapass.submit.disabled=true; 
		//document.registro.submit.style.backgroundImage="url(images/enviarno.gif)";
	}
}


function loginuser(){
	if (document.login.user.value=="Usuario") document.login.user.value="";
}

function loginpass(){
	if (document.login.pass.value=="pass") document.login.pass.value="";
	document.login.pass.setAttribute("type", "password");
}

function loginuser2(){
	document.login2.user2.value="";
}

function loginpass2(){
	document.login2.pass2.value="";
	document.login2.pass2.setAttribute("type", "password");
}
