$(document).ready(function() {
   if ($("a[rel=galeria_foto]") != null) {
	$("a[rel=galeria_foto]").fancybox({
		'showCloseButton'	: false,
		'titlePosition' 	: 'inside',
		'titleFormat'		: function formatTitle(title, currentArray, currentIndex, currentOpts) {
   	 	return '<a href="javascript:;" onclick="$.fancybox.close();"><img src="./html/img/cerrar.gif" border=0 align="right"/></a><br>' + (title && title.length ? '<b>' + title + '</b>' : '&copy www.Moscas de León.com' ) + ' · Imagen ' + (currentIndex + 1) + ' de ' + currentArray.length; }
	});
   }   
	$("#externo").fancybox({
		'showCloseButton'	: false,
		'titlePosition' 	: 'inside',
		'titleFormat'		: function formatTitle(title, currentArray, currentIndex, currentOpts) {
   	 	return '<a href="javascript:;" onclick="$.fancybox.close();"><img src="./html/img/cerrar.gif" border=0 align="right"/></a><br><br>'; },
   	 	'width'			: 640,
   	 	'height'		: 480,
		'autoDimensions'	: true,
		'autoScale'		: true,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'			: 'iframe'
	});

});

var initialwidth,initialheight
var ie5=document.all&&document.getElementById
var ns6=document.getElementById&&!document.all

