function AddOption(obj, text, value)
{
	try
	{
	var newOpt = new Option(text, value);
	obj.options.add(newOpt);
	}catch(e)
	{}
}

function FillMemberTypes( obj )
{
	AddOption(obj, "Male", 1);
	AddOption(obj, "Female", 2);
	AddOption(obj, "Couple", 4);
	AddOption(obj, "Group", 8);
	AddOption(obj, "CD \ TV \ TS", 16);
}

function FillLocations( obj )
{

}

function loadLocations(controlID, required, chooseWord, dynamicShow) 
{
	var sel = document.getElementById("country");
	if(!sel)
		return;
	var countryId = document.getElementById(controlID + "countryId").value;
	if(chooseWord == null)	// just in case the word was not defined yet
	{
		if(required == "True")
			chooseWord = "Choose";
		else
			chooseWord = "All";
	}

	var newOpt = new Option(chooseWord, 0);
	sel.options[sel.options.length] = newOpt;
	//load countries
	for (var i=0; i < locations.length; i++) 
	{
		var location = locations[i];
		var locID = location[0];
		var locName = location[1];
		var isCountry = false;

		if (location[4] == 3) 
		{
			isCountry = true;
			locID = location[3];
		}
		
		if (location[4] == 2) 
		{
			locID = location[2];
		}

		if (isCountry) 
		{
			newOpt = new Option(locName, locID);
			sel.options[sel.options.length] = newOpt;

			if(locID == countryId)
			{			    
				sel.selectedIndex = sel.options.length-1;
				loadChildLocations(controlID, "country", "countryregion", 
					required, chooseWord, dynamicShow);
			}
		}
	}
}

function loadChildLocations(controlID, list, childlist, required, chooseWord, dynamicShow) 
{
	var sel1 = document.getElementById(list);
	
	var parentID = 0;
	try
	{
		parentID = sel1.options[sel1.selectedIndex].value;
	}catch(e){};

	if(childlist == "") 
	{
		document.getElementById(controlID  + list + "Id").value = parentID;
	} 
	else 
	{
		var sel2 = document.getElementById(childlist);
		var tr2 = document.getElementById("tr" + childlist); //tr" +
		var childID = document.getElementById(controlID + childlist + "Id");

		var loading = false;

		//default option
		sel2.options.length = 0;
		if(chooseWord == null)	// just in case the word was not defined yet
		{
			if(required == "True")
				chooseWord = "Choose";
			else
				chooseWord = "All";
		}
		
		var newOpt = new Option(chooseWord, "0");	
		
		sel2.options[sel2.options.length] = newOpt;
		sel2.style.display = "inline";
		sel2.style.visibility = "visible";
		tr2.style.display = "block";
		
		if(childlist == "countryregion")
		{
			document.getElementById("city").style.display = "none";
			document.getElementById("city").style.visibility = "hidden";
			if (dynamicShow == "True")
				document.getElementById("trcity").style.display = "none";
		}
		// add child locations
		for (var i=1; i < locations.length; i++) 
		{
			var location = locations[i];
			var locID = location[0];
			if (location[4] == 3) 
		    {
			    locID = location[3];
		    }    		
		    if (location[4] == 2) 
		    {
			    locID = location[2];
		    }
			var locName = location[1];
			var isChild = false;
			if ( childlist == "city" )
			{
				if (location[4] == 1 && location[2] == parentID )
				    isChild = true;
			}
			else
				if (location[4] == 2 && location[3] == parentID )
					isChild = true;
					
			if (isChild) 
			{	
				newOpt = new Option(locName, locID);
				sel2.options[sel2.options.length] = newOpt;

				// set profile location as selected if loading
				if(childID.value !=0 && locID == childID.value)
				{
					loading = true;
					sel2.selectedIndex = sel2.options.length-1;
					if(childlist == "countryregion")
						loadChildLocations(controlID,"countryregion", "city", required, 
							chooseWord, dynamicShow);
				}
			}
		}

		//update fields with change if not loading
		if(!loading)
		{
			sel2.selectedIndex = 0;
			childID.value = 0;
			document.getElementById(controlID + "cityId").value = 0;
			document.getElementById(controlID + list + "Id").value = parentID;
		}
		if(sel2.options.length <= 1)
		{
			sel2.style.display = "none";
			sel2.style.visibility = "hidden";
			if (dynamicShow == "True")
				tr2.style.display = "none";
		}
	}
}
