var bfcontact = { /** * States and their cities */ stateCityMap: {"37":[{"id":"34","text":"Adelaide"}],"32":[{"id":"35","text":"Brisbane"}],"31":[{"id":"32","text":"Canberra"}],"34":[{"id":"30","text":"Darwin"}],"35":[{"id":"37","text":"Hobart"}],"30":[{"id":"38","text":"Illawarra"},{"id":"33","text":"Newcastle"},{"id":"29","text":"Sydney"}],"29":[{"id":"28","text":"Melbourne"}],"36":[{"id":"39","text":"Perth"}],"33":[{"id":"41","text":"Sydney"}]}, /** * States with no content */ emptyStates: {"37":"1","32":"1","35":"1","30":"1","36":"1"}, /** * States with not cities */ noCitiesStates: [], /** * Load new content in the area */ updateContentArea: function (selectEl) { var $ = jQuery.noConflict(); //update cities var select = $(selectEl); bfcontact.setSelectOptions(select); bfcontact.setDefaultCityOption(); bfcontact.loadContent(); }, /** * Set select options for city * * @param select */ setSelectOptions: function (select) { var $ = jQuery.noConflict(); if (select.attr('name') == 'state_id') { var val = select.val(); var citySelect = $('#city_id'); $('#city_id-lbl').css('display', ''); $('#city_id-replacement').css('display', ''); if (bfcontact.noCitiesStates[val] == '1') { citySelect.empty(); /*citySelect.append( $("") .text('Select city') .val('') );*/ $('#city_id-lbl').css('display', 'none'); $('#city_id-replacement').css('display', 'none'); return; } if (bfcontact.stateCityMap[val] !== undefined) { var selectedOption = citySelect.val(); citySelect.empty(); /*citySelect.append( $("") .text('Select city') .val('') );*/ bfcontact.stateCityMap[val].each(function (optionRow) { var selected = ''; if (optionRow.id == selectedOption) { selected = ' selected=\'selected\''; } citySelect.append( $("") .text(optionRow.text) .val(optionRow.id) ); }); citySelect.selectReplacement(); citySelect.trigger("chosen:updated"); } } }, /** * Load content area */ loadContent: function () { var $ = jQuery.noConflict(); $('#bfcontact_content').html('Loading...'); var requestUrl = 'https://www.campquality.org.au/index.php?option=com_bfcontact&format=raw&view=information'; $('#bfcontact_selects select').each(function (index, el) { var selectEl = $(el); requestUrl += '&' + selectEl.attr('name') + '=' + selectEl.val(); }); $.ajax({ dataType : 'html', url: requestUrl, success: function (data) { $('#bfcontact_content').html(data); //Reload with first city if content is empty if (data == '') { bfcontact.setDefaultCityOption(); //bfcontact.loadContent(); } bfcontact.setAdminEmail(data); } }); }, /** * Set default city option with states that have empty content */ setDefaultCityOption: function () { var $ = jQuery.noConflict(); var stateSelect = $('#state_id'); if (bfcontact.emptyStates[stateSelect.val()] == '1') { var citySelect = $('#city_id'); var options = citySelect.children('option'); if (citySelect.val() == '' && options.length > 1) { citySelect.val(options[1].value); citySelect.selectReplacement(); citySelect.trigger("chosen:updated"); } } }, /** * Set admin email field */ setAdminEmail: function (xmlDoc) { var $ = jQuery.noConflict(); try { var emailAddress = ''; if (xmlDoc == undefined){ emailAddress = $('#contactSendToEmail').val(); } else { emailAddress = $(xmlDoc).filter('input#contactSendToEmail').val(); } $('#adminEmail').val(emailAddress); } catch (e) { ; } } }; jQuery(document).ready(function ($) { bfcontact.setAdminEmail(); $('#bfcontact_selects select').change(function () { bfcontact.updateContentArea(this); }); });