PageSourceSearch

https://www.bestbassgear.com/js/varien/form.js?q=051216

js bestbassgear.com collected 2026-10-02 17:58:11 UTC 15,038 bytes, 415 lines download raw bytes

1/**
2 * Magento
3 *
4 * NOTICE OF LICENSE
5 *
6 * This source file is subject to the Academic Free License (AFL 3.0)
7 * that is bundled with this package in the file LICENSE_AFL.txt.
8 * It is also available through the world-wide-web at this URL:
9 * http://opensource.org/licenses/afl-3.0.php
10 * If you did not receive a copy of the license and are unable to
11 * obtain it through the world-wide-web, please send an email
12 * to [email protected] so we can send you a copy immediately.
13 *
14 * DISCLAIMER
15 *
16 * Do not edit or add to this file if you wish to upgrade Magento to newer
17 * versions in the future. If you wish to customize Magento for your
18 * needs please refer to http://www.magento.com for more information.
19 *
20 * @category    Varien
21 * @package     js
22 * @copyright   Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
23 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
24 */
25VarienForm = Class.create();
26VarienForm.prototype = {
27    initialize: function(formId, firstFieldFocus){
28        this.form       = $(formId);
29        if (!this.form) {
30            return;
31        }
32        this.cache      = $A();
33        this.currLoader = false;
34        this.currDataIndex = false;
35        this.validator  = new Validation(this.form);
36        this.elementFocus   = this.elementOnFocus.bindAsEventListener(this);
37        this.elementBlur    = this.elementOnBlur.bindAsEventListener(this);
38        this.childLoader    = this.onChangeChildLoad.bindAsEventListener(this);
39        this.highlightClass = 'highlight';
40        this.extraChildParams = '';
41        this.firstFieldFocus= firstFieldFocus || false;
42        this.bindElements();
43        if(this.firstFieldFocus){
44            try{
45                Form.Element.focus(Form.findFirstElement(this.form))
46            }
47            catch(e){}
48        }
49    },
50
51    submit : function(url){
52        if(this.validator && this.validator.validate()){
53             this.form.submit();
54        }
55        return false;
56    },
57
58    bindElements:function (){
59        var elements = Form.getElements(this.form);
60        for (var row in elements) {
61            if (elements[row].id) {
62                Event.observe(elements[row],'focus',this.elementFocus);
63                Event.observe(elements[row],'blur',this.elementBlur);
64            }
65        }
66    },
67
68    elementOnFocus: function(event){
69        var element = Event.findElement(event, 'fieldset');
70        if(element){
71            Element.addClassName(element, this.highlightClass);
72        }
73    },
74
75    elementOnBlur: function(event){
76        var element = Event.findElement(event, 'fieldset');
77        if(element){
78            Element.removeClassName(element, this.highlightClass);
79        }
80    },
81
82    setElementsRelation: function(parent, child, dataUrl, first){
83        if (parent=$(parent)) {
84            // TODO: array of relation and caching
85            if (!this.cache[parent.id]){
86                this.cache[parent.id] = $A();
87                this.cache[parent.id]['child']     = child;
88                this.cache[parent.id]['dataUrl']   = dataUrl;
89                this.cache[parent.id]['data']      = $A();
90                this.cache[parent.id]['first']      = first || false;
91            }
92            Event.observe(parent,'change',this.childLoader);
93        }
94    },
95
96    onChangeChildLoad: function(event){
97        element = Event.element(event);
98        this.elementChildLoad(element);
99    },
100
101    elementChildLoad: function(element, callback){
102        this.callback = callback || false;
103        if (element.value) {
104            this.currLoader = element.id;
105            this.currDataIndex = element.value;
106            if (this.cache[element.id]['data'][element.value]) {
107                this.setDataToChild(this.cache[element.id]['data'][element.value]);
108            }
109            else{
110                new Ajax.Request(this.cache[this.currLoader]['dataUrl'],{
111                        method: 'post',
112                        parameters: {"parent":element.value},
113                        onComplete: this.reloadChildren.bind(this)
114                });
115            }
116        }
117    },
118
119    reloadChildren: function(transport){
120        var data = eval('(' + transport.responseText + ')');
121        this.cache[this.currLoader]['data'][this.currDataIndex] = data;
122        this.setDataToChild(data);
123    },
124
125    setDataToChild: function(data){
126        if (data.length) {
127            var child = $(this.cache[this.currLoader]['child']);
128            if (child){
129                var html = '<select name="'+child.name+'" id="'+child.id+'" class="'+child.className+'" title="'+child.title+'" '+this.extraChildParams+'>';
130                if(this.cache[this.currLoader]['first']){
131                    html+= '<option value="">'+this.cache[this.currLoader]['first']+'</option>';
132                }
133                for (var i in data){
134                    if(data[i].value) {
135                        html+= '<option value="'+data[i].value+'"';
136                        if(child.value && (child.value == data[i].value || child.value == data[i].label)){
137                            html+= ' selected';
138                        }
139                        html+='>'+data[i].label+'</option>';
140                    }
141                }
142                html+= '</select>';
143                Element.insert(child, {before: html});
144                Element.remove(child);
145            }
146        }
147        else{
148            var child = $(this.cache[this.currLoader]['child']);
149            if (child){
150                var html = '<input type="text" name="'+child.name+'" id="'+child.id+'" class="'+child.className+'" title="'+child.title+'" '+this.extraChildParams+'>';
151                Element.insert(child, {before: html});
152                Element.remove(child);
153            }
154        }
155
156        this.bindElements();
157        if (this.callback) {
158            this.callback();
159        }
160    }
161}
162
163RegionUpdater = Class.create();
164RegionUpdater.prototype = {
165    initialize: function (countryEl, regionTextEl, regionSelectEl, regions, disableAction, zipEl)
166    {
167        this.countryEl = $(countryEl);
168        this.regionTextEl = $(regionTextEl);
169        this.regionSelectEl = $(regionSelectEl);
170        this.zipEl = $(zipEl);
171        this.config = regions['config'];
172        delete regions.config;
173        this.regions = regions;
174
175        this.disableAction = (typeof disableAction=='undefined') ? 'hide' : disableAction;
176        this.zipOptions = (typeof zipOptions=='undefined') ? false : zipOptions;
177
178        if (this.regionSelectEl.options.length<=1) {
179            this.update();
180        }
181
182        Event.observe(this.countryEl, 'change', this.update.bind(this));
183    },
184
185    _checkRegionRequired: function()
186    {
187        var label, wildCard;
188        var elements = [this.regionTextEl, this.regionSelectEl];
189        var that = this;
190        if (typeof this.config == 'undefined') {
191            return;
192        }
193        var regionRequired = this.config.regions_required.indexOf(this.countryEl.value) >= 0;
194
195        elements.each(function(currentElement) {
196            Validation.reset(currentElement);
197            label = $$('label[for="' + currentElement.id + '"]')[0];
198            if (label) {
199                wildCard = label.down('em') || label.down('span.required');
200                if (!that.config.show_all_regions) {
201                    if (regionRequired) {
202                        label.up().show();
203                    } else {
204                        label.up().hide();
205                    }
206                }
207            }
208
209            if (label && wildCard) {
210                if (!regionRequired) {
211                    wildCard.hide();
212                    if (label.hasClassName('required')) {
213                        label.removeClassName('required');
214                    }
215                } else if (regionRequired) {
216                    wildCard.show();
217                    if (!label.hasClassName('required')) {
218                        label.addClassName('required')
219                    }
220                }
221            }
222
223            if (!regionRequired) {
224                if (currentElement.hasClassName('required-entry')) {
225                    currentElement.removeClassName('required-entry');
226                }
227                if ('select' == currentElement.tagName.toLowerCase() &&
228                    currentElement.hasClassName('validate-select')) {
229                    currentElement.removeClassName('validate-select');
230                }
231            } else {
232                if (!currentElement.hasClassName('required-entry')) {
233                    currentElement.addClassName('required-entry');
234                }
235                if ('select' == currentElement.tagName.toLowerCase() &&
236                    !currentElement.hasClassName('validate-select')) {
237                    currentElement.addClassName('validate-select');
238                }
239            }
240        });
241    },
242
243    update: function()
244    {
245        if (this.regions[this.countryEl.value]) {
246            var i, option, region, def;
247
248            def = this.regionSelectEl.getAttribute('defaultValue');
249            if (this.regionTextEl) {
250                if (!def) {
251                    def = this.regionTextEl.value.toLowerCase();
252                }
253                this.regionTextEl.value = '';
254            }
255
256            this.regionSelectEl.options.length = 1;
257            for (regionId in this.regions[this.countryEl.value]) {
258                region = this.regions[this.countryEl.value][regionId];
259
260                option = document.createElement('OPTION');
261                option.value = regionId;
262                option.text = region.name.stripTags();
263                option.title = region.name;
264
265                if (this.regionSelectEl.options.add) {
266                    this.regionSelectEl.options.add(option);
267                } else {
268                    this.regionSelectEl.appendChild(option);
269                }
270
271                if (regionId == def || (region.name && region.name.toLowerCase() == def)
272                    || (region.name && region.code.toLowerCase() == def)
273                ) {
274                    this.regionSelectEl.value = regionId;
275                }
276            }
277            this.sortSelect();
278            if (this.disableAction == 'hide') {
279                if (this.regionTextEl) {
280                    this.regionTextEl.style.display = 'none';
281                }
282
283                this.regionSelectEl.style.display = '';
284            } else if (this.disableAction == 'disable') {
285                if (this.regionTextEl) {
286                    this.regionTextEl.disabled = true;
287                }
288                this.regionSelectEl.disabled = false;
289            }
290            this.setMarkDisplay(this.regionSelectEl, true);
291        } else {
292            this.regionSelectEl.options.length = 1;
293            this.sortSelect();
294            if (this.disableAction == 'hide') {
295                if (this.regionTextEl) {
296                    this.regionTextEl.style.display = '';
297                }
298                this.regionSelectEl.style.display = 'none';
299                Validation.reset(this.regionSelectEl);
300            } else if (this.disableAction == 'disable') {
301                if (this.regionTextEl) {
302                    this.regionTextEl.disabled = false;
303                }
304                this.regionSelectEl.disabled = true;
305            } else if (this.disableAction == 'nullify') {
306                this.regionSelectEl.options.length = 1;
307                this.regionSelectEl.value = '';
308                this.regionSelectEl.selectedIndex = 0;
309                this.lastCountryId = '';
310            }
311            this.setMarkDisplay(this.regionSelectEl, false);
312        }
313
314        this._checkRegionRequired();
315        // Make Zip and its label required/optional
316        var zipUpdater = new ZipUpdater(this.countryEl.value, this.zipEl);
317        zipUpdater.update();
318    },
319
320    setMarkDisplay: function(elem, display){
321        elem = $(elem);
322        var labelElement = elem.up(0).down('label > span.required') ||
323                           elem.up(1).down('label > span.required') ||
324                           elem.up(0).down('label.required > em') ||
325                           elem.up(1).down('label.required > em');
326        if(labelElement) {
327            inputElement = labelElement.up().next('input');
328            if (display) {
329                labelElement.show();
330                if (inputElement) {
331                    inputElement.addClassName('required-entry');
332                }
333            } else {
334                labelElement.hide();
335                if (inputElement) {
336                    inputElement.removeClassName('required-entry');
337                }
338            }
339        }
340    },
341    sortSelect : function () {
342        var elem = this.regionSelectEl;
343        var tmpArray = new Array();
344        var currentVal = $(elem).value;
345        for (var i = 0; i < $(elem).options.length; i++) {
346            if (i == 0) {
347                continue;
348            }
349            tmpArray[i-1] = new Array();
350            tmpArray[i-1][0] = $(elem).options[i].text;
351            tmpArray[i-1][1] = $(elem).options[i].value;
352        }
353        tmpArray.sort();
354        for (var i = 1; i <= tmpArray.length; i++) {
355            var op = new Option(tmpArray[i-1][0], tmpArray[i-1][1]);
356            $(elem).options[i] = op;
357        }
358        $(elem).value = currentVal;
359        return;
360    }
361}
362
363ZipUpdater = Class.create();
364ZipUpdater.prototype = {
365    initialize: function(country, zipElement)
366    {
367        this.country = country;
368        this.zipElement = $(zipElement);
369    },
370
371    update: function()
372    {
373        // Country ISO 2-letter codes must be pre-defined
374        if (typeof optionalZipCountries == 'undefined') {
375            return false;
376        }
377
378        // Ajax-request and normal content load compatibility
379        if (this.zipElement != undefined) {
380            Validation.reset(this.zipElement)
381            this._setPostcodeOptional();
382        } else {
383            Event.observe(window, "load", this._setPostcodeOptional.bind(this));
384        }
385    },
386
387    _setPostcodeOptional: function()
388    {
389        this.zipElement = $(this.zipElement);
390        if (this.zipElement == undefined) {
391            return false;
392        }
393
394        // find label
395        var label = $$('label[for="' + this.zipElement.id + '"]')[0];
396        if (label != undefined) {
397            var wildCard = label.down('em') || label.down('span.required');
398        }
399
400        // Make Zip and its label required/optional
401        if (optionalZipCountries.indexOf(this.country) != -1) {
402            while (this.zipElement.hasClassName('required-entry')) {
403                this.zipElement.removeClassName('required-entry');
404            }
405            if (wildCard != undefined) {
406                wildCard.hide();
407            }
408        } else {
409            this.zipElement.addClassName('required-entry');
410            if (wildCard != undefined) {
411                wildCard.show();
412            }
413        }
414    }
415}

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.