function clicadob() {
if (document.getElementById("escli").checked == true) {
document.getElementById("enlaza").disabled=false;
} else {
document.getElementById("enlaza").disabled=true;
}
}

function morganiza(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function emailCheck (emailStr) {
/* Verificar si el email tiene el formato user@dominio. */
var emailPat=/^(.+)@(.+)$/; 

/* Verificar la existencia de caracteres. ( ) < > @ , ; : \ " . [ ] */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"; 

/* Verifica los caracteres que son válidos en una dirección de email */
var validChars="\[^\\s" + specialChars + "\]"; 

var quotedUser="(\"[^\"]*\")"; 

/* Verifica si la dirección de email está representada con una dirección IP Válida */ 


var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;


/* Verificar caracteres inválidos */ 

var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
/* domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");


var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
/*alert("Email address seems incorrect (check @ and .'s)");*/
return false
}
var user=matchArray[1]
var domain=matchArray[2]

// Si el user "user" es valido 
if (user.match(userPat)==null) {
// Si no
/*alert("El nombre de usuario no es válido.");*/
return false
}

/* Si la dirección IP es válida */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
/*alert("IP de destino inválida");*/
return false
}
}
return true
}

var domainArray=domain.match(domainPat)
if (domainArray==null) {
/*alert("El dominio parece no ser válido.");*/
return false
}

var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) { 

/*alert("La dicrección debe tener 3 letras si es .'com' o 2 si en de algún pais.");*/
return false
}

if (len<2) {
var errStr="La dirección es erronea"
/*alert(errStr);*/
return false
}

// La dirección de email ingresada es Válida
return true;
}
// End -->

function buscador(){
if (document.getElementById("kbsq").value == "")
{ alert("Debe introducir al menos un criterio de busqueda"); document.getElementById("kbsq").focus(); return false; }

document.getElementById("fbsq").submit();
}

function logea(){
/*var mail=emailCheck(document.getElementById("usu").value);
if (mail == false)
{ alert("Debe introducir un e-mail correcto de usuario"); document.getElementById("usu").focus(); return false; }
*/
if (document.getElementById("usu").value == "")
{ alert("Debe introducir su CIF o NIF"); document.getElementById("usu").focus(); return false; }
if (document.getElementById("pass").value == "")
{ alert("Debe introducir su contrase\u00F1a"); document.getElementById("pass").focus(); return false; }
document.getElementById("facceso").submit();
}

function logeab(){
var mail=emailCheck(document.getElementById("usur").value);
if (mail == false)
{ alert("Debe introducir un e-mail correcto de usuario"); document.getElementById("usur").focus(); return false; }
confirm('Recibira un e-mail con una contrase\u00F1a nueva que podra modificar una vez se conecte');
document.getElementById("faccesor").submit();
}

function Validar(){

if (document.getElementById("Nombre").value == "")
{ alert("Debe proporcionarnos su Nombre"); document.getElementById("Nombre").focus(); return false; }

var mail=emailCheck(document.getElementById("email").value);
if (mail == false)
{ alert("Debe introducir un e-mail correcto"); document.getElementById("email").focus(); return false; }

if (document.getElementById("Privacidad").checked == false)
{ alert("Debe aceptar la politica de privacidad"); document.getElementById("Privacidad").focus(); return false; }

document.getElementById("formcontacto").submit();
}



