var counties = []; var localities = []; var streets = []; var numbers = []; var blocks = []; var entrances = []; var apartments = []; var addressesMap = {}; $(document).ready(function() { $.ajaxSetup({ cache : false }); loadCountyDropDowns(); initCounties(); initLocalities(); initStreets(); initNumberTypeahead(); initBlockTypeahead(); initEntranceTypeahead(); initApartmentTypeahead(); $(document).on("focusout", "#county", function () { localities = []; var countyCode = $(this).parents('form').find('#countyCode').val(); var cityInputElement = $(this).parents('form').find('#city'); var addressInputElement = $(this).parents('form').find('#address'); if (countyCode) { $.getJSON('/checkout/getAddress', {action: "locality", code: countyCode}, function (data) { if (data.localities) { $.each(data.localities, function (i, locality) { var localityName = locality.value; localityName = replaceSymbols(localityName); addressesMap[localityName] = locality; localities.push(localityName); }); if ($.inArray(cityInputElement.val(), localities) == -1) { $(cityInputElement).val(''); $(addressInputElement).val(''); } } }); } }); $(document).on("focusout", "#city", function () { streets = []; var localityCode = $(this).parents('form').find('#localityCode').val(); var streetInputElement = $(this).parents('form').find('#address'); if (localityCode) { $.getJSON('/checkout/getAddress', {action: "street", code: localityCode}, function (data) { if (data.streets) { $.each(data.streets, function (i, street) { var streetName = street.value; streetName = replaceSymbols(streetName); addressesMap[streetName] = street; streets.push(streetName); }); if ($.inArray(streetInputElement.val(), streets) == -1) { $(streetInputElement).val(''); } } }); } }); $(document).on("focusout", "#address", function () { numbers = []; var addressCode = $(this).parents('form').find('#addressCode').val(); var numberInputElement = $(this).parents('form').find('#homeNumber'); if (addressCode) { $.getJSON('/checkout/getAddress', {action: "number", code: addressCode}, function (data) { if (data.numbers) { $.each(data.numbers, function (i, number) { var numberValue = number.value; numberValue = replaceSymbols(numberValue); addressesMap[numberValue] = number; numbers.push(numberValue); }); if ($.inArray(numberInputElement.val(), numbers) == -1) { $(numberInputElement).val(''); } } }); } }); $(document).on("focusout", "#homeNumber", function () { blocks = []; var addressCode = $(this).parents('form').find('#addressCode').val(); var numberValue = $(this).parents('form').find('#homeNumber').val(); var blockInputElement = $(this).parents('form').find('#homeBlock'); if (addressCode && numberValue) { $.getJSON('/checkout/getAddress', {action: "block", code: addressCode, number: numberValue}, function (data) { if (data.blocks) { $.each(data.blocks, function (i, block) { var blockName = block.value; blockName = replaceSymbols(blockName); addressesMap[blockName] = block; blocks.push(blockName); }); if ($.inArray(blockInputElement.val(), blocks) == -1) { $(blockInputElement).val(''); } } }); } }); $(document).on("focusout", "#homeBlock", function () { entrances = []; var addressCode = $(this).parents('form').find('#addressCode').val(); var numberValue = $(this).parents('form').find('#homeNumber').val(); var blockValue = $(this).parents('form').find('#homeBlock').val(); var entranceInputElement = $(this).parents('form').find('#homeEntrance'); if (addressCode && numberValue && blockValue) { $.getJSON('/checkout/getAddress', {action: "entrance", code: addressCode, number: numberValue, block: blockValue}, function (data) { if (data.entrances) { $.each(data.entrances, function (i, entrance) { var entranceName = entrance.value; entranceName = replaceSymbols(entranceName); addressesMap[entranceName] = entrance; entrances.push(entranceName); }); if ($.inArray(entranceInputElement.val(), entrances) == -1) { $(entranceInputElement).val(''); } } }); } }); $(document).on("focusout", "#homeEntrance", function () { apartments = []; var addressCode = $(this).parents('form').find('#addressCode').val(); var numberValue = $(this).parents('form').find('#homeNumber').val(); var blockValue = $(this).parents('form').find('#homeBlock').val(); var entranceValue = $(this).parents('form').find('#homeEntrance').val(); var apartmentInputElement = $(this).parents('form').find('#homeApartments'); if (addressCode && numberValue && blockValue && entranceValue) { $.getJSON('/checkout/getAddress', {action: "apartment", code: addressCode, number: numberValue, block: blockValue, entrance: entranceValue}, function (data) { if (data.apartments) { $.each(data.apartments, function (i, apartment) { var apartmentName = apartment.value; apartmentName = replaceSymbols(apartmentName); addressesMap[apartmentName] = apartment; apartments.push(apartmentName); }); if ($.inArray(apartmentInputElement.val(), apartments) == -1) { $(apartmentInputElement).val(''); } } }); } }); }); function loadCountyDropDowns() { counties = []; $.getJSON('/checkout/getAddress', {action: "county"}, function (data) { if (data.counties) { $.each(data.counties, function (i, county) { var countyName = county.value; countyName = replaceSymbols(countyName); addressesMap[countyName] = county; counties.push(countyName); }); } }); }; function initCounties() { $(".county").typeahead({ source: function (query, process) { process(counties); } , matcher: function (item) { return isMatchedElement(this.query.trim(), item); } , highlighter: function (item) { return highlightElement(this.query.trim(), item); } , updater: function (item) { var countyCode = addressesMap[item].code; var countyCodeElement = $(this.$element).parents('form').find('#countyCode'); countyCodeElement.val(countyCode); return item; } }); } function initLocalities() { $(".city").typeahead({ source: function (query, process) { process(localities); } , matcher: function (item) { return isMatchedElement(this.query.trim(), item); } , highlighter: function (item) { return highlightElement(this.query.trim(), item); } , updater: function (item) { var localityCode = addressesMap[item].code; var localityCodeElement = $(this.$element).parents('form').find('#localityCode'); localityCodeElement.val(localityCode); return item; } }); } function initStreets() { $(".address").typeahead({ source: function (query, process) { process(streets); } , matcher: function (item) { return isMatchedElement(this.query.trim(), item); } , highlighter: function (item) { return highlightElement(this.query.trim(), item); } , updater: function (item) { var streetCode = addressesMap[item].code; var streetCodeElement = $(this.$element).parents('form').find('#addressCode'); streetCodeElement.val(streetCode); return item; } }); } function initNumberTypeahead() { $(".homeNumber").typeahead({ source: function (query, process) { process(numbers); } , matcher: function (item) { return isMatchedElement(this.query.trim(), item); } , highlighter: function (item) { return highlightElement(this.query.trim(), item); } , updater: function (item) { return item; } }); } function initBlockTypeahead() { $(".homeBlock").typeahead({ source: function (query, process) { process(blocks); } , matcher: function (item) { return isMatchedElement(this.query.trim(), item); } , highlighter: function (item) { return highlightElement(this.query.trim(), item); } , updater: function (item) { return item; } }); } function initEntranceTypeahead() { $(".homeEntrance").typeahead({ source: function (query, process) { process(entrances); } , matcher: function (item) { return isMatchedElement(this.query.trim(), item); } , highlighter: function (item) { return highlightElement(this.query.trim(), item); } , updater: function (item) { return item; } }); } function initApartmentTypeahead() { $(".homeApartments").typeahead({ source: function (query, process) { process(apartments); } , matcher: function (item) { return isMatchedElement(this.query.trim(), item); } , highlighter: function (item) { return highlightElement(this.query.trim(), item); } , updater: function (item) { return item; } }); } function isMatchedElement(query, item) { return item.toLowerCase().startsWith(query.toLowerCase()) ? true : false; } function highlightElement(query, item) { var query = query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&'); var regex = new RegExp('(' + query + ')', 'gi'); return item.replace(regex, "$1"); } function replaceSymbols(value) { var symbolsMapping = [ [String.fromCharCode(194), 'A'], [String.fromCharCode(226), 'a'], [String.fromCharCode(258), 'A'], [String.fromCharCode(259), 'a'], [String.fromCharCode(206), 'I'], [String.fromCharCode(238), 'i'], [String.fromCharCode(350), 'S'], [String.fromCharCode(351), 's'], [String.fromCharCode(354), 'T'], [String.fromCharCode(355), 't'] ]; var newValue = value; for (var i = 0; i < symbolsMapping.length; i++) { newValue = newValue.replace(RegExp(symbolsMapping[i][0], 'g'), symbolsMapping[i][1]); } return newValue; } $('#coverageCheckerForm input[type="submit"]').click( function(){ var formElement = $(this).parents('form'); if ($(formElement).valid()) { if($(formElement).hasClass('form-address')) { getAvailableServicesByAddress(formElement); } if($(formElement).hasClass('form-telefon')) { getAvailableServicesByMsisdn(formElement); } return false; } else { $(formElement).validate({ highlight: function (element) { $(element).closest('.form-group').addClass('has-error'); }, unhighlight: function (element) { $(element).closest('.form-group').removeClass('has-error'); }, errorElement: 'div', errorClass: 'help-block error', errorPlacement: function (error, element) { error.insertAfter(element); }, }); } }); function getAvailableServicesByAddress(formElement) { var tab = $(formElement).closest('.tab-pane'); var oldResult = $(tab).find('#coverageResult'); if(oldResult.length > 0) { oldResult.remove(); $(tab).find('#consultant').hide(); } var block = $(formElement).closest('.block'); $.ajax({ async : true, cache : false, url : '/blocks/coverageCheckerNew/coverageCheck.jsp', dataType : 'html', data : { checkBy : "address", role : $(tab).data('role'), judetCode : $(formElement).find("#countyCode").val(), localitateCode : $(formElement).find("#localityCode").val(), stradaCode : $(formElement).find("#addressCode").val(), nrstrada : $(formElement).find("#homeNumber").val(), block : $(formElement).find("#homeBlock").val(), entrance : $(formElement).find("#homeEntrance").val(), apart : $(formElement).find("#homeApartments").val(), }, success : function(data) { $(block).after(data); $(tab).find('#consultant').show(); }, error : function() { } }); }; function getAvailableServicesByMsisdn(formElement) { var tab = $(formElement).closest('.tab-pane'); var oldResult = $(tab).find('#coverageResult'); if(oldResult.length > 0) { oldResult.remove(); $(tab).find('#consultant').hide(); } var block = $(formElement).closest('.block'); $.ajax({ async : true, cache : false, url : '/blocks/coverageCheckerNew/coverageCheck.jsp', dataType : 'html', data : { checkBy : "msisdn", role : $(tab).data('role'), telefon : $(formElement).find("#telefon").val(), }, success : function(data) { $(block).after(data); $(tab).find('#consultant').show(); }, error : function() { } }); };