<table id="jqGrid"></table>
<div id="jqGridPager"></div>
<script>
findControl = function ()
{
    strFormSerialize = $( 'form' ).serialize();
    relaodGrid( strFormSerialize );
}

relaodGrid = function( s_url_param )
{
    jQuery( '#jqGrid' ).jqGrid( 'clearGridData' );
    jQuery( '#jqGrid' ).jqGrid( 'setGridParam', { url: '<?=buildUrl( "pro/gridCotizacion" )?>?' + s_url_param } );
    jQuery( '#jqGrid' ).trigger( 'reloadGrid' );
}
agregarDetalle = function( IdItem, Descripcion )
{
    parent.asignarItem( IdItem, Descripcion );
    parent.closeWindow();
}

asignarCotizacion = function( OrdenId, OrdenNum )
{
    if( $( '#CheckAnticipo' ).is( ':checked' ) ) 
    {
        pr( "esta" );
    	parent.asignarCotizacion( OrdenId, OrdenNum, 1 );
    }
    else
    {
    	pr( "no esta" );
    	parent.asignarCotizacion( OrdenId, OrdenNum, 0 );
    }
}
</script>
<script>
    var jqGridContent = $( "#jqGrid" );
    jqGridContent.jqGrid
    (
        {
            url         : '<?=buildUrl( 'oc/nothing' )?>',
            mtype       : "POST",
            datatype    : "json",
            colNames    : [ 'OrdenId', 'Codigo', 'Cliente', 'Tipo Doc', 'Documento', 'Fecha', 'Estado', 'Moneda', 'Total', '' ],
            colModel    : 
                          [
                            { name: 'OrdenId',          width: 0, align: 'center', hidden: true, hidedlg: false, editrules : { required: true }, key: true },
                            { name: 'OrdenNum',         width: 90, align: 'center', editable: false, sortable: false },
                            { name: 'ClienteNombre',    width: 200, align: 'left', editable: false, sortable: false },
                            { name: 'ClienteDocCod',    width: 60, align: 'center', editable: false, sortable: false },
                            { name: 'ClienteDocNum',    width: 90, align: 'center', editable: false, sortable: false },
                            { name: 'OrdenFecha',       width: 90, align: 'center', editable: false, sortable: false, formatter: 'date', formatoptions: { newformat: 'Y-m-d' } },
                            { name: 'OrdenEstadoDesc',  width: 90,  align: 'center', editable: true, sortable: false },
                            { name: 'OrdenMonedaDesc',  width: 90,  align: 'center', editable: true, sortable: false },
                            { name: 'OrdenNeto',        width: 90, align: 'right', editable: false, sortable: false, formatter: 'number' },
                            {
                                name: 'agree',      width: 30,  align: 'center', sortable: false, search: false, 
                                formatter:function()
                                {
                                    var dos = arguments[2];
                                    return '<span style="cursor:pointer; margin: 0 auto;" class="ui-icon ui-icon-circle-triangle-e" title="Agregar Referencia" onclick="asignarCotizacion( \''+dos.OrdenId+'\', \''+dos.OrdenNum+'\' );"></span>';
                                }
                            }
                          ],
            viewrecords : true,
            height      : $(window).height()-180, 
            width       : $(window).width() - 10,
            sortable    : true,
            multiSort   : true,
            emptyrecords: "No se encontro registros",
            rowNum      : 100,
            rownumbers  : true, // show row numbers
            pager       : "#jqGridPager",
            shrinkToFit : false,
            ondblClickRow: function( rowId, param2, param3, param4 ) 
            {
                pr( arguments );
            },
            onCellSelect: function( rowid, iCol, cellcontent )
            {
            	//pr( arguments );
            },
        }
    );
    
    $( "#jqGrid" ).jqGrid( 'bindKeys' );
    
    $( window ).on
    ( 
        "resize", 
        function () 
        {
            var newWidth = $( "#jqGrid" ).closest( ".ui-jqgrid" ).parent().width() - 20;
            if( newWidth <= 705 )
            {
                jqGridContent.jqGrid( "setGridWidth", newWidth, false );
            }
            else
            {
                jqGridContent.jqGrid( "setGridWidth", newWidth, true);
            }
        }
    );
    if( $( "#jqGrid" ).closest( ".ui-jqgrid" ).parent().width() <= 705 )
    {
        jqGridContent.jqGrid( "setGridWidth", $( "#jqGrid" ).closest( ".ui-jqgrid" ).parent().width() - 5, false );
        //jqGridContent.jqGrid( "setGridWidth", $( "#jqGrid" ).closest( ".ui-jqgrid" ).parent().height() - 270, false );
    }
    else
    {
        jqGridContent.jqGrid( "setGridWidth", $( "#jqGrid" ).closest( ".ui-jqgrid" ).parent().width() - 5, true);
    }

    //pr( $( "#jqGrid" ).parents( 'div.ui-jqgrid-bdiv' ) );
    //pr( $(window).height() );
    //$( "#jqGrid" ).parents( 'div.ui-jqgrid-bdiv' ).css( "height", "auto" );
    
    setTimeout
    (
        function()
        {
            findControl();
            $( "#jqGrid" ).parents( 'div.ui-jqgrid-bdiv' ).css( "height", ($(window).height()-180) );
        },
        300
    );
</script>
