qs.defineNS('app'); app.Wizard = qs.createObject(); app.Wizard.prototype = { options: { form: '#wizard-form', step: null, defaultData: { pregnancyChildren: 1, siblingsCount: 0, parentsCount: 0, childrenCount: 0, dependentsCount: 0, filersCount: 0, filersDependentsCount: 0 }, minIncomeAge: null, minAge: null, maxAgeMA: null, applicantAge: null, pregnancyStatus: null }, taxFilterElementGroups: { filerElementIdList: [ 'dependentsCount', 'dependent', 'spouseFilingStatus' ], filerDependentBranchIdList: [ 'filersCount', 'filer', 'filersDependentsCount', 'filersDependent' ], dependentElementIdList: [ 'isChildOrSpouseTaxFiler', 'isChildLivesUnmarriedParents', 'isChildNoncustodialParent'], otherElementIdList: [ 'siblingsCount', 'sibling', 'parentsCount', 'parent', 'childrenCount', 'child', ] }, multiElementsIdPrefix: [ 'filer', 'dependent', 'filersDependent', 'sibling', 'parent', 'child' ], initialize: function(options) { this.options = options; this.initForm(); }, initForm: function() { this._initFormElements(); this._initCalculatorPopupLink(); }, _initFormElements: function() { $('#applicantsCount').TouchSpin({min: 0, initval: 0}); $('#pregnancyChildren').TouchSpin({min: 1, initval: 1}); $('#dependentsCount, #siblingsCount, #parentsCount, #childrenCount, #filersCount, #filersDependentsCount').TouchSpin({initval: 0, mousewheel: false, stepinterval: 10000}); this._onChangeIE('applicantsCount'); //step 2 this._initBirthElement('birth', ['gender'], ['gender']); this._initGenderElement('input[name="gender"]', 'female', ['pregnancyStatus'], ['pregnancyStatus', 'pregnancyChildren']); this._initRadioElement('input[name="pregnancyStatus"]', 'currently', ['pregnancyChildren'], ['pregnancyChildren']); //step 3 this._initLawfullyElement(); //step 5 this._initSelectElement('insuranceSituation', 'haveNotUse', ['monthlyPremium', 'monthlyPremium_same'], ['monthlyPremium', 'monthlyPremium_same', 'isInsuranceOfferMin', 'isInsuranceOfferMin_same']); this._initInputElement('monthlyPremium', 'this._calcInsuranceOffer', ['isInsuranceOfferMin', 'isInsuranceOfferMin_same'], ['isInsuranceOfferMin', 'isInsuranceOfferMin_same']); //step 6 this._initMarriedElement(); this._initTaxFilingStatusElement('taxFilingStatus'); }, _initLawfullyElement: function() { var self = this, selector = 'input[name="isLawfully"]'; if (!$(selector).length) { return false; } var onClick = function() { var checkedValue = $(selector + ':checked').val(); if (checkedValue == 'y') { self._toggleElementList(['immigrationStatus'], 'show'); self._initImmigrationStatusElement('immigrationStatus'); } else { self._toggleElementList(['immigrationStatus', 'isStatusLongerFiveYear', 'isEnterAsRefugeeOrAsylee'], 'hide'); } }; $(selector + ':radio').unbind('click.initLawfully').bind('click.initLawfully', onClick); onClick(); }, _initImmigrationStatusElement: function(elementId) { var self = this, sensitiveElement = $('#' + elementId); var onChange = function () { var selectedValue = $('#' + elementId +' option:selected').val(); if ('currently' != self.options.pregnancyStatus && 'recently' != self.options.pregnancyStatus && self.options.applicantAge >= 21 && (selectedValue == 'lawful' || selectedValue == 'vawa') ) { self._toggleElementList(['isStatusLongerFiveYear'], 'show'); self._initRadioElement('input[name="isStatusLongerFiveYear"]', 'n', ['isEnterAsRefugeeOrAsylee'], ['isEnterAsRefugeeOrAsylee']); } else { self._toggleElementList(['isStatusLongerFiveYear', 'isEnterAsRefugeeOrAsylee'], 'hide'); } }; sensitiveElement.unbind('change.immigrationStatus').bind('change.immigrationStatus', onChange); onChange(); }, _initRadioElement: function(selector, sensitiveElementValue, showElementIdList, hideElementIdList) { var self = this; if (!$(selector).length) { return false; } var onClick = function() { var checkedValue = $(selector + ':checked').val(); if (checkedValue == sensitiveElementValue) { self._toggleElementList(showElementIdList, 'show'); } else { self._toggleElementList(hideElementIdList, 'hide'); } }; $(selector + ':radio').unbind('click.initRadio').bind('click.initRadio', onClick); onClick(); }, _initMarriedElement: function() { var self = this, selector = 'input[name="isMarried"]', elementIdList = [ 'spouseId', 'isLivingTogether', 'newSpouse', 'spouseAnnualIncome', 'spouseMonthlyIncome', 'spouseFilingStatus', 'spouseFirstRow', 'spouseSecondRow' ]; if (!$(selector).length) { return false; } var onClick = function() { var checkedValue = $(selector + ':checked').val(); if (checkedValue == 'y') { self._toggleElementList(elementIdList, 'show'); var taxFilingStatus = $('#taxFilingStatus option:selected').val(); if (taxFilingStatus != 'filer') { self._setToggleField('spouseFilingStatus', 'hide'); } else { self._initSpouseFilingStatus(); } self._initDependentIncomeElements('spouse'); } else { self._toggleElementList(elementIdList, 'hide'); self._toggleDependentOptions('dependent', null, true); } }; $(selector + ':radio').unbind('click.initMarried').bind('click.initMarried', onClick); onClick(); }, _initSpouseFilingStatus: function() { var self = this, selector = 'input[name="spouseFilingStatus"]'; var onClick = function() { var checkedValue = $(selector + ':checked').val(); if (checkedValue == 'jointly') { self._prepareDependentOptions('dependent'); } else { self._toggleDependentOptions('dependent', null, true); } }; $(selector + ':radio').unbind('click.spouseFilingStatus').bind('click.spouseFilingStatus', onClick); onClick(); }, _initGenderElement: function(selector, sensitiveElementValue, showElementIdList, hideElementIdList) { var self = this, resultAge = false; if (!$(selector).length) { return false; } var onChange = function() { var checkedValue = $(selector + ':checked').val(); var birth = $('#birth').val(); if (birth && self._isValidDate(birth)) { resultAge = parseInt(self._calcAge(birth)); } if (checkedValue == sensitiveElementValue && resultAge && resultAge >= parseInt(self.options.minAge)) { self._toggleElementList(showElementIdList, 'show'); } else { self._toggleElementList(hideElementIdList, 'hide'); } }; $(selector + ':radio').unbind('click.initGender').bind('click.initGender', onChange); onChange(); }, _isValidDate: function (dateString) { var regEx = /^\d{4}-\d{2}-\d{2}$/; return dateString.match(regEx) != null; }, _initBirthElement: function(elementId, showElementIdList, hideElementIdList) { var self = this, sensitiveElement = $('#' + elementId), isGenderFemale = $('input[id="gender-female"]'); if (!sensitiveElement.length) { return false; } if (!isGenderFemale.is(':checked')) { this._toggleElementList(['pregnancyStatus', 'pregnancyChildren'], 'hide'); } var onChange = function () { var elementValue = sensitiveElement.val(); if (elementValue && self._isValidDate(elementValue)) { var result = parseInt(self._calcAge(elementValue)); if (result >= self.options.minAge){ if (isGenderFemale.is(':checked')) { self._setToggleField('pregnancyStatus', 'show'); } self._toggleElementList(showElementIdList, 'show'); } else { self._toggleElementList(['pregnancyStatus', 'pregnancyChildren'], 'hide'); self._toggleElementList(hideElementIdList, 'hide'); } if (result >= self.options.minIncomeAge) { self._toggleElementList(['annualIncome', 'monthlyIncome'], 'show'); self._toggleLabelTooltip('annualIncome', 'hide'); } else { self._toggleElementList(['annualIncome', 'monthlyIncome'], 'hide'); } } else { if (!isGenderFemale.is(':checked')) { self._toggleElementList(['pregnancyStatus', 'pregnancyChildren'], 'hide'); } self._toggleElementList(hideElementIdList, 'hide'); self._toggleElementList(['annualIncome', 'monthlyIncome'], 'hide'); } }; sensitiveElement.unbind('change.initBirth').bind('change.initBirth', onChange); onChange(); }, _toggleLabelTooltip: function(elementId, status) { var toolTipLink = $('#' + elementId + '-label a.wizard-tip:not(#calc_popup)'); var tooltipToggle = $('#' + elementId + '-label span#tooltip-toggle'); if (status == 'hide') { var linkText = toolTipLink.html(); toolTipLink.hide(); if (!tooltipToggle.length) { toolTipLink.after('' + linkText + ''); } } else if (status == 'show') { tooltipToggle.remove(); toolTipLink.show(); } return true; }, _isAgeGreate: function() { return this.options.applicantAge >= this.options.maxAgeMA; }, _initDependentIncomeElements: function(elementPrefix, elIndex, dependentBranch) { var self = this, elementSuffix = (elementPrefix != 'spouse') ? '_' + elIndex : '', sensitiveElement = $('#' + elementPrefix + 'Id' + elementSuffix), annualIncomeElement = $('#' + elementPrefix + 'AnnualIncome' + elementSuffix), monthlyIncomeElement = $('#' + elementPrefix + 'MonthlyIncome' + elementSuffix); /* issue HIST-40 p.5 var incomeRow = $('#' + elementPrefix + 'SecondRow' + elementSuffix + '-element'); */ var onChange = function (e) { var selectedValue = $('#' + elementPrefix + 'Id' + elementSuffix + ' option:selected').val(); if (selectedValue == 'new') { self._toggleElementList(['new' + self._ucFirst(elementPrefix) + elementSuffix], 'show'); annualIncomeElement.val((e == 'init') ? annualIncomeElement.val() : '').attr('disabled', false); monthlyIncomeElement.val((e == 'init') ? monthlyIncomeElement.val() : '').attr('disabled', false); /* issue HIST-40 p.5 incomeRow.show(); */ } else { self._toggleElementList(['new' + self._ucFirst(elementPrefix) + elementSuffix], 'hide'); annualIncomeElement.val('').attr('disabled', true); monthlyIncomeElement.val('').attr('disabled', true); if (selectedValue) { var result = self._getApplicantIncome(selectedValue); if (result) { annualIncomeElement.val(result.annualIncome); monthlyIncomeElement.val(result.monthlyIncome); /* issue HIST-40 p.5 if (!result.show) { incomeRow.hide(); } else { incomeRow.show(); } */ } } } if (elementPrefix == 'filer' && selectedValue && (e != 'init') && dependentBranch) { self._prepareFilersDependentsData(); } if (elementPrefix == 'spouse') { self._prepareDependentOptions('dependent'); self._prepareDependentOptions('child'); self._prepareDependentOptions('sibling'); } else { self._toggleDependentOptions(elementPrefix, selectedValue, true); } }; sensitiveElement.unbind('change.initDependent').bind('change.initDependent', onChange); onChange('init'); }, _initTaxFilingStatusElement: function(elementId) { var self = this, sensitiveElement = $('#' + elementId); var onChange = function () { var selectedValue = $('#' + elementId +' option:selected').val(); if (selectedValue == 'filer') { self._toggleElementList(self.taxFilterElementGroups.dependentElementIdList, 'hide'); self._toggleElementList(self.taxFilterElementGroups.filerElementIdList, 'show'); var checkedValue = $('input[name="isMarried"]:checked').val(); if (checkedValue != 'y') { self._setToggleField('spouseFilingStatus', 'hide'); } else { self._initSpouseFilingStatus(); } self._initFilerCounterElement(false); } else if (selectedValue == 'noTaxNoDependent') { self._toggleElementList(self.taxFilterElementGroups.dependentElementIdList, 'hide'); self._initDependentCountersElements(false); } else if (selectedValue == 'dependent') { self._setToggleField('isChildOrSpouseTaxFiler', 'show'); self._initChildOrSpouseTaxFilerElement('input[name="isChildOrSpouseTaxFiler"]'); } else { self._toggleElementList(self.taxFilterElementGroups.filerElementIdList, 'hide'); self._toggleElementList(self.taxFilterElementGroups.dependentElementIdList, 'hide'); self._toggleElementList(self.taxFilterElementGroups.otherElementIdList, 'hide'); self._toggleElementList(self.taxFilterElementGroups.filerDependentBranchIdList, 'hide'); } }; sensitiveElement.unbind('change.initTaxFilling').bind('change.initTaxFilling', onChange); onChange(); }, _initFilerCounterElement: function(dependentBranch) { this._toggleElementList(this.taxFilterElementGroups.otherElementIdList, 'hide'); if (dependentBranch) { this._initCounterElements('filersCount', 'filer'); this._initCounterElements('filersDependentsCount', 'filersDependent'); this._toggleElementList(['dependentsCount', 'dependent'], 'hide'); } else { this._toggleElementList(this.taxFilterElementGroups.filerDependentBranchIdList, 'hide'); this._initCounterElements('dependentsCount', 'dependent'); } }, _initDependentCountersElements: function(dependentBranch) { this._toggleElementList(this.taxFilterElementGroups.filerElementIdList, 'hide'); if (!this._isAgeGreate()) { this._toggleElementList(['siblingsCount', 'parentsCount'], 'show'); this._initCounterElements('siblingsCount', 'sibling'); this._initCounterElements('parentsCount', 'parent'); if (dependentBranch) { this._initCounterElements('filersCount', 'filer'); this._initCounterElements('filersDependentsCount', 'filersDependent'); this._toggleElementList(['childrenCount', 'child'], 'hide'); } else { this._initCounterElements('childrenCount', 'child'); this._toggleElementList(this.taxFilterElementGroups.filerDependentBranchIdList, 'hide'); } } else { this._toggleElementList(['siblingsCount', 'parentsCount', 'sibling', 'parent'], 'hide'); this._initCounterElements('childrenCount', 'child'); if (dependentBranch) { this._initCounterElements('filersCount', 'filer'); this._initCounterElements('filersDependentsCount', 'filersDependent'); } else { this._toggleElementList(this.taxFilterElementGroups.filerDependentBranchIdList, 'hide'); } } }, _initCounterElements: function(counterElementId, elementPrefix) { var self = this, sensitiveElement = $('#' + counterElementId), prevElementValue = parseInt(sensitiveElement.val()); if (!sensitiveElement.length) { return false; } this._setToggleField(counterElementId, 'show'); var onChange = function (e) { var elementValue = parseInt(sensitiveElement.val()); if (elementValue > 0) { self._toggleElementList([elementPrefix], 'show'); if (elementValue > prevElementValue && elementValue > 1) { var subCount = (prevElementValue != 0) ? elementValue - prevElementValue : elementValue - prevElementValue - 1; self._addDependentForm(elementPrefix, subCount); } else if (elementValue < prevElementValue) { self._deleteDependentForm(elementPrefix, prevElementValue - elementValue); } self._prepareDependentOptions(elementPrefix); $('select[id^="' + elementPrefix + 'Id_"]').each(function(i, e){ self._initDependentIncomeElements(elementPrefix, i, true); }); if (elementPrefix == 'filer' && (e != 'init')) { /* on change filer count */ self._prepareFilersDependentsData(); } self._prepareAnnualIncomeTooltip(elementPrefix); } else { self._toggleElementList([elementPrefix], 'hide'); if (elementPrefix == 'filer' && e == 'init') { $('input#filersDependentsCount').prop('disabled', true); } } prevElementValue = elementValue; }; sensitiveElement.unbind('change.initCounter').bind('change.initCounter', onChange); this._onChangeIE(counterElementId); onChange('init'); }, _prepareDependentOptions: function(elementPrefix) { var spouseId = $('select[id="spouseId"] option:selected').val(); if (elementPrefix == 'dependent') { var isMarried = $('input[name="isMarried"]:checked').val(), taxFilingStatus = $('select[id="taxFilingStatus"] option:selected').val(), spouseFilingStatus = $('input[name="spouseFilingStatus"]:checked').val(); if (isMarried == 'y' && taxFilingStatus == 'filer' && spouseFilingStatus == 'jointly') { this._toggleDependentOptions(elementPrefix, spouseId); } } else if (elementPrefix == 'child' || elementPrefix == 'sibling') { this._toggleDependentOptions(elementPrefix, spouseId); } }, _resetToggleDependentOptions: function(elementPrefix) { $('select[id^="' + elementPrefix + 'Id_"] option').show(); }, _resetDependentElement: function(element) { $(element).prop('selectedIndex', 0).trigger('change'); }, _toggleDependentOptions: function(elementPrefix, alreadyUsedValue, dependentBranch) { var self = this; if (alreadyUsedValue != null && alreadyUsedValue != '' && alreadyUsedValue != 'new') { $('select[id^="' + elementPrefix + 'Id_"]').each(function (i, elementItem) { if (!dependentBranch) { if (alreadyUsedValue == $(elementItem).find('option:selected').val()) { self._resetDependentElement(elementItem); } } $(elementItem).find('option[value="' + alreadyUsedValue + '"]').hide(); }); } self._showUnusedOptions(elementPrefix); }, _showUnusedOptions: function(elementPrefix) { var usedValues = this._getUsedOptionsList(elementPrefix); $('select[id^="' + elementPrefix + 'Id_"] option').each(function (i, option) { if ($.inArray($(option).val(), usedValues) == -1) { $(option).show(); } }); }, _getUsedOptionsList: function(elementPrefix) { var used = this._getSelectedDependentValues(elementPrefix); if (elementPrefix == 'dependent' || elementPrefix == 'child' || elementPrefix == 'sibling') { used = this._addSpouseIdToUsedOptions(elementPrefix, used); } return used; }, _getSelectedDependentValues: function(elementPrefix, dependentBranch) { var values = []; $('select[id^="' + elementPrefix + 'Id_"] :selected').each(function(i, e){ var v = $(e).val(); if (v != '' && (dependentBranch || v != 'new')) { values[i] = v; } }); return values; }, _addSpouseIdToUsedOptions: function(elementPrefix, used) { var spouseId = $('select[id="spouseId"] option:selected').val(); if (spouseId != '' && spouseId != 'new') { if (elementPrefix == 'dependent') { var isMarried = $('input[name="isMarried"]:checked').val(), taxFilingStatus = $('select[id="taxFilingStatus"] option:selected').val(), spouseFilingStatus = $('input[name="spouseFilingStatus"]:checked').val(); if (isMarried == 'y' && taxFilingStatus == 'filer' && spouseFilingStatus == 'jointly') { used.push(spouseId); } } else if (elementPrefix == 'child' || elementPrefix == 'sibling') { used.push(spouseId); } } return used; }, _getSameFilersDependentsData: function(applicantIndex, filersCount, filers) { var request = {}; request.action = 'getFilersDependentsData'; request.applicantIndex = applicantIndex; request.filers = filers; request.filersCount = filersCount; var response = JSON.parse( $.ajax({ url: qs.constant('BASE_URL') + '/' + qs.constant('CURRENT_PAGE'), global: false, type: 'POST', data: request, async: false }).responseText ); return response.result; }, _prepareFilersDependentsData: function(e) { var taxFilingStatus = $('select[id="taxFilingStatus"] option:selected').val(); if (taxFilingStatus == 'dependent') { var applicantIndex = $('input[id="applicantIndex"]').val(), filersCount = $('input[id ="filersCount"]').val(), filers = this._getSelectedDependentValues('filer', true); if (filersCount == filers.length) { var response = this._getSameFilersDependentsData(applicantIndex, filersCount, filers); if (filersCount > 0 || response == 'noAction') { $('input#filersDependentsCount').prop('disabled', false); } else { $('input#filersDependentsCount').prop('disabled', true); this._setDependentElementData('filersDependent', 'filersDependents', []); } if (response != 'noAction') { if ($.isArray(response)) { if (response.length) { this._setDependentElementData('filersDependent', 'filersDependents', response); } else { if (filers.length != 0 && filersCount == filers.length) { this._setDependentElementData('filersDependent', 'filersDependents', []); } } } } } } }, _setDependentElementData: function(elementPrefix, counterElementPrefix, data) { $('input[id ="' + counterElementPrefix + 'Count"]').val(data.length).trigger('change'); if (data.length) { setTimeout(function() { $(data).each(function (i, v) { $('select[id="' + elementPrefix + 'Id_' + i + '"]').val(v).trigger('change'); }); }, 500); } }, /** * onChange for IE11 * @param elementId * @private */ _onChangeIE: function(elementId) { if(navigator.userAgent.match(/Trident\/7\./)) { $('#' + elementId).on('blur', function(){ $('#' + elementId).trigger('change'); }); } }, _prepareAnnualIncomeTooltip: function(elementPrefix) { var self = this, taxFilingStatusValue = $('#taxFilingStatus option:selected').val(); if (this.options.applicantAge < this.options.minIncomeAge || taxFilingStatusValue == 'dependent') { $('[id^="' + elementPrefix + 'AnnualIncome_"]').each(function(i, v){ self._toggleLabelTooltip(elementPrefix + 'AnnualIncome_' + i, 'show'); }); } else { $('[id^="' + elementPrefix + 'AnnualIncome_"]').each(function(i, v){ self._toggleLabelTooltip(elementPrefix + 'AnnualIncome_' + i, 'hide'); }); } }, _deleteDependentForm: function(elementPrefix, count) { while (count--) { this._deleteLastDependentForm(elementPrefix); } }, _deleteLastDependentForm: function(elementPrefix) { $('dt[id^="' + elementPrefix + 'FirstRow_"]').last().remove(); $('dd[id^="' + elementPrefix + 'FirstRow_"]').last().remove(); $('dt[id^="' + elementPrefix + 'SecondRow_"]').last().remove(); $('dd[id^="' + elementPrefix + 'SecondRow_"]').last().remove(); }, _addDependentForm: function(elementPrefix, count) { var lastIndex = $('dd[id^="' + elementPrefix + 'SecondRow_"]').size() - 1; this._setDisabledCountElement(true); qs.ajax(this.options.requestUrl, { action: 'renderDependent', lastIndex: lastIndex, count: count, applicantIndex: $('input[id="applicantIndex"]').val(), elName: elementPrefix }).done(_.bind(this._renderDependentResponse, this)); }, _renderDependentResponse: function (response) { var form, formHtml = _.str.trim(response.formHtml), lastDependentForm = $('dd[id^="' + response.elementName + 'SecondRow_"]').last(); if (lastDependentForm && formHtml && response.elementName && response.formIndex) { form = $(formHtml); lastDependentForm.after(form); if (response.evalData) { $.globalEval(response.evalData); } this._setDisabledCountElement(false); } }, _setDisabledCountElement: function(status) { $('button.bootstrap-touchspin-up').attr('disabled', status); $('button.bootstrap-touchspin-down').attr('disabled', status); }, _initChildOrSpouseTaxFilerElement: function(selector) { var self = this; var onChange = function() { if (!$(selector + ':checked').size()) { self._toggleElementList(['isChildLivesUnmarriedParents', 'isChildNoncustodialParent'], 'hide'); self._toggleElementList(self.taxFilterElementGroups.filerElementIdList, 'hide'); self._toggleElementList(self.taxFilterElementGroups.otherElementIdList, 'hide'); self._toggleElementList(self.taxFilterElementGroups.filerDependentBranchIdList, 'hide'); } else if ( $(selector + ':checked').val() == 'n' || ($(selector + ':checked').val() == 'y' && self._isAgeGreate()) ) { self._toggleElementList(['isChildLivesUnmarriedParents', 'isChildNoncustodialParent'], 'hide'); self._initDependentCountersElements(true); } else { self._setToggleField('isChildLivesUnmarriedParents', 'show'); self._initChildLivesUnmarriedParentsElement('input[name="isChildLivesUnmarriedParents"]'); } }; $(selector + ':radio').click(onChange); onChange(); }, _initChildLivesUnmarriedParentsElement: function(selector) { var self = this; var onChange = function() { if (!$(selector + ':checked').size()) { self._setToggleField('isChildNoncustodialParent', 'hide'); self._toggleElementList(self.taxFilterElementGroups.filerElementIdList, 'hide'); self._toggleElementList(self.taxFilterElementGroups.otherElementIdList, 'hide'); self._toggleElementList(self.taxFilterElementGroups.filerDependentBranchIdList, 'hide'); } else if ($(selector + ':checked').val() == 'y') { self._setToggleField('isChildNoncustodialParent', 'hide'); self._initDependentCountersElements(true); } else { self._setToggleField('isChildNoncustodialParent', 'show'); self._initChildNoncustodialParentElement('input[name="isChildNoncustodialParent"]'); } }; $(selector + ':radio').click(onChange); onChange(); }, _initChildNoncustodialParentElement: function(selector) { var self = this; var onChange = function() { if ($(selector + ':checked').val() == 'y') { self._initDependentCountersElements(true); } else if ($(selector + ':checked').val() == 'n') { self._initFilerCounterElement(true); } else { self._toggleElementList(self.taxFilterElementGroups.filerElementIdList, 'hide'); self._toggleElementList(self.taxFilterElementGroups.otherElementIdList, 'hide'); self._toggleElementList(self.taxFilterElementGroups.filerDependentBranchIdList, 'hide'); } }; $(selector + ':radio').click(onChange); onChange(); }, _currencyToFloat: function(str) { return Number(str.replace(/[^0-9\.]+/g,"")); }, _getApplicantIncome: function(id) { qs.showLoading(); var request = {}; request.action = 'getApplicantIncome'; request.applicantIndex = id; var response = JSON.parse( $.ajax({ url: qs.constant('BASE_URL') + '/' + qs.constant('CURRENT_PAGE'), global: false, type: 'POST', data: request, async: false }).responseText ); qs.hideLoading(); return response.result; }, _calcInsuranceOffer: function(ip) { var request = {}; request.action = 'getInsuranceOffer'; request.ip = this._currencyToFloat(ip); request.applicantIndex = $('input[id="applicantIndex"]').val(); var response = JSON.parse( $.ajax({ url: qs.constant('BASE_URL') + '/' + qs.constant('CURRENT_PAGE'), global: false, type: 'POST', data: request, async: false }).responseText ); return response.result; }, _calcAge: function(birth) { var request = {}; request.action = 'getApplicantAge'; request.birth = birth; var response = JSON.parse( $.ajax({ url: qs.constant('BASE_URL') + '/' + qs.constant('CURRENT_PAGE'), global: false, type: 'POST', data: request, async: false }).responseText ); return response.result; }, callFunction: function (func) { try { return eval(func); } catch (e) {} }, _initInputElement: function(elementId, callMethod, showElementIdList, hideElementIdList) { var self = this, sensitiveElement = $('#' + elementId); if (!sensitiveElement.length) { return false; } var onChange = function() { var elementValue = sensitiveElement.val(); if (elementValue) { if (callMethod) { var result = self.callFunction(callMethod + '("' + elementValue + '")'); if (result){ self._toggleElementList(showElementIdList, 'show'); } else { self._toggleElementList(hideElementIdList, 'hide'); } } else { self._toggleElementList(showElementIdList, 'show'); } } else { self._toggleElementList(hideElementIdList, 'hide'); } }; sensitiveElement.unbind('change.initInput').bind('change.initInput', onChange); onChange() }, _initSelectElement: function(elementId, sensitiveElementValue, showElementIdList, hideElementIdList) { var self = this, sensitiveElement = $('#' + elementId); if (!sensitiveElement.length) { return false; } var onChange = function () { var selectedValue = $('#' + elementId +' option:selected').val(); if(selectedValue == sensitiveElementValue) { self._toggleElementList(showElementIdList, 'show'); } else { self._toggleElementList(hideElementIdList, 'hide'); } }; sensitiveElement.unbind('change.initSelect').bind('change.initSelect', onChange); onChange(); }, _prepareMultiElementIds: function(elementPrefix) { var self = this, elementIdList = ['Id', 'AnnualIncome', 'MonthlyIncome', 'FirstRow', 'SecondRow'], multiElementIds = []; $('select[id^="' + elementPrefix + 'Id_"]').each(function(elIndex, v){ for (var i in elementIdList) { multiElementIds.push(elementPrefix + elementIdList[i] + '_' + elIndex); } multiElementIds.push('new' + self._ucFirst(elementPrefix) + '_' + elIndex); }); return multiElementIds; }, _toggleElementList: function(elementIdList, status) { var self = this; $.each(elementIdList, function(index, elementId) { if (-1 == $.inArray(elementId, self.multiElementsIdPrefix)) { self._setToggleField(elementId, status); } else { /* multi element */ if (status == 'hide') { var formsCount = $('dd[id^="' + elementId + 'SecondRow_"]').size() - 1; if (formsCount >= 1) { self._deleteDependentForm(elementId, formsCount); } } $.each(self._prepareMultiElementIds(elementId), function(index, elementId) { self._setToggleField(elementId, status); }); } }); }, _setToggleField: function(elementId, status) { if (status == 'hide') { $('#' + elementId + '-label').hide(); $('#' + elementId + '-label label').removeClass('required').addClass('optional'); $('#' + elementId + '-element').hide(); this._resetElement(elementId, true); } else if (status == 'show') { $('#' + elementId + '-label').show(); $('#' + elementId + '-label label').removeClass('optional').addClass('required'); $('#' + elementId + '-element').show(); } return true; }, _resetElement: function (elementId, setDefaultValues) { var element = $('#' + elementId); if (element.is('input[type="text"]') || element.is('select')) { if (element.is('input[id$="Count"]') || element.is('input[id="pregnancyChildren"]')) { if (elementId in this.options.defaultData) { element.val(this.options.defaultData[elementId]); } else { element.val(0); } } else { element.val(''); } element.trigger('change'); } else if ($('input[name="' + elementId + '"]')) { $('input[name="' + elementId + '"]').prop('checked', false); } }, _ucFirst: function (inputString) { return inputString.charAt(0).toUpperCase() + inputString.substr(1, inputString.length-1); }, _initCalculatorPopupLink: function() { $('a[id="calc_popup"]').bind('click', _.bind(this.onCalculatorPopupLinkClick, this)); }, onCalculatorPopupLinkClick: function(e) { var obj = this, actionParam = 'incomeCalculatorPopup', actionUrl = qs.constant('BASE_URL_LANGUAGE') + '/' + qs.constant('CURRENT_PAGE'); qs.showLoading(); var request = { action: actionParam }; qs.ajax(actionUrl, request, {qsShowLoading: false}) .fail(function(status) {alert(status.responseText);}) .done(function(data) { if (data && data.form) { var contentObj = $(data.form); delete data['form']; var scripts = []; if (data.scripts) { var loadedScripts = []; var iteration = 0; $('script').each(function() { if ($(this).attr('src')) { loadedScripts[iteration] = $(this).attr('src'); iteration++; } }); iteration = 0; $(data.scripts).each(function() { if ($.inArray($(this).attr('src'), loadedScripts) < 0 && $(this).attr('src')) { scripts[iteration] = $(this).attr('src'); iteration++; } }); } delete data['scripts']; delete data['stylesheets']; var initFunctions = []; for (var key in data) { if (data[key].length) { initFunctions[key] = function() { eval(data[key]); } } } obj._initContent(contentObj, scripts, initFunctions, e.currentTarget); } else { alert('Error loading data'); } }); }, _initContent: function(content, scripts, initFunctions, target) { if (scripts.length) { var obj = this; this._scriptLoaderIterator = 0; for (var key in scripts) { this._loadScript(scripts[key], scripts.length, function() { obj._loadContent(content, initFunctions, target); }); } } else { this._loadContent(content, initFunctions, target); } }, _loadContent: function(content, initFunctions, target) { this._scriptLoaderIterator = 0; $.fancybox({ scrolling: 'auto', closeBtn: false, overlayOpacity: 0.1, maxWidth: 800, autoCenter: true, autoResize: true, margin: 0, padding: 10, content: $('
').html(content), afterShow: function () { $('.fancybox-overlay input:text:first').focus(); }, afterClose: function () { $(target).focus(); } }); content.find('.btn-cancel').removeAttr('onclick').click(function(e) { e.preventDefault(); $.fancybox.close(); }); content.find('.btn-submit').removeAttr('onclick').click(function(e) { e.preventDefault(); var totalIncomeValue = $('input[name="totalIncome"]').val(); if (totalIncomeValue) { $('input[name="annualIncome"]').val(totalIncomeValue); } $.fancybox.close(); }); for (var key in initFunctions) { initFunctions[key](); } qs.hideLoading(); }, _loadScript: function(script, scriptsCount, callback) { var obj = this; $.getScript(script, function() { obj._scriptLoaderIterator++; if (scriptsCount == obj._scriptLoaderIterator) { callback(); } }); } };