emailRe = /^[\w\-\.]*@[\w\-\.]*\.[a-z]{2,5}$/i

function getRadioChoice( group )
{
    var result = null;

    for ( var i=0;
          ( i < group.length ) && ( result == null );
          i++ )
    {
        if ( group[i].checked )
            result = group[i].value;
    }

    return result;
}

function getSelectChoices( group )
{
    var choices = new Array();

    for ( var i=0; i < group.options.length; i++ )
    {
        if ( group.options[i].selected )
            choices[ choices.length ] = group.options[i].value;
    }

    return choices;
}

function getCheckboxChoices( groupName )
{
    var choices = new Array();

    var checkboxes = document.getElementsByName( groupName );

    for ( var i=0; i < checkboxes.length; i++ )
    {
        if ( checkboxes[i].checked )
            choices[ choices.length ] = checkboxes[i].value;
    }

    return choices;
}

function getCheckboxChoicesString( groupName, separator )
{
    var choices = "";

    var checkboxes = document.getElementsByName( groupName );

    for ( var i=0; i < checkboxes.length; i++ )
    {
        if ( checkboxes[i].checked ) {
        	if (choices != "") {
	        	 choices += separator;
	       	}
            choices += checkboxes[i].value;
        }
    }

    return choices;
}

function getRadioChoiceId( group )
{
    var result = 0;

    try
    {
        result = parseInt( getRadioChoice( group ) );
    }
    catch ( e )
    {
        result = 0;
    }

    if ( isNaN( result ) )
        result = 0;

    return result;
}

function getSelectChoiceIds( group )
{
    var choices = new Array();
    var rawChoices = getSelectChoices( group );

    for ( var i=0; i<rawChoices.length; i++ )
    {
        try
        {
            choices[ choices.length ] = parseInt( rawChoices[i] );
        }
        catch ( e ) { /* do nothing */ }
    }

    return choices;
}

function getCheckboxChoiceIds( groupName )
{
    var choices = new Array()
    var rawChoices = getCheckboxChoices( groupName );

    for ( var i=0; i<rawChoices.length; i++ )
    {
        try
        {
            choices[ choices.length ] = parseInt( rawChoices[i] );
        }
        catch ( e ) { /* do nothing */ alert( e ); }
    }

    return choices;
}

function setError( id, msg )
{
    var element = document.getElementById( id );
    element.innerHTML = msg;
}

function addErrorLine( id, msg )
{
    var element = document.getElementById( id );
    element.innerHTML = element.innerHTML + msg + "<br/>";
}

function clearErrorLines( id )
{
    var element = document.getElementById( id );
    element.innerHTML = "";
}

function trimDefault( value, target )
{
    var result = "";

    if ( value != target )
        result = value;

    return result;
}

function isEmail( value )
{
    return ( emailRe.exec( value )!=null );
}
