function abrirventana(src) {
	var direccion = src.getAttribute('href');
	var laventana = window.open(direccion, 'imagen','width=500,height=640');
}

function cerrarventana() {
	window.close();
    window.top.opener.focus();
}

// Recorre todos los forms de la p&aacute;gina y los valida
var g_aErroneos;
var g_aMensajes;
var g_nErrores;
var g_fnPresentacion;

function validarPagina(bPasoAPaso, bSetFocus, fnPresentacion) {
  var nForms = document.forms.length;
  var i=0;
  var bRdoPagina = true;
  g_aErroneos = new Array();
  g_aMensajes = new Array();
  g_nErrores = 0;
  g_fnPresentacion = null;
  
  if (fnPresentacion != null)
    g_fnPresentacion = fnPresentacion;
  
  for (i=0; i<nForms; i++) {
    var bRdoForm = validarForm(document.forms[i], bPasoAPaso);
    if (bRdoPagina && !bRdoForm)
      bRdoPagina = false;
  }
  if (!bRdoPagina) {
    if (bPasoAPaso) {
 //     Presentar(g_aErroneos[0].name + ": " + g_aMensajes[0]);
      Presentar(g_aMensajes[0]);
    } else {
      var i=0;
      var szMensajon = "Errores:";
      for (i=0; i<g_aErroneos.length; i++)
        szMensajon += "\n·" + g_aErroneos[i].name + ": " + g_aMensajes[i];
      Presentar(szMensajon);
    }
    if (bSetFocus) {
      var erroneo = g_aErroneos[0];
      var tipo = erroneo.type;
      erroneo.focus();
      if (tipo == "text" || tipo == "textarea")
        g_aErroneos[0].select();
    }
  }
  return bRdoPagina;
}

function validarUnForm2(bPasoAPaso, bSetFocus, bForm) {
  var nForms = document.forms.length;
  var i=0;
  var bRdoPagina = true;
  g_aErroneos = new Array();
  g_aMensajes = new Array();
  g_nErrores = 0;
  g_fnPresentacion = null;
  fnPresentacion = null;
  
  if (fnPresentacion != null)
    g_fnPresentacion = fnPresentacion;
	
  var bRdoForm = validarForm(document.forms[bForm], bPasoAPaso);
	if (bRdoPagina && !bRdoForm)
      bRdoPagina = false;

  if (!bRdoPagina) {
    if (bPasoAPaso) {
 //     Presentar(g_aErroneos[0].name + ": " + g_aMensajes[0]);
      Presentar(g_aMensajes[0]);
    } else {
      var i=0;
      var szMensajon = "Errores:";
      for (i=0; i<g_aErroneos.length; i++)
        szMensajon += "\n·" + g_aErroneos[i].name + ": " + g_aMensajes[i];
      Presentar(szMensajon);
    }
    if (bSetFocus) {
      var erroneo = g_aErroneos[0];
      var tipo = erroneo.type;
      erroneo.focus();
      if (tipo == "text" || tipo == "textarea")
        g_aErroneos[0].select();
    }
  }
  return bRdoPagina;
}


// Recorre los elementos de cada form y valida los que incluyen un atributo 'regexp'
function validarForm(elForm, bPasoAPaso) {
  var nElements = elForm.elements.length;
  var i=0;
  var bRdo;
  var bRdoForm = true;
  var bSeguir = true;
  var msg;
  for (i=0; (i<nElements) && bSeguir; i++) {
    var elem = elForm.elements[i];
    if (elem.regexp != null) {
        bRdo = validarElementoRegExp(elem);
        if (!bRdo) {
          if (bRdoForm) // Si puede empeorar
            bRdoForm = false;
          msg = elem.message;
          if (msg != null) {
            g_aMensajes[g_nErrores] = msg;
          } else {
            g_aMensajes[g_nErrores] = "<Error desconocido>";
          }
          if (bPasoAPaso) {
            bSeguir = false;
          }
          g_aErroneos[g_nErrores] = elem;
          g_nErrores++;
        } 
    }
  }
  return bRdoForm;
}