function RegValidar(){

if (document.getElementById("Nombre").value == "")
{ alert("Debe proporcionarnos su Nombre"); document.getElementById("Nombre").focus(); return false; }

if (document.getElementById("Apellidos").value == "")
{ alert("Debe proporcionarnos sus Apellidos"); document.getElementById("Apellidos").focus(); return false; }

var mail=emailCheck(document.getElementById("email").value);
if (mail == false)
{ alert("Debe introducir un e-mail correcto"); document.getElementById("email").focus(); return false; }

if (document.getElementById("Nif").value == "")
{ alert("Debe proporcionarnos su CIF/NIF"); document.getElementById("Nif").focus(); return false; }

if (document.getElementById("TEmpresa").value == "0")
{ alert("Debe indicar la fiscalidad que le corresponde"); document.getElementById("TEmpresa").focus(); return false; }

if (document.getElementById("Direccion").value == "")
{ alert("Debe proporcionarnos su direccion"); document.getElementById("Direccion").focus(); return false; }

if (document.getElementById("CP").value == "")
{ alert("Debe proporcionarnos su codigo postal"); document.getElementById("CP").focus(); return false; }

if (document.getElementById("Poblacion").value == "")
{ alert("Debe proporcionarnos su poblacion"); document.getElementById("Poblacion").focus(); return false; }

if (document.getElementById("provincia").value == "")
{ alert("Debe proporcionarnos su provincia"); document.getElementById("provincia").focus(); return false; }

if (document.getElementById("Telefono").value == "")
{ alert("Debe proporcionarnos un telefono de contacto"); document.getElementById("Telefono").focus(); return false; }

if (document.getElementById("Contrasena").value == "")
{ alert("Debe especificar una contrase\u00F1a"); document.getElementById("Contrasena").focus(); return false; }

if (document.getElementById("Contrasenab").value == "")
{ alert("Debe escribir de nuevo la contrase\u00F1a"); document.getElementById("Contrasenab").focus(); return false; }

if (document.getElementById("Contrasena").value == document.getElementById("Contrasenab").value)
{} else
{ alert("Debe escribir la misma contrase\u00F1a"); document.getElementById("Contrasenab").focus(); return false; }

if (document.getElementById("enc1").checked == false && document.getElementById("enc2").checked == false && document.getElementById("enc3").checked == false && document.getElementById("enc4").checked == false && document.getElementById("enc5").checked == false && document.getElementById("enc6").checked == false && document.getElementById("enc7").checked == false && document.getElementById("enc8").checked == false && document.getElementById("enc9").checked == false && document.getElementById("enc10").checked == false)
{ alert("Por favor, marque la forma en que nos ha conocido"); document.getElementById("enc1").focus(); return false; }

if (document.getElementById("concli").checked == false && document.getElementById("escli").checked == false)
{ alert("Debe indicar si ya es cliente de Charonia o no"); document.getElementById("concli").focus(); return false; }

if (document.getElementById("Privacidad").checked == false)
{ alert("Debe aceptar la politica de privacidad"); document.getElementById("Privacidad").focus(); return false; }

//alert("Se esta procediendo a una actualizacion por lo que temporalmente el registro esta inactivo. Disculpen las molestias.");

document.getElementById("registro").submit();
}


function Validaract(){

if (document.getElementById("nombre").value == "")
{ alert("Es obligatorio escribir su Nombre"); document.getElementById("nombre").focus(); return false; }

if (document.getElementById("apellidos").value == "")
{ alert("Es obligatorio escribir sus apellidos"); document.getElementById("apellidos").focus(); return false; }

var mail=emailCheck(document.getElementById("correo").value);
if (mail == false)
{ alert("Debe introducir un e-mail correcto"); document.getElementById("correo").focus(); return false; }

if (document.getElementById("tlfa").value == "")
{ alert("Es obligatorio un tlf de contacto"); document.getElementById("tlfa").focus(); return false; }

alert("Datos de cliente modificados");
document.getElementById("editusu").submit();
}

function Validaractb(){

if (document.getElementById("npass").value == "")
{ alert("Debe especificar su contrase\u00F1a"); document.getElementById("npass").focus(); return false; }

if (document.getElementById("npassb").value == "")
{ alert("Debe escribir la nueva contrase\u00F1a"); document.getElementById("npassb").focus(); return false; }


document.getElementById("editusub").submit();
}

