/* 
 * formhandler.js
 */

var formHandler = function () {
    return {
        checkRequiredFields:function (formid) {
            var got_errors = false;
            var radio_elements_handled = new Array;
            // check if required fields are set.
            $.each($(formid + " input,select,option"), function() {
                if ( $(this).attr("required") ) {
                    // is this an delivery type element
                    if ( $(this).attr('type' ) == 'radio' ) {
                        if ( $(this).attr('id') == 'sex' ) {
                            var checkname = '#' + $(this).attr('id') + ':checked'; // dunno how to do it else
                            if ( $(checkname).val() != 'M' && $(checkname).val() != 'F' && $.inArray ( 'sex', radio_elements_handled ) ) {
                                $(this).parent().addClass('errorfield' );
                                got_errors = true;
                                radio_elements_handled.push('sex');
                            } else {
                                $(this).parent().removeClass('errorfield');
                            }
                        }
                    } else {
                        if ( $(this).val() == '' ) {
                            $(this).addClass('errorfield');
                            got_errors = true;
                        } else {
                            $(this).removeClass ( 'errorfield' );
                        }
                    }
                }
            });

            if ( got_errors ) {
                return false;
            } else {
                return true;
            }
        }
    }
}();
