	function Populate(select, data, showall)
	{
		data.each(
			function(item) {
				if (item.key == "00000000-0000-0000-0000-000000000000" && !showall) return;
				select.options[select.options.length] = CreateOption((item.value.Name == null) ? item.value : item.value.Name, item.key);
			}
		)
	}
		
	function PopulateCities()
	{
		$("City").options.length = 0;
		var province = $F("Province");
	    if (province.length > 1)
	    {
	        return;
	    }	
	
		provinces[province].Cities.each(
			function(item)
			{
				if (province == "00000000-0000-0000-0000-000000000000" && item.key == "00000000-0000-0000-0000-000000000000") return;
				$("City").options[$("City").options.length] = CreateOption(item.value, item.key)
			}
		);
	}

	function PopulateFaculties()
	{
	    $("Faculty").options.length = 0;
		var university = $F("University");
	    if (university.length > 1)
	    {
	        return;
	    }
	
		universities[university].Faculties.each(
			function(item)
			{
				if (university == "00000000-0000-0000-0000-000000000000" && item.key == "00000000-0000-0000-0000-000000000000") return;
				$("Faculty").options[$("Faculty").options.length] = CreateOption(item.value, item.key)
			}
		);
	}
	
	function Add(from, to)
	{
		$A($(from).options).each (
			function(item, index)
			{
				if (item.selected)
				{
					if (!OptionsValueExists($(to).options, item.value))
					{
						$(to).options[$(to).options.length] = CreateOption(item.text, item.value);
					}
				}
			}
		)
		
		$(from).options.selectedIndex = -1;
		
		return false;
	}
	
	function Add2(from, from2, to, divider, allText)
	{
		if
		(  // Both source fileds are unselected (or "All" is selected from both)
			($F(from) == null || $F(from) == "" || $F(from) == "00000000-0000-0000-0000-000000000000")
			&&
			($F(from2) == null || $F(from2) == "" || $F(from2) == "00000000-0000-0000-0000-000000000000")				
		)
		{
			$(from).options.selectedIndex = -1;
			$(from2).options.selectedIndex = -1;
			return false;
		}
		
		var text1 = "";
		var text2 = "";
		var value1 = "";
		var value2 = "";
		var text = "";
		var value = "";
		

		if ($F(from) == null || $F(from) == "" || $F(from) == "00000000-0000-0000-0000-000000000000")
		{
			text1 = "";
			value1 = "00000000-0000-0000-0000-000000000000";
		}
		else
		{
			text1 = $(from).options[$(from).selectedIndex].innerHTML;
			value1 = $F(from);
		}

        var criteriaAddedFromBox2 = false;
        
        for (k=0; k<$(from).options.length; k++)
        {
            if (!$(from).options[k].selected) continue;        
			
			text1 = $(from).options[k].innerHTML;
			value1 = $(from).options[k].value;
			
		    for (i=0; i<$(from2).options.length; i++)
		    {
		        if (!$(from2).options[i].selected) continue;
    		
		        if ($F(from2) == null || $F(from2) == "" || $F(from2) == "00000000-0000-0000-0000-000000000000")
		        {
			        text2 = "";
			        value2 = "00000000-0000-0000-0000-000000000000";
		        }
		        else
		        {
			        text2 = $(from2).options[i].innerHTML;
			        value2 = $(from2).options[i].value;
		        }
        		
		        text = ConstructText(text1, text2);
		        value = value1 + "|" + value2;
        		
		        if (!OptionsValueExists($(to).options, value))
		        {
			        $(to).options[$(to).options.length] = CreateOption(text, value);
			        criteriaAddedFromBox2 = true;
		        }
		    }
		    if (!criteriaAddedFromBox2)
		    {
		        text = ConstructText(text1, "");
		        value = value1 + "|" + "00000000-0000-0000-0000-000000000000";
		        if (!OptionsValueExists($(to).options, value))
		        {
			        $(to).options[$(to).options.length] = CreateOption(text, value);
			        criteriaAdded = true;
		        }
		    }
		}
		
		if (!criteriaAddedFromBox2)
		{
		    for (i=0; i<$(from2).options.length; i++)
		    {
		        if (!$(from2).options[i].selected) continue;
    		
		        if ($F(from2) == null || $F(from2) == "" || $F(from2) == "00000000-0000-0000-0000-000000000000")
		        {
			        text2 = "";
			        value2 = "00000000-0000-0000-0000-000000000000";
		        }
		        else
		        {
			        text2 = $(from2).options[i].innerHTML;
			        value2 = $(from2).options[i].value;
		        }
        		
		        text = ConstructText(text1, text2);
		        value = value1 + "|" + value2;
        		
		        if (!OptionsValueExists($(to).options, value))
		        {
			        $(to).options[$(to).options.length] = CreateOption(text, value);
			        criteriaAdded = true;
		        }
		    }
		}
		
		$(from).options.selectedIndex = -1;
		$(from2).options.selectedIndex = -1;
		
		return false;

	}
	
	function ConstructText(text1, text2)
	{
	    var text = "";
	    if (text1 != "" && text2 != "")
        {
	        text = text1 + " / " + text2;
        }
        else
        {
	        if (text1 != "")
	        {
		        text = text1;
	        }
	        else if (text2 != "")
	        {
		        text = text2;
	        }
        }
        return text;
	}
	
	function AddBeginTermAndBeginYear() 
	{
		var text1 = "";
		var divider = "";
		
		if ($F("BeginTerm") != "00000000-0000-0000-0000-000000000000")
		{
			text1 = $("BeginTerm").options[$("BeginTerm").selectedIndex].text;
			divider = " / ";
		}
		
		var text2 = $("BeginYear").options[$("BeginYear").selectedIndex].text;
		var text = text1 + divider + text2;
		var value = $F("BeginTerm") + "|" + $F("BeginYear");
		
		if (!OptionsValueExists($("BeginTermAndYear").options, value))
		{
			$("BeginTermAndYear").options[$("BeginTermAndYear").options.length] = CreateOption(text, value);
		}
		
		return false;
	}
	
	function Remove(from)
	{
		var removed = 0;
		
		$A($(from).options).each (
			function(item, index)
			{
				if (item.selected)
				{
					$(from).options[index - removed] = null;
					removed++;
				}
			}
		)
		
		return false;
	}
	
	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;
	}
	
	function CreateOption(text, value)
	{
		return new Option(text, value)
	}
	
	function SelectAll(e)
	{
		e = $(e);
		if (e != null)
		{
			for (var i=0; i < e.options.length; i++)
			{
				e.options[i].selected = true;
			}
		}
	}
