﻿function Attributes(){
    this.attributesValue = new Array();
    this.attributesName = new Array();
}

Attributes.prototype = {
    getCount:function(){
        return this.attributesName.length;
    },
    
    getName:function(index){
        return this.attributesName[index];
    },
    
    getValue:function(index){
        return this.attributesValue[index];
    },
    
    get:function(name){
        var index = findIndex(name);
        if(index > -1){
            return this.attributesValue[index];
        }
        return null;
    },
    
    findIndex:function(name){
        for(var i = 0; i < this.attributesName.length; i++){
            if(this.attributesName[i] == name){
                return i;
            }
        }
        return -1;
    },
    
    set:function(name, value){
        var index = this.findIndex(name);
        if(index > -1){
            this.attributesValue[index] = value;
        }else{
            this.attributesValue.push(value);
            this.attributesName.push(name);
        }
    },
    
    clear:function(){
        for( var i = 0; i < this.attributesName.length; i++ ){
            this.attributesName.pop(); this.attributesValue.pop();   
        }
    },
    
    remove:function(name){
        var index = findIndex(name);
        if(index > -1){
            this.attributesValue.splice(index, 1);
            this.attributesName.splice(index, 1);
        }
    },
    
    removeAt:function(index){
        if(index > -1 && index < attributesName.length){
            this.attributesValue.splice(index, 1);
            this.attributesName.splice(index, 1);
        }
    } 
}


function MessageSubElements(parent){
    this.parent = parent;
    this.subElements = new Array();
}

MessageSubElements.prototype = {
    getCount:function(){
        return this.subElements.length;
    },
    
    add:function(tag){
        var m = new MessageElement(tag, this.parent);
        this.subElements.push(m);
        return m;
    },
    
    get:function(tagName){
        var a = new Array();
        for(var i = 0; i < this.subElements.length; i++){
            if(this.subElements[i].tag == tagName){
                a.push(this.subElements[i]);
            }
        }
        return a;
    },
    
    getByIndex: function(index){
        return this.subElements[index];
    },
    
    remove:function(elem){
        var index = -1;
        for(var i = 0; i < this.subElements.length; i++){
            if(this.subElements[i] == elem){
                index = i;
                break;
            }
        }
        
        if(index > -1){
            this.subElements[i].parent = null;
            this.subElements.splice(i, 1);
        }
    }
}


function MessageElement(tagName, parent){
    this.parent = parent;
    this.tag = tagName;
    this.attributes = new Attributes();
    this.subElements = new MessageSubElements(this);
    this.text = "";
}

MessageElement.prototype = {
        toXml:function(){
        var str = "<" + this.tag + " ";
        
        for(var i = 0; i < this.attributes.getCount(); i++){
            str +=  this.attributes.getName(i) + "='" + encodeXml(this.attributes.getValue(i)) + "' ";
        }
        
        if(this.subElements.getCount() == 0 && (this.text == "" || this.text == null)){
            str += "/>";
        }else{
            str += ">";
            if(this.text != "" && this.text != null){
                str += encodeXml(this.text);
            }
            for(var i = 0; i < this.subElements.getCount(); i++){
                str += this.subElements.getByIndex(i).toXml();
            }
            str += "</" +  this.tag + ">";
        }
        return str;
    }
}

function Message(){
    this.id = 0;
    this.command = "";
    this.schema = "http://www.SimpleConnexion.com/basic/1.0";
    this.type = "request";
    this.attributes = new Attributes();
    this.subElements = new MessageSubElements(this);
    this.text = "";
}

Message.prototype = {
    toXml:function(){
        var str = "<msg ";
        
        if(this.id > 0){
            str += "id='" + this.id + "' ";
        }
        
        if(this.command != "" && this.command != null){
            str += "command='"+ this.command + "' ";
        }
        
        if(this.schema != "" && this.schema != null){
            str += "schema='"+ encodeXml(this.schema) + "' ";
        }
        
        if(this.type != "" && this.type != null){
            str += "type='"+ encodeXml(this.type) + "' ";
        }
        
        for(var i = 0; i < this.attributes.getCount(); i++){
            str +=  this.attributes.getName(i) + "='" + encodeXml(this.attributes.getValue(i)) + "' ";
        }
        
        if(this.subElements.getCount() == 0 && (this.text == "" || this.text == null)){
            str += "/>";
        }else{
            str += ">";
            if(this.text != "" && this.text != null){
                str += encodeXml(this.text);
            }
            for(var i = 0; i < this.subElements.getCount(); i++){
                str += this.subElements.getByIndex(i).toXml();
            }
            str += "</msg>";
        }
        
        return str;
    }
}


