var agent=navigator.userAgent.toLowerCase();
var ie = ((agent.indexOf("msie") != -1)&&(agent.indexOf("opera")==-1));
function ListItem(id, text)
{
    this.id = id;
    this.text = text;
}

function addEmptyOption(sel)
{
}

function addPleaseOption(sel)
{
    var opt = document.createElement("OPTION");
    opt.value = 0;
    opt.text = 'Please select:';
    if(ie)
    {
        sel.add(opt);
    }
    else
    {
        sel.add(opt, null);
    }
}

function disableSelect(sel)
{
        sel.disabled = true;
}

function enableSelect(sel)
{
        sel.disabled = false;
}

function setupSelect(sel, opts, pls)
{
    if (opts == null)
    {
        sel.disabled = true;
        return;
    }
    sel.disabled = false;
    fillOptions(sel, opts, pls);
}

function setupSelectById(sel, opts, id, pls)
{
    if (opts == null)
    {
        sel.disabled = true;
        return;
    }
    sel.disabled = false;

    if(pls) addPleaseOption(sel);
    var opt = document.createElement("OPTION");
    opt.value = opts[id].id;
    opt.text = opts[id].text;
    opt.selected = true;
    if(ie)
    {
        sel.add(opt);
    }
    else
    {
        sel.add(opt, null);
    }
}

function setupSelectByKey(sel, opts, id, pls)
{
    if (opts == null)
    {
        sel.disabled = true;
        return;
    }
    sel.disabled = false;
    if(pls) addPleaseOption(sel);
    for (var i in opts)
    {
        if(opts[i].id == id)
        {
            var opt = document.createElement("OPTION");
            opt.value = i;
            opt.text = opts[i].text;
        if(ie)
        {
            sel.add(opt);
        }
        else
        {
            sel.add(opt, null);
        }
        }
    }
}

function clearSelect(sel)
{
    var len  = sel.options.length ;
    for(var i=0; i<len; i++)
        sel.remove(0);
    sel.disabled = true;
}

function setSelected(sel, val)
{
    for(var i = 0; i < sel.options.length; i++)
    {
        if(sel.options[i].value == val)
        {
        sel.selectedIndex = i;
            return;
        }
    }
}

function fillOptions(opts, vals, pls)
{
    if(pls) addPleaseOption(opts);
    for (var i in vals)
    {
        var opt = document.createElement("OPTION");
        opt.value = i;
        opt.text = vals[i].text;
    if(ie)
    {
            opts.add(opt);
        }
        else
        {
            opts.add(opt, null);
        }
    }
}

function filterFiles(fileList, ext)
{
    var ret = new Array();
    for (var i = 0; i < fileList.length; i++)
    {
        var li = fileList[i];
        var fnLen = li.id.length;
        var fileExt = li.id.substring(fnLen - ext.length, fnLen);
        if(fileExt == ext)
            ret[ret.length] = new ListItem(li.id, li.text);

    }
    return ret;
}

function updateCheckBox(checkBox)
{
    if(checkBox.checked)
        checkBox.value = 1;
    else
        checkBox.value = 0;
}
function isChecked(checkBox)
{
    return checkBox.checked;
}

function setupCheckBox(checkBox, val)
{
    if(val == 1)
        checkBox.checked = true;
    else
        checkBox.checked = false;
    updateCheckBox(checkBox);
}

function ltrim(str)
{
    return str.replace("/^s+/", '');
}

function rtrim(str)
{
    return str.replace("/s+$/", '');
}

function trim(str)
{
    return rtrim(ltrim(str));
}

function checkSelect(select, value)
{
    if(select.disabled || select.options[select.selectedIndex].value == value)
        return true;
    return false;
}

