﻿// ---------------------------------------------------------------------
// Description:	CommInterface Client Script file
// 
// Autor:			Hans Hawe
// Date:			20.09.2007
// 
// (c) by team ModulAcht. All rights reserved
// ---------------------------------------------------------------------

// ---------------------------------------------------------------------
// Required script files:                   Common/prototype.js
//                                          Common/KeyValueParser.js
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------

var CommInterface = Class.create();
CommInterface.prototype =
{
    // ---------------------------------------------------------------------
    // Class Constants
    // ---------------------------------------------------------------------

    // ---------------------------------------------------------------------
    // Constructor
    // --------------------------------------------------------------------- 
    initialize : function(elementId)
    {   
        // ---------------------------------------------------------------------
        // member initialization
        // ---------------------------------------------------------------------        
        this.keyValueParser = new KeyValueParser(elementId);  
      
    },
    
    // ---------------------------------------------------------------------
    // Operations
    // ---------------------------------------------------------------------
    
    // ---------------------------------------------------------------------
    // checks whether param key already existst,
    // if so, adds new value to param, and sets old value
    // splits new/old value using '|' as delimiter
    //
    // key:                     key to be added/edited
    // value:                   value for specified key
    // ---------------------------------------------------------------------
    add : function(key, value)    
    {
        // if keyValueParser contains param key
        if(this.keyValueParser.contains(key))
        {            
            // get value/s of key
            var currentValue = this.keyValueParser.get(key);
            
            // split values 
            var elements = currentValue.split('|');
            
            // if their is no value or the "servervalue" is empty
            if(elements.length < 1 || elements[0] == "")
            {
                // add new value
                this.keyValueParser.add(key, "|" + value)
                
            }
            // if their is a server value not empty
            else
            {
                // append new value to old value
                this.keyValueParser.add(key, elements[0] + "|" + value);                
            }     
        }
        else
        {
            // if key is not existend in keyValueparser, add key and its value; leave servervalue empty
            this.keyValueParser.add(key, "|" + value);
        }             
        
        // set the current key-value
        this[key] = this.keyValueParser.get(key);        
    },
    
    // ---------------------------------------------------------------------
    // gets the current value of specified key; returns severvalue, if clientvalue is empty
    // ---------------------------------------------------------------------
   get : function(key)
    {
        // if the given key exists
        if(key != null)
        {
            //get current value
           var temp = this._getClientValue(key);
           
           // if client value is not existent
           if(temp != "")
           {
                // return current client value
                return temp;
           }
           // otherwise return current server value
           else return this._getServerValue(key);   
        }
    },
    
    // ---------------------------------------------------------------------
    // gets the servervalue of the key
    // ---------------------------------------------------------------------
    _getServerValue : function(key)
    {
        // get values
        var temp = this.keyValueParser.get(key);
        // if there is any value
        if(temp != null)
        {
            //split value
            var splitted = temp.split("|");
            // return server(first) value
            return splitted[0];            
        }        
    },
    
    
    // ---------------------------------------------------------------------
    // gets the clientvalue of the key
    // ---------------------------------------------------------------------
    _getClientValue : function(key)
    {
        // get values
        var temp = this.keyValueParser.get(key);
        // of there is any value
        if(temp != null)
        {
            // split value
            var splitted = temp.split("|");
            // return client (second) value
            return splitted[1];
        }
        // otherwise return empty value
        else return "";
    }    
};