if( 'undefined' === typeof( oFormData ) )
{
    var oFormData = {};
}

jQuery.validator.addMethod
(
    "dateOver",
    function( value, element, param )
    {
        //console.log( value, '|', element, '|', param );
        
        var iDateCompare = Date.parse( value ).compareTo( Date.today().addYears( parseInt( param ) * -1 ) );
        return iDateCompare <= 0;
    }
);


$j( document ).ready
(
    function()
    {
        var oForm = $j( "#commentForm" );
        //console.log( oFormData );
    
        if( oFormData.email )
        {
            $j( '#email', oForm )
                .val( oFormData.email );
        }

        if( oFormData.firstname )
        {
            $j( '#vorname', oForm )
                .val( oFormData.firstname );
        }

        if( oFormData.lastname )
        {
            $j( '#nachname', oForm )
                .val( oFormData.lastname );
        }

        if( oFormData.dob_day )
        {
            $j( '#data_birthdate_day option[value=' + oFormData.dob_day + ']', oForm )
                .attr( 'selected', 'selected' )
        }

        if( oFormData.dob_month )
        {
            $j( '#data_birthdate_month option[value=' + oFormData.dob_month + ']', oForm )
                .attr( 'selected', 'selected' )
        }

        if( oFormData.dob_year )
        {
            $j( '#data_birthdate_year option[value=' + oFormData.dob_year + ']', oForm )
                .attr( 'selected', 'selected' )
        }
        
        if( oFormData.dob_day && oFormData.dob_month && oFormData.dob_year )
        {
            $j( "#dob" )
                .attr( "changes", 3 )
                .val( oFormData.dob_year + '-' + oFormData.dob_month + '-' + oFormData.dob_day )
        }

        if( oFormData.gender )
        {
            $j( 'input[name=gender][value=' + oFormData.gender + ']', oForm )
                .attr( 'checked', 'checked' )
        }

        if( oFormData.country )
        {
            $j( '#country option[value=' + oFormData.country + ']', oForm )
                .attr( 'selected', 'selected' )
        }

        if( oFormData.language )
        {
            $j( '#language option[value=' + oFormData.language + ']', oForm )
                .attr( 'selected', 'selected' )
        }

        if( oFormData.globalopt && (oFormData.globalopt !== false && oFormData.globalopt != 'false') )
        {
            $j( '#globalopt', oForm )
                .attr( 'checked', 'checked' )
        }

        if( oFormData.thirdopt && (oFormData.thirdopt !== false && oFormData.thirdopt != 'false'))
        {
            $j( '#thirdopt', oForm )
                .attr( 'checked', 'checked' )
        }

        // 2009-09-23 | sma.azsg | #submit changed to #submitButton ?
        //$j("#submit").click(  function() { $j("#commentForm").submit();});
        $j("#submitButton").click
        (
            function()
            {
                $j("#commentForm").submit();
            }
        );

        if(!$j("#dob").attr("changes"))
        {
            $j("#dob").attr("changes", 0);
        }

        $j("[id^=data_birthdate_]").change
        (
            function()
            {
                $j("#dob")
                    .attr( "changes", parseInt( $j("#dob").attr("changes") ) + 1 )
                    .val( $j("#data_birthdate_year").val() + '-' + $j("#data_birthdate_month").val() + '-' + $j("#data_birthdate_day").val() );
                
                if($j("#dob").attr('changes') >= 3)
                {
                    $j("#commentForm").validate().element( "#dob" );
                } 
            }
        );
        
        $j( '#country' ).change
        (
            function()
            {
                if( $j( '#country' ).attr( 'value' ) == 'US' ) 
                {
                    $j( '#globalopt' ).attr( 'oldstate', $j( '#globalopt' ).attr( 'checked' ) );
                    $j( '#globalopt' ).attr( 'checked', 'checked' );
                }
                else if ( $j( '#globalopt' ).attr( 'checked' ) == true && $j( '#globalopt' ).attr( 'oldstate' ) != 'true' )
                {
                    $j( '#globalopt' ).removeAttr( 'checked' );
                }
            }
        );

        $j("#captcha_code").focus
        (
            function()
            {
                if($j(this).attr("std") != "no")
                {
                    $j(this).attr("value", "").attr("std", "no"); 
                }
            }
        );
        
        return oForm.validate
        (
            {
                rules:
                {
                    email:        { required: true, email: true },
                    vorname:      { required: true },
                    nachname:     { required: true },
                    pass:         { required: true, rangelength: [8, 14] },
                    passconfirm:  { required: true, equalTo: "#pass" },
                    gender:       { required: true },
                    country:      { required: true },
                    language:     { required: true },
                    captcha_code: { required: true, minlength: 5 },
                    dob:          { required: true, dateISO: true, dateOver: 12 }
                },
                
                messages: oValidationMessages || {},
                
                errorPlacement: function( error, element )
                {
                    if (element.attr("name").substr(0,10) == "birthdate_")
                    {
                        error.insertBefore(element.parent().parent().parent());
                    }
                    else if(element.attr("name") == "gender")
                    {
                        error.insertBefore(element.parent().parent());
                    }
                    else
                    {
                        error.insertBefore(element.parent().parent().parent());
                    }
                }
            }
        );
        
    }
);
