<div id="nuevoproveedor" 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 PROVEEDOR</h4>
            </div>
            <div class="modal-body ">
                <form class="form-horizontal" method="post" id="guardar_pro" name="guardar_pro">
                    <div id="resultados_ajax"></div>
                    <div>
                        <label>Tipo Documento</label>
                        <select name="pro_tipo_doc" id="pro_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" >
                        <label>Nit</label>
                        <div class="input-group">
                            <input type="number"  name="pro_nit" id="pro_nit" 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="pro_dv" id="pro_dv" class="form-control input-sm" autocomplete="off"  value="">
                    </div>
                    <div class="">
                        <label>Nombre Comercial</label>
                        <input type="text" name="pro_nombre" id="pro_nombre" class="form-control input-sm" autocomplete="off" value="" required>
                    </div> <!-- /.form-group -->
                     <div class="">
                        <label>Nombre Representante</label>
                        <input type="text" name="pro_nombre_repre" id="pro_nombre_repre" class="form-control input-sm" autocomplete="off" value="" >
                    </div> <!-- /.form-group -->
                    <div class="col-md-6" style="padding-left:0px">
                        <label>Telefono</label>
                        <input type="number"  name="pro_telefono" id="pro_telefono" class="form-control input-sm" autocomplete="off"  value="" >
                    </div> <!-- /.form-group -->
                    <div class="col-md-6" style="padding-left:0px">
                        <label>Codigo Postal</label>
                        <input type="number"  name="pro_codigo_postal" id="pro_codigo_postal" class="form-control input-sm" autocomplete="off"  value="" >
                    </div> <!-- /.form-group -->
                    <div class="">
                        <label>Correo</label>
                        <input type="email"  name="pro_correo" id="pro_correo" class="form-control input-sm" autocomplete="off"  value="" >
                    </div> <!-- /.form-group -->
                   
                    
                    <div class="col-md-6" style="padding-left:0px">
                        <label>Departamento</label>
                        <section id="intro">
                            <select name="pro_departamento" id="pro_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="pro_ciudad" id="pro_ciudad" class="form-control name_list input-sm"  >
                                <option value=""> - </option>
                            </select>
                        </section>
                    </div></br></br></br>
                    <div class="">
                        <label>Direccion 1</label>
                        <input type="text"  name="pro_direccion" id="pro_direccion" class="form-control input-sm" autocomplete="off"  value="" >
                    </div> <!-- /.form-group -->
                    <div class="">
                        <label>Direccion 2</label>
                        <input type="text"  name="pro_direccion2" id="pro_direccion2" class="form-control input-sm" autocomplete="off" value="" >
                    </div> <!-- /.form-group -->

                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                        <button id="guardar_datos" type="submit" class="btn btn-success"><i class="icon-ok"></i> <strong>Registrar</strong></button>
                    </div>
                </form>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>
<script type="text/javascript">
  $(document).ready(function(){
    $("#pro_departamento").change(function(){
      $.get("php_proveedores/lista_ciudades.php","pro_departamento="+$("#pro_departamento").val(), function(data){
        $("#pro_ciudad").html(data);
        console.log(data);
      });
    });
  });


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

function CalcularDv() {
    var arreglo, x, y, z, i, nit1, dv1;
    nit1 = document.guardar_pro.pro_nit.value;
    if (isNaN(nit1)) {
        document.guardar_pro.pro_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.guardar_pro.pro_dv.value = dv1;
    }
}  
</script>