/**
 * Central class for destination multi choice
 * Destinations are drawn from the regionen_xxx/regionenUdf_xxx cachefile.
 * 
 * @author Michael Dreher <dreher@traveltainment.de>
 * @copyright 2011 Traveltainment GmbH
 * @class The subclass for the tour operator multi choice.
 * @extends TTBaseMultiChoice 
 * @constructor 
 */
TTDestinationMultiChoice = function() {
    // Call constructor of base class
    TTDestinationMultiChoice.parent.constructor.call(
        this,
        'ttDestinationMulti',     // Name der Instanzvariable
        'topRegionenCache',       // Hier kommen die Daten hin
        'topRegionenGui',         // Das Formularfeld (select/input)
        ';',                      // Marke-Separator
        'ttDMCContainer',         // Container-ID
        'ttDMCDestination'        // Item Prefix
    );
    
    this.isUdfRegs      = false;
    this.udfRegionCache = [];
    this.labelsHTML     = {};
    this.dynamicChecked = false;
    this.ibeReq         = null;
    //this.flugdauerVal   = '';
    this.zgkIndex       = {};
    this.dataFieldCache = null;
    this.dataArrCache   = [];
    this.fields = {
        'topRegion': null,
        'detail':    null,
        'udf':       null
        //'flugdauer': null
    };

    this.setConfig({
        syncWithCachefile:   true,
        labelAny:            'beliebig',
        labelAnyLayer:       'alle Reiseziele',
        labelMultiChoice:    'Mehrfachauswahl',
        maxLengthLabel:      50,
        //inputGrouped:        true,
        topRegionToHotel:    true,
        addFlugdauerItems:   false,
        flugdauerLabels: [
            'Nahstrecke (bis 2,5h Flug)',
            'Mittelstrecke',
            'Fernstrecke (ab 6h Flug)'
        ],
        flugdauerRegions: [
            [],
            [],
            []
        ]
    });
};

// extend TTBaseMultiChoice
TTOOP.extend(TTDestinationMultiChoice, TTBaseMultiChoice);

/**
 * Initialization of the destination multi choice data.
 */
