﻿// This class provides the client-side code to operate the DynamicDropDownList.
// Must also link to Javascript/AjaxHelper.js to use this file.

function DynamicDropDownList(ctrlId, callPage, textColumnName, valueColumnName, disabled, disabledWhenDependentUnselected, normalCssClass, waitingCssClass, dataErrorMsg) {
    // Enum for control's status.
    this.CtrlStatus = { Disabled: 0, Enabled: 1, Waiting: 2 };
    
    this.ctrl = document.getElementById(ctrlId);
    this.callPage = callPage;
    this.textColumnName = textColumnName;
    this.valueColumnName = valueColumnName;
    if (disabled) {
        this.status = this.CtrlStatus.Disabled;
    }
    else {
        this.status = this.CtrlStatus.Enabled;
    }
    this.ctrlDisabledWhenDependentUnselected = disabledWhenDependentUnselected;
    this.normalCssClass = normalCssClass;
    this.waitingCssClass = waitingCssClass;
    this.dataErrorMsg = dataErrorMsg;
}

DynamicDropDownList.prototype.getListItems = function(dependentValue) {
    if (dependentValue > 0) {
        this.waitCtrl();
        AjaxHelper.callServer(this.callPage, {val: dependentValue}, "listItemsGotten", this);
    }
    else {
        if (!this.ctrlDisabledWhenDependentUnselected) {
            this.waitCtrl();
            AjaxHelper.callServer(this.callPage, {}, "listItemsGotten", this);
        }
        else {
            this.ctrl.options.length = 0;
            this.disableCtrl();
        }
    }
}

DynamicDropDownList.prototype.listItemsGotten = function(data) {
    if (data) {
        this.dataBindCtrl(data);
        this.enableCtrl();
    }
    else {
        alert(this.dataErrorMsg);
        this.disableCtrl();
    }
}

DynamicDropDownList.prototype.dataBindCtrl = function(data) {
    // Clear the items in the control by zeroing its count.
    this.ctrl.options.length = 0;
    
    var dataTable = data["Data"];

    if (dataTable.length == 0) {
        alert(this.dataErrorMsg);
        this.disableCtrl();
    }
    
    if (dataTable.length > 1) {
        // Put the -Please Select- option at the top of the select control.
        this.ctrl.options[0] = new Option("- Please Select -", "0");
    }
        
    for (var i = 0 ; i < dataTable.length ; i++) {
        // Retrieve the text and data items for the control and make a new Option to add to the control.
        var dr = dataTable[i];
        var textItem = dr[this.textColumnName];
        var valueItem = dr[this.valueColumnName];
        this.ctrl.options[this.ctrl.options.length] = new Option(textItem, valueItem);
    }
    
    this.ctrl.selectedIndex = 0;
}

DynamicDropDownList.prototype.enableCtrl = function() {
    if (this.status != this.CtrlStatus.Enabled) {
        this.ctrl.disabled = false;
        this.ctrl.className = this.normalCssClass;
        this.status = this.CtrlStatus.Enabled;
    }
}

DynamicDropDownList.prototype.disableCtrl = function() {
    if (this.status != this.CtrlStatus.Disabled) {
        this.ctrl.options.length = 0;
        this.ctrl.disabled = true;
        this.ctrl.className = this.normalCssClass;
        this.status = this.CtrlStatus.Disabled;
    }
}

DynamicDropDownList.prototype.waitCtrl = function() {
    if (this.status != this.CtrlStatus.Waiting) {
        this.ctrl.options.length = 0;
        this.ctrl.options[0] = new Option("- Please Wait -", "0");
        this.ctrl.className = this.waitingCssClass;
        this.status = this.CtrlStatus.Waiting;
    }
}

DynamicDropDownList.register = function(ctrlId, callPage, textColumnName, valueColumnName, disabled, disabledWhenDependentUnselected, normalCssClass, waitingCssClass, dataErrorMsg) {
    if (typeof dynamicDropDownListCollection == 'undefined') {
        dynamicDropDownListCollection = {};
    }
    dynamicDropDownListCollection[ctrlId] = new DynamicDropDownList(ctrlId, callPage, textColumnName, valueColumnName, disabled, disabledWhenDependentUnselected, normalCssClass, waitingCssClass, dataErrorMsg);
}

DynamicDropDownList.fillCtrl = function(ctrlId, dependentValue) {
//    for (id in dynamicDropDownListCollection) {
//        if (id == ctrlId) {
            dynamicDropDownListCollection[ctrlId].getListItems(dependentValue);
//            return;
//        }
//    }
}
