document.observe("dom:loaded", function() {
	setupConditionalRegistrationQuestions();
	floatbox();
	otherFormErrors();				
});


// Hide/show form options based on which radio button is checked
function setupConditionalRegistrationQuestions() {
	$$("input.reg-nature_of_business").each (
		function (radio) {
			radio.observe('click',
				function(event) {					
					if ($F(this) == 'Fund Management') {
						selectFMElement();
					}
					else if ($F(this) == 'Service Provider') {
						selectSPElement();			
					}					
				}
			);
			
			if (($F(radio) == 'Fund Management') && (radio.checked = true)) {
				selectFMElement();
			}
			else if (($F(radio) == 'Service Provider') && (radio.checked = true)) {
				selectSPElement();
			}			
			
		}
	);
	
	otherOption();
	
}	

// Hide/show inputs based on value of radio and disable the others' inputs
function selectFMElement() {
	$('conditional_fm').show() ;
								
	Form.getElements($('conditional_fm')).each(
		function(formElement) {
			formElement.enable();
		}
	);							
								
	$('conditional_sp').hide();
								
	Form.getElements($('conditional_sp')).each(
		function(formElement) {
			formElement.disable();
		}
	);
	
	$$('.reg-service_provider_options').each(function(inputElement) {					
		inputElement.checked = false ;					
	}) ;	
						
}

function selectSPElement() {
	$('conditional_sp').show();
								
	Form.getElements($('conditional_sp')).each(
		function(formElement) {
			formElement.enable();
		}
	);							
								
	$('conditional_fm').hide();
								
	Form.getElements($('conditional_fm')).each(
		function(formElement) {
			formElement.disable();
		}
	);
	
	$$('.reg-fund_management_options').each(function(inputElement) {					
		inputElement.checked = false ;					
	}) ;
		
}

// If the other text box is clicked add the checked attribute to the last checkbox/radio
function otherOption() {
	//var otherInputs = $$("input#reg-fund_management_options_other", "input#reg-service_provider_options_other");
	var otherInputs = $$("input#reg-service_provider_options_other");
		otherInputs.each (
			function (otherInput) {
				otherInput.observe('focus',
					function(event) {
						var otherInputArray = new Selector('input');
						var lastCheckbox = otherInputArray.findElements(this.up('li').previous()).last();
						lastCheckbox.checked = true;
					}
				);
			}
		);
		
	//var otherCheckbox = $$("input.reg-fund_management_options", "input.reg-service_provider_options");
	var otherCheckbox = $$("input.reg-service_provider_options");
		otherCheckbox.each (
			function (otherCheckboxValue) {
				otherCheckboxValue.observe('click',
					function(event) {
						var value = $F(this);
						//if (value == 'Other - please specify') {
						//	$("reg-fund_management_options_other").focus();
						//}
						//else if (value == 'Other professional - please specify') {
						if (value == 'Other professional - please specify') {
							$("reg-service_provider_options_other").focus();
						}
					}
				);				
			}
		)
}


// Floatbox functions: resizing, closing
function floatbox() {
	$$('body.register a[href*="/registration/login.php"]').each(
		function(loginLink) {
			loginLink.observe('click',
				function(event) {
					parent.fb.loadAnchor('/registration/login.php', 'sameBox:true width:510 height:325');
				}
			);
		}
	);
	
	$$('body.register a[href*="/registration/forgottenpassword.php"]').each(
		function(loginLink) {
			loginLink.observe('click',
				function(event) {
					parent.fb.loadAnchor('/registration/forgottenpassword.php', 'sameBox:true width:510 height:325');
				}
			);
		}
	);
	
	$$('body.login a[href*="/registration/index.php"]').each(
		function(loginLink) {
			loginLink.observe('click',
				function(event) {
					parent.fb.loadAnchor('/registration/index.php', 'sameBox:true width:780 height:480');
				}
			);
		}
	);
	
				
	var closeFloatbox = $("continue");
	if (closeFloatbox) {
		closeFloatbox.observe('click',
			function(event) {				
				var urlPath = parent.window.location.pathname.replace(/[^\/]*$/, "");
				
				if (urlPath.match(/^\/research-and-data/)) {
					parent.showLoggedStatus();
				}
				else {
					parent.showLoggedState();
				}
				
				parent.fb.end();
			}
		);
	}
	
	
	var loginCookieReport = readCookie('hfm_login_id');
	if ($("report-reason") && loginCookieReport) {
		$("report-reason").focus();
	}

}

function otherFormErrors() {
	$$('body.login img[src*="/images/error.gif"]').each (
		function (formError) {
			formError.previous('input').addClassName("highlight-error");
		}
	)
}