TTDestinationMultiChoice.prototype.initData = function() {
    // Reg-Array aus Cachefile
    var arrRegs    = [];
    this.isUdfRegs = false;

    if (this.isPopup) {
        if (typeof opener['ttUDF_' + opener.port] != 'undefined') {
            arrRegs = opener['ttUDF_' + opener.port];
            this.isUdfRegs = true;
        } else {
            arrRegs = opener['TTREGS_' + opener.port];
        }
    } else {
        if (typeof window['ttUDF_' + port] != 'undefined') {
            arrRegs = window['ttUDF_' + port];
            this.isUdfRegs = true;
        } else {
            arrRegs = window['TTREGS_' + port];
        }
    }
    
    // destinations from cachefile
    if (typeof arrRegs != 'undefined' && arrRegs.length > 0) {
        var groupTmp = [];

        if (this.isUdfRegs) {
            // groups from udf cachefile
            for (var i = 0; i < arrRegs.length; i++) {
                // start with 100000 for udf groups
                this.setLabel(parseInt(arrRegs[i].id) + 100000, arrRegs[i].titleJS);

                this.udfRegionCache[arrRegs[i].id] = [];
                
                // Ohne Vorgaben nur die Zielgebiete zur Mehrfachauswahl dazunehmen
                groupTmp[groupTmp.length] = parseInt(arrRegs[i].id) + 100000;
                
                for (var j = 0; j < arrRegs[i].regions.length; j++) {
                    this.setLabel(arrRegs[i].regions[j].topRegion, arrRegs[i].regions[j].titleJS);
                    this.udfRegionCache[arrRegs[i].id][j] = arrRegs[i].regions[j].topRegion;
                }
            }
        } else {
            // groups from standard cachefile
            for (var i = 0; i < arrRegs.length; i++) {
                var destTmp = arrRegs[i].split('|');
                destTmp[0]  = this.parseUmlaute(destTmp[0]);

                this.setLabel(destTmp[3], destTmp[0]);
                this.setZgkIndex(destTmp[1], destTmp[3]);
                
                if (parseInt(destTmp[3]) >= 10000) {
                    // Ohne Vorgaben nur die Zielgebiete zur Mehrfachauswahl dazunehmen
                    groupTmp[groupTmp.length] = destTmp[3];
                }
            }
        }
        
        // Labels flugdauer
        if (this.config.addFlugdauerItems) {
            this.setLabel(200000, this.config.flugdauerLabels[0]);
            this.setLabel(200001, this.config.flugdauerLabels[1]);
            this.setLabel(200002, this.config.flugdauerLabels[2]);
        }
        
        // no groups defined by customer
        if (this.config.groups.length == 0) {
            // Flugdauer Items
            if (this.config.addFlugdauerItems) {
                groupTmp[groupTmp.length] = 200000;
                groupTmp[groupTmp.length] = 200001;
                groupTmp[groupTmp.length] = 200002;
            }
            
            // add default group with all items from cachefile
            this.addGroup(groupTmp);
        } else {
            // remove items from groups if not exist in cachefile
            if (this.config.syncWithCachefile) {
                this.updateGroupsFromCache();
            }
        }
    }
    
    if (this.isPopup && typeof opener.IBE != 'undefined' && typeof opener.IBE.req != 'undefined') {
        this.ibeReq = opener.IBE.req;
    } else if (typeof IBE != 'undefined' && typeof IBE.req != 'undefined') {
        this.ibeReq = IBE.req;
    }
    if (this.ibeReq && typeof this.ibeReq.detail != 'undefined' && (this.ibeReq.detail == 'hotel' || this.ibeReq.detail == 'termine')) {
        this.dynamicChecked = true;
    }
};

TTDestinationMultiChoice.prototype.isUdfRegions = function() {
    if (this.isPopup) {
        return typeof opener['ttUDF_' + opener.port] != 'undefined';
    } else {
        return typeof window['ttUDF_' + port] != 'undefined';
    }
};

TTDestinationMultiChoice.prototype.setZgkIndex = function(zkg, topRegion) {
    this.zgkIndex[zkg] = topRegion;
};

/**
 * Removes tour operators from the user defined groups that do not exist in the cachefile.
 */
TTDestinationMultiChoice.prototype.updateGroupsFromCache = function() {
    var tmpGroups = [];
    for (var i = 0; i < this.config.groups.length; i++) {
        var tmpGroupsSub = [];
        for (var j = 0; j < this.config.groups[i].length; j++) {
            if (typeof this.labels[this.config.groups[i][j]] == 'object') {
                tmpGroupsSub[tmpGroupsSub.length] = this.config.groups[i][j];
            }
        }
        if (tmpGroupsSub.length > 0) {
            tmpGroups[tmpGroups.length] = tmpGroupsSub;
        }
    }
    this.config.groups = tmpGroups;
};

/**
 * Initialize additional fields.
 */
