function CleanName(nameToClean)
{
	// Remove spaces, brackets and slashes
	var cleanedName = nameToClean.replace(/ /g,'');
	cleanedName = cleanedName.replace(/\(/g,'');
	cleanedName = cleanedName.replace(/\)/g,'');
	cleanedName = cleanedName.replace(/\//g,'');

	return cleanedName

}

function loadCountryList()
 {
	var country = document.getElementById(selCountry);

	// Add a 'please select' option
	var newOpt = new Option('Please select','0');
	country.options[0] = newOpt;
	
	// Fill the drop down with the data from the array
	for (var i = 0 ; i < arrCountries.length ; i++) { 
		var newOpt = new Option(arrCountries[i][0], arrCountries[i][1]);
		country.options[i+1] = newOpt;
		// If this is the users country, preselect it
		if (arrCountries[i][2] == IPCode)
		{
			country.options[i+1].selected = "true";
		}
	}
	
	//Now load the segment list
	loadSegmentList();

	// Disable the segment and action lists if a country is not selected
	if (country.selectedIndex == 0)
	{
		var segList = document.getElementById(selSegment);
		segList.disabled = 'true';
		var ActionList = document.getElementById(selAction);
		ActionList.disabled = 'true';
	}

}

function loadSegmentList()
 {
	var country = document.getElementById(selCountry);
	var countryname=country.options[country.selectedIndex].text; 
	var segList = document.getElementById(selSegment);
	var ActionList = document.getElementById(selAction);

	// remove all existing entries from the segment selector
	for(i=segList.length-1; i>=0; i--){
		segList.options[i] = null;
	}

	// Add a 'please select' option
	var newOpt = new Option('Please select','0');
	segList.options[0] = newOpt;
	
	try
	{
		// evaluate the name of the array which contains the data to fill the drop down. Remove spaces and brackets
		var arrlist = eval('arr'.concat(CleanName(countryname)));
		var urllist = eval('arr'.concat(CleanName(countryname)).concat('Urls'));
	}
	catch (e)
	{
		// Disable segment and actionlist
		segList.disabled = 'true';
		ActionList.disabled = 'true';
		return;
	}


	// Fill the drop down with the data from the array
	for (var i = 0 ; i < arrlist.length ; i++) { 
		newOpt = new Option(arrlist[i], urllist[i]);
		segList.options[i+1] = newOpt;
		// If this is the users segment, preselect it
		if (arrlist[i] == ProductName)
		{
			segList.options[i+1].selected = "true";
		}
	}

	// Now load the action list as well
	loadActionList();

	// Enable the segment list only if the user has selected a country
	if (country.selectedIndex != 0)
	{
		segList.disabled = '';
	}

	// Enable the action list only if the user has selected a segment
	if (segList.selectedIndex != 0)
	{
		ActionList.disabled = '';
	}else{
		ActionList.disabled = 'true';
	}


}

function loadActionList()
 {
	var country = document.getElementById(selCountry);
	var countryname=country.options[country.selectedIndex].text; 
	var segment = document.getElementById(selSegment);
	var segmentIndex=segment.selectedIndex; 
	var ActionList = document.getElementById(selAction);

	// remove all existing entries from the action selector
	for(i=ActionList.length-1; i>=0; i--){
		ActionList.options[i] = null;
	}

	// Add a 'please select' option
	var newOpt = new Option('Please Select','0');
	ActionList.options[0] = newOpt;

	try
	{
		// evaluate the name of the arrays which contains the data to fill the drop downs.
		var arrlist = eval('arr'.concat(CleanName(countryname)).concat('ActionText').concat(segmentIndex));
		var urllist = eval('arr'.concat(CleanName(countryname)).concat('ActionUrl').concat(segmentIndex));
	}
	catch (e)
	{
		// Disable the action list
		ActionList.disabled = 'true';
		return;
	}


	// Fill the drop down with the data from the arrays (text & urls)
	for (var i = 0 ; i < arrlist.length ; i++) { 
		var newOpt = new Option(arrlist[i], urllist[i]);
		ActionList.options[i+1] = newOpt;
	}

	//Enable the action list only if the user has selected a segment
	if (segment.selectedIndex != 0)
	{
		ActionList.disabled = '';
	}


}

// Try each selector in turn, redirecting to the last url selected.
function redirect()
{
	// Check they have selected a country at least, otherwise alert and return
	var Action = document.getElementById(selCountry);
	var ActionUrl=Action.options[Action.selectedIndex].value; 
	if (ActionUrl == '0')
	{
		alert("You must select a country");
		return;
	}

	// Save the users selection in webtrends
	dcsMultiTrack('WT.ti','Products and Services selector redirect to local website','DCS.dcsuri','/main/home/prodservices_selector.htm','WT.es',window.location);

	/*if (confirm("You are now leaving www.zurich.com. Thank you for visiting us.") == false)
	{
		return;
	} */

	// Now we have the country url, save it.
	var redirectUrl = ActionUrl;
	
	try
	{
		// Now try the segment selector.
		Action = document.getElementById(selSegment);
		ActionUrl=Action.options[Action.selectedIndex].value; 
	}
	catch (e)
	{
		// Can't read the segment for some reason (probably missing data), so redirect to the country url saved earlier
		window.location = redirectUrl;
		return;
	}

	// Make sure it's not the 'please select' item.
	if (ActionUrl != '0')
	{
		// Now we have the segment url, save it.
		redirectUrl = ActionUrl;
	}

	try
	{
		// next try to read the third 'ActionList' selector.
		Action = document.getElementById(selAction);
		ActionUrl=Action.options[Action.selectedIndex].value; 
	}
	catch (e)
	{
		// Can't read the actionlist for some reason (probably missing data), so redirect to the segment url saved earlier
		window.location = redirectUrl;
		return;

	}
	// Make sure it's not the 'please select' item.
	if (ActionUrl != '0')
	{
		// Now we have the segment url, save it.
		redirectUrl = ActionUrl;
	}

	// Alert in for testing purposes.
	//alert('Redirecting to - ' + redirectUrl);
	window.location = redirectUrl;

}


