// create the mam namespace if it does not already exist
if(mam == undefined) var mam = {};
// requires mam.core.js
if(mam.hub == undefined)
{
mam.hub = new function() {
	var that = this;
	// Public Methods
	jQuery.extend(this,  {
		version: "1.0",
		config: mam.config,
		indexHandler: function() {
			if(!mam.util.env.isDefaultHost()) {
				mam.config.initialize(function() {
					crossDomainCookieTransfer();
					forwardToHomePage();
				}, {
					controlXml: true,
					siteConfigXml: false,
					siteSelectorXml: false
				});
			} else {
				if(loadCookie()) {
					mam.config.initialize(function() {
						var external = false;
						var pathname = "";
						if(isExternalSite()) {
							external = true;
						}
						forwardToHomePage({host: getSiteHost(), pathname: pathname, external: external});
					}, {
						controlXml: true,
						siteSelectorXml: false
					});
				} else {
					forwardToHub();
				}
			}
		},
		hubHandler: function() {
			if(!mam.util.env.isDefaultHost()) {
				forwardToHub();
			} else {
				mam.page.loadPage({
					title: "",
					separator: ""
				});
				mam.config.initialize(function() {
					$(document).ready(function() {
						buildSiteSelector();
					});
				}, {controlXml: true,siteConfigXml: true});
			}
		},
		pageHandler: function() {
			var excludedPage = false;
			var includedPage = false;
			var path = mam.util.url.getPath();
			var excludedPagesRegex = [
				"/core/.*",
				"/core/important_information/.*",
				"/core/hub/hub.html",
                                "/"
			];
			var includedPagesRegex = [
				"/core/home/home.html"
			];
			$.each(excludedPagesRegex, function(i,n){
				var re = new RegExp(n);
				if(re.test(path)) excludedPage = true;
			});
			$.each(includedPagesRegex, function(i,n){
				var re = new RegExp(n);
				if(re.test(path)) includedPage = true;
			});
			// alert("excludedPage:"+excludedPage+", includedPage:"+includedPage);
			// alert(importantInfoConfirmed());
			if(!excludedPage || includedPage) { // allow navigation to excluded pages without visiting the hub. Inclusions override exclusions
				if(!importantInfoConfirmed()) {
					// alert("forward to important info");
					forwardToImportantInfo();
				}
			}
			
			// Call core page handler
			var callCorePageHandler = false;
			var regex = [
				"/core/mellons_boutiques/.*",
				"/core/specialist_asset_managers/.*"
			];
			$.each(regex, function(i,n){
				var re = new RegExp(n);
				if(re.test(path)) callCorePageHandler = true;
			});
			if(callCorePageHandler) mam.hub.corePageHandler();

		},
		importantInfoHandler: function() {
				// alert("importantInfoHandler...");
				$(document).ready(function() {
						// load investor type specific text
						if(loadCookie()) {
							var importantInformationText = getInvestorType(clientType, locations[location].investorTypes[clientType]).importantInformationText;
							if(importantInformationText != undefined && importantInformationText != "") $("#investor-type-specfic").html(importantInformationText);
						} else {
							// Do nothing
						}

						if(loadImportantInfoCookie()) {
							$("#importantInformationCheckbox").get(0).checked = importantInformation.confirmed;
						}
						$("#go-button").click(function(){
								$(".formError").remove(); // clear errors
								// alert("go");
								if($("#importantInformationCheckbox").get(0).checked) {
									importantInformation.confirmed = true;
									storeImportantInfoCookie();
									// alert("forward to home");
									forwardToHomePage();
								} else {
									clearImportantInfoCookie();
									$("fieldset.importantInformation").append('<p class="formError">You must agree to the terms and conditions.</p>')
								}
								return false;
						});
				});
			// load site specifc Important Infomation
				mam.config.initialize(function() {
					$(document).ready(function() {
						// alert(mam.util.path.getSiteImportantInfoPath());
						$("#site-specific-important-info").load(mam.util.path.getSiteImportantInfoPath());
					});
				}, {controlXml: true});
		},
		corePageHandler: function() {
			// alert("1");
			if(!loadCookie() && mam.util.env.isDefaultHost()) { // Show site selector if visitor has not already chosen a site.
				mam.config.initialize(function() {
					$(document).ready(function() {
						buildSiteSelector();
					});
				}, {
					controlXml: true,
					siteConfigXml: true,
					siteSelectorXml: false
				});
			}
		},
		getStatutoryLiteratureTags: function() {
			loadCookie();
			// alert(location);
			return getStatutoryLiteratureTags(location);
		},
		getLanguageTags: function() {
			loadCookie();
			// alert(location);
			return getLanguageTags(location);
		},
		allNonEnglishLanguageTags: function() {
			var tags = new Array();
			for (var i in locations) {
				if(locations[i]['languageTags'] != undefined)
				{
					for(var x = 0; x < locations[i]['languageTags'].length; x++)
					{
						var l = locations[i]['languageTags'][x];
						if(l.toLowerCase() != 'english')
						{
							tags[tags.length] = l;
						}
					}
				}
			}
			return tags;
		}
	});
	var siteSelectorPath = "/core/hub/hub.html";
	var homePagePath = "/home/home.html";
	
	var crossDomainCookieTransfer = function() {
		// get location and investor type from query string if present and store in a cookie
		// alert(window.location.search);
		var query = window.location.search.substring(1);
		// alert("query: "+query);
		var pArray = query.split("&");
		var queryParms = {};
		// alert("parameters length:"+ parameters.length);
		$.each(pArray, function(i,n){
			var pos = n.indexOf('=');
			if (pos > 0) {
				var key = n.substring(0,pos);
				var val = n.substring(pos+1);
				queryParms[key] = val;
			}
		});
		if(queryParms.location != undefined) {
			location = queryParms.location;
			clientType = queryParms.investorType;
			remember = queryParms.remember; // makes it work in TeamSite
			storeCookie();
			forwardToHomePage({host: getSiteHost(), query: ""}); // we have stored the location and investor type info now so forward to home page without the query string.
		}
	};
	
	var locations = {
		empty: {name: "Please choose your location", value: "empty", investorTypes: {}},
		australia: {
			name: "Australia",
			value: "australia", 
			investorTypes: {
				institutional: {site: "australia"},
				wholesale: {site: "australia"}
			}
		},
		austria: {
			name: "Austria",
			value: "austria", 
			investorTypes: {
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			},
			statutoryLiteratureTags: ["austria"],
			languageTags: ["austrian_german"]
		},
		belgium: {
			name: "Belgium", 
			value: "belgium", 
			investorTypes: {
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			}
		},
		brazil: {
			name: "Brazil",
			value: "brazil", 
			investorTypes: {
				institutional: {site: "brazil"},
				wholesale: {site: "brazil"},
				highNetWorthIndividual: {site: "brazil"}
			}
		},
		canada: {
			name: "Canada", 
			value: "canada", 
			investorTypes: {
				institutional: {
					site: "asset-management-us",
					description: "Institutional investors and their consultants - This site provides information on investment strategies suitable for institutional investors, such as corporations, public plans/governments, unions, foundations, endowments and sub-advisory relationships."
				},
				northAmericaNonResidentAlienAdvisory: {site: "international-wholesale"}
			}
		},
		chile: {
			name: "Chile", 
			value: "chile", 
			investorTypes: {
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			}
		},
		denmark: {
			name: "Denmark", 
			value: "denmark", 
			investorTypes: {
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			}
		},
		finland: {
			name: "Finland", 
			value: "finland", 
			investorTypes: {
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			}
		},
		france: {
			name: "France", 
			value: "france", 
			investorTypes: {
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			},
			statutoryLiteratureTags: ["france"],
			languageTags: ["french"]
		},
		germany: {
			name: "Germany", 
			value: "germany", 
			investorTypes: {
				individual: {site: "west-lb"},
				institutional: {site: "west-lb"},
				wholesale: {site: "west-lb"}
			}
		},
		ireland: {
			name: "Ireland", 
			value: "ireland", 
			investorTypes: {
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			},
			statutoryLiteratureTags: ["ireland"]
		},
		italy: {
			name: "Italy", 
			value: "italy", 
			investorTypes: {
				individual: {site: "italian-microsite"},
				institutional: {site: "italian-microsite"},
				wholesale: {site: "italian-microsite"}
			},
			statutoryLiteratureTags: ["italy"],
			languageTags: ["italian"]
		}, 
		japan: {
			name: "Japan", 
			value: "japan", 
			investorTypes: {
				institutional: {site: "japan"}
			}
		},
		luxembourg: {
			name: "Luxembourg", 
			value: "luxembourg", 
			investorTypes: {
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			}
		},
		netherlands: {
			name: "Netherlands", 
			value: "netherlands", 
			investorTypes: {
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			},
			statutoryLiteratureTags: ["netherlands"]
		},
		norway: {
			name: "Norway", 
			value: "norway", 
			investorTypes: {
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			}
		},
		portugal: {
			name: "Portugal", 
			value: "portugal",
			investorTypes: {
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			},
			statutoryLiteratureTags: ["portugal"],
			languageTags: ["portuguese"]
		},
		spain: {
			name: "Spain", 
			value: "spain", 
			investorTypes: {
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			},
			statutoryLiteratureTags: ["spain"],
			languageTags: ["spanish"]
		},
		sweden: {
			name: "Sweden", 
			value: "sweden", 
			investorTypes: {
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			}
		},
		switzerland: {
			name: "Switzerland", 
			value: "switzerland", 
			investorTypes: {
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			},
			statutoryLiteratureTags: ["switzerland"],
			languageTags: ["german"]
		},
		uk: {
			name: "United Kingdom", 
			value: "uk", 
			investorTypes: {
				individual: {site: "newton-public"},
				ifa: {site: "newton-ifa"},
				institutional: {site: "institutional"},
				wholesale: {site: "international-wholesale"}
			}
		},
		us: {
			name: "United States", 
			value: "us", 
			investorTypes: {
				institutional: {
					site: "asset-management-us",
					description: "Institutional investors and their consultants - This site provides information on investment strategies suitable for institutional investors, such as corporations, public plans/governments, unions, foundations, endowments and sub-advisory relationships."
				},
				individual: {
					site: "dreyfus",
					name: "Retail investors and their investment advisors",
					description: "Retail investors and their investment advisors - This site provides information on investment strategies and vehicles for both the no-load and advisor channels."
				},
				highNetWorthIndividual: {site: "private-wealth"},
				northAmericaNonResidentAlienAdvisory: {site: "international-wholesale"}
			}
		}
	};
	
	var getStatutoryLiteratureTags = function(p_location) {
		var defaultTags = ["uk_chile_peru"];
		var location = locations[p_location];
		if(location == undefined || location.statutoryLiteratureTags == undefined) {
			return defaultTags;
		} else {
			return location.statutoryLiteratureTags;
		}
	};

	var getLanguageTags = function(p_location) {
		var defaultTags = ["english"];
		var location = locations[p_location];
		if(location == undefined || location.languageTags == undefined) {
			return defaultTags;
		} else {
			return location.languageTags;
		}
	};

	var investorTypeDetails = {
			individual: {
				name: "Individual Investors",
				value: "individual",
				description: "Individual Investors - This site provides information on funds for direct investment by private individuals.",
				importantInformationText: "am an individual investor"
			},
			highNetWorthIndividual: {
				name: "High net worth individual",
				value: "highNetWorthIndividual",
				description: "High net worth individual - This site provides information on various services provided to high net worth individuals and private offices.",
				importantInformationText: "am a high net worth individual"
			},
			northAmericaNonResidentAlienAdvisory: {
				name: "North America - Non Resident Alien (NRA) Advisory",
				value: "northAmericaNonResidentAlienAdvisory",
				description: "North America - Non Resident Alien (NRA) Advisory - The site provides information for Advisors to NRA&#39;s on offshore mutual funds.",
				importantInformationText: "am a North America - Non Resident Alien (NRA) Advisor"
			},
			institutional: {
				name: "Institutional Investors and their Consultants",
				value: "institutional",
				description: "Institutional Investors and their Consultants - This site provides information on investment strategies suitable for institutional investors, such as pension funds, global corporations, investment & pension consultants, insurance companies and endowment plans.",
				importantInformationText: "am an institutional investor or consultant"
			},
			wholesale: {
				name: "Professional Mutual Fund Investors, Discretionary Asset Managers and Distributors",
				value: "wholesale",
				description: "Professional Mutual Fund Investors, Discretionary Asset Managers and Distributors - This site provides information on funds for investment and distribution throughout the world.",
				importantInformationText: "am a Professional Mutual Fund Investor, Discretionary Asset Manager or Distributor"
			},
			ifa: {
				name: "UK Independent Financial Advisors",
				value: "ifa",
				description: "UK Independent Financial Advisors - This site provides information on UK domiciled funds.",
				importantInformationText: "am a UK Independent Financial Advisor"
			}
		};

	var getInvestorType = function(id, investorType) {
		var defaultInvestorType = investorTypeDetails[id];
		jQuery.extend(defaultInvestorType, investorType);
		// alert(defaultInvestorType.site+"\n"+defaultInvestorType.name);
		return defaultInvestorType;
	};

	var HUB_COOKIE = "hub2"; // Name of the hub cookie
	var location = "";
	var clientType = "";
	var remember = "";
	var cookieExists = {};
	var importantInformation = {};
	importantInformation.confirmed = false;
	var importantInfoConfirmed = function() {
		// alert("cookie: "+loadImportantInfoCookie()+", confirmed: "+importantInformation.confirmed);
		return (loadImportantInfoCookie() && importantInformation.confirmed);
	};
	
	// Private Methods
	var getSite = function() {
		// var site = investorTypes[clientType].locations[location].site;
		var site = locations[location].investorTypes[clientType].site;
		// alert("getSite: "+site);
		if(site != undefined && site != "") {
			return site;
		} else { // no match return default
			return "default";
		}
	};
	var clearCookie = function() {
		var cookie = new Cookie(document, HUB_COOKIE, 0 , "/", null, false);
		cookie.remove();
	};
	var forwardToHub = function(p) {
		var parms = {
				host: mam.util.env.getDefaultHost(),
				preserveHistory: false,
				test: false,
				pathname: siteSelectorPath
		}; // defaults 
		jQuery.extend(parms, p);
		forwardToPage(parms);
	};
	var forwardToImportantInfo = function(p) {
		var parms = {
				host: "",
				preserveHistory: false,
				test: false,
				pathname: "/core/important_information/important_information.html"
		}; // defaults 
		jQuery.extend(parms, p);
		forwardToPage(parms);
	};
	var forwardToHomePage = function(p) {
		var parms = {
				host: "",
				preserveHistory: false,
				test: false,
				pathname: mam.util.path.getSitePath() + homePagePath
		}; // defaults
		jQuery.extend(parms, p);
		forwardToPage(parms);
	};
	var forwardToPage = function(p) {
		var url;
		var parms = {
				host: "",
				preserveHistory: false,
				test: false,
				pathname: "default.html"
		}; // defaults 
		jQuery.extend(parms, p);
		// alert("forwardToPage: host: "+parms.host+"\npreserveHistory: "+parms.preserveHistory+"\ntest: "+parms.test+"\nexternal: "+parms.external);
		url = mam.util.url.constructURL({
			host: parms.host,
			pathname: parms.pathname,
			query: parms.query,
			external: parms.external
		});
		mam.util.url.forward({
			url: url,
			preserveHistory: parms.preserveHistory,
			test: parms.test,
			external: parms.external
		});
	};
	var getSiteHost = function(site) {
		var host;
		var hostElements = getHostElements();
		if(hostElements != null) {
			var host = hostElements.get(0).getAttribute("name");
		} else {
			host = "";
		}
		// alert("getSiteHost: "+host);
		return host;
	};
	var isExternalSite = function(site) {
		var external = false;
		var hostElements = getHostElements(site);
		if(hostElements != null) {
			if("true" == hostElements.get(0).getAttribute("external")) {
				external = true;
			}
		}
		// alert("isExternalSite: "+external);
		return external;
	};
	var getHostElements = function(site) {
		var siteElements;
		var hostElements = null;
		if(mam.config.getControlXml() != undefined) {
			if(site == undefined) site = getSite();
			// alert("method: getSiteHost\n\nsite: "+site+"\n\nenv: "+mam.util.getEnvName()+"\n\nlocation: "+this.location+"\n\nclientType: "+this.clientType);
			siteElements = mam.util.dom.getElementsByAttribute($("site", mam.config.getControlXml()), "id", site);
			if(siteElements.size() == 0) {site = "default";} // if site not found, try default
			siteElements = mam.util.dom.getElementsByAttribute($("site", mam.config.getControlXml()), "id", site);
			hostElements = mam.util.dom.getElementsByAttribute($("host", siteElements), "env", mam.util.env.getEnv());
		}
		return hostElements;
	};
	var loadCookie = function() {
		var cookie = new Cookie(document, HUB_COOKIE, 8760 , "/", null, false);
		if(cookie.load()) {
			cookieExists.hub = true;
			location = cookie.location;
			clientType = cookie.clientType;
			remember = cookie.remember;
		} else {
			cookieExists.hub = false;
		}
		// alert("loadCookie: "+cookieExists.hub);
		return cookieExists.hub;
	};
	var storeCookie = function() {
		var cookie = new Cookie(document, HUB_COOKIE, 8760 , "/", null, false);
		cookie.location = location;
		cookie.clientType = clientType;
		cookie.remember = remember;
		cookie.store();
	};
	var loadImportantInfoCookie = function() {
		var cookie = new Cookie(document, "importantInfo", 8760 , "/", null, false);
		if(cookie.load()) {
			cookieExists.importantInfo = true;
			importantInformation.confirmed = cookie.confirmed;
			// alert(importantInformation.confirmed);
		} else {
			cookieExists.importantInfo = false;
		}
		return cookieExists.importantInfo;
	};
	var storeImportantInfoCookie = function() {
		var cookie = new Cookie(document, "importantInfo", 8760 , "/", null, false);
		cookie.confirmed = importantInformation.confirmed;
		cookie.store();
	};
	var clearImportantInfoCookie = function() {
		var cookie = new Cookie(document, "importantInfo", 0 , "/", null, false);
		cookie.remove();
	};
	var setLocation = function(p_location) {
		location = p_location;
	};
	var setClientType = function(p_clientType) {
		clientType = p_clientType;
	};
	var setRemember = function(p_remember) {
		remember = p_remember;
	};
	var validate = function() {
		// alert("Validate");
		var messages = new Array();
		var valid = true;
		$("div.siteSelector .messages").empty();
		if($("#location").val() == "" || $("#location").val() == "empty"){
			valid = false;
			messages[messages.length] = "Please choose your location";
		} 
		if($("input[@name=investorType][@checked]").val() == "" || $("input[@name=investorType][@checked]").val() == undefined) {
			valid = false;
			messages[messages.length] = "Please choose your investor type";
		}
		if(valid) {
			return go();
		} else {
			if(messages.length > 0) {
				$("div.siteSelector .messages").append('<p class="error">'+messages[0]+'</p>');
				// alert(messages[0]);
			}
			return false;
		}
	};
	var go = function() {
		setLocation($("#location").val());
		setClientType($("input[@name=investorType][@checked]").val());
		setRemember($("#remember").get(0).checked);
		// alert(clientType+":"+location);
		// alert(investorTypes[clientType].locations[location].site);
		if(remember) storeCookie(); else {clearCookie();}
		var external = false;
		var pathname = "";
		var query = "";
		if(!isExternalSite()) {
			// pathname = mam.util.path.getSitePath() + homePagePath;
			query = "location="+location+"&investorType="+clientType+"&remember="+remember;
			// alert(query);
		} else {
			pathname = "";
			external = true;
		}
		forwardToHomePage({host: getSiteHost(), preserveHistory: true, pathname: pathname, query: query, external: external});
		return true;
	};
	var buildSiteSelector = function() {
		//alert("Building site selector...");
		if($("div.siteSelector").size() == 0) $("#main-content").append("<div class=\"siteSelector\"></div>");
		$("div.siteSelector").each(function(){
				$(this).append("<p>To help us direct you to the most appropriate information, please select your country or region and your investor type from the following options:</p>");
				$(this).append('<fieldset class="siteSelectorForm"></fieldset>');
				$.each(investorTypes, function(i, n) {
						$(".siteSelectorForm").append('<div><label for="'+n.value+'" id="'+n.value+'Label" title="'+n.description+'"">'+n.name+'</label><input name="investorType" id="'+n.value+'" type="radio" value="'+n.value+'" title="'+n.description+'"/></div>');
					$("#"+n.value).validate({type:"blur", validation:"notnull", store: true});
					$("#"+n.value).click(function() {selectInvetorType(n);});
					
				});
				$(this).append('<input id="go-button" type="image" src="/core/library/images/go.gif" value="Go" />');
				$(this).append('<p class="preferences"><input id="remember" type="checkbox"/>&nbsp;<strong>Remember my preferences</strong><br/>(only available if your browser has cookies enabled)</p>');
				$("input:radio").addClass("radio");

				$('#go-button').validate({type:'click', checkAll: true, fail: function() {}, success: function() {go();} });
				initValues(investorTypes);
				$(".siteSelectorForm input, .siteSelectorForm label").Tooltip({delay: 0, fixPNG: true, track: true, showBody: " - "});
				$("#tooltip").bgiframe();
		});
		$("div.siteSelector").show();
	};
	var buildSiteSelector = function() {
		// alert("Building site selector...Location:"+location+""+investorType);
		if($("div.siteSelector").size() == 0) $("#main-content").append("<div class=\"siteSelector\"></div>");
		$("div.siteSelector").each(function(){
				// $(this).append("<p>To help us direct you to the most appropriate information, please select your investor type and your country or region from the following options:</p>");
				$(this).append('<fieldset class="siteSelectorForm"></fieldset>');
				buildLocations(locations);
				$("#location").change(function(){selectLocation(this.value)});
				$(this).append('<input id="go-button" type="image" src="/core/library/images/buttons/button.gif" value="Go" />');
				$(this).append('<span class="preferences"><input id="remember" type="checkbox"/>&nbsp;<label for="remember">Remember my preferences</label></span>');
				$(this).append('<div class="messages"></div>');
				// $('#go-button').validate({type:'click', checkAll: true, fail: function() {}, success: function() {go();} });
				$('#go-button').click(function() {validate();});
				$('#go-button').hover(function(){this.src="/core/library/images/buttons/button_ROLL.gif";},function(){this.src="/core/library/images/buttons/button.gif"; });
				initValues(locations);
				// $(".siteSelectorForm input, .siteSelectorForm label").Tooltip({delay: 0, fixPNG: true, track: true, showBody: " - "});
				// $("#tooltip").bgiframe();
		});
		$("div.siteSelector").show();
	};
	var selectInvetorType = function(investorType) {
		setLocation($("#location").val());
		setClientType($("input[@name=investorType][@checked]").val());
		buildLocations(investorType);
	};
	var selectLocation = function(location) {
		//alert(location);
		$("div.siteSelector .messages").empty();
		setLocation($("#location").val());
		setClientType($("input[@name=investorType][@checked]").val());
		buildInvestorTypes(locations[location].investorTypes);
	};
	var initValues = function(locations) {
		loadCookie();
		// alert(loadCookie());
		if(location != "") buildInvestorTypes(locations[location].investorTypes);
		$("#"+clientType).each(function(){
			this.checked = true;
			currentDescription = getInvestorType(clientType, locations[location].investorTypes[clientType]).description;
			showInvestorTypeDescription();
		});
		if (location == undefined || location == "") {
			$("#location").each(function(){this.value = "empty";});
		} else {
			$("#location").each(function(){this.value = location;});
		}
		$("#remember").each(function(){this.checked = remember;});
	};
	var initLocationValue = function() {
		$("#investorType").each(function(){this.value = clientType;});
	};
	var buildLocations = function(locations) {
		$("#location").each(function(){this.options.length = 0});
		if($("#location").size() == 0) $(".siteSelectorForm").append('<select id="location"></select>');
		var i = 0;
		$.each(locations, function(key, o) {
			var selected = false;
			if(o.value == location) selected = true;
			$("#location").get(0).options[i] = new Option(o.name, o.value, false, selected);
			i++;
		});
		mam.validator.clear();
		// $('#location').validate({type:'blur', validation:'notnull', store: true, fail: function() {alert("Please enter your location.");}});
	};
	var buildInvestorTypes = function(investorTypes) {
		currentDescription = "";
		showInvestorTypeDescription();
		$("div#investor-types").remove();
		var investorTypeCount = 0;
		for ( var i in investorTypes ) {investorTypeCount++};
		if(investorTypeCount != 0) {
			$(".siteSelectorForm").append('<div id="investor-types"></div>');
			$("div#investor-types").append('<h3>Investor Type:</h3>');
			//alert("buildInvestorTypes...");
			var j = 0; //counter
			var validateSet = false;
			$.each(investorTypes, function(i, n) {
				// alert(n);
				var investorType = getInvestorType(i, n);
				// if(j==2) $("div#investor-types").append('<br/>'); // insert linebreak before the third item
				$("div#investor-types").append('<div id="'+investorType.value+'_div"><input name="investorType" id="'+investorType.value+'" type="radio" value="'+investorType.value+'" title="'+investorType.description+'"/><label for="'+investorType.value+'" id="'+investorType.value+'Label" title="'+investorType.description+'"">'+investorType.name+'</label></div>');
				$("#"+investorType.value+"_div").hover(function(){showInvestorTypeDescription(investorType.description);},function(){showInvestorTypeDescription();});
				$("#"+investorType.value).click(function(){currentDescription = investorType.description;});
				j++;
				/*
				if(!validateSet) {
					// alert($("#"+investorType.value).size());
					$("#"+investorType.value).validate({
							type:"blur",
							validation:"notnull", 
							store: true, 
							fail: function() {
								alert("Please select your investor type.");
							}
						}
					);
					validateSet = true;
				} */
			});
			// alert(investorTypes[]);
			$("input:radio").addClass("radio");
			// $(".siteSelectorForm input, .siteSelectorForm label").Tooltip({delay: 0, fixPNG: true, track: true, showBody: " - "});
			// $("#tooltip").bgiframe();
			// $("#tooltip").hide();
			
			
		}
	};
	var currentDescription = "";
	var showInvestorTypeDescription = function(description) {
		$("#investorTypeDescription").remove();
		if(description == undefined) {
			// default to selected option
			// alert(investorType+"\n"+clientType);
			description = currentDescription;
		}
		$("#site-selector-centre-column").append('<div id="investorTypeDescription"><p>'+description+'</p></div>');
	};
}
}

