var MultiSelect = new Class({

	_iCount:				null,
	_arrCounties:			null,	

	initialize: 			function()
	{
		this.populateArray();
		
		var ddlCounty = $('county');
		var ulCountyList = $('multicounties');
		
		if(ddlCounty && ulCountyList)
		{
			ddlCounty.list = ulCountyList;
			ddlCounty.addEvent('change', function()
										 {
												var iChosen = this.options[this.selectedIndex].value;
												var strChosen = this.options[this.selectedIndex].innerHTML;
																
												if(iChosen > -1 && !document.msCounties.alreadyPresent(iChosen))
												{
													if(document.msCounties._iCount == 5)
													{
														alert("You cannot select more than 5 regions, please remove one first");
													}
													else
													{
														var newLI = new Element('li');
														var newSpan = new Element('span');
														var newLink = new Element('a');
		
														newLI.setProperty("id", "county_"+iChosen);
		
														newSpan.innerHTML = strChosen;
														
														newLink.countyID = iChosen;
														newLink.innerHTML = "Remove";
														newLink.setProperty("href", "javascript:void(0);");												
														newLink.addEvent("click", function()
																				  {
																						document.msCounties.removeItem(newLink.countyID);	   
																				  });
														
														newLI.adopt(newSpan);
														newLI.adopt(newLink);												
														
														this.list.adopt(newLI);
														
														document.msCounties.addItem(iChosen);
													}
												}
										 });
		}
	},
	
	alreadyPresent:				function(iCountyID)
	{
		return this._arrCounties.contains(iCountyID);
	},
	
	checkCount:					function(blAdding)
	{
		var liNone = $('nocounties');
		if(liNone)
		{
			if(blAdding == true && this._iCount == 0)
			{
				liNone.addClass("hidden");	
			}
			else if(blAdding == false && this._iCount == 0)
			{
				liNone.removeClass("hidden");	
			}
		}
	},
	
	updateCount:				function(blAddOne)
	{
		if(blAddOne == true)
		{
			this._iCount++;
		}
		else
		{
			this._iCount--;
		}

		this.updateHiddenList();
	},
	
	addItem:				function(iCountyID)
	{
		this._arrCounties.push(iCountyID);
		this.checkCount(true);												
		this.updateCount(true);	
	},
		
	removeItem:				function(iCountyID)
	{
		var liToRemove = $("county_"+iCountyID);
		if(liToRemove)
		{		
			liToRemove.parentNode.removeChild(liToRemove);	

			this._arrCounties.erase(iCountyID.toString());
			this.updateCount(false);		
			this.checkCount(false);	
		}
	},
	
	populateArray:			function()
	{
		var inpCounties = $('countyids');
		if(inpCounties)
		{
			if(inpCounties.value != "")
			{
				this._arrCounties = inpCounties.value.split(",");
				this._iCount = this._arrCounties.length;
			}
			else
			{
				this._arrCounties = new Array();
				this._iCount = 0;
			}
		}
	},
	
	updateHiddenList:			function()
	{	
		var inpCounties = $('countyids');
		if(inpCounties)
		{
			inpCounties.value = this._arrCounties.join(",");	
		}
	}
});

window.addEvent('domready', function()
{		
	document.msCounties = new MultiSelect();
});