$(function() {
    //Date.useStrict = true;
    //$('#dg').datagrid('enableFilter');
    //document.getElementById("bd").style.visibility = "false";

    $('#sbTarjeta').switchbutton({
        checked: false,
        onChange: function(checked) {

            RecargoTarjeta(checked);
        }
    });

    $('#sbEnvio').switchbutton({
        checked: false,
        onChange: function(checked) {
            RecargoEnvio(checked);
        }
    });
    $('#sbArmado').switchbutton({
        checked: false,
        onChange: function(checked) {
            RecargoArmado(checked);
        }
    });
    $('#sbEscalera').switchbutton({
        checked: false,
        onChange: function(checked) {
            RecargoEscalera(checked);
        }
    });

    $('#tgdetalle').datagrid({
        singleSelect: true,
        checkOnSelect: true,
        selectOnCheck: true
    });
    $("#recargoTarjeta").val(0);
    $("#recargoEnvio").val(0);
    $("#recargoArmado").val(0);
    $("#recargoEscalera").val(0);

    $('#tgpa').datagrid({
        singleSelect: false,
        checkOnSelect: false,
        selectOnCheck: false
    });
    var pantalla = $('bd');
    pantalla.bind('onReady', function(e) {
        document.getElementById("bd").style.visibility = "true";
        easyloader.locale = 'de';
    });



    $('#cmbProducto').tooltip({
        position: 'right',
        content: '<span style="color:#fff">Elija un producto con sus adicionals y presione el boton suma para adicionarlo al contrato.</span>',
        onShow: function() {
            $(this).tooltip('tip').css({
                backgroundColor: '#666',
                borderColor: '#666'
            });
        }
    });




    //Configuracion de botones de accion
    $('#grabar').linkbutton('disable');
    var s = document.getElementById('bloqueado');
    s.value = 'SI';

    //Toma de Usuario logueado  <?php echo ($sesion->usuario_id); ?>';
    document.getElementById("usuario_id").value = 1;
    //teclas de flecha para navegar en la grilla
    var panel = $('#dg').datagrid('getPanel').attr('tabindex', 0).focus();
    panel.bind('keydown', function(e) {
        switch (e.keyCode) {
            case 38: // up
                selectRow(true);
                return false;
            case 40: // down
                selectRow(false);
                return false;
        }
    });

    var panel = $('dg_resultados').datagrid('getPanel').attr('tabindex', 0).focus();
    panel.bind('keydown', function(e) {
        switch (e.keyCode) {
            case 38: // up
                selectRowr(true);
                return false;
            case 40: // down
                selectRowr(false);
                return false;
        }
    });

    var panel = $('dg_eventos').datagrid('getPanel').attr('tabindex', 0).focus();
    panel.bind('keydown', function(e) {
        switch (e.keyCode) {
            case 38: // up
                selectRowe(true);
                return false;
            case 40: // down
                selectRowe(false);
                return false;
        }
    });



});




function selectRow(up) {
    var t = $('#dg');
    var count = t.datagrid('getRows').length; // row count
    var selected = t.datagrid('getSelected');
    if (selected) {
        var index = t.datagrid('getRowIndex', selected);
        index = index + (up ? -1 : 1);
        if (index < 0) index = 0;
        if (index >= count) index = count - 1;
        t.datagrid('clearSelections');
        t.datagrid('selectRow', index);
    } else {
        t.datagrid('selectRow', (up ? count - 1 : 0));
    }
}


function selectRowr(up) {
    var t = $('#dg_resultados');
    var count = t.datagrid('getRows').length; // row count
    var selected = t.datagrid('getSelected');
    if (selected) {
        var index = t.datagrid('getRowIndex', selected);
        index = index + (up ? -1 : 1);
        if (index < 0) index = 0;
        if (index >= count) index = count - 1;
        t.datagrid('clearSelections');
        t.datagrid('selectRow', index);
    } else {
        t.datagrid('selectRow', (up ? count - 1 : 0));
    }
}


function selectRowe(up) {
    var t = $('#dg_eventos');
    var count = t.datagrid('getRows').length; // row count
    var selected = t.datagrid('getSelected');
    if (selected) {
        var index = t.datagrid('getRowIndex', selected);
        index = index + (up ? -1 : 1);
        if (index < 0) index = 0;
        if (index >= count) index = count - 1;
        t.datagrid('clearSelections');
        t.datagrid('selectRow', index);
    } else {
        t.datagrid('selectRow', (up ? count - 1 : 0));
    }
}

//Cargar detalle en formulario a partir del id del registro seleccionado






$('#dg').datagrid({
    onSelect: function(rowIndex, rowData) {
        var row = $('#dg').datagrid('getSelected');
        cargar(row.id);
    }
});

$('#dg_resultados').datagrid({
    onSelect: function(rowIndex, rowData) {
        var row = $('#dg_resultados').datagrid('getSelected');
        //alert (row.id);
    }
});




$('#dg_eventos').datagrid({
    onSelect: function(rowIndex, rowData) {
        var row = $('#dg_eventos').datagrid('getSelected');

        //alert (row.id);
    }
});




/**
* @rutinas de conversion de fechas para insertar los datebox a la base de datos

*
* @author 	MP
* @version 	20141102
*/




// mascaras
$("#renspa").mask("99.999.9.99999/99");
$("#filtroRenspa").mask("99.999.9.99999/99");

$.mask.definitions["A"] = "[A-Z]";
$("#renapa").mask('A-99999');
$("#cov_cudap").mask('9999999/9999');





/**
* @Sentencias que setean los combos en modo autocomplete

*
* @author 	MP
* @version 	20141103
*/
$('#cmbtipoorigendestino').combobox({ selectOnNavigation: $(this).is(':checked') });
$('#daad_centro_regional_id').combobox({ selectOnNavigation: $(this).is(':checked') });
$('#dil_producto_id').combobox({ selectOnNavigation: $(this).is(':checked') });
$('#dil_matriz_id').combobox({ selectOnNavigation: $(this).is(':checked') });
$('#dil_conjunto_id').combobox({ selectOnNavigation: $(this).is(':checked') });
$('#dil_laboratorio_id').combobox({ selectOnNavigation: $(this).is(':checked') });
$('#cmbestablecimiento').combobox({ selectOnNavigation: $(this).is(':checked') });
$('#cmbprovinciaestablecimiento').combobox({ selectOnNavigation: $(this).is(':checked') });
$('#cmblocalidadestablecimiento').combobox({ selectOnNavigation: $(this).is(':checked') });

$('#cmbprovinciaproductor').combobox({ selectOnNavigation: $(this).is(':checked') });
$('#cmblocalidadproductor').combobox({ selectOnNavigation: $(this).is(':checked') });
$('#cmbproductordestino').combobox({ selectOnNavigation: $(this).is(':checked') });
$('#cmbpaisorigen').combobox({ selectOnNavigation: $(this).is(':checked') });

$('#pp1').tooltip({
    position: 'right',
    content: '<span style="color:#fff">Haga dobleclick para cambiar fecha de cumplimentado y alguna observacion. Haga click en otro lugar de la grilla para fijar el cambio</span>',
    onShow: function() {
        $(this).tooltip('tip').css({
            backgroundColor: '#666',
            borderColor: '#666'
        });
    }
});

$.extend($.fn.validatebox.defaults.rules, {
    isNumber: {
        validator: function(value, param) {
            return /^-?\d+\.?\d*$/.test(value);
        },
        message: 'Por favor ingrese un número válido!'
    }
});