﻿/// <reference path="NexidaJavaScriptLib.js"/>

if (typeof (Nexida) == "undefined") {
    throw "NexidaJavaScriptLib.js is not loaded";
}

Nexida.TP.JavaScript.MapUtility = new Object();

Nexida.TP.JavaScript.MapUtility.UpdatableInputs = new function() {
    var $getById = Nexida.$getById;

    var bag = new Array();

    this.Count = 0;

    this.Contains = function(parentKey, keyName) {
        for (var i = 0; i < bag.length; i++) {
            if (bag[i].key == keyName && bag[i].parentKey == parentKey) {
                return true;
            }
        }
        return false;
    };

    this.Add = function(parentKey, keyName, inputId) {
        if ((inputId != null) && (typeof (inputId) != "undefined") && !this.Contains(parentKey, keyName)) {
            var inputToAdd = $getById(inputId);
            if (inputToAdd != null && inputToAdd.tagName == 'INPUT') {
                bag[this.Count] = {
                    key: keyName,
                    input: inputToAdd,
                    parentKey: parentKey
                };
                this.Count++;
            }
        }
    };

    this.Get = function(parentKey, keyName) {
        for (var i = 0; i < bag.length; i++) {
            if (bag[i].key == keyName && bag[i].parentKey == parentKey) {
                return bag[i].input;
            }
        }
        return null;
    };

    this.GetKeys = function(parentKey) {
        var result = new Array();
        for (var i = 0; i < bag.length; i++) {
            if (bag[i].parentKey == parentKey) {
                result[i] = bag[i].key;
            }
        }
        return result;
    };

    this.GetKeysAndValues = function(parentKey) {
        var result = new Array();

        for (var i = 0; i < bag.length; i++) {
            if (bag[i].parentKey == parentKey) {
                var valueForResult = bag[i].input.value;

                if ((bag[i].input.type == 'checkbox' || bag[i].input.type == 'radio')
                        && Nexida.$browser.IsIE)
                    valueForResult = bag[i].input.checked.toString();
                if ((bag[i].input.type == 'checkbox' || bag[i].input.type == 'radio')
                        && Nexida.$browser.IsMozilla)
                    valueForResult = (bag[i].input.checked == 'checked').toString();
                result[i] = { key: bag[i].key, value: valueForResult };
            }
        }
        return result;
    };

    this.SetKeysAndValues = function(parentKey, keysAndValues) {
        if (keysAndValues.length > 0
            && typeof (keysAndValues[0].key) != "undefined"
            && typeof (keysAndValues[0].value) != "undefined") {

            for (var i = 0; i < keysAndValues.length; i++) {
                var input = this.Get(parentKey, keysAndValues[i].key);
                if (input != null) {
                    if (input.type == 'checkbox' || input.type == 'radio')
                        input.checked = keysAndValues[i].value;
                    else
                        input.value = keysAndValues[i].value;
                }
            }
        }

    };
};
Nexida.TP.JavaScript.MapUtility.Registry = new Nexida.TP.JavaScript.RegistryClass();

Nexida.TP.JavaScript.MapUtility.ShowPopup = function(key, arguments) {
    if (typeof (Sys) != "undefined" && Nexida.TP.JavaScript.MapUtility.Registry.Contains(key)) {
        var obj = Nexida.TP.JavaScript.MapUtility.Registry.GetByKey(key);
        var popupBehaviour = $find(obj.popupBehaviourId);
        if (popupBehaviour != null) popupBehaviour.show();
        var tpMapClientId = obj.tpMapClientId;
        if (tpMapClientId != null) {
            var flashObject = Nexida.$getById(tpMapClientId, true);
            for (var i in arguments) {
                flashObject.SetVariable(arguments[i].key, arguments[i].value);
            }
        }
    }
};
Nexida.TP.JavaScript.MapUtility.HidePopup = function(key) {
    if (typeof (Sys) != "undefined" && Nexida.TP.JavaScript.MapUtility.Registry.Contains(key)) {
        var obj = Nexida.TP.JavaScript.MapUtility.Registry.GetByKey(key);
        var popupBehaviour = $find(obj.popupBehaviourId);
        if (popupBehaviour != null) popupBehaviour.hide();
    }
};