TTDestinationMultiChoice.prototype.initFields = function() {
    this.fields.topRegion = this.isPopup ? opener.document.getElementById('topRegion') : document.getElementById('topRegion');
    if (this.fields.topRegion == null) {
        this.fields.topRegion = this.isPopup ? opener.document.bengine['topRegion'] : document.bengine['topRegion'];
    }
    if (this.fields.topRegion == null) {
        alert('Hidden Field <topRegion> nicht vorhanden!');
    }

    this.fields.detail = this.isPopup ? opener.document.getElementById('detail') : document.getElementById('detail');
    if (this.fields.detail == null) {
        this.fields.detail = this.isPopup ? opener.document.bengine['detail'] : document.bengine['detail'];
    }
    if (this.fields.detail == null) {
        alert('Hidden Field <detail> nicht vorhanden!');
    }

    // hiddenfield für gui
    this.dataFieldCache = this.isPopup ? opener.document.getElementById('topRegionen') : document.getElementById('topRegionen');
    if (this.dataFieldCache == null) {
        this.dataFieldCache = this.isPopup ? opener.document.bengine['topRegionen'] : document.bengine['topRegionen'];
    }
    if (this.dataFieldCache == null) {
        alert('Hidden Field <topRegionen> nicht vorhanden!');
    }

    if (this.isUdfRegions()) {
        this.fields.udf = this.isPopup ? opener.document.getElementById('udf') : document.getElementById('udf');
        if (this.fields.udf == null) {
            this.fields.udf = this.isPopup ? opener.document.bengine['udf'] : document.bengine['udf'];
        }
        if (this.fields.udf == null) {
            alert('Hidden Field <udf> nicht vorhanden!');
        }
    }
};

/**
 * Returns HTML Code of a destination item. 
 * @param {Object} item
 * @param {Object} isSelected
 */
TTDestinationMultiChoice.prototype.getItemSrc = function(item, isSelected) {
    var html      = '';
    var itemTitle = this.isUdfRegs ? this.getItemTitle(item) : '';
    
    if (typeof this.callbacks.printDestination == 'function') {
        // kundenspezifische Anzeigemethode
        html += this.callbacks.printDestination(item, this.labels[item].label, isSelected, this.config.controlType, itemTitle);
    } else {
        if (this.config.controlType == 'input') {
            // Checkboxen
            html += '<div id="' + this.itemPrefix + '_' + item + '" class="' + this.itemPrefix + 'Item' + (isSelected ? 'Checked' : '') + '" onmouseover="if(' + this.instanceName + '.isMouseLeaveOrEnter(event,this)) { ' + this.instanceName + '.mouseOver(this); }" onmouseout="if(' + this.instanceName + '.isMouseLeaveOrEnter(event,this)) { ' + this.instanceName + '.mouseOut(this); }" title="' + itemTitle + '">'
                 +  '    <div class="' + this.itemPrefix + 'Input"><input id="' + this.itemPrefix + 'Checkbox_' + item + '" type="checkbox" value="' + item + '" onclick="' + this.instanceName + '.clickItem(this);"' + (isSelected ? ' checked="checked"' : '') + ' /></div>'
                 +  '    <label for="' + this.itemPrefix + 'Checkbox_' + item + '" class="' + this.itemPrefix + 'Name">' + this.getDestinationLabel(item) + '</label>'
                 +  '    <div class="clearBoth"><!-- --></div>'
                 +  '</div>';
        } else if (this.config.controlType == 'graphic') {
            // Grafische Elemente
            html += '<div id="' + this.itemPrefix + 'Checkbox_' + item + '" class="' + this.itemPrefix + 'Item' + (isSelected ? 'Checked' : '') + '" onclick="' + this.instanceName + '.clickItem(this);" mcValue="' + item + '" onmouseover="if(' + this.instanceName + '.isMouseLeaveOrEnter(event,this)) { ' + this.instanceName + '.mouseOver(this); }" onmouseout="if(' + this.instanceName + '.isMouseLeaveOrEnter(event,this)) { ' + this.instanceName + '.mouseOut(this); }" title="' + itemTitle + '">'
                 +  '<div class="' + this.itemPrefix + 'Name">' + this.getDestinationLabel(item) + '</div>'
                 +  '    <div class="clearBoth"><!-- --></div>'
                 +  '</div>';
        }
    }
    return html;
};

/**
 * Returns the HTML Code of the any item.
 */
