var xmlHttp;

function pageLoad()
{
	showButtons(pageCategory());
	selectButton(pageCategory());
	
	var pageHeight = document.height;
	var headerHeight = document.getElementById('header').offsetHeight;
	var spacerHeight = document.getElementById('pageSpacer').offsetHeight;
	var bodyHeight = document.getElementById('body').offsetHeight;
	var marginTop = (pageHeight - (headerHeight + spacerHeight + bodyHeight))/2;
	document.getElementById('header').style.marginTop= marginTop + 'px';
}

function pageResize()
{
	window.location.href = window.location.href
}

function pageCategory()
{
	var path = document.location.href;
	var siteLoc = path.lastIndexOf('/') + 1;
	var siteCode = path.substr(siteLoc, 3);
	
	var dir = path.substr(0, (siteLoc - 1));
	var cityLoc = dir.lastIndexOf('/') + 1;
	var cityCode = dir.substr(cityLoc, 3);
	
	if (siteCode == "")
	{
		siteCode = "ind";
	}
	
	if (cityCode == "gre" || cityCode == "www")
	{
		var category = siteCode;
	}
	else
	{
		var category = cityCode + ":" + siteCode;
	}
	
	if (category == "ind")
	{
		category = null;
	}
	
	return category;
}

function selectButton(id)
{
	if (id == null)
	{
		return;
	}

	var element = document.getElementById(id);
	
	var leaf = true;

	while (element.id != "nav-panel")
	{
		var divElement = element.getElementsByTagName('div')[1];
		var elementClass = divElement.getAttribute('class');
		
		if (leaf)
		{
			var selectedClass = elementClass + "Selected";
			leaf = false;
			
			divElement.setAttribute('class', selectedClass);
		}
		else
		{
			;
		}
		
		element = element.parentNode;
	}
}

function showButtons(id)
{
	if (id == null)
	{
		return;
	}

	var element = document.getElementById(id);
	
	element.style.cssText = 'display: block';
	//element.setAttribute('style', 'display: block');
	
	var children = element.childNodes;
	var size = children.length;
	
	var spanChildren = 0;
	
	for (var i = 0; i < size; i++)
	{
		var child = element.childNodes[i];
		
		if (child.nodeName == 'SPAN')
		{
			child.style.cssText = 'display: block';
			//child.setAttribute('style', 'display: block');
			
			spanChildren++;
		}	
	}
	
	if (element.parentNode.id != "nav-panel")
	{
		if (spanChildren != 0)
		{
			showAncestors(element.parentNode.id);
		}
		else
		{	
			showParent(element.parentNode.id);
		}
	}
}

function showParent(id)
{
	var element = document.getElementById(id);
	
	element.style.cssText = 'display: block';
	//element.setAttribute('style', 'display: block');
	
	var children = element.childNodes;
	var size = children.length;

	for (var i = 0; i < size; i++)
	{
		var child = element.childNodes[i];
		
		if (child.nodeName == 'SPAN')
		{
			child.style.cssText = 'display: block';
			//child.setAttribute('style', 'display: block');
		}	
	}
	
	if (element.parentNode.id != "nav-panel")
	{
		showAncestors(element.parentNode.id);
	}
}

function showAncestors(id)
{
	var element = document.getElementById(id);
	
	element.style.cssText = 'display: block';
	//element.setAttribute('style', 'display: block');
	
	if (element.parentNode.id != "nav-panel")
	{
		showAncestors(element.parentNode.id);
	}
}

function updatePaypalButton(region, buttonDiv)
{	
	var xmlHttp;
	xmlHttp = GetXmlHttpObject();

	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url = "paypalButtonService.php";
	url = url + "?id=" + buttonDiv + "&region=" + region;
	xmlHttp.onreadystatechange = function() { stateChanged(xmlHttp, buttonDiv); };
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function stateChanged(xmlHttp, id) 
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{
		if (xmlHttp.status == 200)
		{
			document.getElementById(id).setAttribute("value", xmlHttp.responseText);
		}
	}
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
 			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	return xmlHttp;
}