﻿var AjaxCombo = {

    IsVisible: function(id) {
        try {
            var combo = document.getElementById(id);
            if (combo.style.visbility == 'hidden') {
                return false;
            }
            return true;
        }
        catch (ex)
        { }
    },

    GetSelectedValue: function(id) {
        try {
            var combo = document.getElementById(id);
            if (combo != null) {
                var deger = combo.options[combo.selectedIndex].value;
                return deger;
            }
        }
        catch (ex)
        { }
    },
    SetSelectedValue: function(id, value) {
        try {
            var combo = document.getElementById(id);

            if (combo != null) {
                var values = combo.options;
                for (var i = 0; i < values.length; i++) {
                    if (values[i].value == value) {
                        values[i].selected = 'selected';
                    }
                }
            }
        } catch (ex) {

        }
    },
    GetSelectedText: function(id) {
        var combo = document.getElementById(id);
        if (combo != null) {
            return combo.options[combo.selectedIndex].text;
        }
    },
    Selected: function(id) {
        try {
            var combo = document.getElementById(id);
            if (combo != null) {
                return combo.value;
            }
        }
        catch (ex) {

        }
    },
    Null: function(id) {
        var combo = document.getElementById(id);
        if (combo != null) {
            if (combo.options.lenght > 0) {
                combo.options[0].selected = 'selected';
            }
        }
    },

    AjaxFill: function(id, datasource) {
        try {
            if (id.length > 0 && datasource.length > 0) {
                Ajax.GetXml("../../" + datasource, 'AjaxCombo.FillFromAjax', id, 'true');
            }
        }
        catch (ex) {

        }
        //showloading
    },

    FillFromAjax: function(result, id) {
        try {
            var elm = document.getElementById(id);
            elm.options.length = 0;
            var options = result.split('<option value="');
            for (var i = 0; i < options.length; i++) {
                if (options[i].length > 1) {
                    var oOption = document.createElement("OPTION");
                    var text = options[i].split('">');
                    oOption.text = text[1].replace('</option>', '');
                    oOption.value = text[0];
                    elm.options.add(oOption);
                }
            }


            var selectedValueHidden = document.getElementById(id + 'SetSelectedValue');
            if (selectedValueHidden != null && selectedValueHidden != undefined) {
                var selectedValue = selectedValueHidden.value;
                AjaxCombo.SetSelectedValue(id, selectedValue);

                elm.onchange();
            }
        }
        catch (ex) {

        }
        //hide loading
    },

    FillAllComboInPage: function() {
        var tags = document.getElementsByTagName('SELECT');
        for (var i = 0; i < tags.length; i++) {
            try {
                AjaxCombo.AjaxFill(tags[i].id, '');
            }
            catch (ex) {

            }
        }
    }

}
