// Precarga de imágenes
var dir_img = "img/";
if (window.XMLHttpRequest) {
    // IE 7, mozilla, safari, opera 9
    imagenes = new Array("fondo.gif", "cabecera.gif", "pests.gif");
} else {
    // IE 6 y antiguos
    imagenes = new Array("fondo.gif", "cabecera.gif", "pest1A.gif", "pest2A.gif", "pest3A.gif", "pest4A.gif");
}
carga = new Array;
for (var a = 0; a < imagenes.length; a++) {
    carga[a] = new Image;
    carga[a].src = dir_img + imagenes[a];
}

// Formulario login (usa jQuery)
$(document).ready(function() {
    $("#login").focus(function() {
        if ($(this).val() == "usuario") {
            $(this).val("");
        }
    });
    $("#login").blur(function() {
        if ($(this).val().replace(/ /g, '') == "") {
            $(this).val("usuario");
        }
    });
    changeInputType(document.getElementById("pwd"), 'text', 'contraseña', false, true);           
    $("#form_acceder").submit(function() {
        return validaForm();
    });
});

function validaForm() {
        if ($("#login").val().replace(/ /g) == "" || $("#login").val().replace(/ /g) == "usuario") {
            alert("Por favor, introduzca el nombre de usuario.");
            $("#login").focus();
            return false;
        }
        if ($("#pwd").val().replace(/ /g) == "" || $("#pwd").val().replace(/ /g) == "contraseña") {
            alert("Por favor, introduzca la contraseña.");
            $("#pwd").focus();
            return false;
        }
        return true;
}

// Cambia input de tipo text a password
function changeInputType(
  oldElm, // a reference to the input element
  iType, // value of the type property: 'text' or 'password'
  iValue, // the default value, set to 'password' in the demo
  blankValue, // true if the value should be empty, false otherwise
  noFocus) {  // set to true if the element should not be given focus
 
  if(!oldElm || !oldElm.parentNode || (iType.length<4) || 
    !document.getElementById || !document.createElement) return;
  var newElm = document.createElement('input');
  newElm.type = iType;
  if(oldElm.name) newElm.name = oldElm.name;
  if(oldElm.id) newElm.id = oldElm.id;
  if(oldElm.className) newElm.className = oldElm.className;
  if(oldElm.size) newElm.size = oldElm.size;
  if(oldElm.tabIndex) newElm.tabIndex = oldElm.tabIndex;
  if(oldElm.accessKey) newElm.accessKey = oldElm.accessKey;
  newElm.onfocus = function(){return function(){
    if(this.hasFocus) return;
    var newElm = changeInputType(this,'password',iValue,
      (this.value.toLowerCase()==iValue.toLowerCase())?true:false);
    if(newElm) newElm.hasFocus=true;
  }}();
  newElm.onblur = function(){return function(){
    if(this.hasFocus)
    if(this.value=='' || (this.value.toLowerCase()==iValue.toLowerCase())) {
      changeInputType(this,'text',iValue,false,true);
    }
  }}();
 // hasFocus is to prevent a loop where onfocus is triggered over and over again
  newElm.hasFocus=false;
  oldElm.parentNode.replaceChild(newElm,oldElm);
  if(!blankValue) newElm.value = iValue;
  if(!noFocus || typeof(noFocus)=='undefined') {
    window.tempElm = newElm;
    setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
  }
  return newElm;
}

// Hacer submit del form si se pulsa enter (sólo para IE)
function cheqEnter(e) {
  if (document.all) {
    tecla = e.keyCode;
    if (tecla == 13) {
      if (validaForm()) document.getElementById("form_acceder").submit();
    }
  }
}
