﻿var x;
x=$(document);
x.ready(inicializarLoad);

function inicializarLoad(){
    //$("#tablaDemo").tableSorter();    
	 //var t = new ScrollableTable(document.getElementById('myScrollTable'), 100);
	//$("#tablaDemo").Scrollable(300, 900); //este es el que funciona con php	  	

	$('#btnCargar').click(function(e){
		CargarGrilla();								  
	});
	
	$('#btnEliminar').click(function(e){
		$("#tablaDemo").find("tr:gt(0)").remove();						 
	});

	$('#btnCerrar').click(function(e){
		$('#mask').toggle("slow");
		$('#blocker').toggle("slow");
		$('#btnCargar').attr("disabled", false);
	});
	
	
	$('#btnNuevo').click(function(e){
		$('#blocker').css({'width':$(document).width(),'height':$(document).height()});
		$('#blocker').fadeTo("slow",0.4);
		
		$('#mask').css({'width':'50%','height':'50%'});
		$('#mask').css({'left':'27%', 'top':'20%'});
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
		
		$("#txtId").attr("value", "");
		$("#txtNombre").attr("value", "");
		$("#txtApellido").attr("value", "");
		$("#txtAnos").attr("value", "");
		$("#txtTotal").attr("value", "");
		$("#txtDescuento").attr("value", "");
		$("#txtDiferencia").attr("value", "");
		
		$("#carga").fadeOut("slow"); 
	});
	
	$('#btnAceptar').click(function(e){
		var id = document.getElementById('txtId').value;
		var nom = document.getElementById('txtnombre').value;
		var ape = document.getElementById('txtApellido').value;
		var ano = document.getElementById('txtAnos').value;
		var tot = document.getElementById('txtTotal').value;
		var desc = document.getElementById('txtDescuento').value;
		var dife = document.getElementById('txtDiferencia').value;

		var EnvioDatos = {id:id, nombre:nom, apellido:ape, anos:ano, total:tot, descuento:desc, diferencia:dife};
    	$.ajax({
           async:true,
           type: "GET",
           //dataType: "xml",
		   dataType: "html",
           contentType: "application/json; charset=utf-8",
           url:"../DataAccess/Grilla_Actualizar.php",
           data: EnvioDatos, 
           beforeSend:inicioEnvio,
           success:Recepcion_Actualizacion,
           timeout:100000,
           error:problemas
	    });		
	});
}

function Recepcion_Actualizacion(dato){
	$("#carga").text(dato);
	$('#btnCargar').attr("disabled", false);
	$("#carga").fadeOut(4000);
	$('#mask').toggle("slow");
	$('#blocker').toggle("slow");
	$("#ContenedorTable").load("../DataAccess/Grilla_Consulta_Load.php") ;
	$.ajaxSetup({ cache: false });
	
	//load([nombre de la página],[parámetros],[funcion final])

}

function CargarGrilla(){
    var EnvioDatos = {modo:'todos', id:0};
    $.ajax({
           async:true,
           type: "GET",
           //dataType: "xml",
		   dataType: "html",
           contentType: "application/json; charset=utf-8",
           url:"../DataAccess/Grilla_Consulta.php",
           data: EnvioDatos, 
           beforeSend:inicioEnvio,
           success:RecepcionDatos,
           timeout:100000,
           error:problemas
    }); 
}

function inicioEnvio()
{  
    $("#carga").show();
    $("#btnCargar").attr("disabled", true);
    var x=$("#carga");
    x.html('<img src="Imagenes/blue-loading.gif">');
}

function problemas()
{
    $("#carga").text('Problemas en el servidor');
	 $("#btnCargar").attr("disabled", false);
}

function RecepcionDatos(dat)
{
	var datos = StringtoXML(dat);
    var fila = "";
    
	$('datosPersonales', datos).each(function(i){
		var id = $(this).find("id").text();
		var nombre = $(this).find("nombre").text();
		var apellido = $(this).find("apellido").text();
		var ano = $(this).find("anos").text();
		var total = $(this).find("total").text();
		var descuento = $(this).find("descuento").text();
		var diferencia = $(this).find("diferencia").text();
		var idFila = id;
		
		//String(numero);
		
		fila += "<tr id="+idFila+" onmouseover='cambiaColor(this);' style='background-color:#FFFFFF' onmouseout='VolverColor(this);'>";
		fila += "<td>" + nombre + "</td>" +
				"<td>" + apellido + "</td>" +
				"<td>" + ano + "</td>" +
				"<td>" + total + "</td>" +
				"<td>" + descuento + "</td>" +
				"<td>" + diferencia +  "</td>" +
				"<td align='center'>" + 
					"<img onclick='ActualizarPopul("+ id +")' style='cursor:pointer;' alt='Modificar' src='../Imagenes/mantencion.png'  /> &nbsp;&nbsp;"+
					"<img onclick='EliminarRegistro("+ id +")' style='cursor:pointer;' alt='Eliminar' src='../Imagenes/Eliminar.png'  />" + 
				"</td>";
				
		fila += "</tr>";
	});
	
	//$("#info").html(tabla);
	$("#tablaDemo").append(fila);
	$("#btnCargar").attr("disabled", false);
	$("#carga").fadeOut("slow");  

}