TTDestinationMultiChoice.prototype.getItemSrcAny = function() {
    var html = '';
    if (typeof this.callbacks.printAnyItem == 'function') {
        html += this.callbacks.printAnyItem(item, this.config.labelAnyLayer, this.dataArr.length == 0, this.config.controlType);
    } else {
        if (this.config.controlType == 'input') {
            html = '<div id="' + this.itemPrefix + '_0" class="' + this.itemPrefix + 'Item' + (this.dataArr.length == 0 ? 'Checked' : '') + '" onmouseover="if(' + this.instanceName + '.isMouseLeaveOrEnter(event,this)) { ' + this.instanceName + '.mouseOver(this); }" onmouseout="if(' + this.instanceName + '.isMouseLeaveOrEnter(event,this)) { ' + this.instanceName + '.mouseOut(this); }">'
                 + '    <div class="' + this.itemPrefix + 'Input">'
                 + '        <input id="' + this.itemPrefix + 'Checkbox_0" type="checkbox" value="" onclick="' + this.instanceName + '.clickItem(this);"' + (this.dataArr.length == 0 ? ' checked="checked"' : '') + ' />'
                 + '    </div>'
                 + '    <label for="' + this.itemPrefix + 'Checkbox_0" class="' + this.itemPrefix + 'Name">' + this.config.labelAnyLayer + '</label>'
                 + '</div>';
        } else if (this.config.controlType == 'graphic') {
            html = '<div id="' + this.itemPrefix + 'Checkbox_0" class="' + this.itemPrefix + 'Item' + (this.dataArr.length == 0 ? 'Checked' : '') + '" onclick="' + this.instanceName + '.clickItem(this);" mcValue="" onmouseover="if(' + this.instanceName + '.isMouseLeaveOrEnter(event,this)) { ' + this.instanceName + '.mouseOver(this); }" onmouseout="if(' + this.instanceName + '.isMouseLeaveOrEnter(event,this)) { ' + this.instanceName + '.mouseOut(this); }">'
                 + '    <div class="' + this.itemPrefix + 'Name">' + this.config.labelAnyLayer + '</div>'
                 + '</div>';
        }
    }
    return html;
};

/**
 * Returns the label for a destination.
 * @param {int} topRegion
 */
TTDestinationMultiChoice.prototype.getDestinationLabel = function(topRegion) {
    if (typeof this.callbacks.getDestinationLabel == 'function') {
        return this.callbacks.getDestinationLabel(topRegion, this.labels[topRegion].label, this.config.maxLengthLabel);
    }
    if (this.labels[topRegion].label.length > this.config.maxLengthLabel) {
        return this.labels[topRegion].label.substr(0, this.config.maxLengthLabel - 2) + '...';
    } else {
        return this.labels[topRegion].label;
    }
};

/**
 * Returns the title for a given UDF-item (>=100000)
 * @param {Object} item
 */
TTDestinationMultiChoice.prototype.getItemTitle = function(item) {
    if (item >= 100000 && item < 200000) {
        var ret = [];
        for (var i = 0; i < this.udfRegionCache[item - 100000].length; i++) {
            ret[ret.length] = this.labels[this.udfRegionCache[item - 100000][i]].label;
        }
        return ret.join(', ');
    } else {
        return this.labels[item].label;
    }
};

/**
 * 
 */
