

(function( $ ){
	
    /* Add csrf token */
    
    /* options: url: url to post, loading: dom id, success: func, error_data: func, pre_callback, separated_data:boolean, upload: {progressbar: '#progressbar', 'total_loader': '#total_loader', 'status': '#status'} */
    
	$.fn.sendPost = function (options) 
	{

        if(!options.hasOwnProperty("url")) {
            
            throw 'Error: you need an url for use sendPost';
            
            return false;
            
        }
        
        if(!options.hasOwnProperty("loading")) {
            
            options['loading']='#loading';
            
        }
        
        var xhr=false;
        
        if(options.hasOwnProperty("upload")) {
            
            xhr=function() {
                var myXhr = $.ajaxSettings.xhr();
                if(myXhr.upload){
                    
                    var progressbar=options.upload.progressbar;
                    var total_loader=options.upload.total_loader;
                    var status=options.upload.status;
                    
                    $(total_loader).html('');
                    $(progressbar).attr('value', 0);
                    $(status).html('');
                    
                    myXhr.upload.addEventListener('progress',function (event) {
                        
                        $(total_loader).html("Uploaded "+event.loaded+" bytes of "+event.total);
                        var percent = (event.loaded / event.total) * 100;
                        $(progressbar).attr('value', (Math.round(percent)));
                        $(status).html(Math.round(percent)+"% uploaded... please wait");
                            
                    }, false);
                    
                    myXhr.addEventListener("load", function(event) {
                        
                        $(status).html("File loaded successfully!!");
                        
                    }, false);
                }
                return myXhr;
            }
        }
        
        $(this).on('submit', function (e) {

            //Get data of form
            
            form=this;
            
            if(options.separated_data)
            {
                data[options.separated_data]=new FormData($(this)[0]); 
            }
            else
            {
                data=new FormData($(this)[0]); 
            }
            
            
            xhrFields={
              withCredentials: true
            };
            
            
            if(!data.hasOwnProperty("csrf_token")) {
            
                data['csrf_token']=$('#csrf_token_generic').val();
                
            }
            
            //Hide form and show the time icon
            
            $(this).find('.error').hide();
            
            $(options.loading).show();
            
            if(options.hasOwnProperty("pre_callback")) {
            
                options.pre_callback(data);
                
            }
            
            //Ajax
            
            ajax_post={
                type: "POST",
                url: options.url,
                data: data,
                encoding: "UTF-8", 
                xhrFields: xhrFields,
                cache:false,
                contentType:false,
                processData: false,
                success: function (data) {
                
                    $(options.loading).hide();

                    if(data.error) {
                        
                        if(!data.hasOwnProperty("error_csrf")) 
                        {    


                            if(data.hasOwnProperty("csrf_token")) 
                            {

                                $('.csrf_token').attr('value', data.csrf_token);
                                    
                            }
                                                    
                            
                            console.log('Error');
                            
                            if(data.hasOwnProperty('error_form'))
                            {
                                for(i in data.error_form) {
                            
                                    $(i).html(data.error_form[i]);
                                    $(i).show();
                                    
                                }
                            }
                            else
                            if(data.hasOwnProperty('form'))
                            {
                                
                                for(i in data.form) {

                                    $('#'+i+'_error').html(data.form[i]);
                                    $('#'+i+'_error').show();
                                }
                                
                            }
                            
                            if(options.hasOwnProperty("error_data")) {
                                
                                options.error_data(data);
                            }
                            
                            $(form).show();
                        }
                        else
                        {
                            
                            alert('You session expired, the page was reload by security');
                            location.reload();

                            
                        }
                        
                    }
                    else
                    {

                        // If ok then post successful task.
                        
                        if(options.hasOwnProperty("success")) {
                
                            options.success(data);
                            
                        }
                    }
                },
                error: function (data) {
                    
                    //Show the form again
                    
                    if(data.hasOwnProperty("csrf_token")) 
                    {

                        $('.csrf_token').attr('value', data.csrf_token);
                            
                    }
                    
                    $(options.loading).hide();                    
                    alert('Error: '+data.status+' '+data.statusText);

                    
                },
                dataType: 'json'
            };
            
            //console.log(xhr);

            if(xhr!=false) {
                
                ajax_post.xhr=xhr;
                
            }

            $.ajax(ajax_post);
            
            return false;
            
        });

    }

})( jQuery );

function sendGet(element, send_data, loading, success, error_data, pre_callback) {
    
    if(element==undefined) {
        
         throw 'Error: you need an element for use sendGet';
            
        return false;
        
    }
    
    if(send_data==undefined) {
        
        send_data={};
        
    }
        
    url=$(element).attr('url');
    
    if(pre_callback!=undefined) {
        
        options.pre_callback();
        
    }

    $.ajax({
        url: url,
        type: 'GET',
        data:  send_data,
        success: function (data) {

            $(options.loading).hide();

            if(!data.error) {

                if(options.hasOwnProperty("success")) {
                    
                    options.success(data);
                    
                }

            }
            else {

                if(options.hasOwnProperty("error_data")) {
                    
                    options.error_data(data);
                    
                }

            }

        },
        error: function (data) {

            $(options.loading).hide();
            
            alert('Error: '+data.status+' '+data.statusText);

        },
        dataType: 'json'
    });
    
    return false;
    

    
}

(function( $ ){
	
    /* Add csrf token */
    
    /* url: url to post is getted from object*/
    
    /* element_id: element id, options:, loading: dom id, success: func, error_data: func, pre_callback,  */
    
	$.fn.sendGet = function (options) 
	{

        /*if(!options.hasOwnProperty("url")) {
            
            throw 'Error: you need an url for use sendPost';
            
            return false;
            
        }*/
        
        if(!options.hasOwnProperty("loading")) {
            
            options['loading']='#loading';
            
        }

        //$(this).click( function (e) {
        $(this).click( function(e) {
        
            if(options.hasOwnProperty("pre_callback")) {
            
                options.pre_callback();
                    
            }
        
            url=$(this).attr('data-url');
            
            $(options.loading).show();

            $.ajax({
                url: url,
                type: 'GET',
                data:  {},
                success: function (data) {

                    $(options.loading).hide();

                    if(!data.error) {

                        if(options.hasOwnProperty("success")) {
                            
                            options.success(data);
                            
                        }

                    }
                    else {

                        if(options.hasOwnProperty("error_data")) {
                            
                            options.error_data(data);
                            
                        }

                    }

                },
                error: function (data) {

                    $(options.loading).hide();
                    
                    alert('Error: '+data.status+' '+data.statusText);

                },
                dataType: 'json'
            });
            
            return false;
            
        });
    }

})( jQuery );
