 

$(function(){
 
    $('.read_only').click(function(){
        $(this).blur();
    })
 
     
    
    $('.top_parent').hover(function(){
        $(this).children('.sub').fadeIn(200);
    },
    function(){
        $(this).children('.sub').fadeOut(200);
    })
    
    
    
    
    
    
    
    $('.bt_submit').click(function(evt){
        evt.preventDefault();
        enviar = true;
        
        
        
        form = $(this).attr('id');
        url = $(this).attr("alt");
        attrs = $('#'+form).serialize();
        
 
        $.ajax({
            url: url,
            type: "POST",
            data: attrs,
            dataType: "json",
            error: function(){alert("Não foi possível enviar a solicitação, atualize a pagina e tente novamente")},
            success: function(ret){
    
                if(isset(ret['error'])){
           
                    alert("Foram encontrado erros, verifique se os campos requeridos foram preenchidos.");
                    $('#mensagem').html(ret['error']);                    
                }
                else {
 
                    $('#mensagem').html(''); 
                    alert(ret['success']);
                    
                    if($('#id').length==0){
                        // Limpar campos
                        $('#'+form+" input").each(function(){
                            $(this).val('');
                        })   
                    }                                    
                }

            },
            beforeSend: function(){
              $('#mensagem').html("Carregando............"); 
            } 
            
        })
        
        $.ajaxStart(function() {
            $("#mensagem").html("Carregando...");
        });
        
        $.ajaxComplete(function() {
            $("#mensagem").html("");
        });          
    })
     
    
    
})








// Function
function isset () {

    var a = arguments, l = a.length, i = 0, undef;
    
    if (l === 0) {
        throw new Error('Empty isset'); 
    }
    
    while (i !== l) {
        if (a[i] === undef || a[i] === null) {
            return false; 
        }
        i++; 
    }
    return true;
}
