var LocationLoaderWidget = Class.create({
    fromControlId  : null,
    toControlId    : null,
    actionUrl      : null,
    paramName      : null,
    selectedValue  : null,
    selectedCountry: null,
    updateOnLoad   : false,
    toDefaultValue : '',
    toDefaultLabel : '',
    defaultCountryId: 1,
    hasCountry: false,

    initialize: function(params) {
        this.fromControlId = params.fromControlId;
        this.toControlId   = params.toControlId;
        this.actionUrl     = params.actionUrl;
        this.paramName     = params.paramName;
        this.updateOnLoad  = params.updateOnLoad;
        this.selectedValue = params.selectedValue;
        this.selectedCountry = params.selectedCountry;
        this.defaultCountryId = params.defaultCountryId;
        this.hasCountry = params.hasCountry;

        if (null != params.toDefaultValue) {
            this.toDefaultValue = params.toDefaultValue;
        }

        if (null != params.toDefaultLabel) {
            this.toDefaultLabel = params.toDefaultLabel;
        }

		this.hasCountry = false; // TODO:

        if (undefined != $(this.fromControlId)) {
            if (!this.hasCountry && this.defaultCountryId) {
                //TODO check next line - that was added for some purpose
                //$(this.fromControlId).setValue(this.defaultCountryId);
                this.receiveData();
            //TODO fix
            } else if (this.selectedCountry > this.toDefaultValue) {
                this.receiveData();
            }
            Event.observe($(this.fromControlId), 'change', function() {
                this.receiveData();
            }.bind(this));
        }
    },

    receiveData: function() {
        if (undefined == $(this.fromControlId)) {
            return;
        }
        var fromValue = $(this.fromControlId).value;
        if (!fromValue) {
            return;
        }
        var invokeUrl = this.actionUrl + '&' + this.paramName + '=' + fromValue;

        new Ajax.Request(invokeUrl, {
			evalJS: false,
            onSuccess: function(response) {
                var data;
                try {
                    data = response.responseText.evalJSON();
                } catch (e) {
                    data = null;
                }
                this.updateOnLoad = true;
                this.updateData(data);
              }.bind(this),
            onException: function(request, exception) {
                this.failData(request, exception);
            }.bind(this)
        });
    },

    cleanUpControl: function() {
        $(this.toControlId).innerHTML = "";
    },

    insertOption: function(value, text) {
        var option = new Option(text,value);
        if (Prototype.Browser.IE) {
            $(this.toControlId).add(option);
        } else {
            $(this.toControlId).add(option, null);
        }
    },

    updateData: function(location) {
        if (null == location) {
            $(this.toControlId).disable();
            return;
        }
        this.cleanUpControl();
        this.insertOption(this.toDefaultValue, this.toDefaultLabel);

        for (var i = 0; i < location.length; i++) {
            this.insertOption(location[i].id, location[i].name);
        }

        if (this.updateOnLoad) {
            $(this.toControlId).setValue(this.selectedValue);
            this.updateOnLoad = false;
        }
        
        if (location.length == 0) {
            $(this.toControlId).disable();
        } else {
            $(this.toControlId).enable();
        }
        
        if ($(this.toControlId).neededValue)
        {
        	$(this.toControlId).value = $(this.toControlId).neededValue;
        	$(this.toControlId).neededValue = null;
        }
    },

    failData: function(request, exception) {
        this.cleanUpControl();
        this.insertOption(this.toDefaultValue, this.toDefaultLabel);
    }
});