function StringtoXML(text){
	if (window.ActiveXObject){
		var doc=new ActiveXObject('Microsoft.XMLDOM');
		doc.async='false';
		doc.loadXML(text);
	} else {
		var parser=new DOMParser();
		var doc=parser.parseFromString(text,'text/xml');
	}
	return doc;
}


function actualiza(){
}

function ordenarGrilla(columna){
    $("#cargaGrilla").show();
    var x=$("#cargaGrilla");
    x.html('<img src="Imagenes/grid-loading.gif">');

    $("#tablaDemo").tablesorter({
		sortColumn: columna.value	//Índice de la columna o nombre (en minúsculas) que hemos dado a la columna, para ordenar.
	});

    $("#cargaGrilla").fadeOut("slow");
}

function cambiaColor(ids){
	//$('#ids').css({'background':'#e6EEEE'});
	
	//$(ids).css({'background-color':'#FFFFFF'});
	
	var celda = document.getElementById('prueba'); 
	ids.style.background = "#CCCCCC";
	
}

function VolverColor(ids){
	ids.style.background = "#FFFFFF";
}

function ActualizarPopul(ids){
	var EnvioDatos = {modo:'solo', id:ids};
    $.ajax({
           async:true,	
           type: "GET",
           //dataType: "xml",
		   dataType: "html",
           contentType: "application/json; charset=utf-8",
           url:"../DataAccess/Grilla_Consulta.php",
           data: EnvioDatos, 
           beforeSend:inicioEnvio,
           success:Recepcion_ActualizarModalPopul,
           timeout:100000,
           error:problemas
    }); 
}

function Recepcion_ActualizarModalPopul(dato){

        var maskHeight = $(document).height()-200;
        var maskWidth = $(document).width()-200;
		
		//$(document).css({'background-color':'#000000'});
		$("#document").attr("disabled", true);
		
		
		$('#blocker').css({'width':$(document).width(),'height':$(document).height()});
		$('#blocker').fadeTo("slow",0.4);

        $('#mask').css({'width':'50%','height':'50%'});
	    $('#mask').css({'left':'27%', 'top':'20%'});
    	
        $('#mask').fadeIn(1000);	
        $('#mask').fadeTo("slow",0.8);	
		
		var datos = StringtoXML(dato);
		$('datosPersonales', datos).each(function(i){												  
			var id = $(this).find("id").text()
			var nombre = $(this).find("nombre").text()
			var apellido = $(this).find("apellido").text()
			var ano = $(this).find("anos").text();
			var total = $(this).find("total").text();
			var descuento = $(this).find("descuento").text();
			var diferencia = $(this).find("diferencia").text();
			
			$("#txtId").attr("value", id);
			$("#txtNombre").attr("value", nombre);
			$("#txtApellido").attr("value", apellido);
			$("#txtAnos").attr("value", ano);
			$("#txtTotal").attr("value", total);
			$("#txtDescuento").attr("value", descuento);
			$("#txtDiferencia").attr("value", diferencia);

		});
		
		$("#carga").fadeOut("slow"); 
		
		
}

function EliminarRegistro(dato){
	var respuesta = confirm("Esta seguro de eliminar a: "+ dato);	
	if(respuesta == true){
		var EnvioDatos = {id:dato};
		$.ajax({
			   async:true,
			   type: "GET",
			   //dataType: "xml",
			   dataType: "html",
			   contentType: "application/json; charset=utf-8",
			   url:"../DataAccess/Grilla_Eliminar.php",
			   data: EnvioDatos, 
			   beforeSend:inicioEnvio,
			   success:RecepcionEliminacion,
			   timeout:100000,
			   error:problemas
		}); 
	}
}

function RecepcionEliminacion(respu){
	$("#carga").text("Registro :: "+ respu + " :: Eliminado");
	$("#carga").fadeOut(4000);	
	$("#btnCargar").attr("disabled", false);
	$("#ContenedorTable").load("../DataAccess/Grilla_Consulta_Load.php") ;
	$.ajaxSetup({ cache: false });
}

