function valor (id) {
  if (document.getElementById(id) != null) {
    return document.getElementById(id).value;
  } else {
    return null;//alert(id);
  }
}
function nada() {}
function ir(url) { document.location.href = url; }
function atras() { history.back(); }
function abrir (url) { window.open(url); }
function descargar (url) { window.open (url, null, 'width=10,height=10,scrollbars=no'); }
function enviar_formulario (id_formulario) { document.getElementById(id_formulario).submit(); }
function opcion (id_select) { 
  var objSelect = document.getElementById(id_select);
  if (objSelect) return objSelect.getElementsByTagName('option')[objSelect.selectedIndex].value;
}
function redondea (numero) {
 numero = new Number(str_replace(',', '.', numero));
 var con_decimales = numero.toFixed(2);
 con_decimales = con_decimales.toString();
 return con_decimales.replace ("\.", ',');
}

function toggle (id_capa) {
  var capa = document.getElementById(id_capa);
  if (capa) {
    var visible = capa.style.visibility;
    if (visible == 'visible') {
      capa.style.visibility = 'hidden';
      capa.style.display = 'none';
    } else {
      capa.style.visibility = 'visible';
      capa.style.display = 'block';
    }
  }
}

function comprobarNumerico (valor) {
  if (typeof(valor) != 'number') {
    alert ('Sólo se admiten valores numéricos para este dato.');
  }
}

function mostrar (id_capa) {
  var capa = document.getElementById(id_capa);
  if (capa) {
    capa.style.visibility = 'visible';
    capa.style.display = 'block';
  }
}

function ocultar (id_capa) {
  var capa = document.getElementById(id_capa);
  if (capa) {
    capa.style.visibility = 'hidden';
    capa.style.display = 'none';
  }
}

function habilitar (id_capa) {
  var capa = document.getElementById(id_capa);
  if (capa) {
    capa.disabled = "";
  }
}

function deshabilitar (id_capa) {
  var capa = document.getElementById(id_capa);
  if (capa) {
    capa.disabled = "disabled";
  }
}

function seleccionar_todos (contenedor, check) {
  var lista_inputs = document.getElementById(contenedor).getElementsByTagName('input');
  for (var i = 0; i<lista_inputs.length; i++) {
    if (lista_inputs[i].getAttribute('type') == 'checkbox') {
      lista_inputs[i].checked = check;
    }
  }
}

function lista_seleccionados (contenedor) {
  var seleccionados = new Array();
  var lista_inputs = document.getElementById(contenedor).getElementsByTagName('input');
  for (var i = 0; i<lista_inputs.length; i++) {
    if (lista_inputs[i].getAttribute('type') == 'checkbox' && lista_inputs[i].checked == true) {
      seleccionados[seleccionados.length] = lista_inputs[i].value;
    }
  }
  return seleccionados;
}

function opcion_radio (contenedor) {
  var lista_inputs = document.getElementById(contenedor).getElementsByTagName('input');
  for (var i = 0; i<lista_inputs.length; i++) {
    if (lista_inputs[i].getAttribute('type') == 'radio' && lista_inputs[i].checked) {
      return lista_inputs[i].value;
    }
  }
  return 0;
}

function ordenar (col) {
  if (col==document.getElementById('orden').value) {
    if (document.getElementById('sentido').value==0) document.getElementById('sentido').value=1;
    else if (document.getElementById('sentido').value==1) document.getElementById('sentido').value=0;
  } else {
    document.getElementById('orden').value=col;
    document.getElementById('sentido').value=0;
  }
  document.getElementById('formulario_busqueda').submit();
}

function borrar_seleccionados () {
  var confirmado = confirm ('Por favor, confirme que desea eliminar los elementos seleccionados.');
  if (confirmado) {
    document.getElementById('borrar').value='1';
    document.getElementById('formulario_busqueda').submit();
  }
}

function copia_campo(original, copia) { document.getElementById(copia).value = original.value; }

function copia_select(original, copia) { document.getElementById(copia).selectedIndex = original.selectedIndex; }

function numerico (valor) {
  cad = valor.toString();
  for (var i = 0; i < cad.length; i++) {
    var caracter = cad.charAt(i);
    if (caracter < '0' || caracter >'9')
     return false;
  }
  return true;
}

function obtenerDigito(valor) {
  valores = new Array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
  control = 0;
  for (i = 0; i <= 9; i++)
    control += parseInt(valor.charAt(i)) * valores[i];
  control = 11 - (control % 11);
  if (control == 11) control = 0;
  else if (control == 10) control = 1;
  return control;
}

function validar(f) {
  if (f.factu_banco.value == ""  || f.factu_sucursal.value == "" || f.factu_dc.value == "" || f.factu_cuenta.value == "")
    alert("Por favor, introduzca los datos de su cuenta");
  else {
    if (f.factu_banco.value.length != 4 || f.factu_sucursal.value.length != 4 || f.factu_dc.value.length != 2 || f.factu_cuenta.value.length != 10)
      alert("Por favor, introduzca correctamente los datos de su cuenta;" + " no están completos");
    else {
      if (!numerico(f.factu_banco.value) || !numerico(f.factu_sucursal.value) || !numerico(f.factu_dc.value) || !numerico(f.factu_cuenta.value))
        alert("Por favor, introduzca correctamente los datos de su " + "cuenta; no son numericos");
      else {
        if (!(obtenerDigito("00" + f.factu_banco.value + f.factu_sucursal.value) == parseInt(f.factu_dc.value.charAt(0))) || !(obtenerDigito(f.factu_cuenta.value) == parseInt(f.factu_dc.value.charAt(1))))
          alert("Los dígitos de control no se corresponden con los demás" + " números de la cuenta");
        else
          alert ("CCC correcto");
      }
    }
  }
}

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

function estiloBotones () {
  var botones = document.getElementsByTagName('input');
  for (var i = 0; i < botones.length; i++) {
    if (botones[i].type == 'button' || botones[i].type=='submit') {      
      botones[i].className = 'boton';
    }

  }
}

function obtenerDatos (contenedor) {
  var objsInput = document.getElementById(contenedor).getElementsByTagName('input');
  var objsSelect = document.getElementById(contenedor).getElementsByTagName('select');
  var datos = new Object();

  var numInput = objsInput.length;
  var i = 0;
  while (i < numInput) {
    objInput = objsInput[i];
    objNombre = objInput.id;
    objTipo = objInput.type;
    objValor  = objInput.value;
    if (objTipo != 'button') {
      datos[objNombre] = objValor;
    }
    i++;
  }

  var numSelect = objsSelect.length;
  i = 0;
  while (i < numSelect) {
    objSelect = objsSelect[i];
    objNombre = objSelect.id;
    opciones = objSelect.getElementsByTagName('option');
    opcionSeleccionada = objSelect.selectedIndex;
    if (opcionSeleccionada>=0) {
      objValor = opciones[opcionSeleccionada].value;
    } else {
      ObjValor = 0;
    }
    datos[objNombre] = objValor;
    i++;
  }

  return datos;
}

function str_replace(busca, repla, orig)
{
	str 	= new String(orig);

	rExp	= "/"+busca+"/g";
	rExp	= eval(rExp);
	newS	= String(repla);

	str = new String(str.replace(rExp, newS));

	return str;
}

function crearEvento (elemento, tipoEvento, funcion) {
  if (elemento.addEventListener) {
    elemento.addEventListener (tipoEvento, funcion, false);
  } else {
    elemento.attachEvent ('on' + tipoEvento, funcion);
  }
}