﻿
///Funcion que mostrará un mensaje de error devuelto en la llamada a un servicio web (Cuado un servicio web falla antes de ejjecutarlo el servidor)
function MostrarError(webServiceResult) {
    MostrarMensaje("Error en la llamada al servicio web",webServiceResult.get_message());
}

//Cuando recibimos un "callback" del servidor con un objeto result, y este nos indica que no ha ido bien la cosa (.Conexito = false),
// mandamos el result a esta función para que muestre la ventana de error
function MostrarResultadoSinExito(result) {
    MostrarMensaje(result.Operacion, result.Mensaje, result.Errores);
}
//Funcion genérica para mostrar mensajes
//Titulo y texto, pues eso...
//En errores meteremos un list (array) con mensajes de error, para que esta funcion las meta en una lista
function MostrarMensaje(titulo, texto, errores) {
    var html = "<div title='" + titulo + "'><p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>" + texto + "</p>";
    if (errores) {
        html += "<ul>";
        $(errores).each(function(indice, elemento) {
            html += "<li><span>" + elemento + "</span></li>";
        });
        html += "</ul>";
    }
    html += "</div>"
    $(html).dialog({
        resizable: false,
        modal: true,
        buttons: {
            'Cerrar': function() {
                $(this).dialog('close');
            }
        }
    });
}

//Función para pedir confirmación al usuario
//aldecirsi, es la funcion (o código) que se bebe ejecutar cuando se pulsa sobre el botón aceptar
//aldecirno, es la funcion (o código) que se bebe ejecutar cuando se pulsa sobre el botón Cancelar
function Confirmar(titulo, texto, aldecirsi, aldecirno) {
    var html = "<div title='" + titulo + "'><p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>" + texto + "</p>";    
    html += "</div>"
    $(html).dialog({
        resizable: false,
        modal: true,
        buttons: {
            'Aceptar': function() {
                $(this).dialog('close');
                if (jQuery.isFunction(aldecirsi))
                    aldecirsi.call();
            },
            'Cancelar': function() {
                $(this).dialog('close');
                if (jQuery.isFunction(aldecirno))
                    aldecirno.call();
            }
        }
    });
}

///funcion que nos devuelve el valor de un nodo de un treeview http://forums.asp.net/t/1361880.aspx
function GetNodeValue(node) {
    var nodeValue = "";
    var nodePath = node.href.substring(node.href.indexOf(",") + 2, node.href.length - 2);
    var nodeValues = nodePath.split("\\");
    if (nodeValues.length > 1)
        nodeValue = nodeValues[nodeValues.length - 1];
    else
        nodeValue = nodeValues[0].substr(1);

    return nodeValue;
}

//funcion que nos dice si un nodo es raiz
function EsNodoRaiz(nodo) {
    
    return nodo.pathname.indexOf("\\") == -1;
}
