// client side tab change
var Tabs = Class.create(false);
Tabs.prototype = {
	initialize: function() {
		this.registerEvents();
	},

	registerEvents : function() {
		var tabs = $$('ul.tabs li a');
		for (var i=0;i<tabs.length ;i++ ) {
			Event.observe(tabs[i], 'click', this.select_tab.bind(this));
		}
	},

	select_tab : function(e) {
		// unselect previous tab
		var prev = $$('ul.tabs li a.selected');
		if (prev.length == 1) {
			$('fs_' + prev[0].getAttribute("rel")).style.display = 'none';
			prev[0].className = '';
		}

		// select new tab
		var tab = e.element();
		tab.className = 'selected';
		$('fs_' + tab.getAttribute("rel")).style.display = 'block';
	}
}


var MyDetails = Class.create(false);
MyDetails.prototype = {
	initialize: function() {
		this.registerEvents();	
	},

	registerEvents : function() {
		var email = $('txt_email');
		if (email != undefined)
			Event.observe(email, 'blur', this.validate_email.bind(this));

		var continue1 = $('btn_submit_details');
		if (continue1 != undefined)
			Event.observe(continue1, 'click', this.validate_details.bind(this));

		var country = $('opt_country');
		if (country != undefined)
			Event.observe(country,'change',this.select_country.bind(this));
	},

	select_country : function(e) {
		var country = e.element();
		for (var i=0;i<country.options.length;i++ ) {
			if (country.options[i].selected) {
				if (country.options[i].value == "Other") {
					$('div_country').style.display = 'block';
					$('div_state_aus').style.display = 'none';
					$('div_state_nz').style.display = 'none';
					$('div_state_other').style.display = 'block';
				}
				else if (country.options[i].value == "Australia") {
					$('div_country').style.display = 'none';
					$('div_state_aus').style.display = 'block';
					$('div_state_nz').style.display = 'none';
					$('div_state_other').style.display = 'none';
				}
				else if (country.options[i].value == "New Zealand") {
					$('div_country').style.display = 'none';
					$('div_state_aus').style.display = 'none';
					$('div_state_nz').style.display = 'block';
					$('div_state_other').style.display = 'none';
				}
			}
		}
	},

	validate_email : function(e) {

		var email = e.element();
		var btn = $('btn_submit_details');
		var alert = $('alertEmail');

		if (btn != undefined && alert != undefined) {
			if (email.value != '') {
				if ((email.value.indexOf(".") > 2) && (email.value.indexOf("@") > 0)) {
					alert.style.visibility = 'hidden';
					btn.disabled = false;
				}
				else {
					alert.style.visibility = 'visible';
					btn.disabled = true;
				}
			}		
		}
	},

	validate_details : function(e) {

		valid = true;

		var email = $('txt_email');
		var first_name = $('txt_first_name');
		var middle_initial = $('txt_middle_initial');
		var last_name = $('txt_last_name');
		var address = $('txt_address1');
		var suburb = $('txt_suburb');
		var country = $('opt_country');
		var country2 = $('txt_country');
		var postcode = $('txt_postcode');
		var phone = $('txt_phone');
		var mobile = $('txt_mobile');
		var state_aus = $('opt_state_aus');
		var state_nz = $('opt_state_nz');

		var li_email = $('li_email');
		var li_first_name = $('li_first_name');
		var li_middle_initial = $('li_middle_initial');
		var li_last_name = $('li_last_name');
		var li_address = $('li_address');
		var li_suburb = $('li_suburb');
		var li_country = $('li_country2');
		var li_postcode = $('li_postcode');
		var li_postcode2 = $('li_postcode2');
		var li_phone = $('li_phone');
		var li_mobile = $('li_mobile');
		var li_state = $('li_state');
		var li_region = $('li_region');

		var lbl_email = $('lbl_email');
		var lbl_first_name = $('lbl_first_name');
		var lbl_middle_initial = $('lbl_middle_initial');
		var lbl_last_name = $('lbl_last_name');
		var lbl_address = $('lbl_address');
		var lbl_suburb = $('lbl_suburb');
		var lbl_country = $('lbl_country2');
		var lbl_postcode = $('lbl_postcode');
		var lbl_phone = $('lbl_phone');
		var lbl_mobile = $('lbl_mobile');
		var lbl_state = $('lbl_state');
		var lbl_region = $('lbl_region');

		var alert = $('alert_details');

		if (email.value == '') {
			li_email.style.display = 'block';
			lbl_email.style.fontWeight = 'bold';
			valid = false;
		}
		else {
			li_email.style.display = 'none';
			lbl_email.style.fontWeight = 'normal';
		}

		if (first_name.value == '') {
			li_first_name.style.display = 'block';
			lbl_first_name.style.fontWeight = 'bold';
			valid = false;
		}
		else {
			li_first_name.style.display = 'none';
			lbl_first_name.style.fontWeight = 'normal';
		}

		if (middle_initial.value.length > 1) {
			li_middle_initial.style.display = 'block';
			lbl_middle_initial.style.fontWeight = 'bold';
			valid = false;
		}
		else {
			li_middle_initial.style.display = 'none';
			lbl_middle_initial.style.fontWeight = 'normal';
		}

		if (last_name.value == '') {
			li_last_name.style.display = 'block';
			lbl_last_name.style.fontWeight = 'bold';
			valid = false;
		}
		else {
			li_last_name.style.display = 'none';
			lbl_last_name.style.fontWeight = 'normal';
		}

		if (address.value == '') {
			li_address.style.display = 'block';
			lbl_address.style.fontWeight = 'bold';
			valid = false;
		}
		else {
			li_address.style.display = 'none';
			lbl_address.style.fontWeight = 'normal';
		}

		if (suburb.value == '') {
			li_suburb.style.display = 'block';
			lbl_suburb.style.fontWeight = 'bold';
			valid = false;
		}
		else {
			li_suburb.style.display = 'none';
			lbl_suburb.style.fontWeight = 'normal';
		}

		if (country.options[country.selectedIndex].value == "Australia" && state_aus.options[state_aus.selectedIndex].value == "0")
		{
			lbl_state.style.fontWeight = 'bold';
			li_state.style.display = 'block';
			lbl_region.style.fontWeight = 'normal';
			li_region.style.display = 'none';
			li_country.style.display = 'none';
			lbl_country.style.fontWeight = 'normal';
			lbl_country.style.color = "#888";
			valid = false;
		}
		else if (country.options[country.selectedIndex].value == "New Zealand" && state_nz.options[state_nz.selectedIndex].value == "0")
		{
			lbl_state.style.fontWeight = 'normal';
			li_state.style.display = 'none';
			lbl_region.style.fontWeight = 'bold';
			li_region.style.display = 'block';
			li_country.style.display = 'none';
			lbl_country.style.fontWeight = 'normal';
			lbl_country.style.color = "#888";
			valid = false;
		}
		else if (country.options[country.selectedIndex].value == "Other" && country2.value == '') {
			lbl_country.style.color = "#333";
			li_country.style.display = 'block';
			lbl_country.style.fontWeight = 'bold';
			lbl_state.style.fontWeight = 'normal';
			li_state.style.display = 'none';
			lbl_region.style.fontWeight = 'normal';
			li_region.style.display = 'none';
			valid = false;
		}
		else
		{
			lbl_country.style.color = "#333";
			li_country.style.display = 'none';
			lbl_country.style.fontWeight = 'normal';
			lbl_state.style.fontWeight = 'normal';
			li_state.style.display = 'none';
			lbl_region.style.fontWeight = 'normal';
			li_region.style.display = 'none';
		}

		if (postcode.value == '') {
			li_postcode.style.display = 'block';
			lbl_postcode.style.fontWeight = 'bold';
			li_postcode2.style.display = 'none';
			valid = false;
		}
		else if (parseInt(postcode.value) <= 0  || isNaN(parseInt(postcode.value))) {
			li_postcode2.style.display = 'block';
			lbl_postcode.style.fontWeight = 'bold';
			li_postcode.style.display = 'none';
			valid = false;
		}
		else {
			li_postcode2.style.display = 'none';
			lbl_postcode.style.fontWeight = 'normal';
		}

		if (phone.value == '' && mobile.value == '') {
			li_phone.style.display = 'block';
			lbl_phone.style.fontWeight = 'bold';
			valid = false;
		}
		else {
			li_phone.style.display = 'none';
			lbl_phone.style.fontWeight = 'normal';
		}

		console.log(valid);
		if (!valid) {
			alert.style.display = 'block';
			$('details_focus').focus();
			Event.stop(e);
			return false;
		}

		return true;
	}
}

