jQuery(document).ready(function () {

    jQuery("input[name^='cpf']").mask("999.999.999-99");
    jQuery("input[name^='cnpj']").mask("99.999.999/9999-99");
    jQuery("input[name^='dt_']").mask("99/99/9999");
    jQuery("input[name^='hr_']").mask("99:99");
    jQuery("input[name^='cep']").mask("99.999-999");
    jQuery("input[name^='tel_']").mask("(99) 9999-9999");

    jQuery('form.login #email').focus();

    jQuery("input[name^='vlr_']").priceFormat({
        prefix: '',
        centsSeparator: ',',
        thousandsSeparator: '.'
    });

});

// marca ou desmarca todos os inputs checkbox da listagem
function checked_all( element ){

    if(jQuery(element).attr("checked") === true){
        jQuery("input.checkcontrol").attr("checked", true);
    }
    else{
        jQuery("input.checkcontrol").attr("checked", false);
    }

}

// pega todos os valores dos checkbox da listagem
function get_checked() {

    var length = jQuery("input.checkcontrol").length;
    var get = '';

    for(i=0; i<length; i++) {
        input = jQuery("input.checkcontrol:eq("+i+")");
        if(input.attr('checked') === true) {
            if(get == '')
                get += input.val();
            else
                get += ','+input.val();
        }
    }

    jQuery('#get_checked').val(get);

}

// buscar cidades de uma uf
function buscar_cidades(uf){

    if(uf != ''){

        jQuery.post(base_url+'cidades/buscar/'+uf, {},
            function(data){

                if(data != ''){

                    var lines = data.split('-');
                    var option;
                    var size = lines.length-1;
                    var line;

                    jQuery('#cidades_id').html('');
                    option = new Option('SELECIONE', '');
                    jQuery('#cidades_id').append(option);

                    for(var i=0; i<size; i++){
                        line = lines[i].split(';');
                        text = line[1];
                        value = line[0];
                        option = new Option(text, value);
                        jQuery('#cidades_id').append(option);
                    }
                }
            });
    }
    
}

// gerar uma uri baseada na string passada
function generate_uri(string){

    var uri = string.toLowerCase();
    uri = uri.replace(new RegExp(/\s/g),"-");
    uri = uri.replace(new RegExp(/[àáâãäå]/g),"a");
    uri = uri.replace(new RegExp(/æ/g),"ae");
    uri = uri.replace(new RegExp(/ç/g),"c");
    uri = uri.replace(new RegExp(/[èéêë]/g),"e");
    uri = uri.replace(new RegExp(/[ìíîï]/g),"i");
    uri = uri.replace(new RegExp(/ñ/g),"n");
    uri = uri.replace(new RegExp(/[òóôõö]/g),"o");
    uri = uri.replace(new RegExp(/œ/g),"oe");
    uri = uri.replace(new RegExp(/[ùúûü]/g),"u");
    uri = uri.replace(new RegExp(/[ýÿ]/g),"y");
    uri = uri.replace(new RegExp(/\W/g),"-");
    uri = uri.replace(new RegExp(/\---/g),"-");
    uri = uri.replace(new RegExp(/\--/g),"-");

    jQuery('#uri').val(uri);
    return uri;

}


// apagar fotos via ajax
function apagar_fotos(album_id){

    var id = jQuery('#get_checked').val();

    jQuery.post(base_url+'cms/fotos/apagar_n', {
        id: id
    },
    function(data){

        document.location.href = base_url+'cms/fotos/index/'+album_id;

    });
}
