var agent = navigator.userAgent.toLowerCase();
var ie    = ((agent.indexOf("msie") != -1) && (agent.indexOf("opera")==-1));
var mac   = (agent.indexOf("safari") != -1 && !ie);
var pleaseText = "Please select";

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 = pleaseText;

    if(ie || mac)
    {
        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;
    }

    var parent_width = null;

    if (sel.parentNode)
    {
        parent_width = sel.parentNode.clientWidth;
    }

    sel.disabled = false;
    clearSelect(sel);
    fillOptions(sel, opts, pls);

    sel.style.width = (parent_width && sel.clientWidth > parent_width) ? (parent_width - 5) : "";
}

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 || mac)
    {
        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 || mac)
            {
                sel.add(opt);
            }
            else
            {
                sel.add(opt, null);
            }
        }
    }
}

function clearSelect(sel)
{
    var Child = sel.firstChild;

    while (Child)
    {
        sel.removeChild(Child);
        Child = sel.firstChild;
    }
}

function setSelected(sel, val, contains)
{
    var contains = contains || false;

    for(var i = 0; i < sel.options.length; i++)
    {
        if(sel.options[i].value == val || (contains == true && sel.options[i].value.indexOf(val) != -1))
        {
            sel.options[i].selected = true;
            return true;
        }
    }

    return false;
}

function fillOptions(opts, vals, pls)
{
   if(pls)
   {
      addPleaseOption(opts);
   }

    for (var i in vals)
    {
        if (vals[i].id == "-1")
        {
            var optgr   = document.createElement("OPTGROUP");
            optgr.label     = vals[i].text;
            opts.appendChild(optgr);
        }
        else
        {
            var opt = document.createElement("OPTION");
            opt.value       = vals[i].id;

            if (typeof(optgr) != 'undefined')
            {
                setLeavingSelected(opts, opt)

                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)
                {
                    opts.add(opt);
                }
                else
                {
                    opts.add(opt, null);
                }

                setLeavingSelected(opts, opt);
            }
        }
    }
}

function setLeavingSelected(opts, opt)
{
    if (opts.name == "leaving" && opt.value == st_point)
    {
        opt.selected = true;
        st_point = 0;
    }
}

function updatePreferredCurrencies(sel, strCurrs)
{
    var lngCurrDiv = document.getElementById("f_currency");
    var imgCurrDiv = document.getElementById("f_currency_line");
    lngCurrDiv.style.display = "";

    if(imgCurrDiv)
    {
        imgCurrDiv.style.display = "";
    }

    clearSelect(sel);

    var currs_array = strCurrs.split(".");

    for (i in currs_array)
    {
        var currency = currs_array[i].split(":");

        var opt     = document.createElement("OPTION");
        opt.value   = currency[2];
        var oTempDiv = document.getElementById("tempDiv");

        oTempDiv.innerHTML = currency[0] + " (" + currency[1] + ")";
        opt.text = oTempDiv.innerHTML;
        oTempDiv.innerHTML = "";

        if (ie || mac)
        {
            sel.add(opt);
        }
        else
        {
            sel.options.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 checkSelect(select, value)
{
    if(select.disabled || select.options[select.selectedIndex].value == value)
        return true;
    return false;
}

function unescapeHTML(str)
{
    var div         = document.createElement('div');
    div.innerHTML   = str.replace(/<\/?[^>]+>/gi, '');
    return div.childNodes[0] ? (div.childNodes.length > 1 ? $A(div.childNodes).inject('',function(memo,node){ return memo + node.nodeValue }) : div.childNodes[0].nodeValue) : '';
}

function makeHighlighted(select, not_highlight)
{
    for (var i = 0; i < select.options.length; i++)
    {
        if (select[i].text.substr(0, 3) == "]x[")
        {
            select[i].text = select[i].text.substr(3, select[i].text.length);

            if (!not_highlight)
            {
                select[i].className = "highlightedOption";
            }
        }
    }
}

function fillNumeric(sel, minValue, maxValue)
{
    for (minValue; minValue <= maxValue; minValue++)
    {
        var opt = document.createElement("OPTION");
        opt.value = minValue;
        opt.text = minValue;

        if(ie || mac)
        {
            sel.add(opt);
        }
        else
        {
            sel.add(opt, null);
        }
    }
}

function refreshDatesSelect(dayField, mDays)
{
    clearSelect(dayField);

    var opts_dates = new Object();

    for(var i = 1; i <= mDays; i++)
    {
        opts_dates[i] = new ListItem(makeTwoDigit(i), i);
    }

    setupSelect(dayField, opts_dates);
}