﻿function Eliminar(xId, xsTitle, xsTexto, BloqueDelete, Url, vPost, ValuePost) {

    $(function() {

        $("#dialog").dialog("destroy");
        $("#dialog-confirm").html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + xsTexto + '</p>');
        var sPost = "";
        for (var i = 0; i < vPost.length; i++) {
            sPost = sPost + vPost[i] + '=' + ValuePost[i] + '&'
        }
        $("#dialog-confirm").dialog({
            resizable: false,
            height: 140,
            modal: true,
            title: xsTitle,
            buttons: {
                'Aceptar': function() {
                    $(this).dialog('close');
                    $.ajax(
                        {
                            async:true,
                            type: "POST",
                            dataType: "text/html",
                            contentType: "application/x-www-form-urlencoded",
                            url: Url,
                            data: sPost,
                            timeout:10000,
                            beforeSend: function() {

                                $("#dialog-loading").html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + 'Loading....' + '</p>');
                                $("#dialog-loading").dialog({
                                    resizable: false,
                                    height: 100,
                                    modal: true,
                                    title: xsTitle
                                });
                            },
                            success: function(datos) {
                                $("#dialog-loading").dialog('close');
                                var Result = datos.split(',');
                                $("#dialog-response").html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + Result[0] + '</p>');
                                $("#dialog-response").dialog({
                                    resizable: false,
                                    height: 100,
                                    modal: true,
                                    title: xsTitle
                                });
                                setTimeout(CloseDialog, 2500);
                                if (Result[1] == 1) {
                                    $("#" + BloqueDelete + "_" + xId).each(function() {
                                        $(this).fadeOut('slow');
                                    });
                                }
                            }
                        });




                },
                'Cancelar': function() {
                    $(this).dialog('close');
                }
            }
        });
    });

}

function CloseDialog() {

    $("#dialog-response").dialog({ hide: 'slide' });
    $("#dialog-response").dialog('close');

}

function Editar(NombreDialog, xId, xsTitle, xsTexto, BloqueEditar, Url, Altura, Form, vPost, ValuePost) {

    $(function() {

        $("#dialog").dialog("destroy");
        $("#TextoForm").html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + xsTexto + '</p>');

        var sPost = "";
        for (var i = 0; i < vPost.length; i++) {
            sPost = sPost + vPost[i] + '=' + ValuePost[i] + '&'
        }

        $("#" + NombreDialog).dialog({
            resizable: false,
            height: Altura,
            modal: true,
            title: xsTitle,
            buttons: {
                'Aceptar': function() {
                    // hacemos validaciones antes del ajax
                    if (Validar(Form)) {
                        //Sumamos a los datos de post parametrizables los datos del form
                        //vemos si el navegador es chrome porq atua distinto
                        var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
                        if (is_chrome)
                        {
                            for (var i = 0; i < Form.elements.length; i++) {
                                sPost = sPost + Form.elements[i].name + '=' + Form.elements[i].value + '&';
                            }                        
                        }
                        else
                        {
                            for (var i = 1; i < Form.elements.length; i++) {
                                //Arrancamos de 1 para evitar el fieldset
                                sPost = sPost + Form.elements[i].name + '=' + Form.elements[i].value + '&';
                            }                        
                        }


                        $(this).dialog('close');
                        $.ajax(
                        {
                            async: true,
                            type: "POST",
                            dataType: "text/html",
                            contentType: "application/x-www-form-urlencoded",
                            url: Url,
                            data: sPost,
                            timeout: 10000,
                            beforeSend: function() {

                                $("#dialog-loading").html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + 'Loading....' + '</p>');
                                $("#dialog-loading").dialog({
                                    resizable: false,
                                    height: 100,
                                    modal: true,
                                    title: xsTitle
                                });
                            },
                            success: function(datos) {
                                $("#dialog-loading").dialog('close');
                                var Result = datos.split(',');
                                $("#dialog-response").html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + Result[0] + '</p>');
                                $("#dialog-response").dialog({
                                    resizable: false,
                                    height: 100,
                                    modal: true,
                                    title: xsTitle
                                });
                                setTimeout(CloseDialog, 3000);
                                if (Result[1] == 1 && BloqueEditar != "") {
                                    $("#" + BloqueEditar + "_" + xId).html(Result[2]);
                                }
                                
                            }
                        });
                    }

                },
                'Cancelar': function() {
                    $(this).dialog('close');
                }
            }
        });
    });

}