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)
{
    if(!sel)
        return;
    var opt = document.createElement("OPTION");
    opt.value = 0;
    opt.text = 'Please select:';
    try
    {
        if(ie)
        {
            sel.add(opt);
        }
        else
        {
            sel.add(opt, null);
        }
    }
    catch(e)
    {}
}

function disableSelect(sel)
{
    if(!sel)
        return;
        sel.disabled = true;
}

function enableSelect(sel)
{
    if(!sel)
        return;
        sel.disabled = false;
}

function setupSelect(sel, opts, pls)
{
    if(!sel)
        return;
    if (opts == null)
    {
        sel.disabled = true;
        return;
    }
    sel.disabled = false;
    fillOptions(sel, opts, pls);
}

function setupSelectById(sel, opts, id, pls)
{
    if(!sel)
        return;
    if (opts == null)
    {
        sel.disabled = true;
        return;
    }
    sel.disabled = false;

    var span = document.createElement("SPAN");
    span.id = "BOX_IE_FIX1";
    document.body.appendChild(span);
    span = document.getElementById("BOX_IE_FIX1");
    span.style.display = "none";

    if(pls) addPleaseOption(sel);
    var opt = document.createElement("OPTION");
    opt.value = opts[id].id;
    span.innerHTML = opts[id].text;
    opt.text = span.innerHTML;
    opt.selected = true;
    try
    {
        if(ie)
        {
            sel.add(opt);
        }
        else
        {
            sel.add(opt, null);
        }
    }
    catch(e)
    {}
    document.body.removeChild(span);
    span = null;
}

function setupSelectByKey(sel, opts, id, pls)
{
    if(!sel)
        return;
    if (opts == null)
    {
        sel.disabled = true;
        return;
    }

    var span = document.createElement("SPAN");
    span.id = "BOX_IE_FIX1";
    document.body.appendChild(span);
    span = document.getElementById("BOX_IE_FIX1");
    span.style.display = "none";

    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;
	    span.innerHTML = opts[i].text;
            opt.text = span.innerHTML;
            try
            {
                if(ie)
                {
                    sel.add(opt);
                }
                else
                {
                    sel.add(opt, null);
                }
            }
            catch(e)
            {}
        }
    }
    if(pls)
    {
    try
    {
        sel.selectedIndex = sel.selectedIndex + 1;
    }
    catch(e)
    {}
    }
    document.body.removeChild(span);
    span = null;
}

function clearSelect(sel)
{
    try
    {
        var len  = sel.options.length ;
        for(var i=0; i<len; i++)
            sel.remove(0);
        sel.disabled = true;
    }
    catch(e)
    {
    }
}

function setSelected(sel, val)
{
    if(!sel)
        return;
    for(var i = 0; i < sel.options.length; i++)
    {
        if(sel.options[i].value == val)
        {
            sel.selectedIndex = i;
            return;
        }
    }
}

function fillOptions(opts, vals, pls)
{

    var span = document.createElement("SPAN");
    span.id = "BOX_IE_FIX1";
    document.body.appendChild(span);
    span = document.getElementById("BOX_IE_FIX1");
    span.style.display = "none";


    if(pls) addPleaseOption(opts);
    for (var i in vals)
    {
        var opt = document.createElement("OPTION");
        opt.value = i;
	span.innerHTML = vals[i].text;
        opt.text = span.innerHTML;
        try
        {
            if(ie)
            {
                opts.add(opt);
            }
            else
            {
                opts.add(opt, null);
            }
        }
        catch(e)
        {}
    }
    if(pls)
    {
    try
    {
        opts.selectedIndex = opts.selectedIndex + 1;
    }
    catch(e)
    {}
    }
    document.body.removeChild(span);
    span = 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)
{
    try
    {
        if(select.disabled || select.options[select.selectedIndex].value == value)
            return true;
        return false;
    }
    catch(e)
    {
        return false;
    }
}