function iecompattest(){
   return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ocultarla(){
   document.getElementById("dwindow").style.display="none";
}

function loadwindow(url,titulo,width,height){
   if (!ie5&&!ns6)
      window.open(url,"","width=width,height=height,scrollbars=1")
   else {
      document.getElementById("tituloprevio").value='· '+titulo;	
      document.getElementById("dwindow").style.display='';
      document.getElementById("dwindow").style.width=initialwidth=width+"px";
      document.getElementById("dwindow").style.height=initialheight=height+"px";
      document.getElementById("dwindow").style.left="30px";
      document.getElementById("dwindow").style.top=ns6? window.pageYOffset*1+30+"px" : iecompattest().scrollTop*1+30+"px";
      //document.getElementById("cframe").contentWindow.document.body.innerHTML='<img src="'+url+'" border=0 onClick="parent.window.dwindow.style.display=\'none\';" style="cursor:hand;">';
      document.getElementById("cframe").src=url;
   }
}

var nav4 = window.Event ? true : false;
function solonumeros(evt) { 
	// Backspace = 8, Enter = 13, '0' = 48, '9' = 57 
	var key = nav4 ? evt.which : evt.keyCode; 
	return (key <= 13 || (key >= 48 && key <= 57));
}

function resaltarImagen(cur,which){
   strength=(which==1)? 1 : 0.6
   if (cur.style.MozOpacity)
      cur.style.MozOpacity=strength
   else if (cur.filters)
      cur.filters.alpha.opacity=strength*100
}

function leerCookie(nombre) { 
   a = document.cookie.substring(document.cookie.indexOf(nombre + '=') + nombre.length + 1,document.cookie.length); 
   if(a.indexOf(';') != -1)a = a.substring(0,a.indexOf(';')) 
   return a;  
} 

function localizaObj(n, d) {
      var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
      d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
      if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
      for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=localizaObj(n,d.layers[i].document);
      if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function validarNIF(fuente) {
      var abc=fuente.value
      dni=abc.substring(0,abc.length-1)
      let=abc.charAt(abc.length-1)
      if (!isNaN(let))
      {
         alert('Falta la letra del N.I.F.')
         fuente.focus()
         return false
      }
      else
      {
        cadena="TRWAGMYFPDXBNJZSQVHLCKET"
        posicion = dni % 23
        letra = cadena.substring(posicion,posicion+1)
        if (letra!=let.toUpperCase())
        {
           alert("El N.I.F. introducido no es correcto.\nPor favor verifíquelo. No introduzca ni puntos ni guiones.");
           fuente.focus()
           return false
        } 
      }
}

function validarCIF(fuente) {
     var abc=fuente.value;
     var v1 = new Array(0,2,4,6,8,1,3,5,7,9); 
     var temp = 0;

     for( i = 2; i <= 6; i += 2 ) {
        temp = temp + v1[ parseInt(abc.substr(i-1,1))];
        temp = temp + parseInt(abc.substr(i,1));
     }
     temp = temp + v1[ parseInt(abc.substr(7,1))];
     temp = (10 - ( temp % 10));

     if( temp == 10 ) temp = 0;
     if (abc.charAt(abc.length-1)!=temp) {
       alert("El C.I.F. introducido no es correcto.\nPor favor verifíquelo. No introduzca ni puntos ni guiones.");
       fuente.focus();
     }
}
   
function validaFiscal(fuente) {
      let=fuente.value.charAt(0)
      if (!isNaN(let)) {
         validarNIF(fuente)
      } else {
         validarCIF(fuente)
      }
}


function validar(formulario) {
      var errors = '';
      if (formulario.email.value=="") 
      	errors += "Correo electrónico\n";
      else
      if (((formulario.email.value.lastIndexOf("@") < 1) || (formulario.email.value.lastIndexOf(".") <= 2))) errors += "- Introduzca tu direccion de email correctamente\n";

      else
      	if (formulario.email.value!=formulario.email2.value) errors += "- Confirme correctamente su dirección de correo electrónico\n";
      
      
      if (formulario.contrasena.value=="") 
      	errors += "- Contraseña\n";
      else
      	if (formulario.contrasena.value!=formulario.contrasena2.value) errors += "- Confirme correctamente su contraseña\n";
      
      if (formulario.nombre.value=="") errors += "- Nombre y Apellidos\n";
      if (formulario.direccion.value=="") errors += "- Dirección\n";
      if (formulario.localidad.value=="") errors += "- Localidad\n";
      if (formulario.cp.value=="") errors += "- Código Postal\n";
      if (formulario.provincia.value=="") errors += "- Provincia\n";
      if ((formulario.tfno.value=="") && (formulario.movil.value=="")) errors += "- Introduzca su nº de teléfono o su móvil\n";
      
      if (errors=='') {
      	if (formulario.pepe) {
		if (formulario.pepe.value!=(" "+formulario.llave.value)) errors += "- Código de validación incorrecto\n";
	}
      }
      
      if (errors) {
         alert('Errores detectados:\n\n' + errors);
         if (formulario.aceptar)
	 	formulario.aceptar.checked = false;
	 if (document.getElementById('acceso'))
	 	document.getElementById('acceso').disabled = true;
      }
      
      return (errors=='');
}
   
function mostrar_tab(TabViewId, CurrentId)
{
  var TabView = document.getElementById(TabViewId);

  // ***** Tabs *****

  var Tabs = TabView.firstChild;
  while (Tabs.className != "Tabs") Tabs = Tabs.nextSibling;
  var Tab  = Tabs.firstChild;
  var textwidth = 0;
  var i    = 0;
  do
  {
    if (Tab.tagName == "A")
    {
      i++;
      Tab.href = "javascript:cambiarvista('"+TabViewId+"', "+i+");";
      textwidth = Tab.innerHTML.length;      
      Tab.className    = (i == CurrentId) ? "TabDefecto" : "";      
      Tab.blur();
    }
  }
  while (Tab = Tab.nextSibling);

  // ***** Pages *****

  var Pages = TabView.firstChild;
  while (Pages.className != 'Pages') Pages = Pages.nextSibling;
  var Page  = Pages  .firstChild;
  var i     = 0;
  var altura=0;

  do
  {
    if (Page.className == 'Page')
    {
      i++;
      Page.style.display  = (i == CurrentId) ? 'block' : 'none';
    	
      if (i == CurrentId) {
      	if (document.all)
      		Pages.style.height = Page.clientHeight +"px";  
      	else {
      	        if (Pages.offsetHeight) Page.style.height = (Page.scrollHeight)+"px";
      		Pages.style.height = Page.scrollHeight +"px";  
      		Page.style.overflow = "hidden"; 
      		//alert(Page.scrollHeight);
      	}
      }
    }
  }
  while (Page = Page.nextSibling);
}

function cambiarvista(TabViewId, id) { mostrar_tab(TabViewId, id); }
function inicializar_tabs(TabViewId) { mostrar_tab(TabViewId,  1); }

var min=8;
var max=20;
var original=12;
function aumentar() {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }
}

function reducir() {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   
}

function antesdeimprimir(){ 
   if (document.getElementById('filacabecera')) {	 
      document.getElementById('filacabecera').style.visibility = 'hidden';
      document.getElementById('filacabecera').style.position = 'absolute'; 
   }
   if (document.getElementById('filamenus')) {	 
      document.getElementById('filamenus').style.visibility = 'hidden';
      document.getElementById('filamenus').style.position = 'absolute'; 
   }      
   if (document.getElementById('filapie')) {
      document.getElementById('filapie').style.visibility = 'hidden';
      document.getElementById('filapie').style.position = 'absolute'; 
   }
} 

function despuesdeimprimir(){
   if (document.getElementById('filacabecera')) {	 
      document.getElementById('filacabecera').style.visibility = 'visible';
      document.getElementById('filacabecera').style.position = 'relative'; 
   }      
   if (document.getElementById('filamenus')) {	 
      document.getElementById('filamenus').style.visibility = 'visible';
      document.getElementById('filamenus').style.position = 'relative'; 
   }      
   if (document.getElementById('filapie')) {
      document.getElementById('filapie').style.visibility = 'visible';
      document.getElementById('filapie').style.position = 'relative'; 
   }
}

function pixelar(){
    if (ie5) {
    	document.images.imgcab1.filters[0].apply();
    	document.images.imgcab1.filters[0].play();
    	setTimeout("pixelar()",8000);
    }
}

function cargar_ejemplo(direccion) {
	loadwindow(direccion,800,600);
}

function vercalendario(objeto,url,width,height){
   if (!ie5&&!ns6)
      window.open(url,"","width=width,height=height,scrollbars=1")
   else {
      document.getElementById("dwindow").style.display=''
      document.getElementById("dwindow").style.width=initialwidth=width+"px"
      document.getElementById("dwindow").style.height=initialheight=height+"px"
      document.getElementById("dwindow").style.left=PosicionX(document.getElementById(objeto)) + 'px';
      document.getElementById("dwindow").style.top=PosicionY(document.getElementById(objeto)) + 'px';
      //document.getElementById("cframe").contentWindow.document.body.innerHTML='<img src="'+url+'" border=0 onClick="parent.window.dwindow.style.display=\'none\';" style="cursor:hand;">';
      document.getElementById("cframe").src=url;
   }
}

function calendario(objeto) {
   vercalendario(objeto,'./?opcion=calendario&campo='+objeto,230,195);
}

function pixelar(){
    if (ie5) {
    	document.images.imgcab1.filters[0].apply();
    	document.images.imgcab1.filters[0].play();
    	setTimeout("pixelar()",8000);
    }
}

function dos_digitos(numero) {
	if (numero<10)
		return "0"+numero;
	else
		return numero;
}

function reloj() {
	var fecha = new Date();
	var diasemana = ["Domingo", "Lunes","Martes","Miércoles","Jueves","Viernes","Sábado"];
	var meses = ["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"];
	var hoy = diasemana[fecha.getDay()]+", "+fecha.getDate()+" de "+meses[fecha.getMonth()]+" de "+fecha.getFullYear();   
	var hora = dos_digitos(fecha.getHours())+":"+dos_digitos(fecha.getMinutes())+":"+dos_digitos(fecha.getSeconds());
	document.getElementById('fechahorapantalla').value= " " + hoy + " " + hora + " ";
	setTimeout("reloj()",1000);
}
