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(){
   parent.document.getElementById("dwindow").style.display="none";
}

function loadwindow(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="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;
   }
}

function vercalendario(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("fechafactura")) + 'px';
      document.getElementById("dwindow").style.top=PosicionY(document.getElementById("fechafactura")) + '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 PosicionX(obj){
    var curleft = 0;
    if(obj.offsetParent)
        while(1)
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function PosicionY(obj){
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}
  
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 validarForm() {
      var i,p,q,nm,test,num,min,max,errors='',args=validarForm.arguments;
      if (leerCookie('luppa_ln')!='en') 
         var textorequerido='Campos requeridos:';
      else
         var textorequerido='Required fields:';
      
      for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=localizaObj(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' no válido.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' debe ser numérico.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' debe ser un nº entre '+min+' y '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+'\n'; }
      } 
      if (errors) {
         if (leerCookie('luppa_ln')=='en') {
            errors = errors.replace("nombre", "name");
            errors = errors.replace("telefono", "phone");
         }
         alert(textorequerido+'\n\n'+errors);
      }
      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('tituloimprimir')) {	 
      document.getElementById('tituloimprimir').style.visibility = 'visible';
      document.getElementById('tituloimprimir').style.position = 'relative'; 
   }      
   if (document.getElementById('lateral')) {	 
      document.getElementById('lateral').style.visibility = 'hidden';
      document.getElementById('lateral').style.position = 'absolute'; 
   }         
   if (document.getElementById('filapie')) {
      document.getElementById('filapie').style.visibility = 'hidden';
      document.getElementById('filapie').style.position = 'absolute'; 
   }
   if (document.getElementById('separapie')) {
      document.getElementById('separapie').style.visibility = 'hidden';
      document.getElementById('separapie').style.position = 'absolute'; 
   }
   if (document.getElementById('fondopie')) {
      document.getElementById('fondopie').style.visibility = 'hidden';
      document.getElementById('fondopie').style.position = 'absolute'; 
   }
} 

function despuesdeimprimir(){
   if (document.getElementById('filacabecera')) {	 
      document.getElementById('filacabecera').style.visibility = 'visible';
      document.getElementById('filacabecera').style.position = 'relative'; 
   }      
   if (document.getElementById('tituloimprimir')) {	 
      document.getElementById('tituloimprimir').style.visibility = 'hidden';
      document.getElementById('tituloimprimir').style.position = 'absolute'; 
   }      
   if (document.getElementById('lateral')) {	 
      document.getElementById('lateral').style.visibility = 'visible';
      document.getElementById('lateral').style.position = 'relative'; 
   }         
   if (document.getElementById('filapie')) {
      document.getElementById('filapie').style.visibility = 'visible';
      document.getElementById('filapie').style.position = 'relative'; 
   }
   if (document.getElementById('separapie')) {
      document.getElementById('separapie').style.visibility = 'visible';
      document.getElementById('separapie').style.position = 'relative'; 
   }
   if (document.getElementById('fondopie')) {
      document.getElementById('fondopie').style.visibility = 'visible';
      document.getElementById('fondopie').style.position = 'relative'; 
   }
}

   var nav4 = window.Event ? true : false;

   function navegar(opcion) {
      parent.window.location.href='(CONTENIDO)'+opcion;
   }
   
   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 validaremail(fuente) {
      cadena = fuente.value;
      if ((cadena!='') && ((cadena.lastIndexOf("@") < 1) || (cadena.lastIndexOf(".") <= 2))) {
        alert("Introduce un email válido (i.e. usuario@dominio.com)");
        fuente.focus();
      }
   }

   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.");
           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.");
       fuente.focus();
     }
  }
   
   function validaFiscal(fuente) {
      let=fuente.value.charAt(0)
      if (!isNaN(let)) {
         validarNIF(fuente)
      } else {
         validarCIF(fuente)
      }
   }
   
   function validarCCC(c1,c2,c3,c4) { 
      if (!(c1.disabled)) {
      var cc1 = c1.value + "" +c2.value;
      var cc2 = c4.value;
      var dc = c3.value;
      
      if (c1.value.length!=4 || c2.value.length!=4 || c3.value.length!=2 || c4.value.length!=10) {
         alert("El número de cuenta bancaria no es correcto.\nPor favor verifíquelos.");
         return false;
      }
      
      if (!(cc1.match(/^\d{8}$/) && cc2.match(/^\d{10}$/) && dc.match(/^\d{2}$/) )) return false;
      var arrWeights = new Array(1,2,4,8,5,10,9,7,3,6);
      var dc1=0, dc2=0;
       	    
      for (i=7;i>=0;i--) dc1 += arrWeights[i+2] * cc1.charAt(i);
      dc1 = 11 - (dc1 % 11);
      if (11 == dc1) dc1 = 0;
      if (10 == dc1) dc1 = 1;
       	    
      for (i=9;i>=0;i--) dc2 += arrWeights[i] * cc2.charAt(i);
      dc2 = 11 - (dc2 % 11);
      if (11 == dc2) dc2 = 0;
      if (10 == dc2) dc2 = 1;
       	    
      if  (10*dc1+dc2 == dc) {
         return true;
      } else {
         alert("El número de cuenta bancaria no es correcto.\nPor favor verifíquelo.");
         return false;
         c1.focus();
      }
      } else
         return true;
   }    