TTDestinationMultiChoice.prototype.printAdditional = function() {
    // Feld für aktuelle Region füllen
    var dynamicLabel = '';
    var showDynamic  = false;
    var topReg       = this.getTopRegion();
    
    if (typeof this.ibeReq.detail != 'undefined' && topReg && this.labels[topReg] && (this.ibeReq.detail == 'hotel' || this.ibeReq.detail == 'termine')) {
        dynamicLabel = this.labels[topReg].label;
        showDynamic  = true;
    }

    if (showDynamic) {
        var html = '';
        if (typeof this.callbacks.printDynamicItem == 'function') {
            html = this.callbacks.printDynamicItem(dynamicLabel, topReg);
        } else if (this.config.controlType == 'input') {
            html = '<div id="' + this.itemPrefix + '_999999" class="' + this.itemPrefix + 'ItemChecked" onmouseover="if(' + this.instanceName + '.isMouseLeaveOrEnter(event,this)) { ' + this.instanceName + '.mouseOver(this); }" onmouseout="if(' + this.instanceName + '.isMouseLeaveOrEnter(event,this)) { ' + this.instanceName + '.mouseOut(this); }">'
                 + '    <div class="' + this.itemPrefix + 'Input">'
                 + '        <input id="' + this.itemPrefix + 'Checkbox_999999" type="checkbox" value="999999" onclick="' + this.instanceName + '.clickDynamic(this);" checked="checked" />'
                 + '    </div>'
                 + '    <label for="' + this.itemPrefix + 'Checkbox_999999" class="' + this.itemPrefix + 'Name">' + this.getDynamicLabel(dynamicLabel, topReg) + '</label>'
                 + '</div>';
        } else if (this.config.controlType == 'graphic') {
            html = '<div id="' + this.itemPrefix + 'Checkbox_999999" class="' + this.itemPrefix + 'ItemChecked" onclick="' + this.instanceName + '.clickDynamic(this);" mcValue="999999" onmouseover="if(' + this.instanceName + '.isMouseLeaveOrEnter(event,this)) { ' + this.instanceName + '.mouseOver(this); }" onmouseout="if(' + this.instanceName + '.isMouseLeaveOrEnter(event,this)) { ' + this.instanceName + '.mouseOut(this); }">'
                 + '    <div class="' + this.itemPrefix + 'Name">' + this.getDynamicLabel(dynamicLabel, topReg) + '</div>'
                 + '</div>';
        }
        
        document.getElementById(this.itemPrefix + 'Dynamic').innerHTML = html;
        document.getElementById(this.itemPrefix + 'Dynamic').style.display = 'block';
    } else {
        document.getElementById(this.itemPrefix + 'Dynamic').style.display = 'none';
    }
};

/**
 * Handles click on Dynamic item on hotel/termine site.
 * @param {Object} elem
 */
TTDestinationMultiChoice.prototype.clickDynamic = function(elem) {
    if (this.config.controlType == 'input' && !this.isItemChecked(elem) || this.config.controlType == 'graphic' && this.isItemChecked(elem)) {
        // switch off
        this.dynamicChecked = false;
    } else {
        // switch on
        this.dynamicChecked = true;
    }

    if (typeof this.callbacks.clickDynamicCallback == 'function') {
        this.callbacks.clickDynamicCallback(elem, this.dynamicChecked);
    }
};

/**
 * Sets detail and topRegion depending on the selected items.
 */
TTDestinationMultiChoice.prototype.onAfterSubmit = function() {
    if (this.dataArrCache.length > 0) {
        this.dataFieldCache.value = this.dataArrCache.join(this.dataFieldSeparator);
    } else {
        this.dataFieldCache.value = '';
    }
    
    if (this.config.topRegionToHotel && this.dataArr.length == 1 && parseInt(this.dataArr[0]) < 10000) {
        this.fields.detail.value    = 'hotel';
        this.fields.topRegion.value = this.dataArr[0];
    } else if (this.isSelectionChanged() || this.dynamicChecked === false) {
        this.fields.detail.value    = 'zielgebiet';
        this.fields.topRegion.value = '';
        if (this.isUdfRegs) {
            if (this.dataArr.length == 1 && parseInt(this.dataArr[0]) >= 100000) {
                this.fields.udf.value = parseInt(this.dataArr[0]) - 100000;
            } else {
                this.fields.udf.value = '';
            }
        }
    }
};

/**
 * Sets detail and topRegion depending on the selected items.
 */
TTDestinationMultiChoice.prototype.onAfterClear = function() {
    this.dataArrCache = [];
};

/**
 * Returns the label for the dynamic item that shows the region that is actually visited on hotel and termin page.
 * @param {Object} label
 */
