
var LocationSelector = new Class({

	options: {
		helperBaseUrl: '/ajaxhelper.aspx',
		proxyUrl: ''
	},

	initialize: function(el, options){
		this.pa = $(el);
		this.setOptions(options);
		this.states = $('states');
		// find default state/city
		this.citySelection = '';
		this.marketSelection = '';
		if ($('cityselection')) this.citySelection = $('cityselection').getValue();
		if ($('marketselection')) this.marketSelection = $('marketselection').getValue();
		this.states.addEvent('change', this.getCities.bind(this));
		this.getCities();
	},

	// Whenever the user changes the state selection, this runs
	// and also when the page loads
	getCities: function(){
		var dis = this;
		if (this.markets) {
			this.markets.removeEvents();
			this.markets.parentNode.removeChild(this.markets);
			this.markets = null;
		}
		if (this.states.getValue() == '') {
			if (this.cities) {
				this.cities.removeEvents();
				this.cities.parentNode.removeChild(this.cities);
				this.cities = null;
			}
		} else {
			if (!this.cities) {
				this.cities = new Element('select', {
					'name': 'cities',
					'class': 'next',
					'id': 'cities'
				}).injectInside(this.pa);
			}

			// Generating the url string to use in the call
			var url = this.options.helperBaseUrl + '?state=' + this.states.getValue().toUpperCase() + '&selected=' + this.citySelection;

			// if it needs to be proxied, add the proxy and escape the final url
			if (this.options.proxyUrl != '') {
				var url = this.options.proxyUrl + escape(url);
			}

			// making the request
			new Ajax(url, {
				method: 'get',
				update: dis.cities,
				evalScripts: true,
				onComplete: function(resp){
					if (window.ie) {
						dis.insertSelectIE(dis.cities, resp);
					}
					dis.cities.removeEvents();
					dis.cities.addEvent('change', dis.getMarkets.bind(dis));
					if ( (dis.cities.getValue() != '') || (dis.citySelection != '' && dis.citySelection != '0')) {
						// already used the city
						dis.citySelection = '';
						dis.getMarkets();
					}
				}
			}).request();
		}
	},

	getMarkets: function(){
		var dis = this;
		if ( (this.states.getValue() == '' || this.cities.getValue() == '') && this.markets) {
			this.markets.removeEvents();
			this.markets.parentNode.removeChild(this.markets);
			this.markets = null;
		} else {
			if (!this.markets) {
				this.markets = new Element('select', {
					'name': 'markets',
					'class': 'next',
					'id': 'markets'
				}).injectInside(this.pa);
			}

			// Generating the url string to use in the call
			var url = this.options.helperBaseUrl + '?city=' + this.cities.getValue() + '&selected=' + this.marketSelection;

			// if it needs to be proxied, add the proxy and escape the final url
			if (this.options.proxyUrl != '') {
				var url = this.options.proxyUrl + escape(url);
			}

			// making the request
			new Ajax(url, {
				method: 'get',
				update: dis.markets,
				evalScripts: true,
				onComplete: function(resp){
					if (window.ie) {
						dis.insertSelectIE(dis.markets, resp);
					}
					// remove market selection, since we already used it
					if (dis.marketSelection != '') {
						dis.marketSelection = '';
					}
				}
			}).request();
		}
	},

	insertSelectIE: function(target, resp){
		resp = '<select>'+resp+'</select>';
		var temp = new Element('div');
		temp.setHTML(resp);
		target.setHTML('');
		temp.getElements('option').each( function(opt){opt.injectInside(target);} );
	}

});
LocationSelector.implement(new Options);

function checkInput(){
	if (($('states').value == '') || ($('cities').value == ''))
	{
		alert("You must at least select a city in order to search.");
		return false;
	}
}
