var agent = navigator.userAgent.toLowerCase();
var ie    = ((agent.indexOf("msie") != -1) && (agent.indexOf("opera") == -1));
var mac   = (agent.indexOf("safari") != -1 && !ie);

function clearSelect(sel)
{
    if (!sel)
	{
       return;
	}

    var Child = sel.firstChild;

    while (Child)
    {
        sel.removeChild(Child);
        Child = sel.firstChild;
    }
}

function ListItem(id, text)
{
    this.id = id;
    this.text = text;
}

function setupSelect(sel, vals)
{
   clearSelect(sel);
   
   var optgr = null;

    for (var i in vals)
    {
        if (vals[i].id == "-1")
        {
            optgr = document.createElement("OPTGROUP");
            optgr.label = vals[i].text;
            sel.appendChild(optgr);
        }
        else
        {
            var opt = document.createElement("OPTION");
            opt.value = vals[i].id;

            if (optgr)
            {
                if (ie || mac)
                {
                    opt.innerText = vals[i].text;
                }
                else
                {
                    opt.text = vals[i].text;
                }

                optgr.appendChild(opt);
            }
            else
            {
                opt.text = vals[i].text;

                if (ie || mac)
                {
                    sel.add(opt);
                }
                else
                {
                    sel.add(opt, null);
                }
            }
        }
    }
}