function Validaractd(){
/*
if (document.getElementById("npass").value == "")
{ alert("Debe especificar su contrase\u00F1a"); document.getElementById("npass").focus(); return false; }

if (document.getElementById("npassb").value == "")
{ alert("Debe escribir la nueva contrase\u00F1a"); document.getElementById("npassb").focus(); return false; }
*/
document.getElementById("deditusu").submit();
}
function Validaractdn(){
/*
if (document.getElementById("npass").value == "")
{ alert("Debe especificar su contrase\u00F1a"); document.getElementById("npass").focus(); return false; }

if (document.getElementById("npassb").value == "")
{ alert("Debe escribir la nueva contrase\u00F1a"); document.getElementById("npassb").focus(); return false; }
*/
if (document.getElementById("dirrn").value == "")
{ alert("Debe especificar la direccion"); document.getElementById("dirrn").focus(); return false; }
if (document.getElementById("cpn").value == "")
{ alert("Debe especificar el codigo postal"); document.getElementById("cpn").focus(); return false; }
if (document.getElementById("pobn").value == "")
{ alert("Debe especificar la poblacion"); document.getElementById("pobn").focus(); return false; }

document.getElementById("deditusun").submit();
}
function Validaractdd(f){
/*
if (document.getElementById("npass").value == "")
{ alert("Debe especificar su contrase\u00F1a"); document.getElementById("npass").focus(); return false; }

if (document.getElementById("npassb").value == "")
{ alert("Debe escribir la nueva contrase\u00F1a"); document.getElementById("npassb").focus(); return false; }
*/
document.getElementById("deditusu"+f).submit();
}

function Validarcon(){

if (document.getElementById("asunto").value == "")
{ alert("Debe escribir un asunto de la consulta"); document.getElementById("asunto").focus(); return false; }

if (document.getElementById("consulta").value == "")
{ alert("Debe explicar el motivo de su consulta"); document.getElementById("consulta").focus(); return false; }

document.getElementById("rconusu").submit();
}

function quehacer(id) {
if (document.getElementById(id).style.display=="block") {
hide(id);
} else {
show(id);
}
}

function show(id) {
document.getElementById(id).style.display = "block";
document.getElementById(id).style.visibility = "visible";
}

function hide(id) {
document.getElementById(id).style.display = "none";
document.getElementById(id).style.visibility = "hidden";
}

function escogev () {
if ((document.getElementById('qcifa').checked==false && document.getElementById('qcifb').checked==false) || (document.getElementById('qcifa').checked==true && document.getElementById('qcifb').checked==true)) {
	document.getElementById('Nif').value='';
	alert('Seleccione primero si va a insertar un CIF o un NIF');
} else {
	if (document.getElementById('qcifa').checked==true) {
validaCif(document.getElementById('Nif')); }
	if (document.getElementById('qcifb').checked==true) {
validaNif(document.getElementById('Nif')); }
}

}

//Funcion volver
function retornar(id)
{
	control=document.getElementById(id);
	document.getElementById(id).value='';
	document.getElementById('qcifa').checked=false;
	document.getElementById('qcifb').checked=false;
	//document.getElementById(id).select();		
	//document.getElementById(id).focus();
	return;
	
}


//Funciones auxiliares
function IsNumeric(cadena)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < cadena.length && IsNumber == true; i++) 
      { 
      Char = cadena.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

   
// check to see if input is alphabetic
function isAlphabetic(cadena)
{
if (cadena.match(/^[a-zA-Z]+$/))
{
return true;
}
else
{
return false;
} 
}
   



//Funciones validadoras

function validaNif(control) 
{	
  if (control.value=="")
	return;
  var dni=control.value;    
  var numero = dni.substr(0,dni.length-1);
  var let = dni.substr(dni.length-1,1);
  let=let.toUpperCase();
  numero = numero % 23;
  var letra='TRWAGMYFPDXBNJZSQVHLCKET';
  letra=letra.substring(numero,numero+1);    
  //alert(letra);
  if (letra!=let)   
  {
    mensaje("dni","es");
    retornar(control.id);		
  }
}


function validaCif(control)
{ 
  if (control.value=="")
  {
	return;
  }
        var texto=control.value;
        var pares = 0; 
        var impares = 0; 
        var suma; 
        var ultima; 
        var unumero; 
        var uletra = new Array("J", "A", "B", "C", "D", "E", "F", "G", "H", "I"); 
        var xxx; 
         
        texto = texto.toUpperCase(); 
         
        var regular = new RegExp(/^[ABCDEFGHKLMNPQS]\d\d\d\d\d\d\d[0-9,A-J]$/g); 
         if (!regular.exec(texto)) 
			{
				mensaje("cif","es");
				retornar(control.id);		
			}
		 
              
         ultima = texto.substr(8,1); 

         for (var cont = 1 ; cont < 7 ; cont ++){ 
             xxx = (2 * parseInt(texto.substr(cont++,1))).toString() + "0"; 
             impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1)); 
             pares += parseInt(texto.substr(cont,1)); 
         } 
         xxx = (2 * parseInt(texto.substr(cont,1))).toString() + "0"; 
         impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1)); 
          
         suma = (pares + impares).toString(); 
         unumero = parseInt(suma.substr(suma.length - 1, 1)); 
         unumero = (10 - unumero).toString(); 
         if(unumero == 10) unumero = 0; 
          
         if ((ultima == unumero) || (ultima == uletra[unumero])) 
             return true; 
         else 
             {
				mensaje("cif","es");
				retornar(control.id);		
			 }

    } 

	