TTDestinationMultiChoice.prototype.getDynamicLabel = function(label, topRegion) {
    if (typeof this.callbacks.getDynamicLabel == 'function') {
        return this.callbacks.getDynamicLabel(label);
    }
    return label;
};

/**
 * Special handling of the items >= 100000 on remove.
 * @param {Object} elemValue
 */
TTDestinationMultiChoice.prototype.removeItemFromDataArray = function(elemValue) {
    for (var i = 0; i < this.dataArr.length; i++) {
        if (elemValue == this.dataArr[i]) {
            this.dataArr.splice(i, 1);
            break;
        }
    }

    if (elemValue >= 200000) {
        var regions = [];
        var dataTmp = [];
        var fdGroup = this.config.flugdauerRegions[elemValue - 200000];

        for (var i = 0; i < fdGroup.length; i++) {
            if (fdGroup[i] >= 100000) {
                regions = regions.concat(this.udfRegionCache[fdGroup[i] - 100000]);
            }
            regions[regions.length] = fdGroup[i];
        }
        for (var i = 0; i < this.dataArrCache.length; i++) {
            if (!TTBaseMultiChoice.containsItem(this.dataArrCache[i], regions)) {
                dataTmp[dataTmp.length] = this.dataArrCache[i];
            }
        }
        this.dataArrCache = dataTmp;
    } else if (this.isUdfRegs && elemValue >= 100000 && elemValue < 200000) {
        var dataTmp = [];
        for (var i = 0; i < this.dataArrCache.length; i++) {
            if (!TTBaseMultiChoice.containsItem(this.dataArrCache[i], this.udfRegionCache[elemValue - 100000])) {
                dataTmp[dataTmp.length] = this.dataArrCache[i];
            }
        }
        this.dataArrCache = dataTmp;
    } else {
        for (var i = 0; i < this.dataArrCache.length; i++) {
            if (elemValue == this.dataArrCache[i]) {
                this.dataArrCache.splice(i, 1);
                break;
            }
        }
    }
};

/**
 * Special handling of the items >= 100000 on add.
 * @param {Object} elemValue
 */
TTDestinationMultiChoice.prototype.addItemToDataArray = function(elemValue) {
    this.dataArr.push(elemValue);
    if (elemValue >= 200000) {
        var regions = [];
        var fdGroup = this.config.flugdauerRegions[elemValue - 200000];
        for (var i = 0; i < fdGroup.length; i++) {
            if (fdGroup[i] >= 100000) {
                regions = regions.concat(this.udfRegionCache[fdGroup[i] - 100000]);
            }
            regions[regions.length] = fdGroup[i];
        }
        this.dataArrCache = TTBaseMultiChoice.arrUnique(this.dataArrCache.concat(regions));
    } else if (this.isUdfRegs && elemValue >= 100000) {
        this.dataArrCache = TTBaseMultiChoice.arrUnique(this.dataArrCache.concat(this.udfRegionCache[elemValue - 100000]));
    } else {
        this.dataArrCache.push(elemValue);
    }
};

TTDestinationMultiChoice.prototype.updateFromDataFieldAdditional = function() {
    if (this.ibeReq == null) {
        return;
    }
    var update = false;

    var value = this.dataFieldCache.value;  
    if (value != '-1' && value != '-1,' && value != '') {
        this.dataArrCache = value.split(this.dataFieldSeparator);
    } else {
        this.dataArrCache = [];
    }
    
    // Deeplinks aus topRegion, udf importieren
    if (typeof this.ibeReq.udf != 'undefined' && typeof this.ibeReq.detail != 'undefined' && this.ibeReq.udf != '' && this.ibeReq.udf != -1 && (this.ibeReq.detail == 'zielgebiet' || this.ibeReq.detail == 'hotel' || this.ibeReq.detail == 'termine')) {
        this.addItemToDataArray(parseInt(this.ibeReq.udf) + 100000);
        update = true;
    }

    if (typeof this.ibeReq.topRegion != 'undefined' && typeof this.ibeReq.detail != 'undefined' && this.ibeReq.topRegion != '' && this.ibeReq.topRegion != -1 && (this.ibeReq.detail == 'hotel' || this.ibeReq.detail == 'termine')) {
        this.addItemToDataArray(parseInt(this.ibeReq.topRegion));
        update = true;
    }
    
    if (update) {
        if (this.dataArr.length > 0) {
            this.dataField.value = this.dataArr.join(this.dataFieldSeparator);
            this.dataFieldCache.value = this.dataArrCache.join(this.dataFieldSeparator);
        } else {
            this.dataField.value = '';
            this.dataFieldCache.value = '';
        }
    }
};

