// Contact Pages JS

function onContactSelected(selectedText)
	{
		if (selectedText != 'Choose Option')
		{
			$('goButton').setAttribute('href'  , selectedText);
			$('goButton').setAttribute('target', '_blank');
		}
		else
		{
			$('goButton').setAttribute('href', '#');
			$('goButton').setAttribute('target', '');
		}
	}

$('countryDropDownContainer').makePositioned();
	Event.observe(document, 'click', clickEventHandler, false);
	
	$('contactDropDownContainer').makePositioned();
	
	// site id
	var g_siteID="<?php echo $contentpage['Contentpage']['id'] ?>"; // exception for 44
	
	var g_countryListOpen = "";
	var g_contactListOpen = "";
	var g_selectedCountry = "";
	var g_selectedContact = "";
	function clickEventHandler(evt)
	{
		var clickedElement = Event.element(evt);
		    		
		if (clickedElement.id == "countryDropDownLink")
		{
			if (g_countryListOpen)
				hideCountryList();
			else
				showCountryList();
		}
		else if ( clickedElement.id != "countryDropDownList" && ! (Element.descendantOf(clickedElement, 'countryDropDownList') ))
		{
			if(g_siteID==44)
			{
			 // hideTables();
			}
		
			hideCountryList();
		}
		
		if (clickedElement.id == "contactDropDownLink" && g_selectedCountry != "")
		{
			if (g_contactListOpen)
				hideContactList();
			else
				showContactList();
		}
		else if ( clickedElement.id != "contactDropDownList" && ! (Element.descendantOf(clickedElement, 'contactDropDownList') ))
		{
			hideContactList();
		}
	}
	
	
	/* HELPERS */
	function showCountryList(){
		Element.hide('countryDropDownSelectedItem');
 
		Element.show('countryDropDownList');
		g_countryListOpen = true;
	}
	function showContactList(){
		Element.hide('contactDropDownSelectedItem');
				
		Element.show('contactDropDownList');
		g_contactListOpen = true;
	}
	
	function countryListItemSelected(selectedCountry)
	{
	    //alert(selectedCountry); // Botswana
		if(g_siteID==44)
		{
		  //Element.show('DivTest');
		  
		  showTable(selectedCountry);
		  //showTable(tableId);
		}
	
		if (g_selectedCountry != selectedCountry)
		{	
			g_selectedCountry = selectedCountry;
			contactListItemSelected("", "");
			// empty dropdownlist so the previous contents 
			// will not be shown when we select a new country
			Element.update('contactDropDownList','<div id=\"loadingIndicator\" style=\"display: none;\"><img src=\"/img/loading_indicator.gif\" alt=\"Loading Indicator Graphic\" /></div></div><div id=\"contactDropDownSelectedItem\" class=\"dropdown-selectedItem\" style=\"display:none; position: absolute; top: 28px; left: 2px;\">');
			hideContactList();
		}
		hideCountryList();
	}
	function contactListItemSelected(selectedContact, url){
	
		if (g_selectedContact != selectedContact)
		{
			g_selectedContact = selectedContact;

			if (g_selectedContact)
			{
				$('goButton').setAttribute('href', url);
				$('goButton').setAttribute('title', 'Link to ' + selectedContact + ' - Opens in new window');
				$('goButton').setAttribute('target', '_blank');
			}
			else
			{
				$('goButton').setAttribute('href', '#');
				$('goButton').setAttribute('title', '');
				$('goButton').setAttribute('target', '');
			}
		}
		
		hideContactList();
	}
	function hideCountryList(){
		if (g_selectedCountry)
		{
			Element.update('countryDropDownSelectedItem', g_selectedCountry);
			Element.setStyle($('countryDropDownSelectedItem'), {width: Element.getWidth('countryDropDownLink')-10 + 'px'}); 
	
			Element.show('countryDropDownSelectedItem');
		}
		else
		{
			Element.update('countryDropDownSelectedItem', "");
			Element.hide('countryDropDownSelectedItem');
		}
		Element.hide('countryDropDownList');
		g_countryListOpen = false;
	}
	function hideContactList(){		
		if (g_selectedContact)
		{
			Element.update('contactDropDownSelectedItem', g_selectedContact);
			Element.setStyle($('contactDropDownSelectedItem'), {width: Element.getWidth('contactDropDownLink')-10 + 'px'}); 
			
			Element.show('contactDropDownSelectedItem');
		}
		else
		{
			Element.update('contactDropDownSelectedItem', "");
			Element.hide('contactDropDownSelectedItem');
		}
		Element.hide('contactDropDownList');
		
		g_contactListOpen = false;
	}
	
	// 24/5 - Link selection for Business Partner page ...
	function sb_countryListItemSelected(country,url)
	{
	    //alert("Test");
		
		g_selectedCountry = country;
		
		$('goButton').setAttribute('href', url);
		$('goButton').setAttribute('title', 'Link to selected country - Opens in new window');
		$('goButton').setAttribute('target', '_blank');
		hideCountryList();
	}
	
	// hide countryDropDownList, if JS is activated
	Element.hide('countryDropDownList');
