var pop_window;

function getScreenWidth()
{
    if (screen.availWidth) {
        return screen.availWidth;
    } else {
        return 0;
    }
}

function getScreenHeight()
{
    if (screen.availHeight) {
        return screen.availHeight;
    } else {
        return 0;
    }
}

function openFullDialog(pop_url) {

    x = getScreenWidth();
    if(!x) { x = 750; }

    y = getScreenHeight();
    if(!y) { y = 550; }

    openDialog(pop_url, x, y);
}

function openDialog(pop_url) {
    x = openDialog.arguments[1]
        ? openDialog.arguments[1]
        : 'medium';

    if (x == 'small') {
        x = 500;
    } else if (x == 'medium') {
        x = 600;
    } else if (x == 'large') {
        x = 700;
    }

    y = openDialog.arguments[2]
        ? openDialog.arguments[2]
        : 'medium';

    if (y == 'small') {
        y = 400;
    } else if (y == 'medium') {
        y = 500;
    } else if (y == 'large') {
        y = 600;
    }

    if (openDialog.arguments[3]) {
        options = openDialog.arguments[3]+ ',width='+x+',height='+y;
    } else {
        options = 'scrollbars,width='+x+',height='+y;
    }

    pop_window = open(pop_url,'',options);
    setTimeout('focusPop(pop_window)',1000);
}

function focusPop(pop_window){
	pop_window.focus();	
}

function refreshParent()
{
    opener.history.go(0);
}

function toggleSearchForm()
{
   if (document.getElementById('search_container')) {
       var el = document.getElementById('search_container');
       var on_button = document.getElementById('show_search');
       var off_button = document.getElementById('hide_search');
       if (el.style.display == "none") {
           el.style.display = "block";
           on_button.style.display = 'none';
           off_button.style.display = 'inline';
       } else {
           el.style.display = "none";
           on_button.style.display = 'inline';
           off_button.style.display = 'none';
       }
   }
}

function setElDisplay(el_name, state)
{
   if (document.getElementById(el_name)) {
       var el = document.getElementById(el_name);
       if (state) {
           el.style.display = "inline";
       } else {
           el.style.display = "none";
       }
   }
}

function setCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}

function setComboText(form_name, the_select, the_text)
{
    el_select = eval("document." + form_name + '.' + the_select);
    el_text = eval("document." + form_name + '.' + the_text);
    el_text.value = el_select.options[el_select.selectedIndex].value;
}

function setComboSelect(form_name, the_select, the_text)
{
    el_select = eval("document." + form_name + '.' + the_select);
    if (el_select) {
        el_text = eval("document." + form_name + '.' + the_text);
        if (el_text) {
            setSelect(form_name, the_select, el_text.value)
        }
    }
}

// populate form fields with data from an array
// array must have the key as the form field name
// the value as the data to be set
function populateForm(destination_form, the_data)
{
    for (var i in the_data) {
        s = 'document.' + destination_form + '.' + i;
        el = eval(s);

        // text inputs and combo boxes
        if (el) {
            if (el.type == 'text') {
                el.value = the_data[i];
                // if this is a combo box, update the select as well
                select_name = i + '_select';
                setComboSelect(destination_form, select_name, i);
            }
            if (el.type == 'select-one') {
                setSelect(destination_form, i, the_data[i]);
            }
            if (el[0] && el[0].type == 'radio') {
                setRadio(destination_form, i, the_data[i]);
            }
        }
    }
}

function setSelect(form_name, el_name, el_val)
{
    el_select = eval("document." + form_name + '.' + el_name);
    if (el_select) {
        l = el_select.length;
        for (i = 0; i < l; i++) {
            if (el_val == el_select.options[i].value) {
                el_select.selectedIndex = i;
                break;
            }
        }
    }
}

function setRadio(form_name, el_name, el_val)
{
    el_radio = eval('document.' + form_name + '.' + el_name);
    if (el_radio) {
        for (var i = 0; i < el_radio.length; i++) {
            if (el_radio[i].value == el_val) {
                el_radio[i].checked = true;
            }
        }
    }
}

function setCheckbox(form_name, el_name, el_val)
{
    el_cb = eval('document.' + form_name + '.' + el_name);
    if (el_cb) {
        if (el_val == 'on') {
            el_cb.checked = true;
        }
        if (el_val == 'off') {
            el_cb.checked = false;
        }
    }
}

function setTextInput(form_name, el_name, el_val)
{
    el_text = eval('document.' + form_name + '.' + el_name);
    if (el_text) {
        el_text.value = el_val;
    }
}

