
function LoadCitizenship()
{
	var location = window.location + "";
	var isAdmin = location.indexOf("/EditProfile.aspx") != -1; 
	//alert("Location: " + location);
	if (isAdmin == true) EditProfile.LoadCitizenship(LoadCitizenship_Callback);
}

function LoadCitizenship_Callback(response)
{
	//alert("LoadCitizenship_Callback");
	if (null != response && null != response.value)
	{
		var country = response.value;
		if ("object" == typeof(country))
		{
			var mode = "add";
			if (null == country.CountryID || "" == country.CountryID)
				mode = "remove";
			EditCitizenshipList(mode, country.CountryID, country.Description);
		} 
	}
}

function RemoveCitizenship(countryID)
{
	ClearStatus();
	EditCitizenshipList("remove", countryID);
	var location = window.location + "";
	if (location.indexOf("/EditProfile.aspx") != -1)
		EditProfile.RemoveCitizenship(countryID, ServerSideRemoveCitizenship_Callback); 
	else
		Register.RemoveCitizenship(countryID, ServerSideRemoveCitizenship_Callback); 
}

function ServerSideRemoveCitizenship_Callback(response)
{}

function AddCitizenship()
{
	//alert("Add");
	ClearStatus();
	var drpCitizenshipCountries = document.getElementById("drpCitizenshipCountries");
	var countryID = drpCitizenshipCountries.value;
	var countryDescription = drpCitizenshipCountries.options[drpCitizenshipCountries.selectedIndex].text;
	// Do not add more than one optional citizenship
	if (document.getElementById("btnRemoveCitizenship") != null)
		alert("At most only two citizenships are required.");
	else if (countryID == "tt")
		alert("Trinidad & Tobago is already selected as default.");
	else
	{
		var location = window.location + "";
		if (location.indexOf("/EditProfile.aspx") != -1)
		{
			//alert("EditProfile.AddCitizenship(...)");
			EditProfile.AddCitizenship(countryID, countryDescription, ServerSideAddCitizenship_Callback); 
		}			
		else
			Register.AddCitizenship(countryID, countryDescription, ServerSideAddCitizenship_Callback); 
	}
}


function ServerSideAddCitizenship_Callback(response)
{
	var drpCitizenshipCountries = document.getElementById("drpCitizenshipCountries");
	var countryID = drpCitizenshipCountries.value;
	var countryDescription = drpCitizenshipCountries.options[drpCitizenshipCountries.selectedIndex].text;

	//alert(response);

	EditCitizenshipList("add", countryID, countryDescription);
}


function EditCitizenshipList(mode, countryID, countryDescription)
{
	var tdElement = document.getElementById("tdOptionalCitizenship");
	
	if ("add" == mode)
	{
		// Remove <p>None</p> Element first if present
		var pElement = document.getElementById("pOptionalCitizenship");
		if (null != pElement) tdElement.removeChild(pElement);
		
		// Create <span> element for new citizenship information
		var spanElement = document.createElement('span');
		var textNode = document.createTextNode(countryDescription);
		
		// Associate attributes with newly created elements
		spanElement.id = countryID;
		spanElement.className = "CountryDescription";
		spanElement.appendChild(textNode);
		
		// Add 'Country Description' to table
		tdElement.appendChild(spanElement);
		
		// Create <a> element for "Remove" button
		var anchorElement = document.createElement('a');
		
		anchorElement.id = "btnRemoveCitizenship";
		anchorElement.href = "javascript:RemoveCitizenship('" + countryID + "')";
		textNode = document.createTextNode("[Remove]");
		anchorElement.appendChild(textNode);
		
		// Add "Remove" button to table	
		tdElement = document.getElementById("tdRemoveCitizenship");
		tdElement.appendChild(anchorElement);
		
	}
	else if("remove" == mode)
	{
		// Remove <span> element first if present
		var spanElement = document.getElementById(countryID);
		if (null != spanElement) tdElement.removeChild(spanElement);
		
		var tdElement = document.getElementById("tdOptionalCitizenship");		
		var pElement = document.createElement("p");
		var textNode = document.createTextNode("None");
		
		pElement.id = "pOptionalCitizenship";
		pElement.appendChild(textNode);
		
		tdElement.appendChild(pElement);
		
		// Remove "Remove" button if present
		tdElement = document.getElementById("tdRemoveCitizenship");
		var anchorElement = document.getElementById("btnRemoveCitizenship");
		if (null != anchorElement)
			tdElement.removeChild(anchorElement);		
	}
}



function SetStatus(status)
{
	document.getElementById("lblStatus").innerHTML = status;
}


function ClearStatus()
{
	SetStatus("");
}
