function addOptionToSelect(selectBox, addedOption, beforeOption)
{
    try
    {
        selectBox.add(addedOption, beforeOption); // standards compliant; doesn't work in IE
    }
    catch(ex)
    {
        selectBox.add(addedOption, addedOption.selectedIndex); // IE only
    }
}

function getValue(valueStr, offset)
{
    var endString = valueStr.indexOf(";", offset);
    if (endString == -1)
    {
        endString = valueStr.length;
    }
    return unescape(valueStr.substring(offset, endString));
}

function getAttribute(attributeList, attributeName)
{
    var attribute = "";
    var arg = attributeName + "=";
    var argLen = arg.length;
    var attrLen = attributeList.length;
    var i = 0;
    while ( (attribute.length == 0) && (i < attrLen ) )
    {
        var j = i + argLen;
        if (attributeList.substring(i, j) == arg)
        {
            attribute = getValue(attributeList, j);
        }
        else {
            i = attributeList.indexOf(" ", i) + 1;
        }
        if (i == 0)
            break;
    }

    return attribute;
}

function getAttributeInt(attributeList, attributeName)
{
   var valueStr = getAttribute(attributeList, attributeName);
   var value = 0;

   if (valueStr.length != 0)
   {
       value = parseInt(valueStr);
   }

   return value;
}

function parseMin(range)
{
    var divline = range.indexOf("-");
    var min = parseInt(range.substring(0, divline));

    return min;
}

function parseMax(range)
{
    var divline = range.indexOf("-");
    max = parseInt(range.substring(divline + 1, range.length) );

    return max;
}

function popUpURL(urlpop)
{
    var w = 720;
    var h = (screen.height) * 0.75;
    LeftPosition = (screen.width)?(screen.width - w) / 2:100;
    TopPosition = 10;
    settings = 'width=' + w + ',height=' + h + ',screenX=' + LeftPosition + ',screenY=' + TopPosition + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
    sampleWin = open(urlpop, "Report", settings);
}


function setCookie( name, value, expires, path, domain, secure) 
{
    var today = new Date();
    today.setTime( today.getTime() );

    if ( expires ) {
        expires = expires * 1000 * 60 * 60 * 24;
    }

    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + '=' + escape( value ) +
        ( ( expires ) ? ';expires=' + expires_date.toGMTString() : '' ) +
        ( ( path ) ? ';path=' + path : '' ) +
        ( ( domain ) ? ';domain=' + domain : '' ) +
        ( ( secure ) ? ';secure' : '' );
}

function getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg){
            return getCookieVal(j);
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
    }
    return null;
}

function getCookieVal(offset){
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}

function isAgreementViewed()
{
    var cookieValue;

    cookieValue = getCookie("UserAgreementAccept");

    if (cookieValue == null)
    {
        cookieValue = "NO";
    }

    return cookieValue;
}

function saveCheckboxState(checkBoxId, name)
{
    var checkBox = document.getElementById(checkBoxId);
    var checkState = "unchecked"

    if (checkBox.checked)
    {
        checkState = "checked"
    }

     setCookie(name, checkState, 365 * 5, '/');
}

function setCheckboxState(checkBoxId, name)
{
    var checkBox = document.getElementById(checkBoxId);
    var checkState = getCookie(name);

    if (checkState == "unchecked")
    {
        checkBox.checked = false;
    }
    else
    {
        checkBox.checked = true;
    }
}


function saveSelectItems(selectBoxId, name)
{
    var selectBox = document.getElementById(selectBoxId);
    var selectList = "";

    for (var i=0; i < selectBox.options.length; i++)
    {
        if (selectBox.options[i].selected)
        {
            selectList = selectList + selectBox.options[i].value + ",";
        }
    }

    if (selectList.length != null)
    {
        selectList = selectList.substring(0, selectList.length - 1);
    }

    setCookie(name, selectList, 365 * 5, '/');
}

function setSelectedItems(selectBoxId, name, defaultCount)
{
    var selectBox = document.getElementById(selectBoxId);
    var selectList = getCookie(name);

    if ( (selectList == null) || (selectList.length == 0) )
    {
        for (var i=0; i < defaultCount; i++)
        {
            selectBox.options[i].selected = true;
        }
    }
    else
    {
        var selectedItems = selectList.split(",");
        for (var j=0; j < selectBox.options.length; j++)
        {
            selectBox.options[j].selected = false;
            for (var k=0; k < selectedItems.length; k++)
            {
                if (selectBox.options[j].value == selectedItems[k])
                {
                    selectBox.options[j].selected = true;
                    break;
                }
            }
        }

    }
}
