<?php use_helper('UrlFunctions'); ?>
<?php use_helper( 'IncludePartial' ); ?>
<?php use_helper( 'UtilFunctions' ); ?>

<link rel="stylesheet" type="text/css" media="screen" href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap4.min.css" />
<link rel="stylesheet" type="text/css" media="screen" href="https://cdn.datatables.net/responsive/2.2.5/css/responsive.bootstrap4.min.css" />
<!--<link rel="stylesheet" type="text/css" media="screen" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css" />-->

<script type="text/javascript" src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap4.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/responsive/2.2.5/js/dataTables.responsive.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/responsive/2.2.5/js/responsive.bootstrap4.min.js"></script>

<style type="text/css">
.ui-dialog .ui-dialog-titlebar-close 
{
    position: absolute;
    right: .3em;
    top: 50%;
    width: 20px;
    margin: -10px 0 0 0;
    padding: 1px;
    height: 20px;
    background-position: -96px -128px;
    /*background-image: url("images/ui-icons_056b93_256x240.png");*/
    background-image: url("<?=Constantes::ROOT_DIR_APP?>web/css/jquery/themes/start_1.12.1/images/ui-icons_056b93_256x240.png");
}
</style>

<style>
  .ui-autocomplete-loading { background: white url("<?=Constantes::ROOT_DIR_IMG?>common/progress.gif") right center no-repeat; }
