//============================================================================\\
//                          Objetos para generar XML                          \\
//============================================================================\\
/**
 * Objeto Nodo XML 
 */
function  XMLNode (newNodeName,newNodeValue) {
  //Atributos:
        var nodeName;           // Nombre del nodo
        var attrs;              // Atributos de un nodo
        var nodeText;           // Texto del nodo
        var childNodes;         // SubNodos

        // Inicializacion
        this.nodeName = newNodeName;
        this.nodeText = newNodeValue;
        this.attrs = new Array();
        this.childNodes = new Array();
        
  //Métodos:        
        this.addAttribute = XMLNode_addAttribute;
        this.addNode = XMLNode_addNode;
        this.toXML = XMLNode_toXML;
}

// Añade un atributo a la lista de atributos del nodo
function XMLNode_addAttribute(newAttribute) {
    if (newAttribute != null && newAttribute != "")
        this.attrs[this.attrs.length] = newAttribute;
}
// Añade un nodo a la lista de subnodos del nodo
function XMLNode_addNode(newNode) {
    if (newNode != null) this.childNodes[this.childNodes.length] = newNode;
}
// Devuelve el nodo en formato XML
function XMLNode_toXML() {
    var strXML = "";
    strXML += "<" + this.nodeName;
    for (var i=0; i < this.attrs.length; i++) {
        strXML += (this.attrs[i].attrValue == null || this.attrs[i] == "" ? "" : (" " + this.attrs[i].attrName + "='" + this.attrs[i].attrValue + "'") );
    }
    if (this.nodeText != null || this.childNodes.length > 0) {
        strXML += ">";
        strXML += (this.nodeText == null || this.nodeText == "" ? "" : this.nodeText);
        if (this.childNodes.length > 0) {
            for (var j=0; j < this.childNodes.length; j++) {
                strXML += this.childNodes[j].toXML();
            }
        }
        strXML += "</" + this.nodeName + ">";
    } else {
        strXML += "/>";
    }
    return strXML;
}
//___________________________________________________________________________\\
/**
 * Objeto Atributo
 */
function XMLAttribute (newAttrName,newAttrValue) {
    // Atributos
        var attrName;   // Nombre del atributo
        var attrValue;  // Valor del atributo

    // Inicializacion
        this.attrName = newAttrName;
        this.attrValue = newAttrValue;
}




//============================================================================\\
//          Obtencion del XML a partir de los datos del formulario            \\
// Esta es la unica parte que hay que modificar para adaptarla a cada form    \\
// Lo normal es NO UTILIZAR FORMULARIOS en la página HTML, sino que hay que   \\
// simularlos de la siguiente forma:                                          \\
//      <div id='nombreSeccion'>                                              \\
//           <elementoHTML name='nombreElemento'>___</elementoHTML>           \\
//           <elementoHTML name='nombreElemento'>___</elementoHTML>           \\
//      </div>                                                                \\
//      (se pueden tener tantos divs con secciones como se quiera)            \\
//      (se pueden tener divs anidados, simulando subsecciones)               \\
// Lo unico que hay que tener en cuenta es que no hay que repetir el nombre   \\
// de los elementos html dentro de un div                                     \\
//============================================================================\\
/**
 * Obtiene un nodo con todos los subnodos XML a partir de una lista de divs
 * cada uno de los cuales contiene la información de un nodo
 *
 * @param divList: Una lista con los divs que contien la informacion del nodo
 * @param nodeListName: El nombre del nodo lista
 * @param nodeLoaderFunctionName: La funcion a llamar para cargar cada nodo
 */
function getNodeListFromDivs(divList,nodeListName,nodeLoaderFunctionName) {
    if ( divList == null ) return null; // No hay ningun indicador

    var nodeList = new XMLNode(nodeListName);
    if ( divList.length == null) { 
        // Se trata de un solo elemento
        if (divList.tagName != "DIV") return;
        nodeList.addNode( eval(nodeLoaderFunctionName + "(divList)") )
    } else {
        // Hay varios elementos
        for (var i=0; i < divList.length; i++) {
        	if (divList[i].tagName != "DIV") continue;
            nodeList.addNode( eval(nodeLoaderFunctionName + "(divList[i])") );
        }
    }  
    return nodeList;  
}
/**
 * Obtiene un nodo con todos los subnodos XML a partir de una lista de 
 * seleccion multiple
 * @param: selBox: La caja de seleccion multiple
 * @param nodeListName: El nombre del nodo lista
 * @param nodeLoaderFunctionName: La funcion a llamar para cargar cada nodo
 */
function getNodeListFromSelectBox(selBox,nodeListName,nodeLoaderFunctionName) {
    if (selBox == null) return null;
    if (selBox.options.length == 0) return null;

    var nodeList = new XMLNode(nodeListName);
    for (var i=0; i < selBox.options.length; i++) {
        nodeList.addNode( eval(nodeLoaderFunctionName + "(selBox.options[i])") );
    }  
    return nodeList;
}
