<div id="nuevocliente" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Registrar Cliente</h4>
            </div>
            <form name="nuevo_clie" id="nuevo_clie" method="post">
                <div class="modal-body ">
                    <div class="tabbable">
                        <ul class="nav nav-tabs">
                            <li class="active"><a href="#datos" data-toggle="tab">DATOS PERSONALES </a></li>
                            <li class=""><a href="#avanzado" data-toggle="tab">OPCIONES AVANZADAS </a></li>
                        </ul>
                        <div class="tab-content">
                            <div id="resultados_cliente"></div>
                            <div class="tab-pane active" id="datos">
                                <div>
                                    <label>Tipo Documento</label>
                                    <select name="clie_tipo_doc"  class="form-control name_list input-sm" required >
                                        <?php
                                            $datos = mysqli_query($api,"SELECT * FROM type_document_identifications where id=3") ;
                                            while( $fila = mysqli_fetch_array( $datos ) )
                                            {
                                                echo' <option value="'.$fila['id'].'">'.$fila['name'].'</option>';
                                            }
                                           
                                            $datos = mysqli_query($api,"SELECT * FROM type_document_identifications where id<>3") ;
                                            while( $fila = mysqli_fetch_array( $datos ) )
                                            {
                                                echo' <option value="'.$fila['id'].'">'.$fila['name'].'</option>';
                                            }
                                              
                                        ?>
                                    </select>
                                </div>

                                <div class="col-md-9" style="padding-left:0px" > <!-- /.form-group -->
                                    <label>Numero</label>
                                    <div class="input-group">
                                        <input type="number" name="clie_cc" id="clie_cc" onblur="CalcularDv()" class="form-control input-sm" autocomplete="off"  value="" required>
                                        <span class="input-group-addon">
                                            <div id="Info"></div>
                                        </span>            
                                    </div> <!-- /.form-group -->
                                </div>    

                                <div class="col-md-3" style="padding-left:0px"> <!-- /.form-group -->
                                    <label>Dv</label>
                                    <input type="number" name="clie_dv" id="clie_dv" class="form-control input-sm" autocomplete="off"  value="" readonly>
                                </div>

                                <div class="">
                                    <?php if($negocio_neg_id=='7'){ ?>
                                    <label>Nombre Cliente</label>
                                     <?php } else { ?>
                                    <label>Nombre Comercial</label>
                                    <?php } ?>
                                    <input type="text" name="clie_nombre" id="clie_nombre" class="form-control input-sm" autocomplete="off" required value="" autofocus="autofocus">
                                </div> <!-- /.form-group -->
           
                                <div class="">
                                     <?php if($negocio_neg_id=='7'){ ?>
                                    <label>Nombre Negocio</label>
                                     <?php } else { ?>
                                    <label>Nombre Representante</label>
                                    <?php } ?>
                                    <input type="text" name="clie_representante" id="clie_representante" class="form-control input-sm" autocomplete="off" value="" autofocus="autofocus">
                                </div> <!-- /.form-group --> 
                                <div >
                                    <label>Correo</label>
                                    <input type="email"  name="clie_correo" id="clie_correo" class="form-control input-sm" autocomplete="off"  value="" autofocus="autofocus">
                                </div> <!-- /.form-group -->

                                <div >
                                    <label>Telefono</label>
                                    <input type="number" name="clie_telefono" id="clie_telefono" class="form-control input-sm" autocomplete="off" value="" autofocus="autofocus">
                                </div> <!-- /.form-group -->
                               
                                <div class="col-md-6" style="padding-left:0px">
                                    <label>Departamento</label>
                                    <section id="intro">
                                        <select name="clie_departamento" id="clie_departamento" class="form-control name_list input-sm">
                                            <option value=""> - </option>
                                            <?php
                                                $datos = mysqli_query($api,"SELECT * FROM departments order by name asc") ;
                                                while( $fila = mysqli_fetch_array( $datos ) )
                                                {
                                                echo '<option value="'.$fila['id'].'">'.$fila['name'].'</option>';
                                                }
                                            ?>
                                        </select>
                                    </section>
                                </div>
                                <div class="col-md-6" style="padding-left:0px">
                                    <label>Ciudad</label>
                                    <section id="">
                                        <select name="clie_ciudad" id="clie_ciudad" class="form-control name_list input-sm"  >
                                            <option value=""> - </option>
                                        </select>
                                    </section>
                                </div>
                                <div>
                                    <label>Dirección</label>
                                    <input type="text" name="clie_direccion" id="clie_direccion" class="form-control input-sm" autocomplete="off" value="" autofocus="autofocus">
                                </div>
                                <?php if($negocio_neg_id=='7') { ?>
                                <div>
                                    <label>Barrio</label>
                                    <input type="text" name="clie_barrio" id="clie_barrio" class="form-control input-sm" autocomplete="off"  value="" autofocus="autofocus">
                                </div>
                                <?php } else { ?>
                                     <input type="hidden" name="clie_barrio" id="clie_barrio" value="">
                                <?php } ?>

                            </div><!-- /.datos -->

                            <div class="tab-pane" id="avanzado"> 
                                <div>
                                    <label>Tipo Contribuyente</label>
                                    <select name="clie_contribuyente"  class="form-control name_list input-sm" required >
                                        <?php
                                            $datos = mysqli_query($api,"SELECT * FROM type_organizations order by id desc") ;
                                            while( $fila = mysqli_fetch_array( $datos ) )
                                            {
                                                echo '<option value="'.$fila['id'].'">'.$fila['name'].'</option>';
                                            }
                                            ?>
                                    </select>
                                </div>
                                <div>
                                    <label>Tipo Regimen</label>
                                    <select name="clie_regimen" class="form-control name_list input-sm" required >
                                        <?php
                                            $datos = mysqli_query($api,"SELECT * FROM type_regimes order by id desc") ;
                                            while( $fila = mysqli_fetch_array( $datos ) )
                                            {
                                                echo '<option value="'.$fila['id'].'">'.$fila['name'].'</option>';
                                            }
                                        ?>
                                    </select>
                                </div>
                                

                                <?php
                                    $per=mysqli_query($con,"SELECT * FROM modulo_permiso where usuario='$usu_id' and mod_id='13' and modp_nombre='Asignar Tarifa'") ;
                                    $per=mysqli_fetch_array($per);
                                    $estado=$per['estado'];
                                    if ($estado =='Activo') {
                                      
                                ?>
                                    <label>Tarifa</label>
                                    <select name="clie_tarifa" id="clie_tarifa" class="form-control name_list input-sm drop" required >
                                        <?php
                                            $datos = mysqli_query($con,"SELECT * FROM tarifas where empresa='$empresa' ") ;
                                            while( $fila = mysqli_fetch_array( $datos ) )
                                            {
                                            echo '<option value="'.$fila['tar_id'].'">'.$fila['tar_nombre'].'</option>';
                                            }
                                        ?>
                                    </select>
                                <?php    }   else   {  
                                $datos = mysqli_query($con,"SELECT * FROM tarifas where tar_nombre='GENERAL' and empresa='$empresa' ") ;
                                     $fila = mysqli_fetch_array( $datos ) ;
                                     $clie_tarifa=$fila['tar_id'];
                                ?>     
                                    <input type="hidden" name="clie_tarifa" id="clie_tarifa" class="form-control input-sm" autocomplete="off" value="<?php echo $clie_tarifa;?>" readonly>
                                <?php   }   ?>



                                <div class="">
                                    <?php

                                    $per=mysqli_query($con,"SELECT * FROM modulo_permiso where usuario='$usu_id' and mod_id='13'") ;
                                    $per=mysqli_fetch_array($per);
                                    $estado=$per['estado'];
                                    if ($estado =='Activo') {
                                        $readonly='';
                                    }
                                    else
                                    {
                                        $readonly='readonly';
                                    }
                                    ?>

                                    <label>Cupo de Credito</label>
                                    <select name="clie_credito" id="clie_credito" onChange="xxxOnChange(this)" class="form-control name_list input-sm drop" <?php echo $readonly; ?> >
                                            <option value="0">Desactivado</option>
                                            <option value="1">Activado</option>
                                        ?>
                                    </select>
                                  

                                    <div id="1" style="display: none;">
                                        <div class="">
                                            <label>Cupo Credito</label>
                                            <input type="number" name="clie_cupo_credito" id="clie_cupo_credito" class="form-control input-sm" autocomplete="off" value="0"  autofocus="autofocus" onkeyup="fAgrega();">
                                        </div> <!-- /.form-group -->
                                        <div class="">
                                            <label>Disponible</label>
                                            <input type="number" name="clie_disponible" id="clie_disponible" class="form-control input-sm" value="0" autocomplete="off" readonly="readonly" autofocus="autofocus">
                                        </div> <!-- /.form-group -->
                                    </div>

                                    <div id="0" style="display:"> 
                                        <div class=""> </div>
                                    </div>
                                     <?php
                                        if($facturador=='si') 
                                        { ?>
                                         <div class="">
                                        <label>Facturador Electronico</label>
                                        <select name="clie_facturador" id="clie_facturador" class="form-control name_list input-sm">
                                            <option value="no">No</option>
                                            <option value="si">Si</option>
                                        </select>
                                    </div>
                                    <?php } else { ?> 
                                        <input type="hidden" name="clie_facturador" id="clie_facturador" value="no">
                                    <?php }  ?>
                                </div>
                            </div><!-- /.opciones avanzadas-->
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                        <button type="submit" id="btn_cliente" value="btn_cliente" name="btn_cliente" class="btn btn-success" accesskey="s" title="alt + s"><i class="icon-ok"></i> Registrar</button>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>
<script type="text/javascript">
  $(document).ready(function(){
    $("#clie_departamento").change(function(){
      $.get("php_clientes/lista_ciudades.php","clie_departamento="+$("#clie_departamento").val(), function(data){
        $("#clie_ciudad").html(data);
        console.log(data);
      });
    });
  });
