﻿// JScript File
    function showPanel(controlid) {
        var hiddentElements = document.getElementById(controlid + '_HiddenElements');
        if (hiddentElements)
            hiddentElements.style.display = '';
        var popup = ASPxClientPopupControl.GetPopupControlCollection().Get(controlid+'_popupSelect');
        
        LoadItems(controlid);
        popup.ShowAtElementByID(controlid+'_btnedPopup');
        return false;
    }
    
    //Item class
    function Item(id,text)
    {
        this.id = id;
        this.text = text;
        this.toString = function()
        {
            var result = this.id+"|"+this.text;
            return result;
        }
        this.parse = function(value)
        {
            var arrFields = value.split('|');
            this.id=arrFields[0];
            this.text=arrFields[1]; 
        }
    }
    var Items = function()
    {
        this.capacity=0;
        this.indexOf = function(id)
        {
            for(var i=0; i<this.capacity;i++)
            {
                if (this[i] && this[i].id ==id)
                    return i;
            }
            return -1;
        }
        this.Delete = function(id)
        {
            var index = this.indexOf(id);
            if (index>=0)
                delete(this[index]);
        }
        this.Length = function()
        {
            var count=0;
            for(var i=0; i<this.capacity;i++)
            {
                if (this[i])
                    count++;
            }
            return count;
        }
    };
    
    Items.prototype.Add = function(item)
        {
        
            var index = this.indexOf(item.id)
            if(index>=0)
                return index
            else
            {   
                var newIndex = this.capacity;
                
                this[newIndex] = item;
                
                this.capacity++;
                return newIndex;
            }
        }
    Items.prototype.toString = function()
        {
            var result = "";
            for(var i=0;i<this.capacity;i++)
            {
                if (this[i])
                { 
                    result+=this[i].toString();
                    if (i<this.capacity-1)
                        result+="][";
                }
            }
            return result;
        }
    Items.prototype.parse = function(value)
    {
        if(value=='' || value ==null || value =='undefined') return;
        var arrItems = value.split('][');
        for(var i=0;i<arrItems.length;i++)
        {
            if (arrItems[i]!='' && arrItems[i]!=null)
            {
                var item = new Item();
                item.parse(arrItems[i]);
                this.Add(item);
            }
        }
    }
    Items.prototype.Clear = function()
    {
         for(var i=0;i<this.capacity;i++)
            {
                if (this[i])
                { 
                    delete(this[i])
                }
            }
        this.capacity = 0;
    }
       
    function SelectedItems(owner)
    {
        this.itemList = new Items()
        this.Owner = owner;
        this.Update = function()
        {
            this.Owner.innerHTML = "";
            var ownerid = this.Owner.id
            for(var i=0;i<this.itemList.capacity;i++)
            {
                if (this.itemList[i])
                {
                    var link = document.createElement('a');
                    link.innerHTML = this.itemList[i].text;
                    link.setAttribute('itemid',this.itemList[i].id);
                    link.onclick=function(){DeleteItem(ownerid,this);}
                    this.Owner.appendChild(link);
                    if (i!=this.itemList.capacity-1)
                    {
                        this.Owner.appendChild(document.createTextNode('; '));
                    }
                        
                }
            }
            
            var controlid=this.Owner.id.replace('_popupSelect_pnlItems','');
            var controlElement = document.getElementById(controlid);
            //if (controlElement)
            controlElement.SelectedValues = this.Owner.AttachedObj.itemList;
            controlElement.AttachedObj = this.Owner.AttachedObj;
            controlElement.PerformCallback=function(parameter){return MscPerformCallback(this.id,parameter);}
        }
        this.Clear = function()
        {
            this.Owner.AttachedObj.itemList.Clear();
            this.Owner.AttachedObj.Update();
        }
    }
    function DeleteItem(panelid,sender)
    {
         
        var itemid = sender.getAttribute('itemid');
        var pnlItems = document.getElementById(panelid);
        var controlid=pnlItems.id.replace('_popupSelect_pnlItems','');
        var controlElement = document.getElementById(controlid);
        var eventArgs=new Object();
        eventArgs= {Action:'delete',ItemId:itemid,Cancel:false};
        controlElement.doValueDeleting(controlElement,eventArgs);
        if (eventArgs.Cancel) return;
        pnlItems.AttachedObj.itemList.Delete(itemid);
        pnlItems.AttachedObj.Update();
        //pnlItems.AttachedObj.itemList.length--;
        
        
        var hfMultipleSelect = document.getElementById(controlid+ '_hfMultipleSelect');
        if (hfMultipleSelect && hfMultipleSelect.value!='1')
        {
            var popup = ASPxClientPopupControl.GetPopupControlCollection().Get(controlid+'_popupSelect')
            popup.Hide();
            //popupSelect.Hide();
        }
        eventArgs = {Action:'delete',ItemId:itemid};
        controlElement.doValueDeleted(controlElement,eventArgs);
        
    }
    function UpdateTitleText(controlid)
    {
        var result = "";
        var pnlItems = document.getElementById(controlid+ '_popupSelect_pnlItems');
        //var divSelectedItems = document.getElementById(controlid+'_divSelectedItems');
        
        for(var i=0;i<pnlItems.AttachedObj.itemList.capacity;i++)
        {
            if (pnlItems.AttachedObj.itemList[i])
            {
                result+=pnlItems.AttachedObj.itemList[i].text;
                //if (i<pnlItems.AttachedObj.itemList.length-1)
                    result+="; ";
            }
        }
        
        if (result=='')
        {
            var hfEmptyText= document.getElementById(controlid+'_hfEmptyText');
            
            var emptyText =(hfEmptyText)? hfEmptyText.value:'';
            result = emptyText;
        }    
        else
        {
            result = result.substr(0, (result.length-2)) ;
            
        }
        //var tblWidth  = parseInt(document.getElementById(controlid+'_tblContainer').clientWidth);
        //if (tblWidth==0)
        //    tblWidth  = parseInt(document.getElementById(controlid+'_tblContainer').style.width);
        //if (tblWidth>26)
        //    divSelectedItems.style.width = (tblWidth-26)  +'px';
        
        //divSelectedItems.innerHTML = result;
        var btned = ASPxClientControl.GetControlCollection().Get(controlid+'_btnedPopup'); 
        if (btned)
        {    btned.SetValue(result);
        try
        {
            //alert(btned.GetValue()+" "+ controlid+'_btnedPopup and ctl00_Main_ctl00_ctl13_popupEdit_cbpEditPanel_ProjectEdit_mscProgramManager_btnedPopup');
            //aspxEValueChanged('ctl00_Main_ctl00_ctl13_popupEdit_cbpEditPanel_ProjectEdit_mscProgramManager_btnedPopup')
            ASPxClientEdit.prototype.OnValueChanged.call(btned);
            //aspxEValueChanged(controlid+'_btnedPopup');
        }
        catch(e){alert('UpdateTitleText error'+e.message);}
        }
    }
    function FixDxStyle(controlid)
    {
        var hfItems_I =document.getElementById(controlid+'_hfItems_I');
        var hfItems = document.getElementById(controlid+'_hfItems');
        if (hfItems)
        {
            hfItems.className ='';
            hfItems.setAttribute('style','');
        }
        if (hfItems_I)
        {
            if (hfItems_I.offsetParent)
            {
                hfItems_I.offsetParent.setAttribute('style','');
                hfItems_I.offsetParent.className='';
                //hfItems_I.offsetParent.style.width='0px';
            }
            hfItems_I.style.display = 'none';
        }
    }
    function MsgGetByClientId(clientId) {
        LoadItems(clientId);
        var controlElement = document.getElementById(clientId);
        return controlElement
    }
    function MscAddExtendedProperties(controlId) {

        var controlElement = document.getElementById(controlId);
        controlElement.GetEnabled = function() {
            var btned = aspxGetControlCollection().Get(controlId + '_btnedPopup');
            return btned.GetEnabled();
        };
        controlElement.SetEnabled = function(enabled) {
            var btned = aspxGetControlCollection().Get(controlId + '_btnedPopup');
            btned.SetEnabled(enabled);
        };
        controlElement.Clear = function() {
            controlElement.AttachedObj.Clear();
            SaveItems(controlId);
        }
        controlElement.doValueDeleting = function(s,e)
        {
          if (controlElement.onValueDeleting && typeof(controlElement.onValueDeleting)=='function')
          {
            controlElement.onValueDeleting(s,e);
          } 
        }
        controlElement.doValueDeleted = function(s,e)
        {
          if (controlElement.onValueDeleted && typeof(controlElement.onValueDeleted)=='function')
          {
            controlElement.onValueDeleted(s,e);
          } 
        }
        controlElement.doAfterSaveItems = function(s, e) {
            if (controlElement.onAfterSaveItems && typeof (controlElement.onAfterSaveItems) == 'function') {
                controlElement.onAfterSaveItems(s, e);
            }
        }
        
    }
    function LoadItems(controlid,onChangeEventHandler)
    {
        
        var controlElement = document.getElementById(controlid);
        var pnlItems = document.getElementById(controlid+ '_popupSelect_pnlItems');
        pnlItems.AttachedObj= new SelectedItems(pnlItems);
        FixDxStyle(controlid);
        //var hfItems = document.getElementById(controlid+'_hfItems_I');
        var hfItems = document.getElementById(controlid+'_hfItems');
        pnlItems.AttachedObj.itemList.parse(hfItems.value);
        pnlItems.AttachedObj.Update();
        if (onChangeEventHandler && typeof(onChangeEventHandler)=='function')
        {
            controlElement.onValueChanged = onChangeEventHandler;
        }
        UpdateTitleText(controlid);
        MscAddExtendedProperties(controlid); 
        //Debuger.ShowInfo(btnedPopup);
        return controlElement;
    }
    
    function SaveItems(controlid)
    {
        try
        {
            var pnlItems = document.getElementById(controlid+ '_popupSelect_pnlItems');
            
            //var hfItems = document.getElementById(controlid+'_hfItems_I');
            
            var hfItems = document.getElementById(controlid+'_hfItems');
            
            hfItems.value = pnlItems.AttachedObj.itemList.toString();
            UpdateTitleText(controlid);
            var controlElement = document.getElementById(controlid);
            var eventArgs = new Object();
            eventArgs = { Action: 'saveitems', ControlId: controlid };
            controlElement.doAfterSaveItems(controlElement, eventArgs);
        }
        catch(e)
        {
            LoadItems(controlid);
            //alert('SaveItems('+controlid+') - Error:'+e.message);
            
        }
        
    }
    
    function ValueChange(controlid)
    {
        //ClientSide Event onValueChanged
        var controlElement = document.getElementById(controlid);
        //Raise Event
        if (controlElement.onValueChanged && typeof(controlElement.onValueChanged)=='function')
        {
            
            var pnlItems = document.getElementById(controlid+ '_popupSelect_pnlItems');
            var eventArg={Items: pnlItems.AttachedObj.itemList};
            controlElement.onValueChanged(controlElement,eventArg);   
        } 
    }
    
    function WriteLog(message)
    {
        var div =document.getElementById('divLog');
        if (div)
            div.innerHTML+=message+"<br />";
    }
    function getIndexWithoutPageRowCount(pageRowCount,visibleStartIndex)
    {
        //WriteLog('pageRowCount = '+pageRowCount+', visibleStartIndex='+visibleStartIndex);
        if (visibleStartIndex-pageRowCount<0 || pageRowCount ==0) return visibleStartIndex;
        var newIndex = visibleStartIndex-pageRowCount;
        return getIndexWithoutPageRowCount(pageRowCount,newIndex);
    }
    function rowOnClick(controlid,index)
    {   
        
        var grid = aspxGetControlCollection().Get(controlid+'_popupSelect_gridItems');
//        var grid1 = document.getElementById(controlid+'_popupSelect_gridItems');
//        WriteLog('grid.visibleStartIndex ='+grid.visibleStartIndex);
//        WriteLog('grid1.visibleStartIndex ='+grid1.visibleStartIndex);
        //grid.visibleStartIndex = 10;
        //Debuger.ShowInfo(grid);//pageRowCount 
        //visibleStartIndex - Последний раз ето возвращало количесто записей до текущей страницы
        var pagerowcount;
        if (grid.visibleStartIndex!=0)
        {
          pagerowcount = grid.visibleStartIndex;
        }
        else
        {
            if (grid.pageRowCount != 0)
            {
                pagerowcount = grid.pageRowCount;
            }
            else
            {
                var htmlGrid = document.getElementById(controlid + '_popupSelect_gridItems');
                pagerowcount = htmlGrid.visibleStartIndex;
            }
        }
        if (parseInt(pagerowcount) == NaN)
            pagerowcount = 0;
        var indexWithoutPageRowCount = getIndexWithoutPageRowCount(pagerowcount,index);
        //Последние раз ето возвращало количество срок на текущей странице - grid.pageRowCount
        //var indexWithoutPageRowCount = getIndexWithoutPageRowCount(grid.pageRowCount,index);
        //WriteLog('grid.visibleStartIndex ='+grid.visibleStartIndex+ ' grid.pageRowCount='+grid.pageRowCount);
        var id=grid.cpID[indexWithoutPageRowCount];
        var text = grid.cpText[indexWithoutPageRowCount];
        //WriteLog('index ='+indexWithoutPageRowCount+' id='+id+' text='+text);
        var pnlItems = document.getElementById(controlid+ '_popupSelect_pnlItems');
        var hfMultipleSelect = document.getElementById(controlid+ '_hfMultipleSelect');
        if (hfMultipleSelect.value!='1')
            pnlItems.AttachedObj.itemList.Clear();
        pnlItems.AttachedObj.itemList.Add(new Item(id,text));
        pnlItems.AttachedObj.Update();
        
        if (hfMultipleSelect.value!='1')
        {
            var popup = ASPxClientPopupControl.GetPopupControlCollection().Get(controlid+'_popupSelect')
            popup.Hide();
            //popupSelect.Hide();
        }
    }
    function MscPerformCallback(controlId,parameter)
    {
        SaveItems(controlId);
        var gridName = controlId + '_popupSelect_gridItems';
        var btnedPopupName = controlId + '_btnedPopup';
        var btnedPopup = aspxGetControlCollection().Get(btnedPopupName);
        var gridItems = aspxGetControlCollection().Get(gridName);
        //alert(btnedPopup.GetEnabled());
        btnedPopup.SetEnabled(false);
        gridItems.PerformCallback(parameter);
        return false;
    }
    function MscBtnedPopup_Validate(s,e)
    {
        
        if (s.validationGroup=='' ||s.validationGroup==null) return;
        var name = s.name;
        name = name.replace('_btnedPopup','');
        var hfItems = document.getElementById(name+'_hfItems');
        var hfEmptyText = document.getElementById(name+'_hfEmptyText');
        
        var emptyText = (hfEmptyText) ? hfEmptyText.value : '';
        if (hfItems)
        {
            if (hfItems.value==null || hfItems.value=='' || hfItems.value==emptyText)
                e.isValid =false;
            else e.isValid = true;
        }
    }
    
    function MscAddFormComplete(controlId,key)
    {
        gfClosePopup(controlId+"_ppcAddForm");
        //var grid =aspxGetControlCollection().Get(controlId+'_popupSelect_gridItems');
	    MscPerformCallback(controlId,key);
    }
    function MscAddFormCancel(controlId)
    {
        gfClosePopup(controlId+"_ppcAddForm");
    }
    function MscAddForm_Show(controlId)
    {
        gfShowPopup(controlId+'_ppcAddForm',' ');
        gfGetElement(controlId+'_ppcAddForm_cbpAddForm').PerformCallback();
    }
    
    function MscAddForm_CloseUp(s,e)
    {
        var hf = document.getElementById(s.name+"_cbpAddForm_hfAddParam");
        gfClearHidden(s.name+"_cbpAddForm_hfAddParam");
    }
    
    function MscFilterEnter_Click(controlId, e)
    {
        if (e.keyCode == 13)
        {
            MscPerformCallback(controlId);
            if (navigator.appVersion.indexOf("MSIE") != -1)
                e.cancelBubble=true;
            else 
                e.stopPropagation();
            return false;    
        }
    }
    function MscGridItems_EndCallback(s, e) {
        var controlId = s.name.trimEnd1('_popupSelect_gridItems');
        var btnedPopupName = controlId + '_btnedPopup';
        var btnedPopup = aspxGetControlCollection().Get(btnedPopupName);
        btnedPopup.SetEnabled(true);
        //alert(s.name);
    }
    
