/* global ajax*/ /* global g_poll,g_votes,g_num_vote_attempts,g_pv_code,g_contact_hash */ /* global show,hide,translate,display_msg*/ /* global handle_misssed_questions, ask_confirm, clear_sel_options_from_partial_exact_questions, scroll_to_bottom, scroll_to_id */ document.addEventListener( 'DOMContentLoaded' , async () => { }); const send_vote = async () => { // // Handle missed questions // const $btn_vote = document.getElementById( 'btn-vote' ); const is_send_anyhow = $btn_vote && $btn_vote.textContent === translate( 'Submit my vote as is' ); const num_missed = handle_misssed_questions( g_poll.questions , true ); // send-vote // // Send vote following confirmation // if ( num_missed === 0 || is_send_anyhow ) { const is_confirmed = await ask_confirm( 'box-confirm-send-vote' , `${translate( 'Once the form is submitted, the vote cannot be changed. Would you like to continue?' )}` ); if ( is_confirmed ) { // // Handle form // hide( '.err-vote' ); clear_sel_options_from_partial_exact_questions( g_poll.questions ); display_msg( `${ translate( 'Please wait ...' ) }` , 'vote-msg' ); freeze_form(); // // Update num-vote-attempts (used to identify bad connection) // g_num_vote_attempts++; // // Send the form. // if ( window.navigator.onLine ) { const params = `action=save_vote&pv_code=${ g_pv_code }&contact_hash=${ g_contact_hash }&votes=${ encodeURIComponent( JSON.stringify( g_votes ) ) }`; ajax( params, response => { _handle_vote_response( response ); } ); setTimeout( handle_bad_connection, 5000 ); } else { handle_bad_connection(); } } } }; const freeze_form = () => { hide( 'p-btn-vote' ); document.querySelectorAll( 'input[type="checkbox"] , input[type="radio"]' ).forEach( $el => { $el.disabled = true; }); hide( '.toggle-cbs' ); }; const unfreeze_form = () => { const $btn_vote = document.getElementById( 'btn-vote' ); $btn_vote.textContent = translate( 'Submit my vote' ); show( $btn_vote ); document.querySelectorAll( 'input[type="checkbox"] , input[type="radio"]' ).forEach( $el => { $btn_vote.disabled = false; }); show( '.toggle-cbs' ); }; const _handle_vote_response = ( response ) => { g_num_vote_attempts = 0; // Signals handle-bad-connection() that there is a connection. if ( response.success ) { document.getElementById( 'vote-msg' )?.remove(); show( 'box-msg-complete' ); scroll_to_bottom(); } else { display_msg( `${ translate( response.error ) }` , 'vote-msg' ); } }; const handle_bad_connection = () => { if ( g_num_vote_attempts === 0 ) // When vote succeeds, this global is reset to zero, thus indicating a successful vote. { return; } unfreeze_form(); let msg = `

${ translate( 'Your vote was not recorded' ) }

`; if ( g_num_vote_attempts <= 1 ) { msg += `

${ translate( 'There could be a temporary issue with your internet connection, please try again shortly' ) }

`; } else { msg += `

${ translate( 'Your internet connection may be experiencing a problem, please try again shortly' ) }

`; } display_msg( msg , 'vote-msg' ); scroll_to_id( 'vote-msg' ); };