document.documentElement.className = 'jsEnabled_acctSelect_live';
$(document).ready(function(){
	
	var URLs_AT = new Array();
//----------Add / edit button URLS below: ----------//
		URLs_AT["Default"] = "https://secure2.fxcorporate.com/fxtr/?ib=fxcmuk_cfd_xtp";
		URLs_AT["US"] = "https://secure2.fxcorporate.com/fxtr/?ib=fxcmuk_us_xtp";
		URLs_AT["AU"] = "https://secure2.fxcorporate.com/fxtr/?ib=fxcmau_active_trader";
		
	var URLs_MT = new Array();
		URLs_MT["Default"] = "/metatrader-apply.jsp";
		URLs_MT["AU"] = "http://www.forextrading.com.au/metatrader-apply.jsp";
		
	var URLs_CFD = new Array();
		URLs_CFD["Default"] = "/cfd-account-international.jsp";
		URLs_CFD["AU"] = "http://www.forextrading.com.au/cfd-product-info.jsp";
		
	var URLs_FX = new Array();
		URLs_FX["Default"] = "/forex-account-international.jsp";
		URLs_FX["AU"] = "http://www.forextrading.com.au/open-an-account-international.jsp";
		
	var URLs_FX_DE = new Array();
		URLs_FX_DE["Default"] = "/forex-account-de.jsp";
		URLs_FX_DE["AU"] = "http://www.forextrading.com.au/open-an-account-international.jsp";
		
	var URLs_SB = new Array();
		URLs_SB["Default"] = "/spread-betting-account.jsp"
	
//----------When adding new accounts, be sure to add the reference below as well as above ---//
	var $URLs = new Array ();
		$URLs["AT"] = URLs_AT;
		$URLs["MT"] = URLs_MT;
		$URLs["CFD"] = URLs_CFD;
		$URLs["SB"] = URLs_SB;
		$URLs["FX"] = URLs_FX;
		$URLs["FX_DE"] = URLs_FX_DE;

	$("select#sList").attr("selectedIndex", 0);
	
	$("select#sList").change(function(){
		var accts = $("ul#acctBoxWrap").children("li.acctBox");		
		var customGrp = $(".customGrp");	
		var selectedCountryTitle = $("h4#selectedCountry");
		var selectedOption = $(this).children("option:selected");
		var selectedClass = $(selectedOption).attr("class");
		var selectedID = $(selectedOption).attr("id");
		if (selectedID.indexOf("pre_") != -1) {
			selectedID = selectedID.slice(4);
			$(selectedOption).attr("id", selectedID)
		}			
		//reset all elements
		$(selectedCountryTitle).removeClass("on").text("");
		$(accts).hide();
		$(customGrp).children().hide();
		if (selectedOption.val() != "") {
			var acctsToShow = $(accts).filter(".for" + selectedClass);	
			//	if selected class is empty, set acctsToShow to default
			if (selectedClass == "") {
				acctsToShow = $(accts).filter(".forDefault");
				selectedClass = "Default"
			}
			else if (selectedClass != "") {
				//check for existence of div.acctBox with the the class of selectedClass
				if (acctsToShow.length != 0) {
					acctsToShow = $(accts).filter(".for" + selectedClass);
				}
				else if (acctsToShow.length == 0) {
					acctsToShow = $(accts).filter(".forDefault");
				}
			}
			// loop through each acct box
			for (var i = 0; i < acctsToShow.length; i++) {
				var currAcct = $(acctsToShow).eq(i);
				var currAcctType = $(currAcct).attr("id");
				if($(currAcct).attr("class").indexOf("customBox") == -1 ){
					//display customGrp elements
					var currCustomGrps = $(currAcct).find(".customGrp");
					for (var n = 0; n < currCustomGrps.length; n++){
						var thisGrp = $(currCustomGrps).eq(n).children();
						if (selectedClass == "Default") {
							$(thisGrp).filter(".for" + selectedClass).show();
						}
						else if (selectedClass != "Default"){ 
						if (($(thisGrp).filter(".for" + selectedClass).length == 0) && ($(thisGrp).filter(".not" + selectedClass).length == 0)) {
								$(thisGrp).filter(".forDefault").show();
							}
							else {
								$(thisGrp).filter(".for" + selectedClass).show();
							}
						}
					}
					//determine correct url to  use
					var currBtn = $(currAcct).children("a.sBtn");
					var currAcctURLArray = $URLs[currAcctType];
					var currAcctURL = $URLs[currAcctType][selectedClass];
					if (currAcctURL == undefined) {
						currAcctURL = $URLs[currAcctType]["Default"];
					}
					//feed  url to submit button's href
					$(currBtn).attr("href", currAcctURL);
					//add target blank to external URLs
					if (currAcctURL.indexOf("http") != -1) {
						$(currBtn).attr("target", "_blank");
					}
				}
				//add margin to even boxes
				if (i%2 == 0) {
					$(currAcct).addClass("addMargin");
				}
				else if (i%2 != 0) {
					$(currAcct).removeClass("addMargin");
				}
			}
			$(selectedCountryTitle).addClass("on").text(selectedOption.text());
			$(acctsToShow).show();
		}
	});
	
	// target country & account through URL
	var calledCountry;
	var countryIndex;
	calledCountry = unescape(window.location);
	if(calledCountry.indexOf("country") > -1) {
		var marker = calledCountry.indexOf("country=") +8;
		calledCountry = calledCountry.substr(marker, 2).toUpperCase();
		calledCountry = $("select#sList option#" + calledCountry);
		countryIndex = 0;
		if ($(calledCountry).length > 0){
			countryIndex = $(calledCountry).prevAll().size();
			$("select#sList").attr("selectedIndex", countryIndex);
			$("select#sList").trigger('change');
		}
	}
	//add country code to fxcm redirect links
	var theCountry;
	var theHREF;
	$("a.fxcmRedirect").click(function(){
		theCountry = $("select#sList").children("option:selected").attr("id");
		theCountry = theCountry.slice(theCountry.length - 2);
		theHREF = $(this).attr("href");
		theHREF = theHREF + "#country=" + theCountry;
		$(this).attr("href", theHREF);
	});
	
});  
// For instructions, please see: /2009_JAVA/FXCM_UK/web/js/content/acctSelect_live_instructions.doc 
//	
//	11.26.09:
//	Ammended customGrp functionality to allow exclusion of one country. ie, adding "notXX" to prevent a default element from displaying in the case of the selected country not having an associated custom element
//
