﻿var metro = new function($) {

    this.metroList;
    this.options;
    this.meContainer;
    this.meList;

    this.init = function(options) {

        var that = this;
        this.options = options;

        try {
            this.metroList = new Function("return " + options.referenceControlId)();
        } catch (exc) { }
        this.meContainer = document.getElementById(options.relatedControlId);
        if (options.myControlId) {
            try {
                this.meList = new Function("return " + options.myControlId)();
            } catch (e) { }
        }

        if (this.metroList) {

            this.metroList.addEvent('bitAdd', function() {
                that.checkMetro();
            });
            this.metroList.addEvent('bitRemove', function() {
                that.checkMetro();
            });
            if (this.meContainer) {
                this.meContainer.style.display = 'none';
            }
            this.checkMetro();
        }

        if (this.options.StaticReferenceValue) {
            this.checkMetro();
        }
    }

    this.checkMetro = function() {

        var that = this;

        var lstValues;

        try {
            lstValues = this.metroList.getValues();
        }
        catch (ex) { }
        if (!this.options.StaticReferenceValue && lstValues == "") {
            if (that.meContainer != undefined && that.meContainer) that.meContainer.style.display = 'none';
            if (that.meList != undefined && that.meList) that.meList.removeAll();
        }
        else {
            var val;
            var selectedID;
            if (this.metroList) {
                val = this.metroList.getValues();

                if (val.length > 1) {
                    that.meContainer.style.display = 'none';
                    that.meList.removeAll();
                    return;
                }
                selectedID = val[0][0] * 1;
            } else {
                val = this.options.StaticReferenceValue;
                if (val.indexOf("C") == -1) {
                    that.meContainer.style.display = 'none';
                    return;
                }
                selectedID = val * 1;
                if (isNaN(selectedID)) {
                    selectedID = val;
                    selectedID = selectedID.replace("C", "");
                }
            }

            if (isNaN(selectedID)) {
                selectedID = val[0][0];
                if (selectedID.substring(0, 1) != "C") {
                    return;
                }
                selectedID = selectedID.replace("C", "");
            }
            if (val.length > 0 && val[0] && val[0].length > 0) {
                var lUrl = this.options.remoteUrl.replace("[SEARCH]", unescape(encodeURIComponent(selectedID)));

                this.currentRequest = new Request.JSON({ url: lUrl, data: {}, encoding: "windows-1251", urlEncoded: false, onRequest: function() {

                }, onSuccess: function(data) {
                    if (data) {
                        if (that.meContainer) {
                            that.meContainer.style.display = '';
                            if (that.meList != undefined && that.meList) that.meList.showInfoText();
                        }
                    } else {
                        if (that.meContainer) {
                            that.meContainer.style.display = 'none';
                        }
                        that.meList.removeAll();
                    }
                }
                }).send();
            }
        }
    }
}
 (jQuery);