var Membership = Class.create(false);
Membership.prototype = {
	initialize : function() {
		this.registerEvents();
	},

	registerEvents : function(e) {
		var assoc_add = $('btn_assoc_add');
		var assoc_rem = $('btn_assoc_rem');
		if (assoc_add != undefined && assoc_rem != undefined) {
			Event.observe(assoc_add,'click', this.click_selector.bind(this));
			Event.observe(assoc_rem,'click', this.click_selector.bind(this));
		}

		var prac_add = $('btn_prac_add');
		var prac_rem = $('btn_prac_rem');
		if (prac_add != undefined && prac_rem != undefined){
			Event.observe(prac_add,'click', this.click_selector.bind(this));
			Event.observe(prac_rem,'click', this.click_selector.bind(this));
		}

		var assoc_other = $('btn_assoc_other_add');
		var prac_other = $('btn_prac_other_add');
		if (assoc_other != undefined && prac_other != undefined) {
			Event.observe(assoc_other, 'click', this.add_otherassociation.bind(this));
			Event.observe(prac_other, 'click', this.add_otherassociation.bind(this));
		}

		var continue1 = $('btn_submit_membership');
		if (continue1 != undefined)
			Event.observe(continue1, 'click', this.validate_details.bind(this));
	},

	validate_details : function(e) {
		var associations = $('sel_associations2');
		var practices = $('sel_practices2');
		for (var i=0;i<associations.options.length;i++ )
			associations[i].selected = true;

		for (var i=0;i<practices.options.length ;i++ )
			practices[i].selected = true;

		return true;
	},

	add_otherassociation : function(e) {
		
		if (e.element().id == "btn_assoc_other_add") {
			var other = $('txt_assoc_other');
			var right = $('sel_associations2');
		}
		else {
			var other = $('txt_prac_other');
			var right= $('sel_practices2');
		}

		if (other != undefined && right != undefined)
		{
			if (other.value != '')
				right.options[right.options.length] = new Option(other.value, 'other_' + other.value);

			other.value = '';
		}
	},

	click_selector : function(e) {

		var btn = e.element();

		if (btn.name == "btn_assoc") {
			var left = $('sel_associations');
			var right = $('sel_associations2');
		}
		else if (btn.name == "btn_prac") {
			var left = $('sel_practices');
			var right= $('sel_practices2');
		}

		if (left != undefined && right != undefined)
		{
			if (btn.value == ">") { // add any values that are not already added
				for (var i=left.options.length - 1;i>=0;i-- ) {
					if (left.options[i].selected) {
						right.options[right.options.length] = new Option(left.options[i].text, left.options[i].value);
						left.options[i] = null;
					}
				}
			}
			else { // remove the selected values
				for (var i=right.options.length - 1; i>=0;i-- )
					if (right.options[i].selected) {
						left.options[left.options.length] = new Option(right.options[i].text, right.options[i].value);
						right.options[i] = null;
					}
			}
		}
	}
}

