var browserType = "";
var associateInsurers = new Array();
var insurerInfoLocation = "insurer_info";
var pendingListBuildRequest = false;


function associateInsurer(companyName,country,menuEntry,pageName) {
	this.companyName = companyName;
	this.country = country;
	this.menuEntry = menuEntry;
	this.pageName = pageName;
}


function getAssociateInsurers() {

	//for DOM capable browsers
	if (document.implementation && document.implementation.createDocument) {

		associateInsurerXml = document.implementation.createDocument("", "", null);
		associateInsurerXml.onload = processLoad;

	} else if (window.ActiveXObject) { //MS IE

		browserType = "msie";
		associateInsurerXml = new ActiveXObject("Microsoft.XMLDOM");
		associateInsurerXml.async = false;
		associateInsurerXml.onreadystatechange = processLoad;

	} else {

		alert("To use this page you need a browser capable of loading XML documents. Microsoft Internet Explorer 5 or greater or Netscape Navigator 6.2 are recommended.");
		return;

	}

	associateInsurerXml.load("associate_insurers.xml");
}



function processLoad() {

	if (browserType == "msie" && associateInsurerXml.readyState != 4) {
		return false;
	}

	allAssociateInsurers = associateInsurerXml.getElementsByTagName("associateInsurer");

	for (var i = 0; i < allAssociateInsurers.length; i++) {

		var companyName = allAssociateInsurers[i].getElementsByTagName("companyName");
		companyName = companyName[0].firstChild.nodeValue;
		var country = allAssociateInsurers[i].getElementsByTagName("country");
		country = country[0].firstChild.nodeValue;
		var menuEntry = allAssociateInsurers[i].getElementsByTagName("menuEntry");
		menuEntry = menuEntry[0].firstChild.nodeValue;
		var pageName = allAssociateInsurers[i].getElementsByTagName("pageName");
		pageName = pageName[0].firstChild.nodeValue;
		associateInsurers[pageName] = new associateInsurer(companyName,country,menuEntry,pageName);

	}
	
	if (pendingListBuildRequest == true) {
		showAssociateInsurerList();
	}
	
	return true;			

}


function getInformationLocation() {
	return document.getElementById(insurerInfoLocation);
}



function showAssociateInsurer(pageName) {

	var insurerInfo = getInformationLocation();

	if (insurerInfo != null) {
		insurerInfo.innerHTML = buildInsurerInfo(pageName);
	}

	return true;

}



function clearAssociateInsurer() {

	var insurerInfo = getInformationLocation();

	if (insurerInfo != null) {
		insurerInfo.innerHTML = "Select a Country";
	}

	return true;

}



function buildInsurerInfo(pageName) {
	var insurerInfo = "No information available.";			
	insurerInfo = associateInsurers[pageName].country + ": " + associateInsurers[pageName].companyName;
	return insurerInfo;
}



function getAssociateInsurerUrl(id) {

	var aiUrl = null;

	if (associateInsurers[id].pageName) {
		aiUrl = "/igpinfo/shared/network/" + associateInsurers[id].pageName + ".html";
	}

	return aiUrl;
}



function getAssociateInsurer() {
    
    var countryDropDown = document.getElementById("select_country");
    link = countryDropDown.options[countryDropDown.selectedIndex].value;

	if (link != null && link != "") {
	   window.location = "/igpinfo/shared/network/" + link;
	}

}


function stripExtension(name) {
	var newName = name;
	newName = newName.split("*TCN");
	newName = newName[0];
	return newName;
}


function showAssociateInsurerList() {

	var associateInsurerList = document.getElementById("select_country");
	var pageName = "";
	var insurerCountry = "";
	var associateInsurerCount = 0;
	var isSelected = false;
	
	pendingListBuildRequest = false;
	
	if (associateInsurerList != null) {

		for (var associate in associateInsurers) {

			pageName = associateInsurers[associate].pageName;
			
			if (pageName.indexOf("*TCN") != -1) {
				pageName = stripExtension(pageName);
			}
			
			pageName += ".html";
			
			if (window.location.href.indexOf(pageName) != -1) {
			   isSelected = true;   
			}
			
			menuEntry = associateInsurers[associate].menuEntry;
			addOption(associateInsurerList,menuEntry,pageName,isSelected);
			associateInsurerCount ++;
			isSelected = false;
		}

	}
	
	if (associateInsurerCount == 0) {
		pendingListBuildRequest = true;
	}
	
	if (window.location.href.indexOf("index.") == -1 && window.location.href.indexOf(".html") != -1 && window.location.href.indexOf("smg_life.html") == -1) {
		associateInsurerList.selectedIndex = associateInsurerList.selectedIndex + 1;
	}
	
	return true;

}


//create a new option in the selectObject with optionText and optionValue as its values
function addOption(selectObject,optionText,optionValue,isSelected) {
	
	var newOption = new Option(optionText, optionValue, false, isSelected);
	selectObject.options[selectObject.length] = newOption;

}


function showZoomedEurope() {
	document.getElementById("europe_on").style.visibility = "visible";
	return true;
}


//check for the mouseout event on the element itself because IE fires events for the other content of the <div> - which confuses the script.
function smartHide(outElement) {

	if (!outElement.contains(event.toElement)) {
		outElement.style.visibility = "hidden";
		return true;
	}

return false;
}
