
function fnUpdatePossibleValues(oInput, sKey,sSelectedValue)
{
    if(oInput == null)
        return;

    if(typeof sSelectedValue == "undefined" || sSelectedValue == "")
    {
        sSelectedValue = fnGetSelectedValue(oInput);
    }

    // ar is the array of new possible values, taken
    // from the map "pvs"
    var ar = oInput.pvs[sKey];

    // offset for null option ("--Select--")
    if(typeof oInput.options != "undefined" && 
       oInput.options[0] != null && 
       oInput.options[0].value == "")
       offset = 1;
    else
       offset = 0;

    // go backwards to ensure we don't miss anything
    for(i = oInput.options.length - 1; i >= offset; i--)
    {
        oInput.options[i] = null;
    }
    var bSelectedFound = false;
    if(typeof ar != "undefined" && ar.length != null)
    {
        for(i = 0; i < ar.length; i++)
        {
            if(typeof ar[i] == "string")
            {
                sName = fnGetNameFromPV(ar[i]);
                sValue = fnGetValueFromPV(ar[i]);
                oInput.options[i+offset] = new Option(sName,sValue);
//                if(sSelectedValue != "" && sSelectedValue == ar[i])
                if(sSelectedValue != "" && sSelectedValue == sValue)
                {
                    oInput.options[i+offset].selected = true;
                    bSelectedFound = true;
                }
            }
            else
            {
                oInput.options[i+offset] = ar[i];
                if(ar[i] != null && sSelectedValue != "" && sSelectedValue == ar[i].value)
                {
                    oInput.options[i+offset].selected = true;
                    bSelectedFound = true;
                }
            }
        }
    }
    if(typeof oInput.options[0] != "undefined" && !bSelectedFound)
    {
        oInput.options[0].selected = true;
    }

    return bSelectedFound;
}

function fnGetNameFromPV(s)
{
    if(typeof(s) == "undefined" || s == null || s.length == 0)
        return null;

    var nSplit = fnGetFirstDelimiterPosition(s,"/");
    if(nSplit < 0)
        return s;

    return s.substring(0,nSplit);
}

function fnGetValueFromPV(s)
{
    if(typeof(s) == "undefined" || s == null || s.length == 0)
        return null;

    var nSplit = fnGetFirstDelimiterPosition(s,"/");

    if(nSplit < 0)
        return s;

    return s.substring(nSplit+1);
}

// returns the first instance of the character "delim",
// disregarding any backslash escapfication
function fnGetFirstDelimiterPosition(s,delim, nStart)
{
    if(typeof(s) == "undefined" ||
       s == null ||
       s.length == 0 ||
       typeof(delim) == "undefined" ||
       delim == null ||
       delim.length != 1)
        return -1;

    if(typeof(nStart)=="undefined" || nStart == null)
        nStart = 0        
    var nSplit = nStart;

    while(nSplit >= 0)
    {
        nSplit = s.indexOf(delim, nSplit);
        if(nSplit > 0 && s.charAt(nSplit-1) == "\\")
        {
            nSplit++;
            continue;
        }
        else
            break;
        nSplit++;
    }
    return nSplit;
}

function fnGetSelectedIndex(oInput)
{
    var sType = null;
    if(typeof oInput.type != "undefined")
        sType = oInput.type;
    else if(oInput.length > 0)
        sType = oInput[0].type;

    if(typeof oInput != "undefined")
    {
        if(sType.substring(0,6) == "select" && typeof oInput.options != "undefined")
        {
            for(var i = 0; i < oInput.length; i++)
            {
                if(oInput.options[i].selected == true)
                    return i;
            }
        }
        else if(sType == "radio")
        {
            // In case we were passed a radio button, we want to
            // get the group.
            if(typeof oInput[0] == "undefined")
                oInput = oInput.form.elements[oInput.name];
            for(var i = 0; i < oInput.length; i++)
            {
                if(oInput[i].checked == true)
                    return i;
            }
        }
    }
}

function fnGetSelectedValue(oInput)
{
    var answer = "";
    var sType = null;
    if(typeof oInput.type != "undefined")
        sType = oInput.type;
    else if(oInput.length > 0)
        sType = oInput[0].type;

    if(typeof oInput != "undefined")
    {
        var n = -1;
        n = fnGetSelectedIndex(oInput);
        if(sType.substring(0,6) == "select" && typeof oInput.options != "undefined")
        {
            if(n >= 0 && n < oInput.options.length)
                answer = oInput.options[n].value;
        }
        else if(sType == "radio")
        {
            // In case we were passed a radio button, we want to
            // get the group.
            if(typeof oInput[0] == "undefined")
                oInput = oInput.form.elements[oInput.name];
            if(n >= 0 && n < oInput.length)
                answer = oInput[n].value;
        }
    }

    return answer;
}
/*
function fnGetSelectedValue(oInput)
{
    var answer = "";
    var sType = null;
    if(typeof oInput.type != "undefined")
        sType = oInput.type;
    else if(oInput.length > 0)
        sType = oInput[0].type;

    if(typeof oInput != "undefined")
    {
        var n = -1;
        n = fnGetSelectedIndex(oInput);
        if(sType.substring(0,6) == "select" && typeof oInput.options != "undefined")
        {
            if(n >= 0 && n < oInput.options.length)
                answer = oInput.options[n].value;
        }
        else if(sType == "radio")
        {
            // In case we were passed a radio button, we want to
            // get the group.
            if(typeof oInput.length == "undefined")
                oInput = oInput.form.elements[oInput.name];
            if(n >= 0 && n < oInput.length)
                answer = oInput[n].value;
        }
    }

    return answer;
}*/

function fnGetSelectedIndex(oInput)
{
    var sType = null;
    if(typeof oInput.type != "undefined")
        sType = oInput.type;
    else if(oInput.length > 0)
        sType = oInput[0].type;

    if(typeof oInput != "undefined")
    {
        if(sType.substring(0,6) == "select" && typeof oInput.options != "undefined")
        {
            for(var i = 0; i < oInput.length; i++)
            {
                if(oInput.options[i].selected == true)
                    return i;
            }
        }
        else if(sType == "radio")
        {
            // In case we were passed a radio button, we want to
            // get the group.
            if(typeof oInput.length == "undefined")
                oInput = oInput.form.elements[oInput.name];
            for(var i = 0; i < oInput.length; i++)
            {
                if(oInput[i].checked == true)
                    return i;
            }
        }
    }
}