function validaNie(control) 
{	
  if (control.value=="")
	return;	

	var a=control;		
	var temp=a.value.toUpperCase();
	var cadenadni="TRWAGMYFPDXBNJZSQVHLCKET";
	var v1 = new Array(0,2,4,6,8,1,3,5,7,9);
	var posicion=0;
	var letra=" ";
	
	//Residente en España	
	if (a.value.length==9)
	{
		if (temp.substr(0,1)=="X")
		{
			var temp1=temp.substr(1,7);

			posicion = temp1 % 23; /*Resto de la division entre 23 es la posicion en la cadena*/
			letra = cadenadni.substring(posicion,posicion+1);
			if (!/^[A-Za-z0-9]{9}$/.test(temp))
			{ 
				mensaje("nie","es");
				retornar(control.id);	
			}
			else
			{ 
				//Tiene los 9 dígitos, comprobamos si la letra esta bien
				var temp1=temp.substr(1,7);
				posicion = temp1 % 23; /*Resto de la division entre 23 es la posicion en la cadena*/
				letra = cadenadni.charAt(posicion);
				var letranie=temp.charAt(8);
				if (letra != letranie){			
					mensaje("nie","es");
					retornar(control.id);			
				}				
			}
		}
		else
		{
			mensaje("nie","es");
			retornar(control.id);			
		}		
	}else if (a.value.length==14){//14 caracteres, los 2 primeros letras
		var temp1=temp.substr(0,2);
		if (isAlphabetic(temp1)!=true)	
			{
			mensaje("nie","es");
			retornar(control.id);	
			}
	}
	else
	{
			mensaje("nie","es");
			retornar(control.id);			

	}
	
}


function mensaje(msg,lang)
{
	if (lang=="es")
	{
		if(msg=="dni")	
			{
			alert("DNI no valido");
			}
		else if (msg=="cif")
					{
			alert("CIF no valido");
			}
		else if (msg=="nie")
					{
			alert("NIE no valido");
			}			
		else if (msg=="dc")
					{
			alert("El digito de control no es correcto");
			}						
		else if (msg=="completo")
					{
			alert("Rellene todos los datos de la cuenta");
			}									
		else if (msg=="formato")
					{
			alert("Formato incorrecto");
			}									
		else
			alert("Formato no valido");
	}
	
}


function numerico(valor){
  cad = valor.toString();
  for (var i=0; i<cad.length; i++) {
    var caracter = cad.charAt(i);
	if (caracter<"0" || caracter>"9")
	  return false;
  }
  return true;
}

function obtenerDigito(valor){
  valores = new Array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
  control = 0;
  for (i=0; i<=9; i++)
    control += parseInt(valor.charAt(i)) * valores[i];
  control = 11 - (control % 11);
  if (control == 11) control = 0;
  else if (control == 10) control = 1;
  return control;
}


