1var mepr_populate_states = function(obj) { (function($) { 2 var country = obj.val(); 3 var form = obj.closest('.mepr-form'); 4 5 if(form == undefined) { return; } 6 7 // Clean out all the options before re-populating 8 form.find('.mepr-states-dropdown option').remove(); 9 10 var states_dropdown = form.find('.mepr-states-dropdown'); 11 var states_text = form.find('.mepr-states-text'); 12 13 // Ensure we've found a dropdown & text element 14 if (states_dropdown.length <= 0 || states_text.length <= 0) { return; } 15 16 // Grab fieldname and value 17 var fieldname = states_dropdown.data('fieldname'); 18 var value = states_dropdown.data('value').toString(); 19 20 if(fieldname == undefined || value == undefined) { return; } 21 22 // Grab unique suffix 23 var unique_suffix = states_dropdown.data('unique-suffix'); 24 25 // Clean up trailing whitespace 26 fieldname = fieldname.replace(/^\s+/,''); 27 fieldname = fieldname.replace(/\s+$/,''); 28 value = value.replace(/^\s+/,''); 29 value = value.replace(/\s+$/,''); 30 31 var required = !!obj.attr('required'); 32 33 // Check if country is in the list of countries without states 34 var countryWithoutStates = MeprI18n.countries_without_states && MeprI18n.countries_without_states.indexOf(country) !== -1; 35 36 // Helper function to update the asterisk in the label 37 var updateStateFieldAsterisk = function(fieldElement, isRequired) { 38 var label = fieldElement.siblings('.mp-form-label').find('label'); 39 var labelText = label.text(); 40 var hasAsterisk = labelText.indexOf('*') !== -1; 41 42 if (isRequired && !hasAsterisk) { 43 // Add asterisk at the end if required and not present 44 label.text(labelText + '*'); 45 } else if (!isRequired && hasAsterisk) { 46 // Remove all asterisks if not required 47 label.text(labelText.replace(/\*/g, '')); 48 } 49 }; 50 51 if (MeprI18n.states[country] !== undefined) { 52 states_dropdown.attr('name', fieldname).attr('id', 'mepr-address-state' + unique_suffix); 53 states_dropdown.show(); 54 states_text.removeAttr('name').removeAttr('id'); 55 states_text.hide(); 56 if (required && !countryWithoutStates) { 57 states_text.prop('required', false); 58 states_dropdown.attr('required',''); 59 updateStateFieldAsterisk(states_dropdown, true); 60 } else { 61 // Country doesn't require states, so remove required attribute. 62 states_dropdown.prop('required', false); 63 states_text.prop('required', false); 64 updateStateFieldAsterisk(states_dropdown, false); 65 } 66 67 states_dropdown.append('<option value="">' + MeprI18n.please_select_state + '</option>'); 68 69 for (var st in MeprI18n.states[country]) { 70 var selected = (value===st ? ' selected="selected"' : ''); 71 states_dropdown.append('<option value="' + st + '" ' + selected + '>' + MeprI18n.states[country][st] + '</option>'); 72 } 73 } 74 else { 75 states_dropdown.removeAttr('name').removeAttr('id'); 76 states_dropdown.hide(); 77 states_text.attr('name', fieldname).attr('id', 'mepr-address-state' + unique_suffix); 78 states_text.show(); 79 if (required && !countryWithoutStates) { 80 states_dropdown.prop('required', false); 81 states_text.attr('required',''); 82 updateStateFieldAsterisk(states_text, true); 83 } else { 84 // Country doesn't require states, so remove required attribute. 85 states_dropdown.prop('required', false); 86 states_text.prop('required', false); 87 updateStateFieldAsterisk(states_text, false); 88 } 89 } 90})(jQuery)}; 91 92var mepr_set_locate_inputs = function(country, state) { (function($) { 93 if(country !== '' && state !== '') { 94 // If the states are created for this country 95 // but the state is goofy then just blank it out 96 if(MeprI18n.states[country]!==undefined && 97 MeprI18n.states[country][state]===undefined) { 98 state = ''; 99 } 100 } 101 102 // Set the correct values for the country or state dropdowns 103 if ($('.mepr-form .mepr-countries-dropdown').length > 0) { 104 $.each($('.mepr-form .mepr-countries-dropdown'), function(i, obj) { 105 if($(obj).val()===undefined || $(obj).val()==='') { 106 $(obj).val(country); 107 } 108 109 mepr_populate_states($(obj)); 110 }); 111 } 112 113 // Set the states up properly where appropriate 114 if ($('.mepr-form .mepr-states-dropdown').length > 0) { 115 $.each($('.mepr-form .mepr-states-dropdown'), function(i, obj) { 116 117 if($(obj).data('value')===undefined || $(obj).data('value')==='') { 118 var options = $(obj).find('option'); 119 120 $.each(options, function(i, option) { 121 if(state===option.value) { 122 $(obj).attr('data-value',state); 123 $(option).attr('selected','selected'); 124 return false;
125 } 126 }); 127 } 128 129 }); 130 } 131 132 if ($('.mepr-form .mepr-geo-country').length > 0) { 133 $.each($('.mepr-form .mepr-geo-country'), function(i, obj) { 134 $(obj).val(country); 135 }); 136 } 137 138 $('.mepr-form .mepr-countries-dropdown').trigger('mepr-geolocated'); 139})(jQuery)}; 140 141var mepr_ssl_geoip_services = { 142 // The services 143 caseproof: { 144 url: 'https://cspf-locate.herokuapp.com?callback=?', 145 cindex: 'country_code', 146 sindex: 'region_code', 147 used: false, 148 type: 'jsonp', 149 }, 150 //telize: { //TELIZE SHUT DOWN IN 2015 151 // url: 'https://www.telize.com/geoip', 152 // cindex: 'country_code', 153 // sindex: 'region_code', 154 // used: false, 155 // type: 'json', 156 //}, 157 //geoplugin: { 158 // url: 'http://www.geoplugin.net/json.gp?jsoncallback=?', 159 // cindex: 'geoplugin_countryCode', 160 // sindex: 'geoplugin_regionCode', 161 // used: false, 162 // type: 'jsonp', 163 //}, 164 //'ip-api': { 165 // url: 'http://ip-api.com/json/?callback=?', 166 // cindex: 'countryCode', 167 // sindex: 'region', 168 // used: false, 169 // type: 'jsonp', 170 //}, 171 //freegeoip: { //Dead - needs to use https://ipstack.com/ now 172 // url: 'https://freegeoip.net/json/', 173 // cindex: 'country_code', 174 // sindex: 'region_code', 175 // used: false, 176 // type: 'json', 177 //} 178}; 179 180var mepr_locate = function(source_key) { (function($) { 181 if(source_key==undefined) { 182 source_key = 'caseproof'; // default 183 } 184 185 source = mepr_ssl_geoip_services[source_key]; 186 187 // If we've already used this source then assume we're out of choices 188 if(source.used===true) { 189 return mepr_set_locate_inputs('',''); 190 } 191 192 $.ajax({ 193 url: source.url, 194 method: 'GET', 195 timeout: 5000, // 5 seconds ... too much? too little? 196 dataType: source.type, 197 }) 198 .done (function(data) { 199 var state = ((data[source.sindex]!==undefined) ? data[source.sindex] : ''); 200 var country = ((data[source.cindex]!==undefined) ? data[source.cindex] : ''); 201 202 mepr_set_locate_inputs(country, state); 203 }) 204 .fail (function() { 205 mepr_ssl_geoip_services[source_key].used=true; 206 207 for (var k in mepr_ssl_geoip_services) { 208 var next_source = mepr_ssl_geoip_services[k]; 209 210 if(next_source.used===false) { 211 return mepr_locate(k); 212 } 213 } 214 215 mepr_set_locate_inputs('',''); 216 }); 217 218 return false; 219})(jQuery)}; 220 221jQuery(document).ready(function($){ 222 $.each($('.mepr-form .mepr-countries-dropdown'), function(i, obj) { 223 mepr_populate_states($(obj)); 224 $(obj).on('change', function(e) { 225 mepr_populate_states($(obj)); 226 }); 227 }); 228});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.