/**
 * Update the layer.
 * @param {Object} elem
 * @param {Object} elemValue
 * @param {Object} checkState
 */
TTDestinationMultiChoice.prototype.updateItemView = function(elem, elemValue, checkState) {
    // notwendig weil sich checkboxen gegenseitig beeinflussen können (gruppe -> topRegion)
    this.printItems();
};

/**
 * Updates the title of the GUI field.
 */
TTDestinationMultiChoice.prototype.updateGuiFieldTitle = function() {
    var tmpArr = [];
    for (var i = 0; i < this.dataArr.length; i++) {
        if (typeof this.labels[this.dataArr[i]] != 'undefined' && this.dataArr[i] < 100000) {
            tmpArr[tmpArr.length] = this.labels[this.dataArr[i]].label;
        }
    }
    this.guiField.title = tmpArr.join(', ');
};

TTDestinationMultiChoice.prototype.setValue = function(value) {
    if (value == '-1') {
        // Beliebig
        this.dataArr = [];
    } else if (value == '-2') {
        // Mehrfachauswahl
        this.open();
    } else if (value != '') {
        // Einzelnes Element
        this.dataArr = [];
        this.addItemToDataArray(value);
    }
    
    var oldDataFieldValue = this.dataField.value;

    if (this.dataArr.length > 0) {
        this.dataField.value = this.dataArr.join(this.dataFieldSeparator);
    } else {
        this.dataField.value = '';
    }
    
    // selection changed?
    this.isDataChanged = oldDataFieldValue != this.dataField.value;

    // subclass callback after submit
    if (typeof this.onAfterSubmit == 'function') {
        this.onAfterSubmit();
    }

    if (typeof this.callbacks.submitCallback == 'function') {
        this.callbacks.submitCallback(this.dataArr, this.isDataChanged);
    }
};

/**
 * 
 */
