﻿if (typeof ODAAutoPopulator == 'undefined') {
    var ODAAutoPopulator = new Class.create();

    ODAAutoPopulator.prototype = {
        initialize: function(_parentClassFullName, _destElement, _srcElementIDFragment, _autoPopulationPath, _readOnlyAfterPopulation, _cssClassFilter) {
            this.parentClassFullName = _parentClassFullName;
            this.AutoPopulateElements = {};
            this.FieldNames = new Array();
            this.autoPopulationPath = _autoPopulationPath;
            this.readOnlyAfterPopulation = _readOnlyAfterPopulation;
            this.cssClassFilter = _cssClassFilter;
            this.autoPopulationPathSections = _autoPopulationPath.split(';');
            this.destElement = $(_destElement);

            if (this.cssClassFilter == null || this.cssClassFilter.length == 0) {
                this.cssClassFilter = "UserInputControl";
            }


            for (var i = 0; i < this.autoPopulationPathSections.length; i++) {
                var temp = this.autoPopulationPathSections[i].split('.');

                var fieldName = temp[0];
                this.FieldNames[this.FieldNames.length] = fieldName;

                // find the html object that represents the rootProperty
                var objArr = document.body.select('.' + this.cssClassFilter + "[id*='" + fieldName + "']");
                if (objArr == null || typeof (objArr[0]) == 'undefined') {
                    objArr = document.body.select('.' + this.cssClassFilter + "[id*='" + fieldName + "ID']");
                }

                this.onChangeBoundHandler = this.onSrcChange.bindAsEventListener(this);

                if (objArr != null && objArr.length > 0) {
                    var elem = objArr[0];
                    var tempInfo = {};
                    tempInfo.element = elem;
                    tempInfo.value = $F(elem);
                    this.AutoPopulateElements[fieldName] = tempInfo;

                    if (elem.tagName.toLowerCase() == "select") {
                        Event.observe(elem, 'change', this.onChangeBoundHandler);
                    }
                    else if (elem.tagName.toLowerCase() == "input") {
                        if (elem.type.toLowerCase() == "text") {
                            Event.observe(elem, "blur", this.onChangeBoundHandler);
                        }
                        else if (elem.type.toLowerCase() == "checkbox") {
                            Event.observe(elem, "checked", this.onChangeBoundHandler);
                        }
                        else {
                            alert("AutoPopulator is not supported for \"Input\" fields of type \"" + elem.type + "\"");
                        }
                    }
                    else {
                        alert("AutoPopulator is not supported for \"" + elem.tagName + "\"");
                    }
                }
            }

        },

        onSrcChange: function() {
            if (this.FieldNames.length == 0) {
                return;
            }

            var somethingChanged = false;
            var currentValues = new Array();
            for (var i = 0; i < this.FieldNames.length; i++) {
                var fieldName = this.FieldNames[i];
                var autopopulateElementDetails = this.AutoPopulateElements[fieldName];
                currentValues[currentValues.length] = $F(autopopulateElementDetails.element);
                
                if (autopopulateElementDetails != null) {
                    if (autopopulateElementDetails.value == null || autopopulateElementDetails.value.length == 0 || autopopulateElementDetails.value != $F(autopopulateElementDetails.element)) {
                        somethingChanged = true;
                    }
                }

            }

            if (somethingChanged) {
                this.destElement.disable();
                $S(this.destElement, "Loading...");

                ServerFunction("Odatech.Presentation.AutoPopulationHelper.AutoPopulatePropertyValue|"
               + this.parentClassFullName + "||" + currentValues.join(';') + "|" + this.autoPopulationPath + "|true", this.destElement.id);
            }
        }
    };

    AutoPopulatePropertyValue = function(args, context) {
        context = $(context);
        if (args.startsWith("AutoPopulation Failed.")) {
            args = globalReplace(args, "AutoPopulation Failed. ", "");
            odaconsole.debug(args);
            alert("Auto Population Failed.");
        }
        else {
            $S(context, args);
        }

        if (!this.readOnlyAfterPopulation) {
            context.enable();
        }
    };

}