/// /// BestDeal namespace /// Type.registerNamespace('BestDeal'); /// /// BestDeal.UI namespace /// Type.registerNamespace('BestDeal.UI'); /// /// BestDeal.UI.PostCodeFinderExtender control /// BestDeal.UI.PostCodeFinderExtender = function(element) { BestDeal.UI.PostCodeFinderExtender.initializeBase(this, [element]); this._providerUrl = null; this._lookupPostCodeElementId = null; this._selectAddressElementId = null; this._line1ElementId = null; this._line2ElementId = null; this._line3ElementId = null; this._townElementId = null; this._countyElementId = null; this._postCodeElementId = null; this._clientOnLookupAddress = null; this._clientOnFetchAddress = null; } BestDeal.UI.PostCodeFinderExtender.prototype = { /// /// Initialize /// initialize : function() { BestDeal.UI.PostCodeFinderExtender.callBaseMethod(this, 'initialize'); $addHandler(this.get_element(), 'click', Function.createDelegate(this, this._onLookup)); $addHandler($get(this._selectAddressElementId), 'change', Function.createDelegate(this, this._onFetch)); }, /// /// Dispose /// dispose : function() { $clearHandlers(this.get_element()); $clearHandlers($get(this._selectAddressElementId)); BestDeal.UI.PostCodeFinderExtender.callBaseMethod(this, 'dispose'); }, /// /// URL handling /// _appendUrl : function(url, append) { var last_ch = url[url.length - 1]; if(last_ch == '?' || last_ch == '&') return url + append; else if(url.indexOf('?') == -1) return url + '?' + append; else return url + '&' + append; }, _getLookupUrl : function(postCode) { return this._appendUrl(this._providerUrl, 'op=lookup&' + 'postCode=' + postCode); }, _getFetchUrl : function(id) { return this._appendUrl(this._providerUrl, 'op=fetch&id=' + id); }, /// /// Helper methods /// addSelectOption : function(value, text) { // add new option var option = document.createElement('OPTION'); option.value = value; option.text = text; $get(this._selectAddressElementId).options.add(option); }, clearSelect : function(select) { if(typeof(select.options.remove) == 'undefined') { for(var i = select.options.length - 1; i >= 0; i--) select.options[i] = null; } else { for(var i = select.options.length - 1; i >= 0; i--) select.options.remove(i); } }, /// /// Lookup event /// _onLookup : function(e) { var element = this.get_element(); if(element && !element.disabled) { if(this._clientOnLookupAddress) this._clientOnLookupAddress(); this.clearSelect($get(this._selectAddressElementId)); this.addSelectOption('', 'Please Wait... Loading...'); var postCode = $get(this._lookupPostCodeElementId).value; var wr = new Sys.Net.WebRequest(); wr.set_url(this._getLookupUrl(postCode)); wr.set_httpVerb('GET'); wr.set_timeout(20000); wr.set_userContext(this); wr.add_completed(function(executor, e) { var obj = executor.get_webRequest().get_userContext(); obj._onLookupComplete.call(obj, executor, e) }); wr.invoke(); } }, /// /// Lookup complete delegate /// _onLookupComplete : function(executor, e) { try { var data = executor.get_responseData(); if(data == null) { this.addSelectOption('', 'Invalid Post Code...'); return; } // initialise