@@ -15,16 +15,6 @@ require([
15
15
'prototype'
16
16
], function () {
17
17
18
- function inArray(arr, value)
19
- {
20
- for (var i = 0; i < arr.length; i++) {
21
- if (arr[i] === value) {
22
- return true;
23
- }
24
- }
25
- return false;
26
- }
27
-
28
18
var freeModel = Class.create();
29
19
freeModel.prototype = {
30
20
initialize : function()
@@ -93,8 +83,83 @@ $scriptString .= 'this.euCountryList = ' . json_encode($euCountries);
93
83
94
84
$ scriptString .= <<<script
95
85
86
+ this.bindCountryVatNumberRelation();
96
87
this.bindCountryRegionRelation();
97
88
},
89
+ bindCountryVatNumberRelation : function(parentId)
90
+ {
91
+ if (parentId) {
92
+ // todo: fix bug in IE
93
+ var countryElements = $$('#'+parentId+' .countries');
94
+ } else {
95
+ var countryElements = $$('.countries');
96
+ }
97
+ for(var i=0; i<countryElements.size(); i++) {
98
+ Event.observe(countryElements[i], 'change', this.reloadVatNumberField.bind(this));
99
+ this.initVatNumberField(countryElements[i]);
100
+ }
101
+ },
102
+ reloadVatNumberField : function(event)
103
+ {
104
+ this.reload = true;
105
+ var countryElement = Event.element(event);
106
+ if (countryElement && countryElement.id) {
107
+ this.refreshVatNumberField(countryElement);
108
+ this.refreshValidateVatNumberButton(countryElement);
109
+ }
110
+ },
111
+ initVatNumberField : function(element)
112
+ {
113
+ var countryElement = element;
114
+ if (countryElement && countryElement.id) {
115
+ this.refreshVatNumberField(countryElement);
116
+ this.refreshValidateVatNumberButton(countryElement);
117
+ }
118
+ },
119
+ refreshVatNumberField : function(countryElement)
120
+ {
121
+ var vatNumberElement = $(countryElement.id.replace(/country_id/, 'merchant_vat_number'));
122
+ if (vatNumberElement) {
123
+ this.vatNumberElement = vatNumberElement;
124
+ if (vatNumberElement.value.length) {
125
+ vatNumberElement.value = '';
126
+ }
127
+ }
128
+ },
129
+ refreshValidateVatNumberButton : function(countryElement)
130
+ {
131
+ var btnVatNumberElement = $(countryElement.id.replace(/country_id/, 'validate_vat_number'));
132
+ if (btnVatNumberElement) {
133
+ this.btnVatNumberElement = btnVatNumberElement;
134
+ if (countryElement.value.length) {
135
+ this.btnValidateVatNumber(countryElement.value);
136
+ }
137
+ }
138
+ },
139
+ btnValidateVatNumber: function(countryCode)
140
+ {
141
+ var resultElement = document.getElementById("validation_result");
142
+ var isEuCountry = false;
143
+ const fruits = Object.values(this.euCountryList);
144
+
145
+ if ( fruits.includes(countryCode)) {
146
+ isEuCountry = true;
147
+ }
148
+ if (isEuCountry) {
149
+ this.btnVatNumberElement.classList.remove('disabled');
150
+ if (resultElement) {
151
+ resultElement.style.display = "block";
152
+ resultElement.innerHTML = '';
153
+ if ( resultElement.classList.contains('admin__field-error') )
154
+ resultElement.classList.remove('admin__field-error');
155
+ }
156
+ } else {
157
+ this.btnVatNumberElement.classList.add('disabled');
158
+ if (resultElement) {
159
+ resultElement.style.display = "none";
160
+ }
161
+ }
162
+ },
98
163
bindCountryRegionRelation : function(parentId)
99
164
{
100
165
if (parentId) {
@@ -151,12 +216,6 @@ $scriptString .= <<<script
151
216
if (regionElement) {
152
217
this.regionElement = regionElement;
153
218
if (countryElement.value.length) {
154
- var isEUCountry = false;
155
- if ( inArray(this.euCountryList, countryElement.value)) {
156
- isEUCountry = true;
157
- }
158
- this.validateVatNumber(isEUCountry);
159
-
160
219
var url = this.regionsUrl+'parent/'+countryElement.value;
161
220
this.loader.load(url, {}, this.refreshRegionField.bind(this));
162
221
} else {
0 commit comments