var workWin = null;
var infoWin = null;

function close_child_window(win) {
    if ((win != null) && !win.closed) {
        win.close();
        win = null;
    }
    return true;
}

function open_work_window(href, width, height) {
    close_workWin();
    workWin = window.open(href, "tripWWin",
    "toolbar=0,location=0,directories=0,status=0," +
    "menubar=0,scrollbars=1,resizable=1,width=" + width + ",height=" + height);
}

function close_workWin() {
    return close_child_window(workWin)
}

function open_info_window(href) {
    if (!infoWin || infoWin.closed)
        infoWin = window.open(href, "tripIWin",
      "toolbar=0,location=0,directories=0,status=0," +
      "menubar=0,scrollbars=0,resizable=0,copyhistory=0,hotkeys=0," +
      "width=300,height=200,left=80,top=180");
    else
        infoWin.focus();
}

function close_infoWin() {
    return close_child_window(infoWin)
}

function open_test_window(href) {
    window.open(href, "sptestWindow");
}

function close_child_windows() {
    close_workWin();
    close_infoWin();
    return true;
}

function mark_changed(fo, el) {
    var fi = "_" + el;

    if (fo.elements[fi]) {
        fo.elements[fi].value = "1";
    }
}

function select_option(href) {
    close_workWin();
    open_work_window(href, 300, 400)
}

function set_optionreplace(obj, value, text) {
    obj.options[0] = new Option(value + ' - ' + text, value);
}

function clear_optionreplace(obj) {
    set_optionreplace(obj, '', 'N/A')
}

function set_option(obj, value, text) {
    for (i = 0; i < obj.length; i++) {
        if (obj.options[i].value == value) {
            //     alert('Value ' + value + ' is already listed.');
            return;
        }
    }
    var s = null;
    if (obj.length > 0)
        s = obj.options[obj.length - 1].text;
    obj.options[obj.length - 1] = new Option(value + ' - ' + text, value);
    if (s != null)
        obj.options[obj.length] = new Option(s, '');
}

function clear_options(obj) {
    obj.value = "";
}

function get_options(obj) {
    var list = '';
    for (i = 0; i < obj.length; i++) {
        list += obj.options[i].value + ' ';
    }
    return list;
}

function add_option(obj, href) {
    close_workWin();

    workWin = window.open(href + escape(obj.value), "tripWWin",
     "toolbar=0,location=0,directories=0,status=0," +
     "menubar=0,scrollbars=1,resizable=1,width=400,height=200");

    return false;
}

function remove_option(obj) {
    close_workWin();

    var idx = obj.selectedIndex;
    if ((idx == -1) || (idx == obj.length - 1))
        return;

    for (i = idx; i < obj.length - 1; i++) {
        obj.options[i].text = obj.options[i + 1].text;
        obj.options[i].value = obj.options[i + 1].value;
    }
    obj.length = obj.length - 1;
}

function new_option(href) {
    close_workWin();
    window.open(href, "tripWWin");
}

function edit_option(obj, href) {
    close_workWin();

    var idx = obj.selectedIndex;
    if ((idx == -1) || (idx == obj.length - 1))
        return;

    window.open(href + obj.options[idx].value, "tripWWin");
}