</style>
<style>
.header { background-color: #E1E1E1; color: #444444; font-family: Arial,Helvetica,sans-serif; font-size: 11px; font-weight: bold; }
.pager { background-color: #F4F4F4; background-image: none; border-bottom: thin solid #999999; }
select[readonly] 
{
  background: #eee;
  pointer-events: none;
  touch-action: none;
}
</style>
<style type="text/css">
.marcoTabActive {
  /*background:#F1F1F1 none repeat scroll 0 0;*/
  background:#A3C1E0 none repeat scroll 0 0;  font-family:Geneva, Arial, Helvetica, sans-serif;  font-size:14px;  border-top-width: 1px;
  border-right-width: 1px;  border-left-width: 1px;  border-top-style: solid;  border-right-style: solid;  border-left-style: solid;  border-top-color: #999999;
  border-right-color: #999999;  border-left-color: #999999;  font-weight: bold;  /*color:#990000;*/
}
.label { color: #444444; }
.lblSmall { color: #8D8D8D; font-size: 10px; padding-right: 8px; }
</style>
<style>
.codigoItem{ color: blue; text-decoration: underline; cursor: pointer; }
.codigoItem:hover{ text-decoration: none; cursor: pointer; }
</style>

<div class="shadow-sm p-3 mb-2 bg-white rounded card card-body">
    <div class="row">
        <div class="col mb-1">
            <h4 class="text-info text-nowrap mb-0"><?=$s_Titulo?></h4>
        </div>
        <div class="col-md-8 text-right">
            <button type="button" class="btn btn-outline-primary" onclick="location.href='<?=buildUrl( 'oc/list' ) ?>'">LISTADO</button>
        </div>
    </div>
</div>

<ul class="nav nav-tabs m-1" id="myTab" role="tablist">
	<li class="nav-item">
		<a class="nav-link" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">General</a>
	</li>
	<?php if( $o_COM_OrdenCompraCabeceraBN->getIdOrdenCompra() ){ ?>
	<li class="nav-item">
		<a class="nav-link active" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">Detalles</a>
	</li>
	<?php } ?>
</ul>

<?php 
include_partial
( 
    "infohead", 
    array
    ( 
        "o_COM_OrdenCompraCabeceraBN"    => $o_COM_OrdenCompraCabeceraBN,
        "o_GSS_CompaniaBN"               => $o_GSS_CompaniaBN,
        "o_GSS_MonedaMastBN"             => $o_GSS_MonedaMastBN,
        "o_COM_OrdenItemClasificacionBN" => $o_COM_OrdenItemClasificacionBN,
    )
); ?>


<div class="dropdown-divider"></div>

<div id="resultContent" style="">
    <div id="content_alert" style=""></div>
</div>

<div class="form-row mx-0">
    <div class="col-md-6 mb-1">
        <button type="button" id="btnAgregar" class="btn btn-primary btn-sm">Agregar Producto</button> <!--<label> o Presion la tecla + </label>-->
        <script>
            $( "#btnAgregar" )
                .click
                (
                    function()
                    {
                        $( "#content_nvo" ).html( $( "#content_copy_nvo" ).html() );
                        $( "#content_nvo tr #IdItem" ).on( "keyup", eventoPopup );
                        $( "#content_nvo tr #IdItem" ).on( "blur", executeFindItem );
                        $( "#content_nvo tr #IdItem" ).focus();
                        openPopup();
                    }
                );
        </script>

        <?php if( $o_COM_OrdenCompraCabeceraBN->esAfectoAIGV() ){ ?>
        <?php $b_ModoIngreso = $o_COM_OrdenCompraCabeceraBN->getModoIngresoConIGVFlag()=="S" ? true : false; ?>
        <label class="labelData" for="OCconIGV">Modo de Ingreso - Precio Con IGV</label>&nbsp;&nbsp;<input type="checkbox" name="OCconIGV" id="OCconIGV" value="1" <?php if( $b_ModoIngreso ){ echo "CHECKED"; } ?> onclick="actualizarCheckFlagIGV();">
        <?php } ?>
    </div>
    <div class="col-md-6 mb-1 text-right">
        <button type="button" id="btnAgregarRequerimiento" class="btn btn-primary btn-sm">Desde Requerimiento</button>
        <script>
            var IdReq = 0;
            $( "#btnAgregarRequerimiento" )
                .click
                (
                    function()
                    {
                        $( "#exampleModalLongTitle" ).html( "Buscar Requerimiento" );
                        $( '#exampleModalCenter' ).modal( { show:true } );

                        if ( $.fn.DataTable.isDataTable( '#example') )
                        {
                            $( '#example' ).DataTable().destroy();
                        }

                        $('#example tbody').empty();

                        $('#example').DataTable
                        (
                            {
                                ajax: '<?=buildUrl( 'oc/request', "TipoProc=BuscarRequerimiento" )?>',
                                language: {"url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Spanish.json"},
                                //responsive: { details: true },
                                responsive: true,
                                //destroy: true,
                                bRetrieve: true,
                                aoColumns :
                                    [
                                        { "mData": "IdRequerimiento", 'sClass': 'text-center', "title": "Requerimiento" },
                                        { "mData": "Descripcion", 'sClass': 'text-center', "title": "Descripcion" },
                                        { "mData": "ResponsableNombre", 'sClass': 'text-center', "title": "Responsable" },
                                        { "mData": "SucursalDesc", 'sClass': 'text-center', "title": "Sucursal" },
                                        { "mData": "FechaRequerida", 'sClass': 'text-center', "title": "Fecha Req" },
                                        { "mData": "UsuarioCreacion", 'sClass': 'text-center', "title": "Usuario Reg" },
                                        { "mData": "FechaCreacion", 'sClass': 'text-center', "title": "Fecha Reg" },
                                        //{ "mData": "EstadoDesc", 'sClass': 'text-center', "title": "Estado" },
                                        {
                                            'sClass': 'text-center',
                                            "title": "",
                                            'mData': function ( data, type, full )
                                            {
                                                return '<a href=\'javascript:void(0);\' ><i class=\'ace-icon fa fa-search-plus bigger-130\' onclick="asignarRequerimiento(\'' + data.IdRequerimiento + '\')" ></i></a>';
                                            }
                                        }
                                    ]
                            }
                        );

                        //$('#example').DataTable().destroy();
                    }
                );

            asignarRequerimiento = function( Id )
            {
                IdReq = Id;
                pr( arguments );
                $( '#myModal2' ).modal( { show:true } );

                if ( $.fn.DataTable.isDataTable( '#example2' ) )
                {
                    $( '#example2' ).DataTable().destroy();
                }

                $( '#example2 tbody' ).empty();

                $( '#example2' ).DataTable
                (
                    {
                        ajax: '<?=buildUrl( 'oc/request', "TipoProc=BuscarRequerimientoDet&IdReqDet=" )?>' + IdReq,
                        language: {"url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Spanish.json"},
                        //responsive: { details: true },
                        responsive: true,
                        //destroy: true,
                        bRetrieve: true,
                        aoColumns :
                            [
                                { "mData": "IdRequerimientoDet", 'sClass': 'text-center', "title": "Id" },
                                { "mData": "IdItem", 'sClass': 'text-center', "title": "Codigo" },
                                { "mData": "DescripcionItem", 'sClass': 'text-center', "title": "Descripcion" },
                                { "mData": "StockAlmacenActual", 'sClass': 'text-center', "title": "Stock Actual" },
                                { "mData": "StockMinimo", 'sClass': 'text-center', "title": "Stock Minimo" },
                                { "mData": "CantidadSugerida", 'sClass': 'text-center', "title": "Cantidad" },
                                {
                                    'sClass': 'text-center',
                                    "title": "",
                                    'mData': function ( data, type, full )
                                    {
                                        //return '<a href=\'javascript:void(0);\' ><i class=\'ace-icon fa fa-search-plus bigger-130\' onclick="asignarRequerimiento(\'' + data.IdRequerimiento + '\')" ></i></a>';
                                        return '<input class="form-check-input" type="checkbox" name="IdReqDet[]" value="' + data.IdRequerimientoDet + '">';
                                    }
                                }
                            ]
                    }
                );
            }

            agregarDetallesDeRequerimiento = function()
            {
                pr( "Desde Requerimiento Detalle" );
                pr( IdReq );

                var cnt = $("#example2 :checkbox:checked").length;
                var url_param = $( "#example2 :checkbox:checked" ).serialize();
                url_param += "&IdReq=" + IdReq + "&IdOC=<?=$i_Id?>&ModoIngreso=" + $( "#OCconIGV" ).is( ':checked' );

                blockContent( 'myModal2' );
                executeAjaxJson
                (
                    "<?php echo buildUrl( 'oc/saveDetailFrReq' ) ?>",
                    url_param,
                    function( o_json )
                    {
                        unBlockContent( 'myModal2' );

                        ocultarModal2();
                        ocultarModal();

                        if( o_json.error )
                        {
                            //$( elm_sel ).val( CantidadRef );
                            showAlertBootStrap( 'content_alert', 'alert-danger', o_json.mensaje_error );
                            return false;
                        }

                        executeFindDetail();
                    }
                );

            }
        </script>
    </div>
</div>

<form id="form1" name="form1" method="POST" onsubmit="return false;"  enctype="multipart/form-data">
    <input type="hidden" name="s_TipoProc" id="s_TipoProc" value="<?=$s_TipoProc?>">
    <input type="hidden" name="i_Id" id="i_Id" value="<?=$i_Id?>">
    <input type="hidden" name="TypeSub" id="TypeSub" value="0">
    <div class="table-responsive">
        <div id="content_result_grid1">
            <table class="table table-bordered table-hover">
                <thead class="table-primary">
                    <tr>
                        <td width="2%"  align="center" class="header"><b>No</b></td>
                        <td width="10%" align="center" class="header"><b>C&oacute;digo</b></td>
                        <!--<td width="10%" align="center" class="header"><b>CodBarras</b></td>-->
                        <td width="30%" align=""       class="header"><b>Descripci&oacute;n</b></td>
                        <td width="10%" align="center" class="header"><b>Cant.Pedida</b></td>
                        <td width="10%" align="center" class="header"><b>U.Medida</b></td>
                        <td width="10%" align="center" class="header"><b>Precio</b></td>
                        <td width="10%" align="center" class="header"><b>Total</b></td>
                        <td width="5%"  align="center" class="header">&nbsp;</td>
                    </tr>
                </thead>
                <tbody id="content_nvo">
                </tbody>
            </table>
        </div>
    </div>
</form>

<!--
<table class="cuerpo" width="99%" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td valign="top">
            <form id="form1" name="form1" method="POST" onsubmit="return false;"  enctype="multipart/form-data">
                <input type="hidden" name="s_TipoProc" id="s_TipoProc" value="<?/*=$s_TipoProc*/?>">
                <input type="hidden" name="s_TipoProc" id="i_Id" value="<?/*=$i_Id*/?>">
                <input type="hidden" name="TypeSub" id="TypeSub" value="0">
            </form>
        </td>
    </tr>
</table>
-->

<?php if( !$s_externo ){ ?>
<table cellpadding="0" cellspacing="0" width="100%" border="0" style="">
    <tr>
        <td align="center" >
            <input type="button" class="btn btn-primary " value="Salir" onclick="location.href='<?php echo buildUrl( 'oc/list' ) ?>'">
            <!--<input type="button" class="btn btn-primary " value="Enviar Correo" onclick="eviarCorreo();">-->
            <input type="button" class="btn btn-primary " value="Ver PDF" onclick="window.open( '<?=buildUrl( 'oc/printPDF', 'Id='.$i_Id.'&ordennum='.str_pad( $o_COM_OrdenCompraCabeceraBN->getIdOrdenCompra(), 10, "0", STR_PAD_LEFT ) )?>', '_BLANK' );">
        </td>
    </tr>
</table>
<?php } ?>

<script type="text/javascript">
    eviarCorreo = function()
    {
        $( '#myIframe' ).attr( 'src', "<?=buildUrl( 'oc/plnEnvCorreo', "Id=$i_Id" )?>/subject/Aprobar OC <?= str_pad( $o_COM_OrdenCompraCabeceraBN->getIdOrdenCompra(), 10, "0", STR_PAD_LEFT ) ?>" );
        $( '#dialog' ).dialog( 'open' );
    }

    actualizarCheckFlagIGV = function()
    {
        var id_cab = $( "#i_Id" ).val(); //ESTA EN LA CABECERA.

        blockContent( 'content_detail' );
        var str_serialize = "TipoProc=ActualizarModoIngConIGV&IdOrdenCompra=<?=$o_COM_OrdenCompraCabeceraBN->getIdOrdenCompra()?>&ModoIngreso=" + $( "#OCconIGV" ).is( ':checked' );
        executeAjaxJson
        (
            "<?php echo buildUrl( 'oc/request' ) ?>",
            str_serialize,
            function( o_json )
            {
                unBlockContent( 'content_detail' );
                if( o_json.error )
                {
                    mostrarMensajeError( o_json.mensaje_error );
                    return false;
                }
                executeFindDetail();
            }
        );
    }
</script>
                             
<div id="content_result_grid_pl1" style="display:none;">
    <table class="grid" cellspacing="0" cellpadding="0" borde r="0" align="center" width="100%" style="border-bottom: 1px solid #999999;">
        <tbody id="content_copy_nvo">
            <tr>
                <td align="center">&nbsp;</td>
                <td align="center"><input type="text" class="text" name="IdItem" id="IdItem" value="" style="width: 85%; text-align: center;" ></td>
                <!--<td align="center" id="CodigoBarras" style="font-weight:bold;">&nbsp;</td>-->
                <td align="" id="DescripcionCompleta" style="cursor:pointer; color:#2743B2; text-decoration:underline;" onclick="buscarResumenStock( $( '#content_nvo tr #IdItem' ).val() );">&nbsp;</td>
                <td align="center">
                    <input type="text" class="text number" name="CantidadPedida" id="CantidadPedida" value="" style="width: 60%; text-align: center;"
                            onkeypress="return NumCheckDecimal( event, this );"
                            onfocus="this.select()"
                            autocomplete="off"
                            />
                </td>
                <td align="center">
                    <select class="combo" name="IdUnidad" id="IdUnidad" >

                    </select>
                </td>
                <td align="center">
                    <input type="text" class="text number" name="PrecioUnitario" id="PrecioUnitario" value="" style="width: 60%; text-align: center;"
                            onkeypress="return NumCheckDecimal( event, this );" 
                            onfocus="this.select()"
                            autocomplete="off"
                            />
                </td>
                <td align="center">&nbsp;</td>
                <td align="center"><input type="hidden" name="NoAfectoImpuestoVentasFlag" id="NoAfectoImpuestoVentasFlag" value=""/>
                    <input type="button" class="button" value="Guardar" onclick="executeSaveOrderDetail();">
                </td>
            </tr>
        </tbody>
    </table>
</div>


<script type="text/javascript">
$( '#myTab a' ).on
(
    'click', function ( e ) 
    {
        pr( e );
        //pr( $( e.target ).html() );
        if( $( e.target ).html() == "General" )
        {
        	location.href='<?=buildUrl( 'oc/addedit', 's_TipoProc='.$s_TipoProc.'&i_Id='.$i_Id )?>'
        }
        if( $( e.target ).html() == "Detalles" )
        {
        	location.href='<?=buildUrl( 'oc/detail', 's_TipoProc='.$s_TipoProc.'&i_Id='.$i_Id )?>'
        }
    }
)
//$('#myTab li:nth-child(3) a').tab('show'); 
</script>

<?=crearPopupResumenStock( array( "name_function"=>"buscarResumenStock" ) )?>

<?=crearFuentePopupIframe( array( "div_id"=>"dialog", "iframe_id"=>"myIframe", "popup_title"=>"Buscar Producto", "modal"=>true ) )?>

<!-- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -->
<style type="text/css">
    .modal
    {
        padding: 0 !important;
    }
    .modal-dialog {
        max-width: 95% !important;
        height: 90%;
        /*  padding: 0;
          margin: 0;*/
    }

    .modal-content {
        /*border-radius: 0 !important;*/
        height: 90%;
    }

    .modal:nth-of-type(even)
    {
        z-index: 1052 !important;
    }
    .modal-backdrop.show:nth-of-type(even)
    {
        z-index: 1051 !important;
    }
</style>
<!-- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -->

<!-- Modal -  -->
<div class="modal hide fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Buscar Orden de Compra</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">

                <!--<table id="example" class="table table-striped table-hover responsive" style="width:100%">-->
                <table id="example" class="table table-striped table-bordered" style="width:100%">
                </table>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
                <!--<button type="button" class="btn btn-primary">Save changes</button>-->
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    ocultarModal = function()
    {
        $( '#exampleModalCenter' ).modal( 'hide' );
    }
</script>
<div class="modal" id="myModal2" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Requerimiento Detalles</h4>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            </div><div class="container"></div>
            <div class="modal-body">

                <table id="example2" class="table table-striped table-bordered" style="width:100%">
                </table>

            </div>
            <div class="modal-footer">
                <a href="#" data-dismiss="modal" class="btn">Cerrar</a>
                <a href="javascript:void(0);" onclick="agregarDetallesDeRequerimiento();" class="btn btn-primary">Agregar a O/C</a>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    ocultarModal2 = function()
    {
        $( '#myModal2' ).modal( 'hide' );
    }
</script>
<!-- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -->

<script>
b_GuardarItem = false;
executeFindItem = function()
{
    blockContent( 'content_nvo' );
    var item_code = $( "#content_nvo tr #IdItem" ).val();
    var str_serialize = "s_ItemId=" + item_code + "&type=<?=$o_COM_OrdenItemClasificacionBN->getIdItemTipo()?>&ModoIngreso=" + $( "#OCconIGV" ).is( ':checked' );
    executeAjaxJson( 
                        "<?php echo buildUrl( 'oc/findItem', "i_Id=".$i_Id ) ?>", 
                        str_serialize,
                        function( o_json )
                        {
                            //console.log( o_json );
                            //ocultarCargaImg();
                        	unBlockContent( 'content_nvo' );
                            if( o_json.error )
                            {
                                mostrarMensajeError( o_json.mensaje_error );
                                return false;
                            }
                            if( o_json.result )
                            {
                                b_GuardarItem = true;
                                $( "#content_nvo tr #DescripcionCompleta" ).html( o_json.DescripcionCompleta );
                                //$( "#content_nvo tr #CodigoBarras" ).html( o_json.CodigoBarras );
                                //$( "#content_nvo tr #AfectoImpuesto2Flag" ).html( o_json.NoAfectoImpuestoVentasFlag );

                                //$( "#content_nvo tr #IdUnidad" )[0].value = o_json.UnidadCompra;

                                //ahora se carga las unidades equivalentes
                                $( "#content_nvo tr #IdUnidad" )[0].options.length = 0;
                                $.each
                                (
                                    o_json.a_Unidad, function ( i, o_Unidad )
                                    {
                                        $( "#content_nvo tr #IdUnidad" ).append
                                        (
                                            $( '<option>', { value: o_Unidad.IdUnidad, text : o_Unidad.UnidadDesc } )
                                        );
                                    }
                                );
                                //
                                $( "#content_nvo tr #IdUnidad" )[0].value = o_json.UnidadCompra;


                                $( "#content_nvo tr #NoAfectoImpuestoVentasFlag" ).val( o_json.NoAfectoImpuestoVentasFlag );
                                $( "#content_nvo tr #PrecioUnitario" ).val( o_json.PrecioUnitario );
                                if( o_json.SiManejaLote )
                                {
                                    $( "#content_nvo tr #IdLote" ).prop( 'readonly', false );
                                    $( "#content_nvo tr #IdLote" ).removeClass( "text0" );
                                    $( "#content_nvo tr #FechaVencimiento" ).prop( 'readonly', false );
                                    $( "#content_nvo tr #FechaVencimiento" ).removeClass( "text0" );
                                }
                                else
                                {
                                    $( "#content_nvo tr #IdLote" ).prop( 'readonly', true );
                                    $( "#content_nvo tr #IdLote" ).addClass( "text0" );
                                    $( "#content_nvo tr #FechaVencimiento" ).prop( 'readonly', true );
                                    $( "#content_nvo tr #FechaVencimiento" ).addClass( "text0" );
                                }
                                $( "#content_nvo tr #CantidadPedida" ).focus();
                            }
                        } 
                   );
}
actualizarCantidadPrecioxLinea = function( IdTR, IdOrdenCompra, IdSecuencia, IdItem, CantRef )
{
	var CantidadPedida  = $( "#" + IdTR + "_Cantidad" ).val();
	//var PrecioUnitario  = $( "#" + IdTR + "_Precio" ).val();
	var ModoIngreso = $( "#OCconIGV" ).is( ':checked' );
	//blockContent();
	//unBlockContent();
	
	if( CantidadPedida == CantRef )
	{
	    return false;
	}
	
	var str_serialize = $( "#" + IdTR ).find( "input, select" ).serialize();
	console.log( str_serialize );
	
	blockContent( 'content_detail' );
	var i_Id = $( "#i_Id" ).val() + "_" + IdSecuencia;
    str_serialize += "&s_TipoProc=act&ModoIngreso=" + ModoIngreso + "&i_Id=" + i_Id + "&IdItem=" + IdItem;
    
    executeAjaxJson
    ( 
        "<?php echo buildUrl( 'oc/saveDetail' ) ?>", 
        str_serialize,
        function( o_json )
        {
        	unBlockContent( 'content_detail' );
            if( o_json.error )
            {
                //$( elm_sel ).val( CantidadRef );
                mostrarMensajeError( o_json.mensaje_error );
                return false;
            }
            executeFindDetail();
            b_GuardarItem = false;
        } 
   );
    
}
eventoPopup = function( object )
{
    console.log( object );
    if( object.which === 107 ) 
    {
        object.target.value = "";
		openPopup();
    }
}
openPopup = function()
{
	var s_param = "type/<?=$o_COM_OrdenItemClasificacionBN->getIdItemTipo()?>";
    $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'transacciones/listItem' )?>' + s_param );
    $( '#dialog' ).dialog( 'open' );
    
    //$( '#myIframe' ).attr( 'src', '<?=buildUrl( 'oc/listItem', 'type='.$o_COM_OrdenItemClasificacionBN->getIdItemTipo() )?>' );
    //$( '#dialog' ).dialog( 'open' );	
}
closeWindow = function()
{
    $( '#dialog2' ).dialog( 'close' );
    $( '#dialog' ).dialog( 'close' );
}
asignarItem = function( id_item, item_descripcion, id_unidad )
{
    pr( arguments );
    pr( $( "#content_nvo tr #IdItem" ) );
    pr( $( "#content_nvo tr #DescripcionCompleta" ) );
    $( "#content_nvo tr #IdItem" ).val( id_item );
    $( "#content_nvo tr #DescripcionCompleta" ).html( item_descripcion );
    $( "#content_nvo tr #IdUnidad" ).val( id_unidad );

    closeWindow();
    
    setTimeout
    (
        function()
        {
            $( "#content_nvo tr #CantidadPedida" ).focus();
        }, 300
    )
    
    //$( "#content_nvo tr #IdItem" ).trigger( "blur" );
}
</script>
<script>
validacionPrecio = function( v_PrecioValue )
{
    if( v_PrecioValue == "" )
    {
        alert( "Precio vacia." );
        return false;
    }
    if( !$.isNumeric( v_PrecioValue ) )
    {
        alert( "Precio no valida." );
        return false;
    }
    var f_Precio = parseFloat( v_PrecioValue );
    if( f_Precio <= 0 )
    {
        alert( "El Precio ingresado no es valida." );
        return false;
    }
    return true;
}
validacionCantidad = function( v_CantValue )
{
    if( v_CantValue == "" )
    {
        alert( "Cantidad vacia." );
        return false;
    }
    if( !$.isNumeric( v_CantValue ) )
    {
        alert( "Cantidad no valida." );
        return false;
    }
    i_Cant = parseInt( v_CantValue );
    if( i_Cant < 1 )
    {
        alert( "La cantidad ingresada no es valida." );
        return false;
    }
    return true;
}
executeSaveOrderDetail = function()
{
    if( b_GuardarItem )
    {
        var elm = $( '#content_nvo' ).find( "input[name='CantidadPedida']" );
        var b_Return = validacionCantidad( elm.val() );
        if( !b_Return ) return false;
        
        var elm = $( '#content_nvo' ).find( "input[name='PrecioUnitario']" );
        var b_Return = validacionPrecio( elm.val() );
        if( !b_Return ) return false;
        
        var i_Id = $( "#i_Id" ).val();
        
        var str_serialize = $('#content_nvo :input').serialize();
        str_serialize += "&i_Id=" + i_Id + "&s_TipoProc=nvo";

        var ModoIngreso = $( "#OCconIGV" ).is( ':checked' );
        str_serialize += "&ModoIngreso=" + ModoIngreso;
        
        console.log( str_serialize );
    	
        executeAjaxJson
        (
            "<?php echo buildUrl( 'oc/saveDetail' ) ?>",
            str_serialize,
            function( o_json )
            {
                console.log( o_json );
                //ocultarCargaImg();
                if( o_json.error )
                {
                    mostrarMensajeError( o_json.mensaje_error );
                    return false;
                }
                executeFindDetail();
                b_GuardarItem = false;
            }
        );
    }
    else
    {
        alert( "No se puede guardar." );
    }
}
executeFindDetail = function()
{
	var i_Id = $( "#i_Id" ).val();
	
    var a_Params = [];
    a_Params.push( "i_Id=" + i_Id );

    executeAjaxUpdater
    (
        "content_result_grid1",
        "<?php echo buildUrl( 'oc/findDetail' ) ?>",
        a_Params.join( "&" ),
        function( response )
        {
        }
    );
}
executeDeleteDetail = function( i_Id )
{
    x = confirm( "Esta seguro de eliminar el detalle?" );
    if( x )
    {
        a_Params = [];
        a_Params.push( "i_Id=" + i_Id );
        executeAjaxJson
        (
            "<?=buildUrl( 'oc/deleteDetail' )?>",
            a_Params.join( "&" ),
            function( o_json )
            {
                if( o_json.error )
                {
                    mostrarMensajeError( o_json.mensaje_error );
                    return true;
                }
                executeFindDetail();
            }
        );
    }
}
</script>
<script>
//ESCAPE, DETECTA EL F5, CTRL+F5
$( document ).bind
(
    'keyup', 
    function( e ) 
    {
        //console.log( e.which );
        if( e.which === 116 ) 
        {
           
        }
        if( e.which === 82 && e.ctrlKey ) 
        {
           
        }
        if( e.which === 119 ) 
        {
           
        }
        if( e.which === 107 ) // +
        {
            $( "#btnAgregar" ).trigger( "click" );
        }
        if( e.which === 27 ) 
        {
            //$( "#content_nvo" ).html( "" );
            executeFindDetail();
        }
    }
);
</script>
<script>
executeFindDetail();
</script>