

function validateForm() {
//  generic form validation function
//  pass a list of form field objects, and each will be checked for a value
//  tim@saltstonemedia.co.uk

    var flds = validateForm.arguments;
    var val = true;
    var err = '';
    
    for( var i = 0; i < flds.length; i++ ) {
        if( typeof( flds[ i ].type ) != 'undefined' ) {
            switch( flds[ i ].type ) {
                case 'select-one' : {
                    // select box - check that the selected item has a value
                    if( ! flds[ i ].options[ flds[ i ].selectedIndex ].value ) val = false;
                    break;
                }
                case 'checkbox' : {
                    // checkbox - should be checked
                    if( ! flds[ i ].checked ) val = false;
                    break;
                }
                default : {
                    // text, textarea, hidden - check that it has a value
                    if( ! flds[ i ].value ) val = false;
                }
            } 
        } else {
            // type is not defined, so it's probably a group of radio buttons
            var chk = false;
            for( var btn = 0; btn < flds[ i ].length; btn++ ) {
                // step through each item in the group and see if any are checked
                if( flds[ i ][ btn ].type == 'radio' ) {
                    if( flds[ i ][ btn ].checked ) chk = true;
                }
            }
            if( ! chk ) val = false;
        }
    } 
    if( ! val ) alert( 'Please complete all required fields.' );
    return val;
}