﻿// ---------------------------------------------------------------------
// Description:	TestControl
// 
// Autor:			Herbert Granofszky
// Date:			01.08.2007
// 
// (c) by team modulacht. All rights reserved
// ---------------------------------------------------------------------

// ---------------------------------------------------------------------
// Required script files:                 Common/prototype.js
// ---------------------------------------------------------------------

var KeyValueParser = Class.create();
KeyValueParser.prototype = 
{  
    // ---------------------------------------------------------------------
    // Constants Definitions
    // ---------------------------------------------------------------------
    
    KEYPREFIX : "__dvKey__",
    
    // ---------------------------------------------------------------------
    // Constructor
    // ---------------------------------------------------------------------    
    initialize : function(elementId)
    {
        // ---------------------------------------------------------------------
        // Member Init
        // ---------------------------------------------------------------------
        this.id = elementId;
        this.thisObject = this;
        this.element = $(elementId);
    },
    
    destroy : function(argId)
    {
        this.element = null;
        this.thisObject = null;
        this.id = null;
    },
      
    // ---------------------------------------------------------------------
    // Operations
    // ---------------------------------------------------------------------
        
    // ---------------------------------------------------------------------
    // Adds a key/value pair to the list
    //
    // key                          :   The key under which the value is 
    //                                  stored
    // value                        :   The value to be stored
    // ---------------------------------------------------------------------
    add : function(key, value) 
    {
        // check if the types are really strings and convert them eventually
        if (typeof key != "string")
        {
            key = key.toString();
        }
        if (typeof value != "string")
        {
            value = value.toString();
        }
        
        // sanity check:    the '=' - character is not allowed for use in a key
        if (key.indexOf('=') != -1) return -1;
        
        // sanity check:    KEYPREFIX mustn't be a substring of the value
        if (value.indexOf(this.KEYPREFIX) != -1) return -1;
    
        // check whether the key is already in the list
        var valueStartIndex = this.getValueIndex(key);
        if (valueStartIndex != -1) 
        {
            // key already exists -> replace the value
            var splitted = this.getListString().split(this.KEYPREFIX + key + "=" + this.get(key));
            var newString = splitted[0] + this.KEYPREFIX + key + "=" + value + splitted[1];
        } 
        else
        {
            // it's a new key -> append it to the string
            var newString = this.getListString() + this.KEYPREFIX + key + "=" + value;
        }
        this.setListString(newString);
    },
    
    // ---------------------------------------------------------------------
    // Clears the list
    // ---------------------------------------------------------------------
    clear : function() 
    {
        this.setListString(""); 
    },
    
    // ---------------------------------------------------------------------
    // Checks whether there is a key exists in the list
    //
    // key                          :   The key to be searched for in the 
    //                                  list
    // returns      true            :   The list contains the key
    //              false           :   otherwise
    // ---------------------------------------------------------------------
    contains : function(key) 
    {
        return this.get(key) != null;
    },
    
    // ---------------------------------------------------------------------
    // Retrieves the value stored under the specified key
    //
    // key                          :   The key to be searched for in the
    //                                  list
    // returns the value stored under the specified key or <null> if there
    // is no value stored under the specified key
    // ---------------------------------------------------------------------
    get : function(key) 
    {
        if (typeof key != "string")
        {
            key = key.toString();
        }
        var startIndex = this.getValueIndex(key);
        if (startIndex == -1) return null;
        
        var endIndex = this.getListString().indexOf(this.KEYPREFIX, startIndex);
        if (endIndex != -1) 
        {
            return this.getListString().substring(startIndex, endIndex);
        }
        else
        {
            return this.getListString().substring(startIndex);
        }
    },
    
    // ---------------------------------------------------------------------
    // Removes a key/value pair from the list
    //
    // key                          :   The key of the pair to be removed
    // ---------------------------------------------------------------------
    remove : function(key) 
    {
        var startIndex = this.getKeyIndex(key);
        if (startIndex == -1) return;
        
        var endIndex = this.getValueEndIndex(key);
        var newString = this.getListString().substring(0, startIndex) + this.getListString().substring(endIndex);
        this.setListString(newString);
    },
    
    
    // ---------------------------------------------------------------------
    // Private Operations
    // ---------------------------------------------------------------------
    
    
    // ---------------------------------------------------------------------
    // Retrieves the key/value pair - list (String)
    // ---------------------------------------------------------------------
    getListString : function() 
    {
        return this.element.value;
    },
    
    
    // ---------------------------------------------------------------------
    // Stores the specified string as the key/value - list String in the
    // element associated with this String
    // 
    // newString                     :  The String to be set
    // ---------------------------------------------------------------------
    setListString : function(newString) {
        this.element.value = newString;
    },
    
    
    // ---------------------------------------------------------------------
    // Retrieves the character index of the specified key in the list
    //
    // key                          :   The key whose character index is
    //                                  requested
    // ---------------------------------------------------------------------
    getKeyIndex : function(key) 
    {
        return this.getListString().indexOf(this.KEYPREFIX + key);
    },
    
    
    // ---------------------------------------------------------------------
    // Retrieves the character index of the beginning of the value
    // associated with the specified key
    //
    // key                          :   The key
    // ---------------------------------------------------------------------
    getValueIndex : function(key) 
    {
        var keyIndex = this.getKeyIndex(key);
        if (keyIndex == -1) return -1;
        
        return keyIndex + this.KEYPREFIX.length + key.length + 1;
    },
    
    
    // ---------------------------------------------------------------------
    // Retrieves the character index of the end of the value associated with
    // the specified key
    //
    // key                          : The key
    // ---------------------------------------------------------------------    
    getValueEndIndex : function(key) 
    {
        var valueStartIndex = this.getValueIndex(key);
        if (valueStartIndex == -1) return -1;
        
        var valueEndIndex = this.getListString().indexOf(this.KEYPREFIX, valueStartIndex);
        if (valueEndIndex == -1)
        {
            return this.getListString().length;
        }
        else {
            return valueEndIndex;
        }
    }
};