﻿$.inserirtelefone = function(tipo) {
    var numero = $('#txtTelefone' + tipo).val();
    if (numero == '') $.show("Informe o número do telefone.");
    else {
        $('#listaTelefones' + tipo).append("<tr id=\"trTelefone_" + $.somentenumeros(numero) + "\"><td>" + numero +
            " <a onclick=\"$.removetelefone('" + tipo + "','" + numero +
            "');\"><img src=\"img_site/bot.gif\" border=\"0px\" alt=\"Excluir\"/></a></td></tr>");

        $.adicionatelefone(tipo, numero);
        $('#txtTelefone' + tipo).val('');
    }
}
$.listatelefones = function(tipo, ls) {
    $('#listaTelefones' + tipo).empty();
    $('#telefones' + tipo).val('');
    if (ls.length > 0) {
        for (var i = 0; i < ls.length; i++) {
            $('#listaTelefones' + tipo).append("<tr id=\"trTelefone_" + $.somentenumeros(ls[i].NumeroTelefone) + "\"><td>" + ls[i].NumeroTelefone +
                " <a onclick=\"$.removetelefone('" + tipo + "','" + ls[i].NumeroTelefone +
                "');\"><img src=\"img_site/bot.gif\" border=\"0px\" alt=\"Excluir\"/></a></td></tr>");

            $.adicionatelefone(tipo, ls[i].NumeroTelefone);
        }
    }
}
$.removetelefone = function(tipo, numero) {
    $('#trTelefone_' + $.somentenumeros(numero)).remove();
    var telefones = $('#telefones' + tipo).val();
    if (telefones.indexOf(',') > 0) {
        if (telefones.indexOf("'"+numero + "',") >= 0) telefones = telefones.replace("'"+numero + "',", '');
        else telefones = telefones.replace(",'" + numero + "'.", '') + ".";
    } else telefones = telefones.replace("'"+numero + "'.", '');
    $('#telefones' + tipo).val(telefones);
}
$.adicionatelefone = function(tipo,numero) {
    var telefones = $('#telefones' + tipo).val();
    var addCat = true;
    if (telefones != '') {
        if (telefones.indexOf(',') > 0) if (telefones.indexOf(numero + ',') == -1) telefones = telefones.replace(".", "") + ",'" + numero + "'.";
        else addCat = false;
        else if (telefones.indexOf(numero) == -1) telefones = telefones.replace(".", '') + ",'" + numero + "'.";
        else addCat = false;
    } else telefones = "'" + numero + "'.";
    
    if (addCat) $('#telefones' + tipo).val(telefones);
}
