currRegion = "";

// Usually, the string will be the URL search string
function assignKeyValuePairsFromString( source )
{
	var idx = -1;
	var query = "";
	var srch = "";
	var pairs = null;
	
	if ( (idx = source.indexOf('?')) == 0)
		srch = source.substr(1);
	else
		srch = source;

	query = srch.substr(idx);
	pairs = query.split('&');

//	alert( "idx = " + idx + "\nquery = " + query + "\npairs = " + pairs );

	for (i=0;i<pairs.length;i++)
	{
		keyValue = pairs[i].split('=');
		if (keyValue.length == 1)
			continue;

		key   = keyValue[0];
		value = keyValue[1];

		eval( key + " = '" + value + "' " );
	}
}

function appendRegionToLink( theLink, theRegion )
{
	var idx    = -1;
	var base   = "";
	var source = theLink.href;
		
	if ( (idx = source.indexOf('?')) > 0)
		base = source.substr(0, idx);

	else
		base = source;

	if ( theRegion != null )
		currRegion = theRegion;

	// Cannot append since it might repeat the key/value pair
	// Note any other arguments will be lost
	theLink.href = base + "?region=" + currRegion;
	return true;
}

function buildBackToRegion()
{
	var htmlStr;
	htmlStr  = "<a class='home' ";
	htmlStr += "href='" + relatedLinks[ currRegion ][0]['link'] + "' ";
	htmlStr += "onClick='appendRegionToLink( this, null );'"
	htmlStr += ">";
	htmlStr += "Return to " + relatedLinks[ currRegion ][0]['text'] + "</a>";
	document.write( htmlStr );
}

// Parse the URL and update the region
assignKeyValuePairsFromString( document.location.search );
currRegion = region;
//currRegion = ( typeof region == "undefined" ) ? "Unknown" : region;

/**
alert( "currRegion = " + currRegion );
alert( "relatedLinks = " + relatedLinks );
alert( "relatedLinks[ currRegion ] = " + relatedLinks[ currRegion ] );
alert( "relatedLinks[ currRegion ][0] = " + relatedLinks[ currRegion ][0] );
**/

// What if the region is not defined? [[ Not Implemented Yet ]]
if ( relatedLinks &&  relatedLinks[ currRegion ]  && relatedLinks[ currRegion ][0] )
{
	regionText = relatedLinks[ currRegion ][0]['text'];
	regionLink = relatedLinks[ currRegion ][0]['link'];
}

else
{
	regionText = "[Not Specified]"
	regionLink = "ServicesDynamic.html?region=error"
}
