$.fn.select2.defaults.set('language', 'es');

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
})

function post(path, params, method) {
    method = method || "post";
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    var token = document.createElement("input");
    token.setAttribute("type", "hidden");
    token.setAttribute("name", "_token");
    token.setAttribute("value", $('meta[name="csrf-token"]').attr('content'));
    form.appendChild(token);

    for (var key in params) {
        if (params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
        }
    }
    document.body.appendChild(form);
    form.submit();
};

jQuery.validator.addMethod("fileSize", function(value, element, param) {
    var size = parseFloat(((element.files[0].size)*(0.000001)).toFixed(2));
    if(size <= param){
        return true;
    } else {
        return false;
    }
}, "El archivo no puede ser mayor a {0} MB");

jQuery.validator.setDefaults({
    highlight: function (element, errorClass, validClass) {
        $(element).addClass(errorClass);
        $(element).closest('.form-group').addClass('has-error');
    },
    unhighlight: function (element, errorClass, validClass) {
        $(element).removeClass(errorClass);
        $(element).closest('.form-group').removeClass('has-error');
    },

    errorPlacement: function(error, element) {
        if (element.parent('.input-group').length) {
            error.insertAfter(element.parent());      // radio/checkbox?
        } else if (element.hasClass('filestyle')) {
            // console.log(element.next('.bootstrap-filestyle'));
            error.insertAfter(element.next('.bootstrap-filestyle'));  // select2
        } else if (element.hasClass('select2')) {
            error.insertAfter(element.next('span'));  // select2
        } else {
            error.insertAfter(element);               // default
        }

    }
});