﻿function WMultiCombo76(Prefix)
{
    this.Prefix = Prefix;
    this.name = '';
    this.autoPostBack = false;
    
    this.cbMain = null;
    this.tblMain = document.getElementById(this.Prefix + 'tblMain');
    this.edtSelectedValue = document.getElementById(this.Prefix + 'edtSelectedValue');
    this.divSelectedText = document.getElementById(this.Prefix + 'divSelectedText');
    this.btnAdd = null;
    this.btnClear = null;
    this.container = null;
    this.uniqueId = '';
    
    this.type = 'WMultiCombo76';
    this.DefaultText = null;
        
    this.btnAdd_onClick = function()
    {
        var curId = this.cbMain.getSelectedValue();
        var SelectedArr = this.edtSelectedValue.value.split(';');
        
	    if (this.edtSelectedValue  &&  this.divSelectedText  &&  ArrayIndexOf(SelectedArr, curId) == -1  &&  curId != -1)
	    {
	        if (this.edtSelectedValue.value == '') this.divSelectedText.innerHTML = '';
	        this.divSelectedText.innerHTML += (this.divSelectedText.innerHTML != '' ? ', ' : '') + this.cbMain.getSelectedText();
	        this.edtSelectedValue.value += (this.edtSelectedValue.value != '' ? ';' : '') +  curId;
	        
	        if (this.edtSelectedValue.value != '') 
	            this.btnClear.setIsUserVisible(true);
	        
	        if (!this.cbMain.wgrData.isEmpty)
	        {
	            var firstTR = this.cbMain.wgrData.firstTR(false);
		        if (firstTR)
		        {
		             this.cbMain.wgrData.setCurTR(firstTR);
		             this.cbMain.setCurTR(firstTR);
		             this.cbMain.lastKeyValue  = this.cbMain.getSelectedValue();
		             this.btnAdd.setIsUserVisible(false);
		             this.cbMain.edtMain.value = '...';
		             this.cbMain.setAllValueText('...');
		        }
	        }
	    }
    }
    
    this.btnClear_onClick = function()
    {
        this.edtSelectedValue.value = '';
        this.divSelectedText.innerHTML = '';
        this.btnClear.setIsUserVisible(false);
        
        if (this.cbMain  &&  !this.cbMain.wgrData.isEmpty)
        {
            var firstTR = this.cbMain.wgrData.firstTR(false);
		    if (firstTR)
		    {
		        this.cbMain.wgrData.setCurTR(firstTR);
		        this.cbMain.setCurTR(firstTR);
		        this.cbMain.lastKeyValue  = this.cbMain.getSelectedValue();
		        this.btnAdd.setIsUserVisible(false);
		        this.cbMain.edtMain.value = this.DefaultText;
		        this.cbMain.setAllValueText(this.DefaultText);
		    }
         }
         this.valueChanged();
    }
    
    this.cbMain_onSelectedValueChanged = function(MultiExtComboBox, onServer)
    {
    1;
        MultiExtComboBox.btnAdd.setIsUserVisible(MultiExtComboBox.cbMain.getSelectedValue() != '-1');            
            
        var curId = MultiExtComboBox.cbMain.getSelectedValue(); 
        if (curId != "-1"  &&  MultiExtComboBox.divSelectedText.innerHTML == MultiExtComboBox.DefaultText)
            MultiExtComboBox.divSelectedText.innerHTML = '';
        
        var selectedArr = MultiExtComboBox.edtSelectedValue.value.split(';');
        if (ArrayIndexOf(selectedArr, curId) == -1)
            MultiExtComboBox.valueChanged(onServer);
            
    }
    
    this.valueChanged = function(onServer)
    {
        1;
        if (this.autoPostBack  && !onServer) 
            this.doPostBack('SelectedIndexChanged');
    }
    
    this.doPostBack = function(eventArgument) 
	{
	1;
		if (!this.container)
		{
		    var theform = document.forms[0];
		    theform.__EVENTTARGET.value = this.uniqueId;
		    theform.__EVENTARGUMENT.value = eventArgument;
		    theform.submit();
		}
		else
		    __doWCPostBack(this.container, this.uniqueId, eventArgument)
	}
    
    this.setSelectedItemsIDStr = function(val)
    {
        this.edtSelectedValue.value = val;
        this.btnClear.setIsUserVisible(val != '');
    }
    
    this.getSelectedItemsIDStr = function()
    {
    1;
        return this.edtSelectedValue;
    }
    
    this.setSelectedItemsTextStr = function(val)
    {
        1;
        this.divSelectedText.innerHTML = val;
    }
    
    
    this.getSelectedItemsTextStr = function()
    {
    1;
        return this.divSelectedText.innerHTML;
    }
}

function Controls76_WMultiCombo76_Builder(name, obj_id)
{
    this.name = name;
    try { this.obj = eval(obj_id); }
    catch (ex) { this.obj = null; }
    
    this.properties = new Array('SelectedItemsIDStr', 'SelectedItemsTextStr');
    
    this.get_prop = function(prop_name)
    {
        if (this.obj != null)
            switch(prop_name)
            {
                default: return eval('if (this.obj.get' + prop_name + ') this.obj.get' + prop_name + '()');
            }
            
        return "";
    }
    
    this.set_prop = function(prop_name, prop_value)
    {
        1;
        if (this.obj != null)
            switch(prop_name)
            {
                default: eval('if (this.obj.set' + prop_name + ') this.obj.set' + prop_name + '(prop_value)');
            }
    }
}    