TTDestinationMultiChoice.prototype.updateGuiField = function() {
    var label  = '';
    var detail = '';
    var topReg = null;

    if (this.ibeReq && typeof this.ibeReq.detail != 'undefined') {
        detail = this.ibeReq.detail;
        topReg = this.getTopRegion();

        if (topReg && this.labels[topReg] && (this.ibeReq.detail == 'hotel' || this.ibeReq.detail == 'termine')) {
            label = this.labels[topReg].label;
        }
    }

    if (this.config.inputType == 'text') {
        // Text-Input
        if (label != '' && !this.isSelectionChanged()) {
            this.guiField.value = label;
        } else if (this.dataArr.length == 0 || typeof this.labels[this.dataArr[0]] == 'undefined') {
            this.guiField.value = this.config.labelAny;
        } else if (this.dataArr.length == 1) {
            this.guiField.value = this.labels[this.dataArr[0]].label;
        } else {
            this.guiField.value = this.labels[this.dataArr[0]].label + ' ...';
        }
    } else {
        // Select-Input
        if (this.dataArr.length == 0) {
            this.guiField.selectedIndex = 0;
        } else {
            if (this.isUdfRegs) {
                var selectedUDF = null; 
                var selectMulti = false;
                var tmpArr      = [];
                var arraysEqual = false;

                // Testen ob nur eine der vorhandenen UDF-Gruppen selektiert ist
                for (var i = 0; i < this.dataArr.length; i++) {
                    if (parseInt(this.dataArr[i]) >= 100000 && parseInt(this.dataArr[i]) < 200000 && selectedUDF != null) {
                        selectMulti = true;
                        break;
                    }
                    if (parseInt(this.dataArr[i]) >= 100000 && parseInt(this.dataArr[i]) < 200000) {
                        selectedUDF = parseInt(this.dataArr[i]);
                        tmpArr[0]   = selectedUDF.toString();
                        tmpArr      = tmpArr.concat(this.udfRegionCache[selectedUDF - 100000]);
                        tmpArr      = tmpArr.sort(function(a, b) { return a - b; });
                    }
                }
                
                // Sind beide Arrays gleich?
                if (this.dataArr.length == tmpArr.length) {
                    arraysEqual = true;
                    for (var i = 0; i < this.dataArr.length; i++) {
                        if (this.dataArr[i] != tmpArr[i]) {
                            arraysEqual = false;
                            break;
                        }
                    }
                }
                
                if (selectMulti || selectedUDF == null || !arraysEqual) {
                    this.guiField.selectedIndex = 1;
                } else {
                    this.selectItem(selectedUDF);
                }
            } else {
                if ((detail == 'hotel' || detail == 'termine') && topReg && !this.isSelectionChanged()) {
                    this.selectItem(topReg);
                } else if (this.dataArr.length == 1) {
                    this.selectItem(this.dataArr[0]);
                } else {
                    this.guiField.selectedIndex = 1;
                }
            }
        }
    }
    this.updateGuiFieldTitle();
};

TTDestinationMultiChoice.prototype.selectItem = function(item) {
    for (var i = 0; i < this.guiField.length; i++) {
        if (this.guiField.options[i].value == item) {
            this.guiField.selectedIndex = i;
            break;
        }
    }
};

/**
 * Add additional region item of visited region to select on hotelpage if it does not already exist in the select. 
 */
TTDestinationMultiChoice.prototype.getSelectAdditionalTop = function() {
    var res = [];
    if (this.ibeReq && typeof this.ibeReq.detail != 'undefined' && (this.ibeReq.detail == 'hotel' || this.ibeReq.detail == 'termine')) {
        var topReg = this.getTopRegion();
        
        if (topReg) {
            var topRegExists = false;
            for (var i = 0; i < this.config.groups.length; i++) {
                for (var j = 0; j < this.config.groups[i].length; j++) {
                    if (this.config.groups[i][j] == topReg) {
                        topRegExists = true;
                    }
                }
            }        

            if (!topRegExists) {
                res[0] = this.labels[topReg];
            }
        }
    }
    return res;
};

TTDestinationMultiChoice.prototype.getTopRegion = function() {
    if (typeof this.ibeReq.topRegion != 'undefined' && this.ibeReq.topRegion != '' && this.ibeReq.topRegion != '-1' && this.ibeReq.topRegion < 10000) {
        return this.ibeReq.topRegion;
    } else if (typeof this.ibeReq.zielgebiets_kenner != 'undefined' && this.ibeReq.zielgebiets_kenner != '' && this.ibeReq.zielgebiets_kenner != '-1' && typeof this.zgkIndex[this.ibeReq.zielgebiets_kenner] != 'undefined') {
        return this.zgkIndex[this.ibeReq.zielgebiets_kenner];
    }
    return null;
};

TTDestinationMultiChoice.prototype.resetData = function() {
    this.dataArr      = [];
    this.dataArrCache = [];
};

TTDestinationMultiChoice.prototype.resetToValue = function(value) {
    this.resetData();
    this.addItemToDataArray(value);
    this.printItems();
};

// Default instance
var ttDestinationMulti = new TTDestinationMultiChoice();