function validaNif2(c1,c2)
{	
	var campo1=(document.getElementById(c1).value);
	var campo2=(document.getElementById(c2).value);	  
  
	if (campo1 == ""  || campo2 == ""){
	  return;
	}else{		
		var dni=campo1+campo2;  
		var numero = dni.substr(0,dni.length-1);
		var let = dni.substr(dni.length-1,1);
		numero = numero % 23;
		var letra='TRWAGMYFPDXBNJZSQVHLCKET';
		letra=letra.substring(numero,numero+1);  
		if (letra!=let)   
		{
			mensaje("dni","es");	
			retornar(c2);			
		}
	}
}

function validaCif2(c1,c2)
{	
	var campo1=(document.getElementById(c1).value);
	var campo2=(document.getElementById(c2).value);	  
  
	if (campo1 == ""  || campo2 == ""){
	  return;
	}else{
		//alert("Cif "+campo1+" "+campo2);
		
		var texto=campo1+campo2; 
        var pares = 0; 
        var impares = 0; 
        var suma; 
        var ultima; 
        var unumero; 
        var uletra = new Array("J", "A", "B", "C", "D", "E", "F", "G", "H", "I"); 
        var xxx; 
         
        texto = texto.toUpperCase(); 
         
        var regular = new RegExp(/^[ABCDEFGHKLMNPQS]\d\d\d\d\d\d\d[0-9,A-J]$/g); 
         if (!regular.exec(texto)) 
			{
				mensaje("cif","es");
				retornar(control.id);		
			}
		 
              
         ultima = texto.substr(8,1); 

         for (var cont = 1 ; cont < 7 ; cont ++){ 
             xxx = (2 * parseInt(texto.substr(cont++,1))).toString() + "0"; 
             impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1)); 
             pares += parseInt(texto.substr(cont,1)); 
         } 
         xxx = (2 * parseInt(texto.substr(cont,1))).toString() + "0"; 
         impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1)); 
          
         suma = (pares + impares).toString(); 
         unumero = parseInt(suma.substr(suma.length - 1, 1)); 
         unumero = (10 - unumero).toString(); 
         if(unumero == 10) unumero = 0; 
          
         if ((ultima == unumero) || (ultima == uletra[unumero])) 
             return true; 
         else 
             {
				mensaje("cif","es");
				retornar(c2);		
			 }

    } 		
		
		
	
}

function validaNie2(c1,c2)
{	
	var campo1=(document.getElementById(c1).value);
	var campo2=(document.getElementById(c2).value);	  
  
	if (campo1 == ""  || campo2 == ""){
	  return;
	}else{
		//alert("Nie "+campo1+" "+campo2);		

			var a=campo1+campo2; 		
			var temp=a.toUpperCase();
			var cadenadni="TRWAGMYFPDXBNJZSQVHLCKET";
			var v1 = new Array(0,2,4,6,8,1,3,5,7,9);
			var posicion=0;
			var letra=" ";

			//Residente en España	
			if (a.length==9)
			{
				if (temp.substr(0,1)=="X")
				{
					var temp1=temp.substr(1,7);

					posicion = temp1 % 23; /*Resto de la division entre 23 es la posicion en la cadena*/
					letra = cadenadni.substring(posicion,posicion+1);
					if (!/^[A-Za-z0-9]{9}$/.test(temp))
					{ 
						mensaje("nie","es");
						retornar(c2);	
					}
					else
					{ 
						//Tiene los 9 dígitos, comprobamos si la letra esta bien
						var temp1=temp.substr(1,7);
						posicion = temp1 % 23; /*Resto de la division entre 23 es la posicion en la cadena*/
						letra = cadenadni.charAt(posicion);
						var letranie=temp.charAt(8);
						if (letra != letranie){			
							mensaje("nie","es");
							retornar(c2);			
						}				
					}
				}
				else
				{
					mensaje("nie","es");
					retornar(c2);		
				}		
			}else if (a.length==14){//14 caracteres, los 2 primeros letras
				var temp1=temp.substr(0,2);
				if (isAlphabetic(temp1)!=true)	
					{
					mensaje("nie","es");
					retornar(c2);	
					}
			}
			else
			{
					mensaje("nie","es");
					retornar(c2);		

			}

	}
}

