//FUNCIONES AVISO LEGAL

function openWindow(){
  window.open("avisolegal.html","miVentana","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizeble=0,width=510,height=500,top=100")
    }
    
function openWindowProteccion(){
  window.open("avisolegal.html#ancla","miVentana","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizeble=0,width=510,height=500,top=100")
}
//--FIN FUNCIONES AVISO LEGAL



//FUNCIONES VALIDACION
function hayLetras(cadena){
  var letra;
  var i=0;
  var resultado=false;
  
   while (i<cadena.length){
    letra = cadena.charAt(i);
    if  (((letra >= 'a') && (letra <= 'z')) || ((letra >= 'A') && (letra <= 'Z'))){
      resultado=true;
      break;
    }//if 
    i++;
  }//while
  
  return resultado;
}//hayLetra  

/* 
   Funcion que detecta si hay numeros en una cadena de caracteres.
   El parametro que recibe es la cadena a examinar.
   Devuelve TRUE si encuentra alguno. Si no, FALSE.
*/
function hayNumeros(cadena){

  var letra;
  var i=0;
  var resultado=false;
  
  while (i<cadena.length){
    letra = cadena.charAt(i);
    if  ((letra >= '0') && (letra <= '9')){
      resultado=true;
      break;
    }//if 
    i++;
  }//while
  
  return resultado;
}//hayNumeros

  function letraExiste(letra){
    
    var letras=["T"," ","R","W","A","G","M","Y","F","P","D","X","B","N","J","Z","S","Q","V","H","L","C","K","E","I","Y","O","É","Í","Ó","Ú","Á",".",","];
    var miLetra=letra;
    var existe=false;
    
    miLetra=miLetra.toUpperCase();
      
    for(var i=1;i<letras.length;i++){
      if(miLetra==letras[i]){
        existe=true;
      }
    }
    return existe; 
  }//fin letraExiste


function validar_texto(e) { // 1
    tecla = (document.all) ? e.keyCode : e.which; // 2
    if (tecla==8) return true; // 3
    patron =/[áÁA-Za-z0-9ñÑ".,¿?\s]/; // 4
    te = String.fromCharCode(tecla); // 5
    return patron.test(te); // 6
} 

function validarEmail(elemento){
  var pos_at=-1;
  var pos_punto=-1;
  var i=0;
  
  while (i<elemento.value.length){
    if (elemento.value.charAt(i) == '@'){
      pos_at=i;
      break;
    }
    i++;
  }
  
  i=0;
  
  while (i<elemento.value.length){
    if(elemento.value.charAt(i)=='.'){
      pos_punto=i;
      break;
    }
    i++;
  }
    
  if ((pos_at > pos_punto) || (pos_at == -1) || (pos_punto == -1)){
    alert('Email inv\u00e1lido. Vuelva a introducir su correo electr\u00f3nico');
    elemento.value="";
    elemento.focus();
    return false;
  }
  else{return true;} 
}//validarEmail

 function eliminarEspaciosIzq(cadena){
    var miCadena=cadena;
    
    for(var i=0; i<cadena.length; i++){
      
      var letra=cadena.charAt(i); 
      
      if(letra==' '){       
        miCadena=miCadena.substring(1);    
      }
      else{
        break;
      }      
    }
    return miCadena;
  }//fin eliminarEspaciosIzq

function validarTextareaPublico(elemento){
  
  elemento.value=eliminarEspaciosIzq(elemento.value);

  if(elemento.value=="") {
    alert('Introduce alg\u00fan comentario');
    elemento.focus();
    return false;
  }else{return true;}
}

function validarTextarea(elemento){
  
  elemento.value=eliminarEspaciosIzq(elemento.value);

  if(elemento.value=="") {
    alert('Introduce alguna descripci\u00f3n');
    elemento.focus();
    return false;
  }else{return true;}
}

function validarReferencia(elemento){
  if(hayLetras(elemento.value)){
    alert('La referencia debe contener s\u00f3lo d\u00edgitos');
    elemento.value="";
    elemento.focus();
    return false;
  }
  else if(elemento.value.length != 5){
          alert('La referencia debe tener una longitud de 5 d\u00edgitos');
          elemento.value="";
          elemento.focus();
          return false;
        }
  else{return true;}
}//validarCodigo

function validarReferenciaArchivo(elemento){
  if(hayLetras(elemento.value)){
    alert('La referencia debe contener s\u00f3lo d\u00edgitos');
    elemento.value="";
    elemento.focus();
    return false;
  }
  else if(elemento.value.length != 4){
          alert('La referencia debe tener una longitud de 4 d\u00edgitos');
          elemento.value="";
          elemento.focus();
          return false;
        }
  else{return true;}
}//validarCodigo


function validarPrecio(precio){

  if ((precio.value>=0)&&(precio.value<=999999)&&(precio.value.length>0)){
    return true;
  }else{return false;}
}


function validarFormCategoria(formulario){
  var categoria=document.getElementById("categoria");
  var descripcion=document.getElementById("descripcion");
  
  categoria.value=eliminarEspaciosIzq(categoria.value);
  if(categoria.value!="" && categoria.value.length>0){
    if(validarTextarea(descripcion)){
      formulario.submit();
    }
  }else{
    alert("Introduce un nombre de categor\u00eda");
  }
  
}
function validarFormReferencia(formulario,elemento){
  if (validarReferencia(elemento)){
    formulario.submit();
  } 
}

function validarFormMarca(formulario,marca){

  marca=eliminarEspaciosIzq(marca);
  if(marca.length>0 && marca!=""){
    formulario.submit();
  }else{
    alert("Introduce una marca");
  }
}
function validarProducto(formulario){
  var referencia=document.getElementById("referencia");
  var modelo=document.getElementById("modelo");
  var precio=document.getElementById("precio");
  
  modelo_limpio=eliminarEspaciosIzq(modelo.value);
  modelo.value=modelo_limpio;
  
  if(validarReferencia(referencia)){
    if(modelo.value!=""){
      if(validarPrecio(precio)){
        formulario.submit();
      }else{
        alert("Introduce un precio")
        precio.focus();
        }
    }else{
      alert ("Introduce un modelo")
      modelo.focus();
      }
  }
}

function validarContacto(formulario){
  var email=document.getElementById("email");
  var comentarios=document.getElementById("comentarios");


  if (validarEmail(email)){
    if(validarTextareaPublico(comentarios)){
      alert("Su consulta ha sido enviada con \u00e9xito\nEn breve nos pondremos en contacto con usted\nGracias por su inter\u00e9s");
      formulario.submit();
      valida="true";
    }
  }
  
}
