function loadButton(button = null, disabled = true)
{
    if(button != null)
        btn = button;
        
    if(disabled == true)
    {
        textButton = btn.html();
        btn.prop('disabled', true);
        btn.html(textLoad);//+' '+textButton
    }
    else
    {
	    if(btn != null)
	    {
            btn.prop('disabled', false);
	    	btn.html(textButton);
	    	btn = null;
	    }
    }
}

function get_ajax_($data = [], $uri, $success = null, $error = null )
{
    if($error == null)
    {
        $error = function($erro){
            if($erro.msg != undefined)
                alert($erro.msg);
            else{
                console.log($erro.responseText);
                    msg = $($($erro.responseText)[7]).find('h1').text();
                if(msg == '')
                    msg = $($($erro.responseText)[8]).find('h1').text();
                if(msg == '')
                    msg =   $($erro.responseText).find('h4').text()+'\n'+$($($erro.responseText).find('p')[1]).text();
                if(msg == '' || msg == undefined || msg == 'undefined')
                    msg =   "ERRO!";
                alert(msg);
            }
        };
    }
    
    if($success == null)
    {
        $success = function(e){
            console.log(e);
        };
    }
    
    $.ajax({
		url: base_url+$uri, 
		dataType: 'json',
		type: 'POST',
		data: $data,
		success: function(retorno)
		{
		    loadButton(null,false);
		    if(retorno.valid == false)
		    {   
		        $error(retorno);
		    }
		    else
		    {
		        $success(retorno)
		    }
		},
		error: function(error)
		{
		    loadButton(null,false);
		    $error(error);
		}
    });
    
 }
 
function getForm($form)
{
    data = [];
    var nameRadio = [];
    
    //$($form).find('input:not([type=radio]):input:not([type=checkbox]):input:not([type=file])').each(function(key,value){
    $($form).find('input:not([type=radio]):input:not([type=checkbox]):input:not([type=file])').each(function(key,value){
        if(this.name != "")
            data.push({"name": this.name, "value": this.value});
    });
    
    $($form).find('input[type=file]').each(function(key,value){
        if(this.name != ""){
            data.push({"name": this.name, "value": $(this).data('value')});
        }
    });
    
    
    
    $($form).find('textarea').each(function(key,value)    {
        if(this.name != ""){
            
            if(typeof CKEDITOR !== 'undefined')
                data.push({"name": this.name, "value": CKEDITOR.instances[this.name].getData()});
            else
                data.push({"name": this.name, "value": this.value});
        }
    });
    
    
    /*$($form).find('textarea').each(function(key,value){
       
        if(this.name != "")
        {
            if(CKEDITOR.instances[this.name] != undefined){
                 //data.push({"name": this.name, "value": CKEDITOR.instances[this.name].getData()});
                 ckeditorValue = $($($($('#cke_'+this.id)[0])[0]).find('iframe').contents().find("body")).html();
                 data.push({"name": this.name, "value": ckeditorValue});
                 if(ckeditorValue == undefined)
                    data.push({"name": this.name, "value": this.value});
            }
            else
            {
                data.push({"name": this.name, "value": this.value});
            }
        }
    });*/
    
    $($form).find('input[type=radio]').each(function(key,value){
        if(nameRadio[this.name] == undefined)
            nameRadio[this.name] = '';
        if($(this).is(':checked'))
            nameRadio[this.name] = this.value;
    });
    
    for(var name in nameRadio){
        data.push({"name": name, "value": nameRadio[name]});
    };
    
    $($form).find('select option:selected').each(function(key,value)    {
        if(this.parentElement.localName == 'select')
            data.push({"name": this.parentElement.name, "value": this.value});
            
        if(this.parentElement.parentElement.localName == 'select')
            data.push({"name": this.parentElement.parentElement.name, "value": this.value});
    });
    
    $($form).find('input[type=checkbox]:checked').each(function(key,value)    {
        if(this.name != "")
            data.push({"name": this.name, "value": this.value});
    });
    
    $($form).find('input[type=checkbox]:not(:checked)').each(function(key,value)    {
        if(this.name != "")
            data.push({"name": this.name, "value": ""});
    });
    
    return data;
}