function addFieldToForm(form, campo){
    var valor;
    valor = document.getElementById(campo).value;
    form.action = form.action + '/'+valor;
    return true;
}

function alertProduct(message,button) {
    jQuery.msgbox('<h3>'+message+'</h3>',
        {
            type: "info",
            buttons: [ {
                    type: "submit",
                    value: button}
            ]
        }
    );
}
function chlng(value) {
    jQuery('<div></div>').load('/ecarty.com/public/language/'+value, function () {
       window.location.reload();
    });
}

function dialogModal(id,title,text) {
    jQuery(id).click(function() {
        jQuery("<div>"+text+"</div>").dialog({
          modal:true,
          title: title,
          minWidth:550,
          maxHeight:600
        })
    });
}

function addcategoria(url) {
    var dialogOpts = {
        title: "Agregar Categoría",
        modal: true,
        autoOpen: false,
        buttons: {
            "Guardar": function() {
                document.getElementById('guardar_cat').click();
                jQuery(this).dialog("destroy");
            },
            "Cerrar": function() {
                jQuery(this).dialog("destroy");
            }
        },
        height: 300,
        width: 300,
        open: function() {
            jQuery("#ui-dialog").load(url);
        }
    }

    jQuery("#ui-dialog").children().remove();
    jQuery("#ui-dialog").dialog("destroy");
    jQuery("#ui-dialog").dialog(dialogOpts);
    jQuery("#ui-dialog").dialog("open");
}

function addmarca(url) {
    var dialogOpts = {
        title: "Agregar Marca",
        modal: true,
        autoOpen: false,
        buttons: {
            "Guardar": function() {
                document.getElementById('guardar_marca').click();
                jQuery(this).dialog("destroy");
            },
            "Cerrar": function() {
                jQuery(this).dialog("destroy");
            }
        },
        height: 300,
        width: 300,
        open: function() {
            jQuery("#ui-dialog").load(url);
        }
    }

    jQuery("#ui-dialog").children().remove();
    jQuery("#ui-dialog").dialog("destroy");
    jQuery("#ui-dialog").dialog(dialogOpts);
    jQuery("#ui-dialog").dialog("open");
}

function language(value) {
    jQuery.ajax({
                url: '/ecarty.com/administracion/language/'+value,
                type:'POST',
                cache: false,
                dataType: 'text',
                async: false,
                success: function (html) {
                    window.location.reload();
                }
        });

}

function msgDialog(div) {
    jQuery(div).dialog({
        modal: true,
        buttons: {
            "Aceptar": function() {
                jQuery(this).dialog("close");
            }
        }
    });
}

function ajaxDialog(settings) {
    var div = settings.div;
    var dialogOpts = {
        title: settings.title,
        modal: true,
        autoOpen: false,
        bgiframe: true,
        buttons: {
            "Cancelar": function() {
                jQuery(div).dialog("destroy");
            },
            "Guardar": settings.fSave
        },
        height: settings.height,
        width: settings.width,
        open: function() {
            jQuery(div).html('Cargando...');
            jQuery(div).load(settings.url);
        }
    };
    jQuery(div).children().remove();
    jQuery(div).dialog("destroy");
    jQuery(div).dialog(dialogOpts);
    jQuery(div).dialog("open");
}

function ajaxSubmit(form,callBack) {
    jQuery.post(form.attr('action'),form.serialize(),function(data,status,jqXHR){callBack(data)});
}

function ajaxSubmitJSON(form,callBack) {
    jQuery.post(form.attr('action'),form.serialize(),function(data,status,jqXHR){callBack(data)},"json");
}


function ajaxWindow(settings) {
    settings.ajaxCallback = function(data) {
        var div = jQuery(data).find(settings.selectorError);
        if (div.html()) {
            msgDialog(div);
        } else {
            settings.ajaxCallbackOk(data);
            jQuery(settings.div).dialog("destroy");
        }
    }
    
    settings.fSave = function() {
        ajaxSubmit(jQuery(settings.form),settings.ajaxCallback);
    }

    ajaxDialog(settings);

}
