/* newWindow - opens a new browser window with the features specified.
 * url, name, features, replace
 */
function newWindow(url, name, w, h, scrollbars, resizable, menubar, toolbar) {
   var winleft = (screen.width - w) / 2;
   var wintop = (screen.height - h) / 2;

   features = 'height='+h+', width='+w+', top='+wintop+', left='+winleft+'';

   if(scrollbars == 'yes') { features += ',scrollbars=yes'; }
   if(resizable  == 'yes') { features += ',resizable=yes'; }
   if(menubar  == 'yes') { features += ',menubar=yes'; }
   if(toolbar  == 'yes') { features += ',toolbar=yes'; }

   var win = window.open(url, name, features);
   win.focus();
}
/*
    append an option/value pair to a select list. l = list, o = option,
    v = value
*/
function appendSelectOption(l,o,v) {
    var opt = document.createElement('option');
    opt.setAttribute('value', v);
    opt.appendChild(document.createTextNode(o));
    l.appendChild(opt);
    return;
}
/*
    populateSelect() fills a select element from an array of option/value pairs
    The array must have elements like obj.option and obj.value.
*/
function populateSelect(id, list) {
    var opt = null;
    if(!id) { return false; }
    id.length = 0;
    for(var i in list) {
        opt = document.createElement('option');
        opt.setAttribute('value', list[i].value);
        opt.appendChild(document.createTextNode(list[i].option));
        id.appendChild(opt);
    }
}
/*
    Returns the index in a select list for the given option value. sid is the
    id of a select list, opt is the name of the option.
*/
function getSelectOptionIndex(sid, opt) {
    //assumes default value is 'null' for non-selected elements
    if(!opt) { opt = 'null'; }
    var index = 0;
    for ( var i = 0; i <= sid.length; i++) {
        if ( sid.options[i] ) {
            if ( sid.options[i].value === opt ) {
                index = i;
                break;
            }
        }
    }
    return index;
}
function getSelectedOption(list) {
    if(!list) { return false; }
    if(!list.selectedIndex >= 0) { return false; }
    var v = list.options[list.selectedIndex].option;
    return v == null ? false : v;
}
function getSelectedValue(list) {
    if(!list) { return false; }
    if(!list.selectedIndex >= 0) { return false; }
    var v = list.options[list.selectedIndex].value;
    return v == null ? false : v;
}
/* empty out a select list */
function clearSelectList(list) {
    if(!list) { return false; }
    list.length = 0;
    return;
}
/*
    getSelectedValues() returns a comma separated string of values from
    a form select object
*/
function getSelectedValues(id) {
    var str = new String();
    for(var i = 0; i < id.options.length; i++) {
        if(id.options[i].selected) {
            str += str.length > 0                                                               ? ',' + id.options[i].value
                : id.options[i].value;
        }
    }
    return str.length > 0 ? str : null;

    //if (str.length > 0) { return str; }
    //return;
}
/* set the selected value in a select list */
function setSelectedValue(list, value) {
    if ( typeof(list) == 'string' ) {
        list = document.getElementById(list);
    }
    if (!list) { return false; }
    for(var i = 0; i <= list.length; i++){
        if(list.options[i].value === value) {
            list.options.selectedIndex = i;
            return;
        }
    }
    return;
}
/*
    dateToString formats a JavaScript date object to ISO 8601 format
*/
function dateToString(dateObj) {
    var day = dateObj.getDate();
    var month = dateObj.getMonth() + 1;
    var year = dateObj.getFullYear();
    var re = /^(\d{1})$/;
    day = day.toString();
    day = day.replace(re, "0$1");
    month = month.toString();
    month = month.replace(re, "0$1");
    return year + '-' + month + '-' + day;
}
/*
    strToDateObj - takes an ISO string YYYY-MM-DD and returns a Javascript
    Date object
*/
function strToDateObj(s) {
  var dateStr = s.split('-');
  var year = dateStr[0];
  var month = dateStr[1];
  // months are zero based, thanks
  month--;
  var day = dateStr[2];
  var dateObj = new Date(year,month,day);
  return dateObj;
}
/*
    Returns the value for the selected radio option in a group
*/
function getRadioSelection ( name ) {
    var input = document.getElementsByTagName('input');
    for ( var i = 0; i < input.length; i++ ) {
        if ( input[i].name == name ) {
            if ( input[i].checked ) {
                return input[i].value;
            }
        }
    }
    return;
}
/*
    hide/unhide an element
*/
function toggle(id) {
    var el = document.getElementById(id);

    if ( el.style.display == 'block' ) {
        el.style.display = 'none';
    }
    else {
        el.style.display = 'block';
    }

    return;
}
