jQuery( document ).ready( function() {
    oMandatoryFields = jQuery( '#contactForm .mandatory' );
    
    // when the form is submitted check for validation
    jQuery( '#contactForm' ).submit( function( e ) {
        iFailed = 0;
        oMandatoryFields.each( function() {
            if( jQuery( this ).val() == '' ) {
                jQuery( this ).addClass( 'fixMe' );
                iFailed++;
                jQuery( this ).bind( 'mousedown', function() {
                    if( jQuery( this ).hasClass( 'fixMe' )) {
                        jQuery( this ).removeClass( 'fixMe' );
                        iFailed--;
                    }
                });
            }
       });
       // let's make a call to Google to verify the Recaptcha
       var sVerifyURL = 'inc/recaptcha_verify.php';
       var oPostData = {
         'challenge': jQuery( '#recaptcha_challenge_field' ).val(),
         'response': jQuery( '#recaptcha_response_field' ).val()
       };
       jQuery.ajax({
         type: 'POST',
         url: sVerifyURL,
         data: oPostData,
         dataType: 'json',
         async: 'true',
         success: function( oData ) {
           if( !oData.bResult ) {
             iFailed++;
           } else {
             // do we need to do anything else here??
             // actually yes, set a flag to say the
             // captcha has been passed correctly

             // perform another test on the serverside
             // for when javascript is off...
             // but if javascript is off on the client side
             // then the captcha won't show anyway...
             // conundrums.
           }
         }
       });
       if( iFailed ) {
           alert( 'Oops! Seems we\'ve got some fields that still require information. Please try again!' );
           e.preventDefault();
       }
    })
});
