function PopulateFaculties(allText)
{
	var u = document.getElementById("fieldUniversityId");
	var f = document.getElementById("fieldFacultyId");
	
	if (u != null && u.selectedIndex != -1 && f != null)
	{
		var s;
		
		s = u.options[u.selectedIndex].value;
		
		f.options.length = 0;
		
		f.options[f.options.length] = new Option(allText, '00000000-0000-0000-0000-000000000000');
		
		for (key in universities[s].Faculties)
		{
			f.options[f.options.length] = new Option(universities[s].Faculties[key], key);
		}
	}
	
	return false;
}

function PopulateCities(allText)
{
	var p = document.getElementById("fieldProvinceId");
	var c = document.getElementById("fieldCityId");
	
	if (p != null && p.selectedIndex != -1 && c != null)
	{
		var s;
		
		s = p.options[p.selectedIndex].value;
		
		c.options.length = 0;
		c.options[c.options.length] = new Option(allText, '00000000-0000-0000-0000-000000000000');
		
		for (key in provinces[s].Cities)
		{
			c.options[c.options.length] = new Option(provinces[s].Cities[key], key);
		}
	}
	
	return false;
}

function AddToBeginTerms(listBoxTo, listBoxFrom, textBoxFrom)
{
	if (listBoxTo == null || listBoxFrom == null || listBoxFrom.selectedIndex == -1)
	{
		return false;
	}
	else
	{
		if (textBoxFrom == null || !textBoxFrom.value.replace(/^\s*|\s*$/g, "").match(/^\d{4,4}$/))
		{
			var value = listBoxFrom.options[listBoxFrom.selectedIndex].value + "|<%# int.MinValue %>";
		
			if (!OptionsValueExists(listBoxTo.options, value))
			{
				listBoxTo.options[listBoxTo.options.length] = new Option(listBoxFrom.options[listBoxFrom.selectedIndex].text, value);
			}
		}
		else
		{
			var value = listBoxFrom.options[listBoxFrom.selectedIndex].value + "|" + textBoxFrom.value.replace(/^\s*|\s*$/g, "");
			
			if (!OptionsValueExists(listBoxTo.options, value))
			{
				listBoxTo.options[listBoxTo.options.length] = new Option(listBoxFrom.options[listBoxFrom.selectedIndex].text + " / " + textBoxFrom.value.replace(/^\s*|\s*$/g, ""), listBoxFrom.options[listBoxFrom.selectedIndex].value + "|" + textBoxFrom.value.replace(/^\s*|\s*$/g, ""));
			}
		}
		
		textBoxFrom.value = "";
		
		return false;
	}
}

function AddToSelectedSingle(listBoxTo, listBoxFrom)
{
	if (listBoxTo == null || listBoxFrom == null || listBoxFrom.selectedIndex == -1)
	{
		return false;
	}
	else
	{
		for (i=0; i<listBoxFrom.options.length; i++)
		{
		    if (listBoxFrom.options[i].selected)
		    {
		        if (!OptionsValueExists(listBoxTo.options, listBoxFrom.options[i].value))
		        {		
			        listBoxTo.options[listBoxTo.options.length] = new Option(listBoxFrom.options[i].text, listBoxFrom.options[i].value);
		        }
		    }
		}
			
		listBoxFrom.selectedIndex = -1;
		
		return false;
	}
}

