function setPostAction(action) {
	var postAction = this.elements["postAction"];
	if (postAction == null) {
		//Create the hidden if not found
		postAction = document.createElement("input");
		postAction.setAttribute("type", "hidden");
		postAction.setAttribute("name", "postAction");
		this.appendChild(postAction);
	}
	postAction.value = action;
}

Behaviour.register({
	'#backButton': function(button) {
		button.onclick = function() {
			if (isPublic) {
				self.location.href = context + "/do/login";
			} else {
				history.back();
			}
		}
	},
	
	'#assignBrokerCheck': function(check) {
		check.onclick = function() {
			var isChecked = check.checked;
			$$('tr.trBroker').each(function(tr) {
				Element[isChecked ? 'show' : 'hide'](tr);
				if (isChecked) {
					$('brokerUsername').focus();
				} else {
					setValue('newBrokerId', '');
					setValue('brokerUsername', '');
					setValue('brokerName', '');
				}
			});
		}
	},

	'#brokerUsername': function(input) {
		var div = $('brokersByUsername');
		prepareForMemberAutocomplete(input, div, {paramName:"username", brokers:true}, 'newBrokerId', 'brokerUsername', 'brokerName', 'comments');
	},

	'#brokerName': function(input) {
		var div = $('brokersByName');
		prepareForMemberAutocomplete(input, div, {paramName:"name", brokers:true}, 'newBrokerId', 'brokerUsername', 'brokerName', 'comments');
	},
	
	'form': function(form) {
		form.onsubmit = function() {
			return requestValidation(form);
		}
	},
	
	'#saveAndNewButton': function(button) {
		button.onclick = setPostAction.bind(button.form, 'new');
	},
	
	'#saveAndOpenProfileButton': function(button) {
		button.onclick = setPostAction.bind(button.form, 'openProfile');
	}
});

Event.observe(self, "load", function() {
	(getObject("member(user).username") || getObject("member(name)")).focus();
	var assignBroker = $('assignBrokerCheck');
	if (assignBroker) assignBroker.checked = false;
	if (isPublic) {
		var antiBotImage = $('antiBotImage');
		antiBotImage.src = antiBotImageSrc + "?random=" + new Date().getTime();
		antiBotImage.show();
	}
})