</script>
<script type="text/javascript">
    function showContent() {
        element = document.getElementById("content");
        clie_credito = document.getElementById("clie_credito");
        if (clie_credito.checked) {
            element.style.display='block';
        }
        else {
            element.style.display='none';
        }
    }
    function fAgrega()
      {
        document.getElementById("clie_disponible").value = document.getElementById("clie_cupo_credito").value;
      }

function xxxOnChange(sel) {
      if (sel.value=="1"){
           divC = document.getElementById("1");
           divC.style.display = "";
           divT = document.getElementById("0");
           divT.style.display = "none";
      }else  {
           divC = document.getElementById("1");
           divC.style.display="none";

           divT = document.getElementById("0");
           divT.style.display = "";
      }
}

$(document).ready(function() {  
  $('#clie_cc').blur(function(){
    $('#Info').html('<img src="img/ajax-loader.gif" alt="" height="15px"  />').fadeOut(1000);
    var clie_cc = $(this).val();    
    var dataString = 'clie_cc='+clie_cc;
    
    $.ajax({
            type: "POST",
            url: "php_clientes/verificar_cliente.php",
            data: dataString,
            success: function(data) {
        $('#Info').fadeIn(1000).html(data);
        //alert(data);
            }
        });
    });              
});    

function CalcularDv() {
    var arreglo, x, y, z, i, nit1, dv1;
    nit1 = document.nuevo_clie.clie_cc.value;
    if (isNaN(nit1)) {
        document.nuevo_clie.clie_dv.value = "X";
        alert('Numero del Nit no valido, ingrese un numero sin puntos, ni comas, ni guiones, ni espacios');
    } else {
        arreglo = new Array(16);
        x = 0;
        y = 0;
        z = nit1.length;
        arreglo[1] = 3;
        arreglo[2] = 7;
        arreglo[3] = 13;
        arreglo[4] = 17;
        arreglo[5] = 19;
        arreglo[6] = 23;
        arreglo[7] = 29;
        arreglo[8] = 37;
        arreglo[9] = 41;
        arreglo[10] = 43;
        arreglo[11] = 47;
        arreglo[12] = 53;
        arreglo[13] = 59;
        arreglo[14] = 67;
        arreglo[15] = 71;
        for (i = 0; i < z; i++) {
            y = (nit1.substr(i, 1));
            x += (y * arreglo[z - i]);
        }
        y = x % 11
        if (y > 1) { dv1 = 11 - y; } else { dv1 = y; }
        document.nuevo_clie.clie_dv.value = dv1;
    }
}

</script>