﻿//utilityservices.js


function DoOptionControlChanged( idChangedOptionControl,idAffectedOptionControl, service )
{
    var affectedOptionControl = document.getElementById( idAffectedOptionControl );
    if(  !IsNullOrEmpty(affectedOptionControl  ))
    {
        var nNodeCount = affectedOptionControl.childNodes.length;
        for( n = nNodeCount-1; n >= 0; n--)
        {
            affectedOptionControl.removeChild( affectedOptionControl.childNodes[n] );
        }
        
        var changedOptionControl = document.getElementById( idChangedOptionControl );
        if(  !IsNullOrEmpty(changedOptionControl  ))
        {
            var val = changedOptionControl.value;
            if(  !IsNullOrEmpty(val  ))
            {
                 service( idAffectedOptionControl, val, OnOptionsListCompleted );
            }
        }
    }
}

function OnOptionsListCompleted( result )
{
    if ( !IsNullOrEmpty(result ))
    {
        var aResults = result.split("|");
        var strID = aResults[0];
        
        if(  !IsNullOrEmpty(strID  ))
        {
            LoadOptions( result );
        }
    }
}
function LoadOptions( options )
{
    var aResults = options.split("|");
    var strID = aResults[0];
        
    if(  !IsNullOrEmpty(strID  ))
    {
        var elem = document.getElementById(strID);
        if(  !IsNullOrEmpty(elem  ))
        {
            for( var i = 1; i < aResults.length; i++)
            {
                var oOption = document.createElement("OPTION");
                
                str = aResults[i].trim();
                var a = str.split("=");
                oOption.text=a[1];
                ++i;
                str = aResults[i].trim();
                a = str.split("=");
                oOption.value=a[1];
               
                elem.options.add(oOption);
            }
        }
    }
}



function DoOptionListAddFromSource( strOptionListSource, strOptionListTarget )
{
    var sourceOptionControl = document.getElementById( strOptionListSource );
    if(  !IsNullOrEmpty(sourceOptionControl  ))
    {
        var targetOptionControl = document.getElementById( strOptionListTarget );
        if(  !IsNullOrEmpty(targetOptionControl  ))
        {
            var text = sourceOptionControl.outerText;
            var value = sourceOptionControl.value;
            if(  !IsNullOrEmpty(value  )&&  !IsNullOrEmpty(text ))
            {
                var oOption = document.createElement("OPTION");
                oOption.text=text;
                oOption.value=value;
                targetOptionControl.options.add(oOption);
            }
        }
    }
}




if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();