function confirm_href(prompt, href)
{
  if (confirm(prompt))
      window.location = href;
}

function radio_value(radio_set)
{
    for (var i=0; i<radio_set.length; i++)
        {
            if (radio_set.item(i).checked)
		return radio_set.item(i).value;
        }
    return false;
}

function get_selected_option(select_id)
{
    var select = (typeof(select_id) == "string") ? get_element(select_id) : select_id;
    return select.options[select.selectedIndex];
}

function is_valid_date(day, month, year)
{
    var date = new Date(year,month,day);
    return ((day==date.getDate()) && (month==date.getMonth()) && (year==date.getFullYear()));
}

function date_string_is_correct(date)
{
    var parts = date.split("/");
    var m = parts[0];
    var d = parts[1];
    var y = parts[2];
    return is_valid_date(d, m-1, y);
}


function is_valid_email(email) 
{
    return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email));
}

function foreach(proc, container)
{
    for (i = 0; i < container.length; ++i)
	proc(container.item(i));
}

function map(proc, items)
{
    var a = new Array();

    for (var i = 0; i < items.length; ++i)
        a.push(proc(items.item(i)));

    return a;
} 

function append_strings(strings, separator)
{
    var ret = "";
    for (var i = 0; i < strings.length; ++i)
	{
	    ret += strings[i];
	    if ((i + 1) < strings.length) ret += separator;
	}
    return ret;
}

function open_in_small_window(url)
{
    var name = "common_window_" + unique_id();
    var w = 500;
    var h = 400;
    var options = "status=0,toolbar=0,scrollbars=1,width=" + w + ",height=" + h;
    var win = window.open(url, name, options);
    
    var x = (screen.width - w) / 2;
    var y = (screen.height - h) / 2;
    win.moveTo(x, y);
}

// Removes leading whitespaces
function ltrim(value)
{
    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");
}

// Removes ending whitespaces
function rtrim(value)
{
    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim(value)
{
    return ltrim(rtrim(value));
}


