/* ________________________________________________________________________________________ * join_B_js.php * * Javascript functions for join_B.php ________________________________________________________________________________________ */ var price = '15'; var suffix = ''; add_onload(sub_event_handler); // add on change handler to subscription length box function sub_event_handler() { document.getElementById('subscription_length').onchange = display_price; } /* Automatically change the displayed membership price based on country selected. Function triggered by changing country pull-down menu */ function display_price() { var sub_type = ''; // Read chosen radio button var checkedButton = getCheckedRadio(document.getElementById('join_B').elements.subscription_type); if (checkedButton) { sub_type = checkedButton.value; } // Read selected country var menu = document.getElementById('country_menu'); var chosen_country = menu.options[menu.selectedIndex].text; var sub_length = Number(document.getElementById('subscription_length').value); console.log(chosen_country); switch (sub_type) { case 'life': price = '250'; suffix = ''; break; case 'fixed-term': if (chosen_country == 'United Kingdom') price = 15 * sub_length; else price = 20 * sub_length; suffix = ''; break; case 'recurring': if (chosen_country == 'United Kingdom') price = '15'; else price = '20'; suffix = 'per year'; break; } // Store new selection in HTML hidden input field document.getElementById('cost').value = price; document.getElementById('suffix').innerHTML = suffix; document.getElementById('display_cost').innerHTML = String(price); } function getCheckedRadio(radio_group) { for (var i = 0; i < radio_group.length; i++) { var button = radio_group[i]; if (button.checked) { return button; } } return undefined; }