﻿function DropDown()
{
    this.lockWait = false;
    this.findPosition = function(oElement)
    {
        if (typeof (oElement.offsetParent) != 'undefined')
        {
            for (var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent)
            {
                posX += oElement.offsetLeft;
                posY += oElement.offsetTop;
            }
            return [posX, posY];
        } else
        {
            return [oElement.x, oElement.y];
        }
    };

    // nastala zmena v inpute a vola sa webservis
    this.zmena = function(ID, maxCount, cisSelectId, cisConditionId, extParam)
    {
        a = this.findPosition(getE(ID));
        getE('list' + ID).style.left = a[0] + 'px';
        getE('list' + ID).style.top = (a[1] + 20) + 'px';
        this.startCount = 0;
        this.cakanieScroll = false;
        this.itIsAll = false;
        this.scrollTop = 0;

        if (getE('list' + ID).style.display == 'block')
        {
            getE('list' + ID).style.display = 'none';
        }
        else
        {
            document.body.onclick = function() { getE('list' + ID).style.display = 'none'; };
            // volanie webservisu
            text = '';
            if (getE(ID + 'Val').value == '')
            {
                text = getE(ID).value;
            }

            Sys.Net.WebServiceProxy.invoke('Moduly/WebServices/CisKoder.asmx', 'GetDataHTML', false, { "textSearch": text, "startCount": 0, "maxCount": maxCount, "selectedID": cisSelectId, "selectedIDKP": cisConditionId, "clientID": ID, "extraParam": extParam }, this.PutIn, this.Failed, null);
            // koniec volania web servisu        
        }
    };

    this.PutIn = function(resp)
    {
        if (resp.selectedID == '')
        {
            getE('list' + resp.id).innerHTML = resp.html;
            getE('list' + resp.id).style.display = 'block';
            // nastavenie sirky listu
            getE('list' + resp.id).style.width = getE(resp.id).clientWidth + 'px';
            // nastavenie vysky listu
            lis = getE('list' + resp.id).getElementsByTagName('a');
            if (getE('list' + resp.id).clientHeight > lis[lis.length - 1].offsetTop)
            {
                getE('list' + resp.id).style.height = lis[lis.length - 1].offsetTop + 20;
                CisDataField.itIsAll = true;
            }
        }
        else // som koderom
        {
            CisDataField.koder(resp.selectedID, resp.conditionID, resp.id);
        }

    };

    this.PutInAdd = function(resp)
    {
        getE('list' + resp.id).innerHTML += resp.html;
        getE('list' + resp.id).style.display = 'block';
        CisDataField.itIsAll = resp.itIsAll; 
    };
    
    // pouzita v CisKoder.cs
    this.SetValue = function(value, id, clientID)
    {
        getE(clientID).value = value;
        getE(clientID + 'Val').value = id;
        getE('list' + clientID).style.display = 'none';
        if (getE(clientID + 'Val').onchange)
        {
            var onChangeFunction = getE(clientID + 'Val').onchange;
            onChangeFunction();
        }
    };
    this.Failed = function(error)
    {
        if (barISPage.adminMode == 1 || barISPage.debug == 1)
        {
            alert('ADMIN:' + error.get_message());
        }
        else
        {
            alert('Chyba požiadavky,ak problém pretrvá kontaktujte administrátora.');
        }
    };
    this.Wait = function(clientID, maxCount, cisSelectId, cisConditionId, extParam, waitTime)
    {
        this.startCount = 0;
        this.cakanieScroll = false;
        this.itIsAll = false;
        this.scrollTop = 0;
        
        value = getE(clientID).value;

        if (this.timo != null)
            clearTimeout(this.timo);
        
        this.timo = setTimeout("CisDataField.TimeOutChange('" + clientID + "', " + maxCount + ", '" + cisSelectId + "', '" + cisConditionId + "', '" + extParam + "', " + waitTime + ", '" + value + "')", waitTime);
        if (value == '')
            getE('list' + clientID).style.display = 'none';
    };

    this.timo = null;

    this.TimeOutChange = function(clientID, maxCount, cisSelectId, cisConditionId, extParam, waitTime, value)
    {
        if (value != '')
        {
            if (getE(clientID).value != value)
            {
                CisDataField.Wait(clientID, maxCount, cisSelectId, cisConditionId, extParam, waitTime);
            }
            else
            {
                getE('list' + clientID).style.display = 'none';
                CisDataField.zmena(clientID, maxCount, cisSelectId, cisConditionId, extParam);
            }
        }
        else
        {
            getE('list' + clientID).style.display = 'none';
        }
    };

    this.setWaitScroll = function(wait)
    {
        this.cakanieScroll = wait;
    };

    this.scroll = function(ID, cisSelectId, cisConditionId, extParam)
    {
        var obj = getE('list' + ID);
        if (CisDataField.itIsAll == false)
        {
            if (CisDataField.cakanieScroll == false)
            {
                var height = obj.clientHeight + 20;
                if (obj.scrollTop > this.scrollTop && obj.scrollTop + height > obj.scrollHeight)
                {
                    this.scrollTop = obj.scrollTop;
                    this.startCount = this.startCount + 200;
                    maximCount = 200;
                    this.cakanieScroll = true;
                    // volanie webservisu
                    text = '';
                    if (getE(ID + 'Val').value == '')
                    {
                        text = getE(ID).value;
                    }

                    Sys.Net.WebServiceProxy.invoke('Moduly/WebServices/CisKoder.asmx', 'GetDataHTML', false, { "textSearch": text, "startCount": this.startCount, "maxCount": maximCount, "selectedID": cisSelectId, "selectedIDKP": cisConditionId, "clientID": ID, "extraParam": extParam }, this.PutInAdd, this.Failed, null);
                    // koniec volania web servisu
                    setTimeout("CisDataField.setWaitScroll(false)", 1800);
                }
            }
        }
    };

    this.startCount = 0;
    this.cakanieScroll = false;
    this.itIsAll = true;
    this.scrollTop = 0;

    this.koder = function(selectID, conditionID, clientID)
    {
        if (typeof (iPhone) == 'undefined')
        {
            Koder(selectID, clientID, 'False', 'False', '');
        }
        else
        {
            var koderDiv = document.createElement('div');  
            koderDiv.setAttribute('id', 'koder');

            var DivHlavicka = document.createElement('div');
            DivHlavicka.className = 'toolbar';
            DivHlavicka.innerHTML = '<h1 id="koderNadpis">Vyberte si položku</h1><a href="#" class="backBaris" onclick="CisDataField.VymazKoder()"></a>';
            DivHlavicka.style.display = 'block';

            var koderBody = document.createElement('div');
            koderBody.setAttribute('id', 'koderBody');
            koderBody.style.display = 'block';

            koderDiv.style.position = 'absolute';
            koderDiv.style.left = '0px';
            koderDiv.style.top = '0px';
            koderDiv.style.width = '100%';
            koderDiv.style.height = '100%';
            koderDiv.style.display = 'block';
            koderDiv.style.backgroundColor = 'white';
            var obal = document.createElement('div');
            obal.appendChild(DivHlavicka);
            obal.appendChild(koderBody);
            koderDiv.appendChild(obal);
            document.body.appendChild(koderDiv);

            iDetail(312, 'selId;' + selectID + ';conId;' + conditionID + ';dfId;' + clientID, '2', koderBody, 'koderBody');
            
            iPhone.iShow('koder',false);
        }
    };

    this.Predpln = function(value,text, clientID)
    {
        getE(clientID).value = text;
        getE(clientID+'Val').value = value;
        // vymazanie nalepky 
        CisDataField.VymazKoder();
        // vymazanie nalepky
    };

    this.VymazKoder = function()
    {
        var koder = getE('koder');
        if (koder != null)
        {
            iPhone.iShow('koder',true);
            koder.parentNode.removeChild(koder);
        }
    };

    this.init = function(json)
    {
        AMOnlyOnWrite = 0;
        AMOnlyOnButtClick = 1;
        AMWriteAndButtClick = 2;

        // text input
        var iText = getE(json.id);
        iText.readOnly = json.readOnly;
        iText.style.width = json.width;
        if (json.rightClick == true)
        {
            iText.setAttribute("onContextMenu", 'BarISMenuCis(0,' + json.selectId + ',this);return false');
        }

        if (json.readOnly == false && json.ActionMode != AMOnlyOnButtClick)
        {
            iText.setAttribute("onKeyDown", 'CisDataField.Wait(\'' + json.id + '\' ,' + json.maxCount + ', \'' + json.selectId + '\',\'' + json.conditionId + '\',\'' + json.extraParam + '\', ' + json.waitTime + ');getE(\'' + json.id + 'Val\').value = \'\'');
        }

        // hidden field
        var iHidden = getE(json.id + 'Val');
        if (json.onChange != '')
        {
            iHidden.onChange = json.onChange;
        }

        // button 
        var bImage = getE('button' + json.id);
        if (json.readOnly == false && json.ActionMode != AMOnlyOnWrite)
        {
            if (json.cssClass != '')
            {
                bImage.className = json.cssClass + 'Image';
            }
            else
            {
                bImage.className = 'DefaultDropDownImage';
            }
            bImage.setAttribute("onClick", "CisDataField.zmena(\'" + json.id + "\'," + json.maxCount + ", \'" + json.selectId + "\',\'" + json.conditionId + "\',\'" + json.extraParam + "\')");
        }
        // list roleta

        var lRoleta = getE('list' + json.id);
        if (json.cssClass != '')
        {
            lRoleta.className = json.cssClass + 'List';
        }
        else
        {
            lRoleta.className = 'DefaultDropDownList';
        }
        lRoleta.style.display = 'none';
        if (json.orezText == true)
        {
            lRoleta.style.overflow = 'hidden';
        }
        if (json.widthList != '')
        {
            lRoleta.width = json.widthList;
        }

        //// dolievanie pri scrollovani
        lRoleta.setAttribute("onScroll", 'CisDataField.scroll(\'' + json.id + '\', \'' + json.selectId + '\', \'' + json.conditionId + '\', \'' + json.extraParam + '\')');
        //// dolievanie pri scrollovani END
    }
}

CisDataField = new DropDown();


