﻿function CorrectSeparator(obj)
{
    if(obj.value.indexOf('.') > -1)
    {
        obj.value = obj.value.replace('.', ',');
    }
}

function CorrectValues(obj, direction)
{
    var elems;
    if(obj)
    {
        elems = new Array();
        elems[0] = obj;
        
        direction = obj.getAttribute("inc") != null ? 0 : 1;
    }
    else elems = document.getElementsByTagName('INPUT');
    
    var elems2 = document.getElementsByTagName('INPUT');
    
    var UsdUah = GetUsdUahValue();
    var UahUsd = GetUahUsdValue();
    for(var i = 0; i < elems.length; i++)
    {
        if(elems[i].getAttribute("usd") != null && (elems[i].getAttribute("inc") != null && direction == 0 || elems[i].getAttribute("exp") != null && direction == 1))
        {
            for(var j = 0; j < elems2.length; j++)
            {
                if(elems2[j].getAttribute("grn") != null && elems[i].getAttribute("currencyId") == elems2[j].getAttribute("currencyId") && (elems2[j].getAttribute("inc") != null && direction == 0 || elems2[j].getAttribute("exp") != null && direction == 1))
                {
                    elems2[j].value = ('' + (Math.round((GetFloatValue(elems[i]) * (direction == 0 ? UahUsd : UsdUah) - (direction == 0 ? 0 : 0.5)) * 1000) / 1000)).replace('.', ',');
                }
            }
        }
    }
}

function GetFloatValue(obj)
{   
        var v = parseFloat(obj.value.replace(',', '.'));
        if(isNaN(v) || v == Infinity)
            v = 0;
        
        return v;
}
   

    function ChangeRate(obj, direction)
    {
        var elems = document.getElementsByTagName('INPUT');
        var tmp;
        var tmpFirst;
        var CurrVal = parseFloat(obj.value.replace(',', '.'));
        var FirstValue = GetCurrValue(obj.id);
        if(FirstValue == null)
            FirstValue = parseFloat(obj.getAttribute("firstValue").replace(',', '.'));
        var dRate = FirstValue / CurrVal;
        for(var i = 0; i < elems.length; i++)
        {
            if((elems[i].getAttribute("inc") != null && direction == 0 || elems[i].getAttribute("exp") != null && direction == 1) && elems[i].getAttribute("grn") != null)
            {
                tmpFirst = GetCurrValue(elems[i].id);
                if(tmpFirst == null)
                    tmpFirst = parseFloat(elems[i].getAttribute("firstValue").replace(',', '.'));
                //tmp = tmpFirst * parseFloat(elems[i].value.replace(',', '.'));
                tmp = tmpFirst / dRate;// / CurrSelectedValue * CurrVal;
                tmp = Math.round(tmp * 1000) / 1000;
                //tmp = Math.round(direction = 0 ? tmp * 1000 : tmp * 1000 - 0.5) / 1000;
                elems[i].value = ('' + (isNaN(tmp) || tmp == Infinity  ? 0 : tmp)).replace('.', ',');
                
                if(tmp != Infinity && !isNaN(tmp) && tmp != 0)
                    SetCurrValue(elems[i].id, tmp);
            }
        }
        
        if(CurrVal != Infinity && !isNaN(CurrVal) && CurrVal != 0)
            SetCurrValue(obj.id, CurrVal);
//        CurrSelectedValue = CurrVal;
    }
    
    function CheckForModifications(obj)
    {
        var elems = document.getElementsByTagName('INPUT');
        var tmp = GetCurrValue(obj.id);
        if(tmp == null)
            tmp = parseFloat(obj.value.replace(',', '.'));
        var CurrVal = parseFloat(obj.value.replace(',', '.'));
        var Rate = tmp / CurrVal;
        
        
        for(var i = 0; i < elems.length; i++)
        {
            if(elems[i].getAttribute("currencyId") == obj.getAttribute("currencyId") 
                && obj.getAttribute("usd") != null && elems[i].getAttribute("grn") != null 
                && ((elems[i].getAttribute("inc") != null && obj.getAttribute("inc") != null) 
                    || (elems[i].getAttribute("exp") != null && obj.getAttribute("exp") != null)))
            {
                tmp = GetCurrValue(elems[i].id);
                if(tmp == null)
                    tmp = parseFloat(elems[i].value.replace(',', '.'));
                tmp = tmp / Rate;// CurrSelectedValue * CurrVal;
                tmp = Math.round(elems[i].getAttribute("inc") != null ? tmp * 1000 : tmp * 1000 - 0.5) / 1000;
                elems[i].value = ('' + (isNaN(tmp) || tmp == Infinity ? 0 : tmp)).replace('.', ',');
                
                if(tmp != Infinity && !isNaN(tmp) && tmp != 0)
                    SetCurrValue(elems[i].id, tmp);
            }
        }
        if(CurrVal != Infinity && !isNaN(CurrVal) && CurrVal != 0)
            SetCurrValue(obj.id, CurrVal);
        else if(obj.getAttribute("grn") != null && CurrVal == 0)
            SetCurrValue(obj.id, CurrVal);
    }

    // MENU
    var MainMenuItems = new Array();
    
    function initializeMenuItem(ClientID, IsSelected, IsEnabled, IsSeparator)
    {
        var mi = new menuItem();
        mi.clientid = ClientID;
        mi.selected = IsSelected;
        mi.enabled = IsEnabled;
        mi.separator = IsSeparator;
        
        mi.index = MainMenuItems.length;
        MainMenuItems[MainMenuItems.length] = mi;
    }
    
    function menuItem()
    {
        this.clientid = null;
        this.selected = null;
        this.enabled = null;
        this.separator = null;
        
        this.index = null;
        
        this.next = function()
        {
            if(this.index < MainMenuItems.length - 1)
                return MainMenuItems[this.index + 1];
            else
                return null;
        }
        this.prev = function()
        {
            if(this.index > 0)
                return MainMenuItems[this.index - 1];
            else
                return null;
        }
    }
    
    function changeMenuItemStatus(iItemIndex, bActivate)
    {
        var mi = MainMenuItems[iItemIndex];
        if(!mi.separator)
        {
            var _hlk = document.getElementById(mi.clientid);
            //var _img = _hlk.getElementsByTagName('img')[0];
            if(bActivate)
            {
                _hlk.className += ' activated';
           //     _img.src = _img.src.toLowerCase().replace('.jpg', '_activated.jpg');
            }
            else
            {
                _hlk.className = _hlk.className.replace(' activated', '');
            //    _img.src = _img.src.toLowerCase().replace('_activated.jpg', '.jpg');
            }
            
            if(mi.prev() != null && mi.prev().separator)
                changeMenuItemStatus(mi.prev().index, bActivate);
            if(mi.next() != null && mi.next().separator)
                changeMenuItemStatus(mi.next().index, bActivate);
        }
        else
        {
            var _img = document.getElementById(mi.clientid);
            if(!mi.selected && mi.enabled)
            {
                if(bActivate)
                {
                    _img.className += ' activated';
               //     _img.src = _img.src.toLowerCase().replace('.jpg', '_activated.jpg');
                }
                else
                {
                    _img.className = _img.className.replace(' activated', '');
                //    _img.src = _img.src.toLowerCase().replace('_activated.jpg', '.jpg');
                }
                //_img.style.visibility = (bActivate ? 'hidden' : 'visible');
            }
        }
}
