$(document).ready(function () {
	$(".input-number").keypress(function (e) {
		if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
			return false;
		}
	});
	$(".input-alpha").keypress(function (e) {
		if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57) && (e.which < 65 || e.which > 90) && (e.which < 97 || e.which > 122)) {
			return false;
		}
	});

	// var url_actual = $(location).attr('pathname');
	// var segmentos 	= url_actual.split("/");

	// if (segmentos[2] == "add") {
	// 	document.getElementById("warehouse_province").disabled=true;
	// 	document.getElementById("warehouse_district").disabled=true;
	// } else {
	// 	console.log("edicion")
	// 	$("#warehouse_department option:selected").text($("#warehouse_department_value").val());

	// 	var warehouse_department = $("#warehouse_department option:selected").text();

	// 	$.ajax({
	// 		type: "get",
	// 		url: base_url+'Referral_guide/search_provincia/'+warehouse_department,
	// 		dataType: "json",
	// 		success: function (data) {
	// 			document.getElementById("warehouse_province").disabled=false;
	// 			document.getElementById("warehouse_district").disabled=true;

	// 			$('option', '#warehouse_province').remove();
	// 			$('option', '#warehouse_district').remove();
	// 			$("#warehouse_province").append('<option value="">SELECCIONAR</option>');
	// 			$("#warehouse_district").append('<option value="">SELECCIONAR</option>');
	// 			$.each(data,function(key, registro) {
	// 				$("#warehouse_province").append('<option value='+registro.Provin+'>'+registro.Provin+'</option>');
	// 			});

	// 			$("#warehouse_province option:selected").text($("#warehouse_province_value").val());

	// 			var warehouse_province = $("#warehouse_province option:selected").text();
	// 			var warehouse_department = $("#warehouse_department option:selected").text();	

	// 			$.ajax({
	// 				type: "get",
	// 				url: base_url+'Referral_guide/search_Distrito/'+warehouse_province+'/'+warehouse_department,
	// 				dataType: "json",
	// 				success: function (data) {
	// 	             	document.getElementById("warehouse_district").disabled=false;
	// 					$('option', '#warehouse_district').remove();
	// 					$("#warehouse_district").append('<option value="">SELECCIONAR</option>');
	// 					$.each(data,function(key, registro) {
	// 						$("#warehouse_district").append('<option value='+registro.ubigeo+'>'+registro.distrito+'</option>');
	// 					});

	// 					$("#warehouse_district option:selected").text($("#warehouse_district_value").val());
	// 				}
	// 			});		
	// 		}
	// 	});	
	// }

	$("form").submit(function (e) {
		valido = $('form')[0].checkValidity();

		if (valido == true) {
			$('#ajaxCall').show();
		}
	});	
	$('#warehouse_department').change(function(e) {
        var departament = $(this).val();
        loadProvince(departament);
    });
    $('#warehouse_province').change(function(e) {

        var departament = $("#dapartament").val();
        var province = $(this).val();
        loadDistrict(departament, province);
    });
});


function loadProvince(departament) {
    var departament = departament;
    $.ajax({
        type: "get",
        url: base_url + 'traders/search_province/' + departament,
        dataType: "json",
        async: false,
        success: function(response) {
            $("#warehouse_province").prop('disabled', false);
            $("#warehouse_district").prop('disabled', true);
            $('option', '#warehouse_province').remove();
            $('option', '#warehouse_district').remove();
            $("#warehouse_province").append('<option value="0">SELECCIONAR</option>');
            $("#warehouse_district").append('<option value="0">SELECCIONAR</option>');
            $.each(response.data, function(key, registro) {
                $("#warehouse_province").append('<option value="' + registro.codProv + '">' + registro.provincia + '</option>');
            });
        }
    });
}

function loadDistrict(departament, province) {

    var departament = departament;
    var province = province;
    $.ajax({
        type: "get",
        url: base_url + 'traders/search_District/' + province + '/' + departament,
        dataType: "json",
        async: false,
        success: function(response) {
            $("#warehouse_district").prop('disabled', false);
            $('option', '#warehouse_district').remove();
            $("#warehouse_district").append('<option value="0">SELECCIONAR</option>');
            $.each(response.data, function(key, registro) {
                $("#warehouse_district").append('<option value=' + registro.ubigeo + '>' + registro.distrito + '</option>');
            });
        }
    });

}
function loadProvinceWarehouse() {	
	$('body').on('change', '#warehouse_department', function(){
		var warehouse_department = $("#warehouse_department option:selected").text();	

		$.ajax({
			type: "get",
			url: base_url+'Referral_guide/search_provincia/'+warehouse_department,
			dataType: "json",
			success: function (data) {
				document.getElementById("warehouse_province").disabled=false;
				document.getElementById("warehouse_district").disabled=true;

				$('option', '#warehouse_province').remove();
				$('option', '#warehouse_district').remove();
				$("#warehouse_province").append('<option value="">SELECCIONAR</option>');
				$("#warehouse_district").append('<option value="">SELECCIONAR</option>');
				$.each(data,function(key, registro) {
					$("#warehouse_province").append('<option value='+registro.Provin+'>'+registro.Provin+'</option>');
				});
			}
		});		
	});
}

function loadDistrictWarehouse() {
	$('body').on('change', '#warehouse_province', function(){		
		var warehouse_province = $("#warehouse_province option:selected").text();
		var warehouse_department = $("#warehouse_department option:selected").text();		

		$.ajax({
			type: "get",
			url: base_url+'Referral_guide/search_Distrito/'+warehouse_province+'/'+warehouse_department,
			dataType: "json",
			success: function (data) {
             	document.getElementById("warehouse_district").disabled=false;
				$('option', '#warehouse_district').remove();
				$("#warehouse_district").append('<option value="">SELECCIONAR</option>');
				$.each(data,function(key, registro) {
					$("#warehouse_district").append('<option value='+registro.ubigeo+'>'+registro.distrito+'</option>');
				});
			}
		});		
	});
}