var Passwords = Class.create(false);
Passwords.prototype = {
	initialize : function() {
		this.registerEvents();
	},

	registerEvents : function(e) {
		var btn = $('btn_submit_password');
		if (btn != undefined) {
			Event.observe(btn, 'click', this.submit_click.bind(this));
		}
	},

	submit_click : function(e) {
		var old = $('txt_password_old');
		var new1 = $('txt_password_new1');
		var new2 = $('txt_password_new2');

		var lbl_old = $('lbl_password_old');
		var lbl_new1 = $('lbl_password1');
		var lbl_new2 = $('lbl_password2');

		var li_old = $('li_old_password');
		var li_new1 = $('li_new_password1');
		var li_new2 = $('li_new_password2');
		var li_match = $('li_match_passwords');

		var alert = $('alert_password');

		var valid = true;

		if (old.value == '') {
			lbl_old.style.fontWeight = 'bold';
			li_old.style.display = 'block';
			valid = false;
		}
		else {
			lbl_old.style.fontWeight = 'normal';
			li_old.style.display = 'none';
		}

		if (new1.value == '') {
			lbl_new1.style.fontWeight = 'bold';
			li_new1.style.display = 'block';
			valid = false;
		}
		else {
			lbl_new1.style.fontWeight = 'normal';
			li_new1.style.display = 'none';
		}

		if (new2.value == '') {
			lbl_new2.style.fontWeight = 'bold';
			li_new2.style.display = 'block';
			valid = false;
		}
		else {
			lbl_new2.style.fontWeight = 'normal';
			li_new2.style.display = 'none';
		}

		if (new1.value != new2.value) {
			li_match.style.display = 'block';
			valid = false;
		}
		else {
			li_match.style.display = 'none';
		}

		if (!valid) {
			alert.style.display = 'block';
			$('password_focus').focus();
			Event.stop(e);
			return false;
		}
		
	}
}
