var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;
var peticion_httpa=new Array();
function isset(variable_name) {
    try {
         if (typeof(eval(variable_name)) != 'undefined')
         if (eval(variable_name) != null)
         return true;
     } catch(e) { }
    return false;
   }
   
function inicializa_xhr() {
	if (window.XMLHttpRequest)
	{
	// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
	// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}
function printPage(){
	window.print();
}


function ajustarFecha(fecha){
var ajustar=document.getElementById(fecha).value;
var resultado;
var dia="";
var mes="";
var ano="";
var pointer=0;
var numerodigitos=0;
var miFecha = new Date();
for (i=0;i<ajustar.length;i++){
	if (ajustar.charAt(i)==" " || ajustar.charAt(i)=="/" ||ajustar.charAt(i) =="-"){
		pointer=pointer+1;

		numerodigitos=0;
	
	}
	else{
		if (ajustar.charAt(i)>=0 && ajustar.charAt(i)<=9){
			if (pointer==0)
				dia=dia+ajustar.charAt(i);
			if (pointer==1)
				mes=mes+ajustar.charAt(i);
			if (pointer==2)
				ano=ano+ajustar.charAt(i);
			numerodigitos++;

			if (numerodigitos>2){
	
				return true;
			}
		}
	}
}
if (dia.length==1)
	dia="0"+dia;
if (mes.length==0){
	mes=miFecha.getMonth();
	mes=mes+1;
	if (mes<10)
		mes="0"+mes;
}
if (mes.length==1)
	mes="0"+mes;

if (ano.length==0)
	ano=miFecha.getFullYear();
if (ano.length==1){
	var ano2="0"+miFecha.getFullYear();
	ano2=ano2.charAt(3);
	ano="20"+ano2+ano;
}
if (ano.length==2)
	ano="20"+ano;
document.getElementById(fecha).value= dia+"/"+mes+"/"+ano;
	return true;

	

}	
function muestraAyuda(){
	if (document.getElementById("divayuda").style.display=="none")
		document.getElementById("divayuda").style.display='';
	else
		document.getElementById("divayuda").style.display='none';
}
function getStyle(el,styleProp)
{
var x = document.getElementById(el);
if (x.currentStyle)
var y = x.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
return y;
}
function imprimir(Parametro)
{

var bName = navigator.appName;
var bVer = parseFloat(navigator.appVersion);

var contenido = document.getElementById(Parametro).innerHTML;
ventana=window.open("/print.php","ventana","width=560");
ventana.document.open();
ventana.document.write('<html><head><title>Versión para imprimir</title><link rel="stylesheet" type="text/css" href="estilos/estilogeneral.css"><\/head><body style=\"background-color: #FFFFFF\"><script>function imprimir(param){window.print();}</script>');

ventana.document.write(contenido);
ventana.document.close();


ventana.focus();
ventana.print();

}
function imprimir3(Parametro)
{

var bName = navigator.appName;
var bVer = parseFloat(navigator.appVersion);

var contenido = document.getElementById(Parametro).innerHTML;
ventana=window.open("/print.php","ventana","width=560");
ventana.document.open();
ventana.document.write('<html><head><title>Versión para imprimir</title><link rel="stylesheet" type="text/css" href="estilos/estilogeneral.css"><\/head><body style=\"background-color: #FFFFFF;font-size:12px\"><script>function imprimir(param){window.print();}</script>');

ventana.document.write(contenido);

ventana.document.write(contenido);
ventana.document.close();


ventana.focus();
ventana.print();

}
function imprimir2(Parametro)
{

var bName = navigator.appName;
var bVer = parseFloat(navigator.appVersion);

var contenido = document.getElementById(Parametro).innerHTML;
ventana=window.open("/print.php","ventana","width=560");
ventana.document.open();
ventana.document.write('<html><head><title>Versión para imprimir</title><link rel="stylesheet" type="text/css" href="estilos/estilogeneral.css"><\/head><body style=\"background-color: #FFFFFF\"><div style=\"width:90%\">');

ventana.document.write(contenido);
ventana.document.write('</div>');
ventana.document.close();


ventana.focus();
ventana.print();
ventana.close();
}
function llamadaAjax(page,params, functionCallBack,denom) {
	var total=Math.round(Math.random() * 1000);
	peticion_httpa[total] = inicializa_xhr();
	if(peticion_httpa[total]) {
		peticion_httpa[total].open("POST", page, true);
		peticion_httpa[total].onreadystatechange = function (){respuestaAjax(total,functionCallBack,params)};
		peticion_httpa[total].setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
		if (denom!="" && params!=""){
			var query_string = denom+"="+ encodeURIComponent(params) +"&nocache=" + Math.random();		
		}
		if (denom=="" && is_array(params)){
			var query_string="";
			for (var x in params){
				query_string=query_string+x+"="+params[x]+"&";
			}
			query_string=query_string+"nocache=" + Math.random();	
		}else
		if (params==""){
			var query_string = "nocache=" + Math.random();			
		}
		peticion_httpa[total].send(query_string);
	}	
}		
function respuestaAjax(total,functionCallBack,params){
	if(peticion_httpa[total].readyState == READY_STATE_COMPLETE) {
		if(peticion_httpa[total].status == 200) {
			functionCallBack(params,peticion_httpa[total].responseText);
		}
		if (peticion_httpa[total].status == 500 || peticion_httpa[total].status == 404){
			alert("Ha ocurrido un error en las transacciones, intentelo de nuevo");
		}
	}
}
function h(item){
	document.getElementById(item).style.display="none";
}
function s(item){
	document.getElementById(item).style.display="block";
}
function c(item){
	if (document.getElementById(item).value!=null)
		return document.getElementById(item).value;
	else
		return document.getElementById(item).innerHTML;		
		
}
function v(item,valor){
	if (document.getElementById(item).value!=null)
		document.getElementById(item).value=valor;
	else
		document.getElementById(item).innerHTML=valor;		
		
}
function e(item){
	if (document.getElementById(item)!=null)
		return true;
	else
		return false;
}
function number_format(num)
{    
    var n = num.toString();
    var nums = n.split('.');
    var newNum = "";
    if (nums.length > 1)
    {
        var dec = nums[1].substring(0,2);
        newNum = nums[0] + "." + dec;
    }
    else
    {
    newNum = num;
    }
    return newNum;
}
function isNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}
function lanzarPasarela(tipo){
	if (tipo==1)
		 document.getElementById("pasarela").submit();
	if (tipo==5)
		 document.getElementById("pasarela").submit();
}
function volver(paginao,paginam){
	s(paginam);
	h(paginao);
}
function is_array(input){
    return typeof(input)=='object'&&(input instanceof Array);
  }
function ValidaFormulario()
{
	vale=true;
	if (document.getElementById("nombre").value == ""){
		document.getElementById("nombre").className="input2";
		vale=false;
	}
	else{
		document.getElementById("nombre").className="input1";
	}
	if (document.getElementById("apellidos").value == ""){
		document.getElementById("apellidos").className="input2";
		vale=false;
	}
	else{
		document.getElementById("apellidos").className="input1";
	}
	if (document.getElementById("localidad").value == ""){
		document.getElementById("localidad").className="input2";
		vale=false;
	}
	else{
		document.getElementById("localidad").className="input1";
	}
	if (document.getElementById("direccion").value == ""){
		document.getElementById("direccion").className="input2";
		vale=false;
	}
	else{
		document.getElementById("direccion").className="input1";
	}
	if (document.getElementById("provincia").value == ""){
		document.getElementById("provincia").className="input2";
		vale=false;
	}
	else{
		document.getElementById("provincia").className="input1";
	}
	if (document.getElementById("telefono").value == ""){
		document.getElementById("telefono").className="input2";
		vale=false;
	}
	else{
		var telefono=new String(document.getElementById("telefono").value);
		var contador=0;
		var valido=1;
		for (var i=0;i<telefono.length;i++){
		
			if ((telefono.charCodeAt(i) >= 48) && (telefono.charCodeAt(i)<=57))
				contador++;
			else
				if (telefono.charCodeAt(i)!=32)
					valido=0;
				
		}

		if (valido==1 && contador >8){
			document.getElementById("telefono").className="input1";		
		}
		else{
			document.getElementById("telefono").className="input2";
			vale=false;
		}
	}
	if (document.getElementById("cp").value == ""){
		document.getElementById("cp").className="input2";
		vale=false;
	}
	else{
		var telefono=new String(document.getElementById("cp").value);
		var contador=0;
		var valido=1;
		for (var i=0;i<telefono.length;i++){
		
			if ((telefono.charCodeAt(i) >= 48) && (telefono.charCodeAt(i)<=57))
				contador++;
			else
					valido=0;
				
		}

		if (valido==1 && contador <7 && contador >4){
			document.getElementById("cp").className="input1";		
		}
		else{
			document.getElementById("cp").className="input2";
			vale=false;
		}
	}
	if (document.getElementById("cp").value == ""){
		document.getElementById("cp").className="input2";
		vale=false;
	}
	else{
		var telefono=new String(document.getElementById("cp").value);
		var contador=0;
		var valido=1;
		for (var i=0;i<telefono.length;i++){
		
			if ((telefono.charCodeAt(i) >= 48) && (telefono.charCodeAt(i)<=57))
				contador++;
			else
					valido=0;
				
		}

		if (valido==1 && contador <7 && contador >4){
			document.getElementById("cp").className="input1";		
		}
		else{
			document.getElementById("cp").className="input2";
			vale=false;
		}
	}
	if (document.getElementById("fnac1").value!="" && document.getElementById("fnac2").value!="" && document.getElementById("fnac3").value!=""){
		if (ValidarFecha(document.getElementById("fnac1").value,document.getElementById("fnac2").value,document.getElementById("fnac3").value)==1){
			document.getElementById("fnac3").className="input1";
			document.getElementById("fnac1").className="input1";
			document.getElementById("fnac2").className="input1";
		}
		else{
			document.getElementById("fnac3").className="input2";
			document.getElementById("fnac1").className="input2";
			document.getElementById("fnac2").className="input2";
			vale=false;
		}
	}
	else{
		document.getElementById("fnac3").className="input2";
		document.getElementById("fnac1").className="input2";
		document.getElementById("fnac2").className="input2";
		vale=false;
	}

	if (document.getElementById("dni").value != ""){
		var telefono=new String(document.getElementById("dni").value);
		if (telefono.length>7){
			document.getElementById("dni").className="input1";		
		}
		else{
			document.getElementById("dni").className="input2";
			vale=false;
		}
	}
	if (document.getElementById("pass1")!=null){
		document.getElementById("pass1").className="input1";
		document.getElementById("pass2").className="input1";
		if (document.getElementById("pass1").value==""){
			document.getElementById("pass1").className="input2";
			vale=false;
		}
		if (document.getElementById("pass2").value==""){
			document.getElementById("pass2").className="input2";
			vale=false;
		}
		if (document.getElementById("pass2").value!=document.getElementById("pass1").value){
			vale=false;
			document.getElementById("pass1").className="input2";
			document.getElementById("pass2").className="input2";
		}
			
	}
		
	if (vale==false){
		document.getElementById("textoalerta").style.display="";
		
	}
	return vale;
		
}

function ValidarFecha(dia,mes,ano){

	var RealFecha= new Date();	// Para sacar la fecha de hoy
	// Cadena Año
	var Ano= new String(ano);
	// Cadena Mes
	var Mes= new String(mes);
	// Cadena Día
	var Dia= new String(dia);

	// Valido el año
	if (isNaN(Ano) || Ano.length<4 || parseFloat(Ano)<1900){
		return 0;
	}
	// Valido el Mes
	if (isNaN(Mes) || parseFloat(Mes)<1 || parseFloat(Mes)>12){
		return 0;
	}
	// Valido el Dia
	if (isNaN(Dia) || parseInt(Dia, 10)<1 || parseInt(Dia, 10)>31){
		return 0;
	}
	if (Mes==4 || Mes==6 || Mes==9 || Mes==11 || Mes==2) {
		if (Mes==2 && Dia > 28 || Dia>30) {
			return 0;
		}
	}
	
  //para que envie los datos, quitar las  2 lineas siguientes
  return 1;

}
function validaLibreta(i_entidad,i_oficina,i_digito,i_cuenta){
// VALIDACIÓN DE CUALQUIER LIBRETA DE CUALQUIER ENTIDAD BANCARIA.
// Funcion recibe como parámetro la entidad, la oficina,
// el digito (concatenación del de control entidad-oficina y del de control entidad)
// y la cuenta. Espera los valores con 0's a la izquierda.
// Devuelve true o false.
// NOTAS:
// Formato deseado de los parámetros:
// - i_entidad (4)
// - i_oficina (4)
// - i_digito (2)
// - i_cuenta (10)
var wtotal,wcociente, wresto;
if (i_entidad.length != 4){
return false;
}
if (i_oficina.length != 4){
return false;
}
if (i_digito.length != 2){
return false;
}
if (i_cuenta.length != 10){
return false;
}
wtotal = i_entidad.charAt(0) * 4;
wtotal += i_entidad.charAt(1) * 8;
wtotal += i_entidad.charAt(2) * 5;
wtotal += i_entidad.charAt(3) * 10;
wtotal += i_oficina.charAt(0) * 9;
wtotal += i_oficina.charAt(1) * 7;
wtotal += i_oficina.charAt(2) * 3;
wtotal += i_oficina.charAt(3) * 6;
// busco el resto de dividir wtotal entre 11
wcociente = Math.floor(wtotal / 11);
wresto = wtotal - (wcociente * 11);
//
wtotal = 11 - wresto;
if (wtotal == 11){
wtotal=0;
}
if (wtotal == 10){
wtotal=1;
}
if (wtotal != i_digito.charAt(0)){
return false;
}
//hemos validado la entidad y oficina
//-----------------------------------
wtotal = i_cuenta.charAt(0) * 1;
wtotal += i_cuenta.charAt(1) * 2;
wtotal += i_cuenta.charAt(2) * 4;
wtotal += i_cuenta.charAt(3) * 8;
wtotal += i_cuenta.charAt(4) * 5;
wtotal += i_cuenta.charAt(5) * 10;
wtotal += i_cuenta.charAt(6) * 9;
wtotal += i_cuenta.charAt(7) * 7;
wtotal += i_cuenta.charAt(8) * 3;
wtotal += i_cuenta.charAt(9) * 6;

// busco el resto de dividir wtotal entre 11
wcociente = Math.floor(wtotal / 11);
wresto = wtotal - (wcociente * 11);
//
wtotal = 11 - wresto;
if (wtotal == 11){wtotal=0;}
if (wtotal == 10){wtotal=1;}

if (wtotal != i_digito.charAt(1)){
//alert(wtotal+' y no '+i_digito.charAt(1));
return false;
}
// hemos validado la cuenta corriente

return true;
}
function borrarFila(obj) {
	var i=obj.rowIndex;
	var tab = obj.parentNode;
	tab.deleteRow(i);

}
