/* funciones*/
function esVacio(cadena){
	return ((cadena==null)||(cadena.length ==0));
}

function esBlanco(cadena){
	cadena = cadena.replace(/\s/g,"");
	cadena = (cadena =="")? true:false;
	return cadena;
}
function esNumerico(cadena){
	if(esVacio(cadena))return false;
	cadena = cadena.match(/\D/);
	cadena = (cadena == null)?true:false;	
	return cadena;
}
function esEmail(cadena){
	if(esVacio(cadena))return false;
	if(cadena.match(/\s/)!=null)return false;//Comprobamos que no hay espacios
	var cadenas = cadena.split("@");
	if(cadenas.length!=2)return false;
	if(esVacio(cadenas[0]) || esVacio(cadenas[1]))return false;
	var cadenas1 = cadenas[0].split(".")
	for(c=0;c<cadenas1.length;c++){
		if(esVacio(cadenas1[c]))return false;
		var tmp = cadenas1[c].match(/[^0-9 a-z A-Z ñ Ñ \! \# \$ \% \& \' \* \+ \- \/ \= \? \^ \_ \` \{ \| \} \~]/);
		if(tmp!=null)return false;
	}
	var cadenas1 = cadenas[1].split(".")
	if(cadenas1.length<2)return false;
	for(c=0;c<cadenas1.length;c++){
		if(esVacio(cadenas1[c]))return false;
		var tmp = cadenas1[c].match(/[^0-9 a-z A-Z ñ Ñ \- \_]/);
		if(tmp!=null)return false;
	}
	if(cadenas1[cadenas1.length-1].length>4 ||cadenas1[cadenas1.length-1].length<2)return false;
	return true;
}
function esNif(cadena){
	if(esVacio(cadena))return false;
	var digitos =['T','R','W','A','G','M','Y','F','P','D','X','B','N','J','Z','S','Q','V','H','L','C','K','E'];
	var numero = cadena.substring(0,cadena.length-1);
	var letra = cadena.substring(cadena.length-1).toUpperCase();
	if(numero.match(/\D/)!=null || letra.match(/[^A-Z]/)!=null)return false;
	var resto = numero % 23;
	if (digitos[resto] != letra)return false;
	return true;
}
function esNie(cadena){
	cadena = cadena.toUpperCase();
	if(cadena.match(/[A-Z]\d{7}[A-Z]/)==null)return false;
	var a = cadena.substring(0,1);
	var v;
	switch(a){
		case "X":v=0; break;
		case "Y":v=1; break;
		case "Z":v=2; break;
		default: return false;
	}
	cadena = cadena.replace(a,v);
	var t = esNif(cadena);
	return t;
}
function esPass(cadena){
	if(esVacio(cadena))return false;
	return true;
}
function esCif(cadena){
	if(esVacio(cadena))return false;
	cadena = cadena.replace(/\s/g,"").toUpperCase();
	if(cadena.length!=9)return false;
	var digitosC = ['Z','A','B','C','D','E','F','G','H','I','J']
	var digitos = ['A','B','C','D','E','F','G','H','K','L','M','N','P','Q','S']
	var letra = cadena.substring(0,1);
	var tf = false;
	for(i=0;i<15;i++)
		if(letra==digitos[i])tf=true;
	if(!tf)return false;
	var numero = cadena.substring(1,cadena.length-1);
	var dc = cadena.substring(cadena.length-1);
	if(numero.match(/\D/)!=null || letra.match(/[^A-Z]/)!=null)return false;
	var a=0;
	for(i=0; i<numero.length;i++){
		var n = parseInt(numero.substring(i,i+1),10);
		if(i%2!=0)a+=n;
		else{
			n = String(n*2);
			for(x=0;x<n.length;x++)a += parseInt(n.substring(x,x+1),10);
		}
	}
	var n =String(a).substring(1,2);
	n=10-parseInt(n,10);
	if(dc!= digitosC[n] && dc!=n)return false;	
	return true
}
function esFecha(dia,mes,anho){
	var meses = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
	if(!esNumerico(dia)||!esNumerico(mes)||!esNumerico(anho)||anho.length!=4)return false;
	mes = parseInt(mes,10);
	if(anho%4 == 0) meses[2] = 29;
	if(mes>12 || mes<=0 || dia>meses[mes] || dia<=0 || anho<1900)return false;
	var hoy = new Date();
	var year = hoy.getFullYear();
	var monz = hoy.getMonth();
	var dey = hoy.getDate();
	if((year-18 < anho)||(year-18==anho && monz+1<mes)||(year-18==anho && monz+1==mes && dey<dia))return false;
	return true;
}


/* Para la identificación*/
function identifica() {
		if (valida()){ 
			document.getElementById('form_clientes').action="validar_cliente.asp?valida=home";
			document.getElementById('form_clientes').submit();
		}
}

function valida() 
{
	if (esBlanco(document.getElementById('form_clientes').cod_cli.value)) 
	{
		alert('Debe introducir un código que le identifique como cliente.');
		document.getElementById('form_clientes').cod_cli.focus();
		return false;
	}
	if (esBlanco(document.getElementById('form_clientes').pass.value)) 
	{
		alert('Debe introducir su clave.');
		document.getElementById('form_clientes').pass.focus();
		return false;
	}
return true;
}



/* Para el formulario de alta de datos cliente*/
function valida_addclientes() 
{
	if (esBlanco(document.getElementById('form_alta_clientes').nombre.value))
	{
		alert('Debe indicarnos una razón social correcta.');
		document.getElementById('form_alta_clientes').nombre.focus();
		return false;
	}
	
	if (esBlanco(document.getElementById('form_alta_clientes').empresa.value)) 
	{
		alert('Debe introducir el nombre de su Óptica.');
		document.getElementById('form_alta_clientes').empresa.focus();
		return false;
	}

	if (esBlanco(document.getElementById('form_alta_clientes').email.value)) 
	{
		alert('Debe indicarnos su e-mail.');
		document.getElementById('form_alta_clientes').email.focus();
		return false;
	}

	if (!esEmail(document.getElementById('form_alta_clientes').email.value)) 
	{
		alert('Debe introducir un e-mail válido.');
		document.getElementById('form_alta_clientes').email.focus();
		return false;
	}

	if (esBlanco(document.getElementById('form_alta_clientes').cif.value))
	{
		alert('Debe indicarnos su cif/nif/nie/pass.');
		document.getElementById('form_alta_clientes').cif.focus();
		return false;
	}

	if (!esNumerico(document.getElementById('form_alta_clientes').telefono.value)) 
	{
		alert('Debe indicarnos su teléfono.');
		document.getElementById('form_alta_clientes').telefono.focus();
		return false;
	}

return true;
}

function aceptar_addclientes() {
		
		if (valida_addclientes()){ 
			if (document.getElementById('form_alta_clientes').consiente.checked)
			{
				document.getElementById('form_alta_clientes').consiente.value="S";
			}
			document.getElementById('form_alta_clientes').action="_addcliente.asp";
			document.getElementById('form_alta_clientes').submit();
		}
}

/* Para el formulario de modificacion de datos cliente*/
function valida_modclientes() 
{
	if (esBlanco(document.getElementById('mod_clientes').empresa.value)) 
	{
		alert('Debe introducir el nombre de su Óptica.');
		document.getElementById('mod_clientes').empresa.focus();
		return false;
	}

	if (esBlanco(document.getElementById('mod_clientes').nombre.value))
	{
		alert('Debe indicarnos una razón social correcta.');
		document.getElementById('mod_clientes').nombre.focus();
		return false;
	}

	if (esBlanco(document.getElementById('mod_clientes').email.value)) 
	{
		alert('Debe indicarnos su e-mail.');
		document.getElementById('mod_clientes').email.focus();
		return false;
	}

	if (!esEmail(document.getElementById('mod_clientes').email.value)) 
	{
		alert('Debe introducir un e-mail válido.');
		document.getElementById('mod_clientes').email.focus();
		return false;
	}

	if (esBlanco(document.getElementById('mod_clientes').cif.value))
	{
		alert('Debe indicarnos su cif/nif.');
		document.getElementById('mod_clientes').cif.focus();
		return false;
	}

	if (!esNumerico(document.getElementById('mod_clientes').telefono.value)) 
	{
		alert('Debe indicarnos su teléfono.');
		document.getElementById('mod_clientes').telefono.focus();
		return false;
	}

return true;
}
function aceptar_modclientes() {
		
		if (valida_modclientes()){ 
			if (document.getElementById('mod_clientes').consiente.checked)
			{
				document.getElementById('mod_clientes').consiente.value="S";
			}
			document.getElementById('mod_clientes').action="_modcliente.asp";
			document.getElementById('mod_clientes').submit();
		}
}

/* Para el formulario de clave perdida*/
function valida_clave_perdida(){
	if (esBlanco(document.getElementById('form_clave_perdida').cod_cli.value)) 
	{
		alert('Debe introducir un código que le identifique como cliente.');
		document.getElementById('form_clave_perdida').cod_cli.focus();
		return false;
	}
	if ((!esNif(document.getElementById('form_clave_perdida').cif.value)) && (!esNie(document.getElementById('form_clave_perdida').cif.value)) && (!esPass(document.getElementById('form_clave_perdida').cif.value))&& (!esCif(document.getElementById('form_clave_perdida').cif.value)))
	{
		alert('Debe indicarnos un cif/nif/nie/pass correcto.');
		document.getElementById('form_clave_perdida').cif.focus();
		return false;
	}
return true;
}
function aceptar_clave_perdida() {
		if (valida_clave_perdida()){ 
			document.getElementById('form_clave_perdida').action="_soliclave.asp";
			document.getElementById('form_clave_perdida').submit();
		}
}


/* Para el buscador*/
function valida_buscador() 
{
	if (esBlanco(document.getElementById('form_buscar').text_busca.value) && (document.getElementById('form_buscar').select_busca.value=='-1')) 
	{
		alert('Debe introducir un texto por el que buscar, o seleccionar un criterio de búsqueda.');
		document.getElementById('form_buscar').text_busca.focus();
		return false;
	}

	if (esBlanco(document.getElementById('form_buscar').text_busca.value) && (document.getElementById('form_buscar').select_busca.value=='0')) 
	{
		alert('Debe introducir un patrón de búsqueda, si selecciona en TODOS LOS PRODUCTOS.');
		document.getElementById('form_buscar').text_busca.focus();
		return false;
	}
return true;
}

function aceptar_buscador() {
		if (valida_buscador()){ 
			document.getElementById('form_buscar').action="res_busca.asp";
			document.getElementById('form_buscar').submit();
			
		}
}



