// Check the formobj.fields listed in the comma-separated list.
// Each fieldname in list can have an optional display label,
// like this: realname:Full name,email:Email address etc
//
// Returns false if one or more required fields is missing or invalid.
// any field with email in its name is checked for being a valid
// email address.
//
function mailerCheck( formobj, list )
{
    var missing = "";
    var names = list.split(",");

    for ( i=0; i < names.length; i++ )
    {
        var elname = names[i];
        var parts = elname.split(":");

        elname    = parts[0];
        var label;

        if ( parts.length > 1 )
        {
            label = parts[1];
        }
        else
        {
            label = elname;
        }

        if ( formobj.elements[elname] )
        {
            var theval = formobj.elements[elname].value;

            theval = theval.replace(/ /g, "");

            if ( theval == "" )
            {
                if ( missing == "" )
                {
                    missing = label;
                }
                else
                {
                    missing += "\n" + label;
                }
            }
            else if ( elname.toUpperCase().indexOf("EMAIL") >= 0 )
            {
                res = echeck( theval );
                if ( res != "" )
                {
                    missing += "\n" + label + " - " + res;
                }
            }
        }
    }

    if ( missing == "" )
    {
        return true;
    }
    else
    {
        alert("The following fields must be completed\n\n" + missing );
        return false;
    }
}

// Return true if at least one of the checkboxes of the specified name
// is checked
//
function isChecked( formobj, checkboxname )
{
    var foundChecked = false;
    for ( i=0; !foundChecked && i < formobj.elements.length; i++ )
    {
        var elname = formobj.elements[i].name;

        if ( elname == checkboxname )
        {
            var theval = formobj.elements[i].value;

            if ( formobj.elements[i].checked )
            {
                foundChecked = true;
            }
        }
    }

    return foundChecked;
}

// Return true if at least one of the checkboxes of the specified name
// is checked
//
function multipleCheckboxValue( formobj, checkboxname )
{
    var val = "";
    for ( i=0; i < formobj.elements.length; i++ )
    {
        var elname = formobj.elements[i].name;

        if ( elname == checkboxname )
        {
            if ( formobj.elements[i].checked )
            {
                if ( val != "" )
                {
                    val += ", ";
                }
                val += formobj.elements[i].value;
            }
        }
    }

    return val;
}

function echeck( strng )
{
    // From http://developer.apple.com/internet/javascript/validation.html
    //
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) {
           return "Please enter a valid email address";
    }

    // Again, we want to check to make sure that no forbidden characters have slipped in.
    // For email addresses, we're forbidding the following: ( ) < > [ ] , ; : \ / "
    //
    var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
    if (strng.match(illegalChars)) {
       return "The email address contains illegal characters";
    }

    return "";
}