function AddToSelected(listBoxTo, listBoxFrom, listBoxFrom2, divider, allText)
{
	divider = divider || "|";
	
	if (listBoxTo == null || ((listBoxFrom == null || listBoxFrom.selectedIndex == -1) && (listBoxFrom2 == null || listBoxFrom2.selectedIndex == -1)))
	{
		return false;
	}
	else
	{
		// There's a single select from both source controls
		if (listBoxFrom != null && listBoxFrom.selectedIndex != -1)
		{
			if (listBoxFrom2 != null && listBoxFrom2.selectedIndex != -1)
			{
				for (i=0; i<listBoxFrom.options.length; i++)
				{
				    for (j=0; j<listBoxFrom2.options.length; j++)
				    {
				        if ( (listBoxFrom.options[i].selected && listBoxFrom.options[i].value != "00000000-0000-0000-0000-000000000000") && 
					        (listBoxFrom2.options[j].selected && listBoxFrom2.options[j].value != "00000000-0000-0000-0000-000000000000"))
				        {
					        if (!OptionsValueExists(listBoxTo.options, listBoxFrom.options[i].value + divider + listBoxFrom2.options[j].value))
					        {
						        if ((listBoxTo.id == "fieldSelectedCities" || listBoxTo.id == "fieldSelectedFaculties") && listBoxFrom.options[i].value == "00000000-0000-0000-0000-000000000000")
						        {
							        listBoxTo.options[listBoxTo.options.length] = new Option(listBoxFrom2.options[j].text, listBoxFrom.options[i].value + divider + listBoxFrom2.options[j].value);
						        }
						        else
						        {
							        listBoxTo.options[listBoxTo.options.length] = new Option(listBoxFrom.options[i].text + " / " + listBoxFrom2.options[j].text, listBoxFrom.options[i].value + divider + listBoxFrom2.options[j].value);
						        }
					        }
				        }
				    }
				}
				
				listBoxFrom2.selectedIndex = -1;
			}
			else // Multiple selection from the one of the boxes
			{
				for(i=0; i<listBoxFrom.options.length; i++)
				{
				    if (listBoxFrom.options[i].selected && listBoxFrom.options[i].value != "00000000-0000-0000-0000-000000000000")
				    {
					    if (!OptionsValueExists(listBoxTo.options, listBoxFrom.options[i].value + divider + "00000000-0000-0000-0000-000000000000"))
					    {
						    listBoxTo.options[listBoxTo.options.length] = new Option(listBoxFrom.options[i].text + " / " + allText, listBoxFrom.options[i].value + divider + '00000000-0000-0000-0000-000000000000');
					    }
				    }
				}
			}
			
			listBoxFrom.selectedIndex = -1;
		}
		else // Multiple selection from the other of the boxes
		{
			if (listBoxFrom2 != null && listBoxFrom2.selectedIndex != -1)
			{
			    for(i=0; i<listBoxFrom2.options.length; i++)
			    {
				    if (listBoxFrom2.options[i].selected && listBoxFrom2.options[i].value != "00000000-0000-0000-0000-000000000000")
				    {
					    if (!OptionsValueExists(listBoxTo.options, "00000000-0000-0000-0000-000000000000" + divider + listBoxFrom2.options[i].value))
					    {
						    if (listBoxTo.id == "fieldSelectedCities" || listBoxTo.id == "fieldSelectedFaculties")
						    {
							    listBoxTo.options[listBoxTo.options.length] = new Option(listBoxFrom2.options[i].text, "00000000-0000-0000-0000-000000000000" + divider + listBoxFrom2.options[i].value);
						    }
						    else
						    {
							    listBoxTo.options[listBoxTo.options.length] = new Option(allText + ' / ' + listBoxFrom2.options[i].text, '00000000-0000-0000-0000-000000000000' + divider + listBoxFrom2.options[i].value);
						    }
					    }
				    }
				}
				
				listBoxFrom2.selectedIndex = -1;
			}
		}
	}
	
	return false;
}

function RemoveFromSelected(listBoxSelected)
{
	if (listBoxSelected == null)
	{
		return false;
	}
	else
	{
		for (var i=0; i < listBoxSelected.options.length; i++)
		{
			if (listBoxSelected.options[i].selected)
			{
				listBoxSelected.options[i] = null;
				i--;
			}
		}
		
		return false;
	}
}

	function SelectAll(e)
{
	if (e != null)
	{
		for (var i=0; i < e.options.length; i++)
		{
			e.options[i].selected = true;
		}
	}
}

function OptionsValueExists(options, value)
{
	if (options != null || value != null)
	{
		for (var i=0; i < options.length; i++)
		{
			if (options[i].value == value) 
			{
				return true;
			}
		}

		return false;
	}
	else
	{
		return false;
	}
}