// Valida un elemento con atributo 'regexp', de momento no soportamos 'radio' ni 'checkbox'
function validarElementoRegExp(elem) {
  var tipo = elem.type;
  var valor = "<valor>";
  var regexp = elem.regexp;
  var bTipoOk = false;
  var bRdo = true;

  if (tipo == "text") {
    bTipoOk = true;
    valor = elem.value;
    bRdo = cumpleCondiciones(valor, regexp);
  }
  if (tipo == "select-one") {
    bTipoOk = true;
	if (elem.options.selectedIndex != -1)
    	valor = elem.options[elem.options.selectedIndex].value;
    else
		valor = "";
    bRdo = cumpleCondiciones(valor, regexp);
  }
  if (tipo == "select-multiple") {
    // Inicialmente TRUE. Si alguno seleccionado no cumple pasa a FALSE
    // Si no hay selecci&oacute;n, aplica la regla sobre una cadena en blanco
    bTipoOk = true;
    var i=0;
    var nOptions = elem.options.length;
    var bHaySeleccion = false;
    bRdo = true;
    for (i=0; (i<nOptions) && bRdo; i++) {
      var opt = elem.options[i];
      if (opt.selected) {
        bHaySeleccion = true;
        bRdo = cumpleCondiciones(opt.value, regexp);
      }
    }
    if (!bHaySeleccion)
      bRdo = cumpleCondiciones("", regexp);
  }
  if (tipo == "textarea") {
    bTipoOk = true;
    valor = elem.value;
    bRdo = cumpleCondiciones(valor, regexp);
  }
  if (tipo == "password") {
    bTipoOk = true;
    valor = elem.value;
    bRdo = cumpleCondiciones(valor, regexp);
  }
  
  if (!bTipoOk)
    alert("validador.js: Tipo '" + tipo + "' no soportado.");

  return bRdo;
}

function cumpleRegExp(value, exp) {
  var bCumple = false;
  var regexp = new RegExp(exp);
  bCumple = regexp.test(value);
  return bCumple;
}

function Presentar(szTexto) {
  if (g_fnPresentacion != null)
    g_fnPresentacion(g_aErroneos, g_aMensajes);
  else
    alert(szTexto);
}

function fechaCorrecta(day, month, year)
{
month = month - 1;  // pq los meses en Javascript van de 0-11
var temp = new Date(year, month, day);
if ( (year == temp.getFullYear()) && (month == temp.getMonth()) && (day == temp.getDate()) )
	return true;
else
	return false;
}

function cumpleCondiciones(valor, validador)
{
var regexpFecha = "^(0[1-9]|[1-2][0-9]|3[0-1])/(0[1-9]|1[0-2])/[0-9]{4}$";
var regexpEMail = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";
var regexpNumeroEntero = "^[+|-]?[\\d]+$";
var regexpNumeroDecimal = "^[+|-]?[\\d]+(,[\\d]+)?$";
var regexpNumeroEnteroSinSigno = "^[\\d]+$";
var regexpNumeroDecimalSinSigno = "^[\\d]+(,[\\d]+)?$";
var regexpCadenaSinEspacios = "^[\\S]+$";
var regexpCadena = "^.+$";
var regexpNIF = "^[\\d]{7,8}[A-Za-z]$";
var regexpCadenaSinNumerosNiEspacios = "^[^\\d^\\s]+$";
var regexpCadenaSinNumeros = "^[\\D]+$";
var regexpCadenaNoSoloEspacios = "\\S";
var regexpCodigoPostal = "^[\\d]{5}$";
var regexpTelefono = "^[6|9][\\d]{8}$";
var regexpVISA = "^[\\d]{16}$";
var regexpMasterCard = "^[\\d]{16}$";
var regexpCuentaCorriente = "^[\\d]{4}-[\\d]{4}-[\\d]{2}-[\\d]{10}$";
var resultado = true;

// Los m&aacute;s comunes al principio por eficiencia
if (validador == "_cadenaSinEspacios")
	resultado = cumpleRegExp(valor, regexpCadenaSinEspacios);
else if (validador == "_cadena")
	resultado = cumpleRegExp(valor, regexpCadena);
else if (validador == "_cadenaSinNumeros")
	resultado = cumpleRegExp(valor, regexpCadenaSinNumeros);
else if (validador == "_cadenaSinNumerosNiEspacios")
	resultado = cumpleRegExp(valor, regexpCadenaSinNumerosNiEspacios);
else if (validador == "_numeroEnteroSinSigno")
	resultado = cumpleRegExp(valor, regexpNumeroEnteroSinSigno);
else if (validador == "_email")
	resultado = cumpleRegExp(valor, regexpEMail);
else if (validador == "_NIF")
	resultado = cumpleRegExp(valor, regexpNIF) && NIFCorrecto(valor);
else if (validador == "_numeroEntero")
	resultado = cumpleRegExp(valor, regexpNumeroEntero);
else if (validador == "_fecha")
	resultado = cumpleRegExp(valor, regexpFecha) && fechaCorrecta(valor.substr(0,2), valor.substr(3,2), valor.substr(6,4));
else if (validador == "_cadenaNoSoloEspacios")
	resultado = cumpleRegExp(valor, regexpCadenaNoSoloEspacios);
else if (validador == "_codigoPostal")
	resultado = cumpleRegExp(valor, regexpCodigoPostal);
else if (validador == "_telefono")
	resultado = cumpleRegExp(valor, regexpTelefono);
else if (validador == "_VISA")
	resultado = cumpleRegExp(valor, regexpVISA) && esVISA(valor);
else if (validador == "_MasterCard")
	resultado = cumpleRegExp(valor, regexpMasterCard) && esMasterCard(valor);
else if (validador == "_cuentaCorriente")
	resultado = cumpleRegExp(valor, regexpCuentaCorriente) && esCuentaCorriente(valor);
else if (validador == "_numeroDecimal")
	resultado = cumpleRegExp(valor, regexpNumeroDecimal);
else if (validador == "_numeroDecimalSinSigno")
	resultado = cumpleRegExp(valor, regexpNumeroDecimalSinSigno);
else
	resultado = cumpleRegExp(valor, validador);
return resultado;
}

