function reescribe(tipo)
{
   var cambiar = document.getElementById("pc");
   cambiar.options.length = 0;
   for (i=0; i<seleccion[tipo].length; i++)
      cambiar.options[i] = new Option(seleccion[tipo][i],seleccion[tipo][i]);
}

function abre (url, largura, altura)
{
   var esquerda = (screen.width - largura)/2;
   var topo = (screen.height - altura)/2;
   window.open(url,'elimina','height=' + altura + ', width=' + largura + ', top=' + topo + ', left=' + esquerda);
}

function favoritos()
{
   if (window.external)
      external.AddFavorite(location.href, document.title);
}

function salvar()
{
   if (document.execCommand)
      document.execCommand("SaveAs",1,"Construidos_finca.htm");
}

function olvido() {
    if (document.getElementById("usuario").value != "")
        document.location = "olvido.php?usuario=" + document.getElementById("usuario").value;
    else alert ("* Escribe el nombre usuario en la ventana de login!\n\n* Write your username into login window!")
}

function avisolegal() {
     window.open('aviso_legal.html','aviso','toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,width=500,height=300');
 }

function compruebaTodo (arrayCampo)
{
   var cont;
   var estado=1;
   for (cont=0; cont<arrayCampo.length; cont++)
   {
      if (arrayCampo[cont+1] == "Email")
      {
         if (estado == 1)
            estado = compruebaEmail(arrayCampo[cont]);
         else
            compruebaEmail(arrayCampo[cont]);
      }
      else if (arrayCampo[cont+1] == "Iguales")
      {
         if (estado == 1)
            estado = compruebaCamposIguales(arrayCampo[cont]);
         else
            compruebaCamposIguales(arrayCampo[cont]);
      }
      else if (arrayCampo[cont+1] == "Entero")
      {
         if (estado == 1)
            estado = compruebaEntero(arrayCampo[cont]);
         else
            compruebaEntero(arrayCampo[cont]);
      }
      else if (arrayCampo[cont+1] == "Requerido")
      {
         if (estado == 1)
            estado = compruebaRequerido(arrayCampo[cont]);
         else
            compruebaRequerido(arrayCampo[cont]);
      }
   }
   if (estado == 0)
      alert ("Los campos en rojo son obligatorios y deben estar correctamente rellenados.");
   return estado;
}

function idioma(idiom)
{
   return window.open("<?php if($_SERVER['PHP_SELF']) echo $_SERVER['PHP_SELF']; else echo 'index.php';?>?idioma="+idiom,"_self");
}

function mascaraImagen(dir)
{
   dir = dir.value;
   if (dir != "")
   {
      var tam = dir.length;
      var extension = dir.substring((tam-4),tam);
      extension = extension.toLowerCase();
      if (extension.indexOf(".jpg") == -1)
      {
         allowSubmit=false;
         alert("Sólo puede añadir archivos de fotos y en formato .jpg");
         return false;
      }
      else
         return true;
   }
}

function compruebaEmail(obj)
{
   obj.style.backgroundColor="white";
    var email = obj.value;
   if (email.indexOf('@',0) > 0 && email.indexOf('.',0) > 0)
   {
      if (email.indexOf(' ',0) != -1 || email.indexOf('/',0) != -1
        || email.indexOf(';',0) != -1 || email.indexOf('<',0) != -1
        || email.indexOf('>',0) != -1 || email.indexOf('*',0) != -1
        || email.indexOf('|',0) != -1 || email.indexOf('`',0) != -1
        || email.indexOf('&',0) != -1 || email.indexOf('$',0) != -1
        || email.indexOf('!',0) != -1 || email.indexOf('"',0) != -1
        || email.indexOf(':',0) != -1)
      {
         obj.value="";
         obj.style.backgroundColor="red";
         return false;
      }
      else
         return true;
   }
      else
   {
      obj.value="";
      obj.style.backgroundColor="red";
      return false;
   }
}

function compruebaEntero(obj)
{
   obj.style.backgroundColor="white";

   obj.style.backgroundColor="red";
   return true;
}

function compruebaCamposIguales(obj)
{
   obj.style.backgroundColor="white";
   var obj2 = document.all.Password2;
   if (obj.value != obj2.value)
   {
      obj.value="";
      obj2.value="";
      obj.style.backgroundColor="red";
      obj2.style.backgroundColor="red";
      return false;
   }
    else
         return true;
}

function compruebaRequerido(obj)
{
   obj.style.backgroundColor="white";
   if ((obj.selectedIndex=="") || (obj.value == ""))
   {
      obj.style.backgroundColor="red";
      return false;
   }
   else
      return true;
}

function formateaEnteroConPuntos (id)
{
   var tam, valor, temp, cont, tres;
   tam = id.value.length;
   // Si la persona digitó algo diferente de números enteros, no lo coge.
   if ((event.keyCode < 48) || (event.keyCode > 57))
   {
      event.returnValue = false;
   }
   else
   {
      valor = id.value;
      temp = "";
      // Miro la string y veo si ya he puesto puntos en ella. Y lo que no es punto copio a temp.
      for (cont=0; cont<id.value.length; cont++)
      {
         if (valor.charAt(cont)!= '.')
         {
            temp += valor.charAt(cont);
         }
      }
      tres = 0;
      // Percorro de derecha a izquierda contando las casas y añadiendo el punto.
      valor= "";
      for (cont=temp.length; cont>=0; cont--)
      {
         if ((tres%3 == 0) && (tres!=0))
         {
            valor = '.' + valor;
         }
         valor = temp.charAt(cont) + valor;
         tres++;
      }
      id.value = valor;
   }
}

function formateaEntero (id)
{
   var tam;
   tam = id.value.length;
   // Si la persona digitó algo diferente de números enteros, no lo coge.
   if ((event.keyCode < 48) || (event.keyCode > 57))
   {
      event.returnValue = false;
   }
}

function formateaNumeroPunto (id)
{
   var tam;
   tam = id.value.length;
   if ((event.keyCode < 46) || (event.keyCode > 57))
   {
      if (event.keyCode == 44)
         alert('No separar centenas y utilizar el punto como separador de decimales.');
      event.returnValue = false;
   }
}

function formateaNumeroComa (id)
{
   var tam;
   tam = id.value.length;
   if ((event.keyCode < 44) || (event.keyCode > 57) || (event.keyCode == 45) || (event.keyCode == 46))
   {
      if (event.keyCode == 46)
         alert('Utilizar la coma como separador.');
      event.returnValue = false;
   }
}

// Lo que no debe aparecer debe estar dentro de una layer llamada "identificador"
function imprime(identificador)
{
    document.all(identificador).style.visibility = 'hidden';
    if (document.all)
   {
        if (navigator.appVersion.indexOf("5.0") == -1)
      {
            var OLECMDID_PRINT = 6;
            var OLECMDEXECOPT_DONTPROMPTUSER = 2;
            var OLsECMDEXECOPT_PROMPTUSER = 1;
            var WebBrowser = "<OBJECT ID=\"WebBrowser1\" WIDTH=0 HEIGHT=0 CLASSID=\"CLSID:8856F961-340A-11D0-A96B-00C04FD705A2\"></OBJECT>";
            document.body.insertAdjacentHTML("beforeEnd", WebBrowser);
            WebBrowser1.ExecWB(6, 2);
            WebBrowser1.outerHTML = "";
        }
      else
            window.print();
    }
   else
       window.print();
    document.all(identificador).style.visibility = 'visible';
}
