// -------------------------------------------------------------
// General purpose routines.
// -------------------------------------------------------------

// -------------------------------------------------------------
// Get the value of cookie.
// -------------------------------------------------------------

function get_cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) {
	return null;
    }
    if (start == -1) {
	return null;
    }
    var end = document.cookie.indexOf(";",len);
    if (end == -1) {
	end = document.cookie.length;
    }
    return unescape(document.cookie.substring(len,end));
}

// -------------------------------------------------------------
// Set the value of a cookie.
// -------------------------------------------------------------

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

// -------------------------------------------------------------
// Set the value of a select form. Because it is convenient to 
// do so, this can be called with an undefined sel, in which
// case this function doesn't do anything.
// -------------------------------------------------------------

function set_select(sel, val) {
    if(sel) {
	if(sel.options[sel.selectedIndex].value ==val) {
	    return;
	}
	for(var i = 0; i < sel.options.length; ++i) {
	    if(sel.options[i].value ==val) {
		sel.options[i].selected = true;
	    } else {
		sel.options[i].selected = false;
	    }
	}
    }
}

// -------------------------------------------------------------
// Set the value of a radio button form.
// -------------------------------------------------------------

function set_radio(rad, val) {

    for(var i = 0; i < rad.length; ++i) {
	if(rad[i].value ==val) {
	    rad[i].checked = true;
	} else {
	    rad[i].checked = false;
	}
    }
}
