window.addEvent('domready', function() {
	
	if($('locations').getProperty('value') == 'municipality')
	{
		new Request.JSON({
			url: basePath + '/ajax/getMunicipalities',
			onComplete: function(municipalities)
			{
				$('municipality_select').options.length = 0;
				var count = 0;
				for(var i in municipalities)
				{
					if(typeof municipalities[i] != 'function' && typeof municipalities[i] != 'object')
					{
						var opt = new Element('option',
						{
							'value' : municipalities[i],
							'text': municipalities[i]
						});
						$('municipality_select').options[count] = opt;
						count++;
					}
				}
				$('district_div').setProperty('style', 'display: none;');
				$('municipality_div').setProperty('style','display: block;');
			}
		}).send();
	}
	else if($('locations').getProperty('value') == 'district')
	{
		new Request.JSON({
			url: basePath + '/ajax/getDistricts',
			onComplete: function(districts)
			{
				$('district_select').options.length = 0;
				var count = 0;
				for(var i in districts)
				{
					if(typeof districts[i] != 'function' && typeof districts[i] != 'object')
					{
						var opt = new Element('option',
						{
							'value' : districts[i],
							'text' : districts[i]
						});
						$('district_select').options[count] = opt;
						count++;
					}
				}
				$('municipality_div').setProperty('style','display: none;');
				$('district_div').setProperty('style', 'display: block;');
			}
		}).send();
	}
	else
	{
		$('municipality_div').setProperty('style','display: none;');
		$('district_div').setProperty('style', 'display: none;');
	}
	setup_selects();
	setup_submit();
});


function setup_submit()
{
	if($('nxtstep'))
	{
		$('nxtstep').addEvent('click', function(e)
		{
			if($('locations').getProperty('value')) {
				var event = new Event(e);
				e.stop();
				var selected = {};
				selected['municipality'] = {};
				selected['district'] = {};
				var mcount = 0;
				for(var i = 0; i < $('municipality_select').options.length; i++)
				{
					if($('municipality_select').options[i].selected)
					{
						selected['municipality'][mcount] = $('municipality_select').options[i].value;
						mcount++;
					}
				}
				var dcount = 0;
				for(var i = 0; i < $('district_select').options.length; i++)
				{
					if($('district_select').options[i].selected)
					{
						selected['district'][dcount] = $('district_select').options[i].value;
						dcount++;
					}
				}
				if($('locations').getProperty('value') == 'district' && dcount < 1)
					alert('You must select at least one School District');
				else if($('locations').getProperty('value') == 'municipality' && mcount < 1)
					alert('You must select at least one Municipality');
				else {
					new Request.JSON({
						'url' : basePath + '/ajax/save_search/step_one',
						'data' : 
						{
							'1a' : $('locations').getProperty('value'),
							'1b' : selected
						},
						onComplete: function() { window.location.href = basePath + '/custom-search/step3' }
					}).send();
				}
			}
			else
				alert('You must select a Location');
		});
	}
}

function check_municipality_select()
{
	var selected = 0;
	var string = 'Municipalities -> ';
	for(var i = 0; i < $('municipality_select').options.length; i++)
	{
		if($('municipality_select').options[i].selected)
		{
			selected++;
			string += $('municipality_select').options[i].text+', ';
		}
	}
	if(selected > 5)
	{
			alert('You may only select up to 5 municipalities.');
			for(var i = ($('municipality_select').options.length -1); i > 0; i--)
			{
				if($('municipality_select').options[i].selected)
				 	$('municipality_select').options[i].selected = false;
				string = 'Municipalities -> ';
			}
	}
	if(string.match(','))
	{
		string = string.substring(0, string.lastIndexOf(','));
	}
	$('municipality_message').set('text',string);
}

function check_district_select()
{
	var selected = 0;
	var string = 'School Districts -> ';
	for(var i = 0; i < $('district_select').options.length; i++)
	{
		if($('district_select').options[i].selected)
		{
			selected++;
			string += $('district_select').options[i].text+', ';
		}
	}
	if(selected > 5)
	{
			alert('You may only select up to 5 school districts.');
			for(var i = ($('district_select').options.length -1); i > 0; i--)
			{
				if($('district_select').options[i].selected)
				 	$('district_select').options[i].selected = false;
				string = 'School Districts -> ';
			}
	}
	if(string.match(','))
	{
		string = string.substring(0, string.lastIndexOf(','));
	}
	$('district_message').set('text',string);
}

function reset_messages() {
	$('municipality_message').set('text','Municipalities -> ');
	$('district_message').set('text','School Districts -> ');
}

function setup_selects()
{
	if($('municipality_select'))
		$('municipality_select').addEvent('change',function() {reset_messages(); check_municipality_select(); });
	if($('district_select'))
		$('district_select').addEvent('change', function() {reset_messages(); check_district_select(); });
	
	
	if($('locations'))
	{
		$('locations').addEvent('change', function() {
			if($('locations').getProperty('value') == 'municipality')
			{
				new Request.JSON({
					url: basePath + '/ajax/getMunicipalities',
					onComplete: function(municipalities)
					{
						$('municipality_select').options.length = 0;
						var count = 0;
						for(var i in municipalities)
						{
							if(typeof municipalities[i] != 'function' && typeof municipalities[i] != 'object')
							{
								var opt = new Element('option',
								{
									'value' : i,
									'text': municipalities[i]
								});
								
								try {
									$('municipality_select').options[count] = opt;
								}
								catch(e) {
									var opt = document.createElement('OPTION');
									opt.text = municipalities[i];
									opt.value = i;
									document.getElementById('municipality_select').options.add(opt);
								}
								count++;
							}
						}
						// $('district_div').setProperty('style', 'display: none;');
						// $('municipality_div').setProperty('style','display: block;');
						$('district_div').style.display = 'none';
						$('municipality_div').style.display = '';
					}
				}).send();
			}
			else if($('locations').getProperty('value') == 'district')
			{
				new Request.JSON({
					url: basePath + '/ajax/getDistricts',
					onComplete: function(districts)
					{
						$('district_select').options.length = 0;
						var count = 0;
						for(var i in districts)
						{
							if(typeof districts[i] != 'function' && typeof districts[i] != 'object')
							{
								var opt = new Element('option',
								{
									'value' : i,
									'text' : districts[i]
								});
								try {
									$('district_select').options[count] = opt;
								}
								catch(e) {
									var opt = document.createElement('OPTION');
									opt.text = districts[i];
									opt.value = i;
									document.getElementById('district_select').options.add(opt);
								}
								count++;
							}
						}
						$('district_div').style.display = '';
						$('municipality_div').style.display = 'none';
					}
				}).send();
			}
			else
			{
				$('district_div').style.display = 'none';
				$('municipality_div').style.display = 'none';
			}
		});
	}
}