var NITKSelection_adfcallbackfunction;
var NITKSelection_objSelectionLoadingIndicatorId = "NITKSelection_selectionLoadingSpan";
var NITKSelection_txtSelectionLoadingIndicatorId = "NITKSelection_selectionLoadingText";

function NITKSelection_selectionLoadLayersResponse(sReturnValue, context) 
{
    NITKFramework_hide(NITKSelection_objSelectionLoadingIndicatorId);
    NITKSelection_updateLayerListControl(sReturnValue, "&", "^");
}

function NITKSelection_updateLayerListControl(sReturnValue, sKeyValueDelimeter, sSubSubValueDelimeter) {
    //alert('sKeyValueDelimeter=' + sKeyValueDelimeter + ', sSubSubValueDelimeter=' + sSubSubValueDelimeter + ', sReturnValue=' + sReturnValue);
    var arKeyValuePairs = sReturnValue.split(sKeyValueDelimeter);
    var bHasValues = true;
    
    // For each callback value returned
    for (var iIdx = 0; iIdx < arKeyValuePairs.length; iIdx++) {
        var arVals = arKeyValuePairs[iIdx].split("=");
        var selectCtrl = document.getElementById(arVals[0]);
        var iOptCnt = selectCtrl.options.length;                  
        
        // Remove options from dropdown
        for (var iOptIdx = iOptCnt - 1; iOptIdx >= 0; iOptIdx--) {
            selectCtrl.removeChild(selectCtrl.options[iOptIdx]);
        }
        
        var arOptionVals = null;
        
        if (arVals.length > 1)
        {
            arOptionVals = arVals[1].split('|');
        }        
        
        bHasValues = false;
        
        if (arOptionVals != null) {
            if (arOptionVals.length != 0) {  
                bHasValues = true;
            }
        }
        
        if (bHasValues) {
            // Add options returned from callback
            for (var iOptIdx = 0; iOptIdx < arOptionVals.length; iOptIdx++) {
                if (sSubSubValueDelimeter != "") {
                    
                    var sVals = arOptionVals[iOptIdx].split(sSubSubValueDelimeter);
                    var opt = document.createElement("option");
                    opt.innerHTML = sVals[0];
                    opt.value = sVals[1];
                    opt.title = sVals[0];
                    selectCtrl.appendChild(opt);
                }
                else {
                    var sVals = arOptionVals[iOptIdx]
                    var opt = document.createElement("option");
                    opt.innerHTML = sVals;
                    opt.value = sVals;
                    opt.title = sVals;
                    selectCtrl.appendChild(opt);
                }
            }
        }
        else {
            var opt = document.createElement("option");
            opt.innerHTML = "None";
            opt.value = "None";
            selectCtrl.appendChild(opt);
        }
    }
}

function NITKSelection_selectionLoadLayersError() 
{
    NITKFramework_hide(NITKSelection_objSelectionLoadingIndicatorId);
    alert("Error: Could not update layers");
}
