﻿// For some reason the extjs library calls a blank image (http://extjs.com/s.gif) from their servers.
// Since the call is not using SSL it causes IE to show a warning.
Ext.BLANK_IMAGE_URL = '/libs/ext-2.2/resources/images/default/s.gif';

var ACW = {
	dateformats: 'n/j/Y|n/j/y|n/j|n-j-Y|n-j-y|n-j|mdy|mdY',
	
	eventHelper: function (e) {
		this.event = e || window.event;
		
		this.preventDefault = function () {
			if (this.event.preventDefault) {
				this.event.preventDefault();
			} else {
				this.event.returnValue = false;
			}
		};
	},
	
	getEventHelper: function (e) { return new ACW.eventHelper(e); },
	
	eventListener: function (obj, evType, fn) {
		if (obj.addEventListener) {
			obj.addEventListener(evType, fn, false);
			return true;
		} else if (obj.attachEvent) {
			var r = obj.attachEvent('on' + evType, fn);
			return r;
		} else {
			return false;
		}
	},
	
	alert: function (msg) {
		window.alert(msg);
	},
	
	confirm: function (msg) {
		return window.confirm(msg);
	},
	
	prompt: function (msg) {
		return window.prompt(msg);
	},
	
	getEl: function (id) {
		return window.document.getElementById(id);
	},
	
	createNewWindowLinks: function () {
		var aLinks = ACW.getElementsByClass('js_NewWindow', 'a');
		
		for (var i = 0, len = aLinks.length; i < len; i++) {
			ACW.eventListener(aLinks[i], 'click', function (uri) {
				return function (e) {
					window.open(uri);
					ACW.getEventHelper(e).preventDefault();
				};
			}(aLinks[i].href));
		}
	},
	
	getElementsByClass: function (cls, tag, root) {
		var p = root || window.document;
		var t = tag || '*';
		var els = p.getElementsByTagName(t);
		var outEls = [];
		
		for (var i = 0, len = els.length; i <= len; i++) {
			if (els[i] && els[i].className.search(new RegExp('\\b' + cls + '\\b', 'g')) !== -1) {
				outEls[outEls.length] = els[i];
			}
		}
		
		return outEls;
	},
	
	run: function () {
		ACW.createNewWindowLinks();
	}
};

ACW.COMBOBOX = {
	run: function (searchFieldName, valueFieldName, serverPage) {
		var searchEl = Ext.get(searchFieldName);
		var idEl = Ext.get(valueFieldName);
		
		if (searchEl && idEl) {
			var ds = new Ext.data.Store({
				proxy: new Ext.data.HttpProxy({url: serverPage}),
				reader: new Ext.data.JsonReader({
					root: 'rows'
				}, [
					{name: 'v', mapping: 'v'},
					{name: 'n', mapping: 'n'}
				])
			});
			
			var search = new Ext.form.ComboBox({
				store: ds,
				minChars: 1,
				displayField: 'n',
				valueField: 'v',
				typeAhead: false,
				loadingText: 'Searching&hellip;',
				applyTo: searchEl
			});
			
			search.on('select', function(combo, record, index) {
				idEl.dom.value =  record.data.v;
			});
			
			search.on('blur', function(combo, record, index) {
				if ((idEl.dom.value != "") && (combo.getRawValue() == "")) {
					idEl.dom.value = "";
				};
			});
		}
	}
};

Ext.onReady(function () {
	ACW.COMBOBOX.run('City_Search', 'City_Id', '/async-calls/get-cities-json.aspx');
});