//Prototype of trim function
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function SecListBox(ListBox,text,value)
{
	//alert(ListBox,text,value);
	try
	{
		var option=document.createElement("OPTION");
		option.value=value;
		option.text=text;
		ListBox.options.add(option)
	}
	catch(er)
	{
		alert(er)
	}
}
function FirstListBox()
{
	try
	{
		var addedcity;
		var count=document.getElementById("citylist").options.length;
		var i=0;
		for(i=0;i<count;i++)
		{
			if(document.getElementById("citylist").options[i].selected)
			{
				addedcity=document.getElementById("citylist").options[i].value;
				
				SecListBox(document.getElementById("chosencities"),
						   document.getElementById("citylist").options[i].value,
						   document.getElementById("citylist").options[i].value);
				
				document.getElementById("citylist").remove(i);
				break
			}
		}
		var ctr=document.getElementById("hiddencities");
		if((ctr.value.trim()).length > 0)
			ctr.value += ",'" + addedcity + "'";
		else
			ctr.value += "'" + addedcity + "'";
		
	}
	catch(er)
	{
		alert(er)
	}
}
function SortAllItems()
{
	var arr=new Array();
	for(i=0;i<document.getElementById("citylist").options.length;i++)
	{
		arr[i]=document.getElementById("citylist").options[i].value
	}
	arr.sort();
	RemoveAll();
	for(i=0;i<arr.length;i++)
	{
		SecListBox(document.getElementById("citylist"),arr[i],arr[i])
	}
}
function RemoveAll()
{
	try
	{
		document.getElementById("citylist").options.length=0
	}
	catch(er)
	{
		alert(er)
	}
}
function SecondListBox()
{
	try
	{
		var removedcity;
		var count=document.getElementById("chosencities").options.length;
		for(i=0;i<count;i++)
		{
			if(document.getElementById("chosencities").options[i].selected)
			{
				removedcity=document.getElementById("chosencities").options[i].value;
				SecListBox(document.getElementById("citylist"),document.getElementById("chosencities").options[i].value,document.getElementById("chosencities").options[i].value);
				document.getElementById("chosencities").remove(i);
				break
			}
		}
		var ctr=document.getElementById("hiddencities");
		ctr.value=RemoveEntries(ctr.value,"'" + removedcity + "'" );
		SortAllItems()
	}
	catch(er)
	{
		alert(er)
	}
}
//remove from  array
function RemoveEntries(Source, ValueToRemove)
{
	var s="";
	var tmparray=Source.split(",");
	var i;
	for (i=0;i<tmparray.length ;i++ )
	{		
		if(tmparray[i] == ValueToRemove)
			;
		else
			s+=tmparray[i] + ",";
	}
	return s.substring(0,s.length-1);
}


/*********************COMMUNITY LISTBOX FUNCTION ******************/
	function _4ListBox(ListBox,text,value)
	{
		try
		{
			var option=document.createElement("OPTION");
			option.value=value;
			option.text=text;
			ListBox.options.add(option)
		}
		catch(er)
		{
			alert(er)
		}
	}
	function ThirdListBox()
	{
		try
		{
			var addedcomm;
			var count=document.getElementById("commlist").options.length;
			var i=0;
			for(i=0;i<count;i++)
			{
				if(document.getElementById("commlist").options[i].selected)
				{
					addedcomm=document.getElementById("commlist").options[i].value;
					_4ListBox(document.getElementById("chosencomm"),document.getElementById("commlist").options[i].value,document.getElementById("commlist").options[i].value);
					document.getElementById("commlist").remove(i);
					break
				}
			}
			var ctr=document.getElementById("hiddencomm");
			if((ctr.value.trim()).length > 0)
				ctr.value += ",'" + addedcomm + "'";
			else
				ctr.value += "'" + addedcomm + "'";			
		}
		catch(er)
		{
			alert(er)
		}
	}
	function SortAllCommItems()
	{
		var arr=new Array();
		for(i=0;i<document.getElementById("commlist").options.length;i++)
		{
			arr[i]=document.getElementById("commlist").options[i].value
		}
		arr.sort();
		RemoveAllComm();
		for(i=0;i<arr.length;i++)
		{
			_4ListBox(document.getElementById("commlist"),arr[i],arr[i])
		}
	}
	function RemoveAllComm()
	{
		try
		{
			document.getElementById("commlist").options.length=0
		}
		catch(er)
		{
			alert(er)
		}
	}
	function ForthListBox()
	{
		try
		{
			var removedcomm;
			var count=document.getElementById("chosencomm").options.length;
			for(i=0;i<count;i++)
			{
				if(document.getElementById("chosencomm").options[i].selected)
				{
					removedcomm=document.getElementById("chosencomm").options[i].value;
					_4ListBox(document.getElementById("commlist"),document.getElementById("chosencomm").options[i].value,document.getElementById("chosencomm").options[i].value);
					document.getElementById("chosencomm").remove(i);
					break
				}
			}
			var ctr=document.getElementById("hiddencomm");
			ctr.value=RemoveEntries(ctr.value,"'" + removedcomm + "'" );
			SortAllCommItems()
		}
		catch(er)
		{
			alert(er)
		}
	}
	function SelectedCities(selectedValue)
	{			
		if(selectedValue.trim() == "")
			return;
		var tmparray=(selectedValue.trim()).split(',');		
		var toremove = "'";
		var re = new RegExp(toremove, 'gi');
		for(ctr=0;ctr<tmparray.length;ctr++)
		{
			for(i=0;i<document.getElementById('citylist').options.length;i++)
			{
				if(document.getElementById('citylist').options[i].value == tmparray[ctr].replace(re,""))
				{
					document.getElementById('citylist').options[i].selected=true;
					//FirstListBox();
					addedcity=document.getElementById("citylist").options[i].value;				
					SecListBox(document.getElementById("chosencities"),
						   document.getElementById("citylist").options[i].value,
						   document.getElementById("citylist").options[i].value);				
					document.getElementById("citylist").remove(i);
					break;
				}
			}
		}
	}

	function SelectedCommunities(selectedValue)
	{
						alert(selectedValue);
		if(selectedValue.trim() == "")
			return;
		var tmparray=(selectedValue.trim()).split(',');		
		var removecommarray=new Array();
		var removecommarrayctr=0;
		var toremove = "'";
		var re = new RegExp(toremove, 'gi');

		for(ctr=0;ctr<tmparray.length;ctr++)
		{			
			for(i=0;i<document.getElementById('commlist').options.length;i++)
			{
				if(document.getElementById('commlist').options[i].value == tmparray[ctr].replace(re,""))
				{					
					document.getElementById('commlist').options[i].selected=true;					
					//ThirdListBox();
					addedcomm=document.getElementById("commlist").options[i].value;
					_4ListBox(document.getElementById("chosencomm"),document.getElementById("commlist").options[i].value,document.getElementById("commlist").options[i].value);
					document.getElementById("commlist").remove(i);
					break;
				}
			}
		}
	}