// Inicializar
var tabbar = null;
var informe = null;
var alerta  = "..";

$(document).ready(function(){
    try{
	   // Eventos
	   $("#txtIdentificacion").keypress(function(event){if(event.keyCode==13)consultar();else return true;});	   
	   
       jsonrpc = new JSONRpcClient("/PagosTesoreriaDistrital/JSON-RPC");
	      
	   document.getElementById("txtIdentificacion").focus();	   	   
    }
	catch(e){
       alert("Cargar Objetos() [ "+e.name + " - "+e.message+" ]");
    }
});
//---------------------------------------------------
function validarDatos(){
    try{
		var campos = new Array();
		var i=-1;
						
		// validación sintactica
		campos[++i] = new Array($("#txtIdentificacion"), "Identificacion", $("#txtIdentificacion").val(), "TEXTO");
		
		sintaxsis = validar(campos);     
		
		return sintaxsis;
	}
	catch(e){
	    alert("Excencion: Metodo validarDatos() [ "+e.name + " - "+e.message+" ]");
	}
}
//---------------------------------------------------
function consultar(){
    
    try{
        //if($("#txtIdentificacion").val() != '8784052'){
		if(validarDatos()){
			ponerIndicador("IndicadorConsulta", "Procesando...");
			
			var hashMap = new Object();
			var datos   = new Object();
	
			datos["tercero"]        = $("#lstTipoIdentificacion").val()+" "+$("#txtIdentificacion").val();
			datos["numeroCuentas"]  = document.getElementById("Ultima").checked ? 1 : document.getElementById("lstNumeroCuenta").value;
	
			hashMap["javaClass"]    = "java.util.HashMap";
			hashMap["map"]          = datos;     

            switch($("#txtIdentificacion").val()){
			   case "45554777*": 
			        alerta = "<img src='Imagenes/ids_emoticon_rose.gif'><a href=\"javascript:mensaje()\" style='text-decoration:none'>&nbsp;&nbsp;click aqui</a>";
			   break;
			   
			   default:alerta = "";break;
			}			
	
			jsonrpc.Buscador.getInformeCuentasTercero(resultadoConsulta, hashMap);
		}
         /*}else{
                 alert("Señor usuario, consulte una sola vez por dia.\n El numero reiterado de consultas no acelerara su pago !!");
          }*/
	}catch(e){
	    alert("Excepcion: Metodo consultar() [ "+e.name + " - "+e.message+" ]");
	}

    //alert("por favor intente mas tarde el sistemas esta actualizando sus datos");
}
//---------------------------------------------------
function mensaje(){
   alert("Puedes ver este mensaje ?\n\nES PORQUE UNO DE NUESTROS INGENIEROS, TE PIENSA CUANDO TRABAJA");
}
//---------------------------------------------------
function resultadoConsulta(i, exception){
    
	quitarIdicador("IndicadorConsulta");
	
	informe = i;
	
	if(informe.map["Operacion"] == "OK"){
	    if(informe.map["existenDatos"]){
            if( tabbar == null ){            
			   tabbar = new dhtmlXTabBar("tabEventos", "top");	 
			   tabbar.setSkin('dhx_skyblue');
			   tabbar.setImagePath("Recursos/libScript/dhtmlxTabbar/codebase/imgs/");
			   tabbar.attachEvent("onSelect", detalleCuenta);
			   
			   $("#CeldaLinea").css('background-color', '#ffffff');
			}			
		    crearTabsCuentas( informe );		   	
		}else{
		   limpiarDatos();
		   if( tabbar != null) tabbar.clearAll();		   		   
		   tabbar = null;
		   $("#CeldaLinea").css('background-color', '#D4E7FF');
		   alert("No existen datos");
		}
	}else{
        alert(informe.map["mensaje"]);	
	}
}
//---------------------------------------------------
function crearTabsCuentas( informeEventos ){
     try{
		 var listaCuentas = informeEventos.map["listaCuentas"].split(';');
		
		 tabbar.clearAll();
		
		 for(i=0; i < listaCuentas.length; i ++){
		    tabbar.addTab(i, "&nbsp;&nbsp;"+listaCuentas[i]+"&nbsp;&nbsp;", "*");
		    var cuenta = informeEventos.map["C"+i];
		    
		    var grid = tabbar.cells(i).attachGrid();
		    grid.setImagePath("Recursos/libScript/dhtmlxGrid/codebase/imgs/");		
			grid.enableMultiline(true);	
            grid.enableTooltips("false,false,false");				
		    grid.parse(cuenta.eventos);
		 }
		 
		 tabbar.setTabActive(0);
		 
	}catch(e){
	    alert("Excepcion: Metodo crearTabsCuentas() [ "+e.name + " - "+e.message+" ]");
	}
}
//---------------------------------------------------
function detalleCuenta(index){
    var cuenta = informe.map["C"+index];
		   
	$("#txtNombres").html( cuenta.tercero+"&nbsp;  "+alerta );
	$("#txtTipoCuenta").html( cuenta.tipoCuenta );
	$("#txtPeriodo").html( cuenta.periodo );
	$("#txtVigencia").html( cuenta.vigencia );
	
	return true;		
}
//---------------------------------------------------
function limpiarDatos(){
    $("#listaCuentas").html("");
	$("#txtNombres").html("");
	$("#txtTipoCuenta").html("");
	//$("#txtIdentificacion").val("");
	$("#txtPeriodo").html("");
	$("#txtVigencia").html("");
		   
	$("#tabEventos").html("");
	
	document.getElementById("txtIdentificacion").focus();
	
	//clearTimeout(timerID);
}
//---------------------------------------------------
function abrirTeclado(){
   var estado = document.getElementById("chEstadoTeclado").checked;
   
   document.getElementById("chEstadoTeclado").checked   = !estado;
   document.getElementById("dteclado").style.visibility = !estado ? "visible":"hidden";
}
//---------------------------------------------------
function ponerTipoIdentificacion(i){
   document.getElementById("lstTipoIdentificacion").selectedIndex = i;
}
//---------------------------------------------------
function ponerNumero(n){
   var valorActual = document.getElementById("txtIdentificacion").value;
   document.getElementById("txtIdentificacion").value = valorActual+n;
}
//---------------------------------------------------
function quitarNumero(){
   var valorActual = document.getElementById("txtIdentificacion").value;
   document.getElementById("txtIdentificacion").value = valorActual.substr(0, valorActual.length-1);
}

//---------------------------------------------------
function abrirReporte(){
   window.print();
}
//---------------------------------------------------
function exportarPdf(){
    alert("Exportar Pdf");
}
//---------------------------------------------------
function exportarTxt(){
     alert("Exportar Txt");
}