function validarTodoDeGolpe()
{
return validarPagina(false, true);
}

function validarTodo()
{
return validarPagina(true, true);
}

function validarUnForm(bForm)
{
return validarUnForm2(true, true, bForm);
}

function letraNIF(numNIF)
{
var letras = "TRWAGMYFPDXBNJZSQVHLCKE";
return letras.charAt(numNIF % 23);
}

function NIFCorrecto(NIF)
{
letraDelNIF = NIF.charAt(NIF.length - 1).toUpperCase();
var numDelNIF = NIF.substr(0, NIF.length - 1);
return letraDelNIF == letraNIF(numDelNIF);
}

function esTarjetaCredito(st) {
  if (st.length > 19)
    return (false);
  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) == 0)
    return (true);
  else
    return (false);
}

function esVISA(cc)
{
if (((cc.length == 16) || (cc.length == 13)) && (cc.substring(0,1) == 4))
	return esTarjetaCredito(cc);
return false;
}

function esMasterCard(cc)
{
firstdig = cc.substring(0,1);
seconddig = cc.substring(1,2);
if ((cc.length == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5)))
	return esTarjetaCredito(cc);
return false;
}

function calcularDC(Banco, Cuenta)
{
var pesos = new Array(6,3,7,9,10,5,8,4,2,1);
var result = '';
var iTemp = 0;
var n = 0;
for (n=0;n<=7;n++)
	iTemp  = iTemp + Banco.substr(7 - n, 1) * pesos[n];
result = 11 - (iTemp % 11);
if (result > 9)
	result = 1 - (result % 10);
iTemp = 0;
for (n=0;n<=9;n++)
	iTemp  = iTemp + Cuenta.substr(9 - n, 1) * pesos[n];
iTemp = 11 - (iTemp % 11);
if (iTemp > 9)
	iTemp = 1 - (iTemp % 10);
result = result * 10 + iTemp;
return(result);
}

function esCuentaCorriente(cuenta)
{
var banco = cuenta.substr(0,4) + cuenta.substr(5,4);
var numCuenta = cuenta.substr(13,10);
var DC = cuenta.substr(10,2);
return DC == calcularDC(banco, numCuenta);
}

function checkBrowser(){
	this.ver=navigator.appVersion
	this.dom=document.getElementById?1:0
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
	this.ie4=(document.all && !this.dom)?1:0;
	this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5)
	return this
}
bw=new checkBrowser()

function getObj (obj) {
	return obj=bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?document[obj]:0; 
}

function getEstiloObj (obj) {
	return obj=bw.dom?document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?document[obj]:0; 
}