﻿/*
Javascript data object holding data on a single configuration parameter
*/

var FieldObject = Class.create();

FieldObject.prototype =
{
    // Constructor
    initialize : function(piFieldXmlNode, piEntityType)
    {
        // Declare private variable holding the type of the entity the field belongs to
        this._entityType = piEntityType;
        
        // Declare variable holding the system name of the field
        this.SystemName;
        // Declare variable holding the display name of the field
        this.DisplayName;
        // Declare vafriable holding the value for the active culture
        this.Value;
        // Declare variable holding the current lingo values of the field
        this.Values = new Hash();
        // Declare variable holding all valid values for this field
        this.ValueList = new Array();
        
        // Declare variable holding the field's xml data
        this._xmlData = piFieldXmlNode;
        
        // Call ProcessXmlData to fill the variables
        this.ProcessXmlData();    
    },
    
    ProcessXmlData : function()
    {
        try
        {
            // Fill SystemName variable
            this.SystemName = this._xmlData.attributes.getNamedItem('NAME').value;
            // Fill DisplayName
            this.DisplayName = Lingo.GetLingoItem(this.SystemName, this._entityType.toUpperCase());
                    
            // Get the value xml nodes
            var valueNodes = this._xmlData.SelectNodes('VALUE');
            // Iterate through xml node list and push each value list item into Values Hash
            for(var i = 0; i < valueNodes.length; i++)
            {
                // Get the culture key of the current value
                var cultureKeyAttribute = valueNodes[i].attributes.getNamedItem('CULTURE');
                // Check if the culture key attribute is empty and set to current user culture key if so
                if((cultureKeyAttribute == null) || (cultureKeyAttribute.value.blank()))
                {
                    var cultureKey = Site.User.Language;
                }
                else
                {
                    var cultureKey = cultureKeyAttribute.value;
                }
                
                this.Values[cultureKey] = valueNodes[i].text;
            }
            
            // Get the valuelist xml nodes
            var valueListNodes = this._xmlData.SelectNodes('VALUELIST/VALUE');
            // Iterate through xml node list and push each valuelist item into ValueList Array
            for(var j = 0; j < valueListNodes.length; j++)
            {
                this.ValueList.push(valueListNodes[j]);
            }
            
            // Set lingo value
            this.Value = this.Values[Site.User.Language];
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'ProcessXmlData', arguments, 'FieldObject', null, e, false)
        }
    }
}