$(document).on( "click", "#agrega-contacto", function(event){
var cloned = $("#contacto_list >.contacto:first-child").clone(true).appendTo("#contacto_list");
cloned.removeClass("d-none");
cloned.find(".form-control").attr("readonly", false).attr("disabled", false).attr("required", true);
cloned.find("input").val("");
cloned.find(":first-child").focus();
//cloned.find('select').get(0).selectedIndex = 0;
console.log(cloned.length);
var tipoObj = cloned.find(".tipo");
var selectedOpts = tipoObj.find('option:selected');
if(selectedOpts.data("subtipo") > 0 ){
cloned.find('.subtipo').parent().removeClass("d-none")
loadSubtipo(tipoObj);
}else{
cloned.find('.subtipo').parent().addClass("d-none");
cloned.find('.subtipo').append($(""));
}
//hideBorraContacto();
});
$(".borra-contacto").click(function(){//quita contacto
$(this).parents(".contacto").remove();
});
$(".tipo").change(function(){
var selectedOpts = $(this).find('option:selected');
if(selectedOpts.data("subtipo") > 0 ){
$(this).parents('.contacto').find('.subtipo').parent().removeClass("d-none")
//$(this).parents('.contacto').find('.subtipo').attr("readonly", false).attr("disabled", false).attr("required", true);
loadSubtipo($(this));
}else{
$(this).parents('.contacto').find('.subtipo').parent().addClass("d-none")
$(this).parents('.contacto').find('.subtipo').find('option').remove();//borra
$(this).parents('.contacto').find('.subtipo').append($(""));
}
});
function loadSubtipo(tipoObj){
var idTipo = tipoObj.val();
$.ajax({
url: './action/subtipocontacto_select.php',
type: 'POST',
dataType: 'json',
data: { id: idTipo },
success: function(result) {
if(result["error"]!= "" && result["error"] !== undefined){
$("#errorBox").collapse('show');
$("#errorBox_text").html(result["error"]);
$('#messageBox')[0].scrollIntoView({ block: "end" });
}else{
var subtipoObj = tipoObj.parents('.contacto').find('.subtipo');
if(result["subtipo"]!= "" && result["subtipo"] !== undefined && result["subtipo"].length > 0){
subtipoObj.find('option').remove();//borra
for(var i=0; i"));
subtipoObj.find("option:last").attr("value", result["subtipo"][i]["valor"])
.text(result["subtipo"][i]["desc"]);
if(result["subtipo"][i]["seleccionado"] == 1){
subtipoObj.find("option:last").attr("selected", true);
}
}
}else{
$("#errorBox").collapse('show');
$("#errorBox_text").html("No se encontraron subtipos de contacto");
$('#messageBox')[0].scrollIntoView({ block: "end" });
}
}
},
error: function(jqXHR, textStatus, errorThrown ){
$("#errorBox").collapse('show');
$("#errorBox_text").html(errorThrown);
$('#messageBox')[0].scrollIntoView({ block: "end" });
}
});//ajax
}