
var JoinDetails = Class.create(false);
JoinDetails.prototype = {
	initialize: function() {
		this.valid_password = true;
		this.valid_email = true;
		this.registerEvents();	
	},

	registerEvents : function() {
		var password2 = $('txt_password2');
		var password1 = $('txt_password');
		if (password2 != undefined && password1 != undefined) {
			Event.observe(password2, 'blur', this.validate_passwords.bind(this));
			Event.observe(password1, 'blur', this.validate_passwords.bind(this));
		}

		var email = $('txt_email');
		if (email != undefined)
			Event.observe(email, 'blur', this.validate_email.bind(this));

		var continue1 = $('btn_continue1');
		if (continue1 != undefined)
			Event.observe(continue1, 'click', this.validate_details.bind(this));

		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 country = $('opt_country');
		if (country != undefined)
			Event.observe(country,'change',this.select_country.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));
		}
	},

	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 = '';
		}
	},

	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';
				}
			}
		}
	},

	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;
					}
			}
		}
	},

	validate_passwords : function(e) {

		var password1 = $('txt_password');
		var password2 = $('txt_password2');

		var btn = $('btn_continue1');
		var alert = $('alertMismatch');

		if (password2 != undefined && password1 != undefined && alert != undefined && btn != undefined) {
			if ((e.element().id == 'txt_password' && password2.value != '') || e.element().id == 'txt_password2' ) {
				if (password2.value != password1.value) {
					alert.style.visibility = 'visible';
					btn.disabled = true;
					this.valid_password = false;
				}
				else {
					alert.style.visibility = 'hidden';
					this.valid_password = true;
					if (this.valid_email)
						btn.disabled = false;
				}
			}
		}
	},

	validate_email : function(e) {

		var email = e.element();
		var btn = $('btn_continue1');
		var alert = $('alertEmail');

		if (btn != undefined && alert != undefined) {
			if (email.value != '') {
				if ((email.value.indexOf(".") > 2) && (email.value.indexOf("@") > 0)) {
					alert.style.visibility = 'hidden';
					this.valid_email = true;
					if (this.valid_password)
						btn.disabled = false;
				}
				else {
					alert.style.visibility = 'visible';
					btn.disabled = true;
					this.valid_email = false;
				}
			}		
		}
	},

	validate_details : function(e) {

		valid = true;

		var email = $('txt_email');
		var password1 = $('txt_password');
		var password2 = $('txt_password2');
		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_password = $('li_password');
		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_password = $('lbl_password');
		var lbl_password2 = $('lbl_password2');
		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 prac_address = $('txt_prac_address1');
		var prac_suburb = $('txt_prac_suburb');
		var prac_postcode = $('txt_prac_postcode');
		var prac_phone = $('txt_prac_phone');

		var li_prac_postcode2 = $('li_prac_postcode2');
		var li_prac_address = $('li_prac_address');
		var li_prac_suburb = $('li_prac_suburb');
		var li_prac_postcode = $('li_prac_postcode');
		var li_prac_phone = $('li_prac_phone');

		var lbl_prac_address = $('lbl_prac_address1');
		var lbl_prac_suburb = $('lbl_prac_suburb');
		var lbl_prac_postcode = $('lbl_prac_postcode');
		var lbl_prac_phone = $('lbl_prac_phone');

		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 (password1.value == '' || password2.value == '') {
			li_password.style.display = 'block';
			lbl_password.style.fontWeight = 'bold';
			valid = false;
		}
		else {
			li_password.style.display = 'none';
			lbl_password.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';
			li_postcode.style.display = 'none';
		}

		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';
		}


		if (!$('chk_samedetails').checked && $('chk_public').checked)
		{
			if (prac_address.value == '')
			{
				li_prac_address.style.display = 'block';
				lbl_prac_address.style.fontWeight = 'bold';
				valid = false;
			}
			else
			{
				li_prac_address.style.display = 'none';
				lbl_prac_address.style.fontWeight = 'normal';
			}

			if (prac_suburb.value == '')
			{
				li_prac_suburb.style.display = 'block';
				lbl_prac_suburb.style.fontWeight = 'bold';
				valid = false;
			}
			else
			{
				li_prac_suburb.style.display = 'none';
				lbl_prac_suburb.style.fontWeight = 'normal';
			}

			if (prac_postcode.value == '') {
				li_prac_postcode.style.display = 'block';
				lbl_prac_postcode.style.fontWeight = 'bold';
				li_prac_postcode2.style.display = 'none';
				valid = false;
			}
			else if (parseInt(prac_postcode.value) <= 0  || isNaN(parseInt(prac_postcode.value))) {
				li_prac_postcode2.style.display = 'block';
				lbl_prac_postcode.style.fontWeight = 'bold';
				li_prac_postcode.style.display = 'none';
				valid = false;
			}
			else {
				li_prac_postcode2.style.display = 'none';
				lbl_prac_postcode.style.fontWeight = 'normal';
			}

			if (prac_phone.value == '')
			{
				li_prac_phone.style.display = 'block';
				lbl_prac_phone.style.fontWeight = 'bold';
				valid = false;
			}
			else
			{
				li_prac_phone.style.display = 'none';
				lbl_prac_phone.style.fontWeight = 'normal';
			}
		}
		else
		{
			lbl_prac_address.style.fontWeight = 'normal';
			li_prac_address.style.display = 'none';
			lbl_prac_suburb.style.fontWeight = 'normal';
			li_prac_suburb.style.display = 'none';
			lbl_prac_postcode.style.fontWeight = 'normal';
			li_prac_postcode.style.display = 'none';
			lbl_prac_phone.style.fontWeight = 'normal';
			li_prac_phone.style.display = 'none';
		}


		if (!valid) {
			alert.style.display = 'block';
			$('a_focus').focus();
			Event.stop(e);
			return false;
		}

		// select all items in associations and practices for _POSTing
		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;
	}
}

var JoinPayment = Class.create(false);
JoinPayment.prototype = {
	initialize: function() {
		this.registerEvents();	
	},

	registerEvents : function() {
		var submity = $('btn_submit');

		if (submity != undefined)
			Event.observe(submity, 'click', this.validate_payment.bind(this));

		var cancel = $('btn_cancel');

		if (cancel != undefined)
			Event.observe(cancel, 'click', this.cancel_click.bind(this));

		var chk_samedetails = $('chk_samedetails');
		if (chk_samedetails != undefined)
		{
			Event.observe(chk_samedetails, 'click', this.click_samedetails.bind(this));
		}

		Event.observe($('chk_public'), 'click', this.click_public.bind(this));
	},

	cancel_click : function(e) {
		window.location = "home.html";
	},

	click_public : function(e) {
		var details = $('prac_details');
		if (e.element().checked)
			details.style.display = 'block';
		else
			details.style.display = 'none';
	},

	click_samedetails : function(e) {
		var address1 = $('txt_prac_address1');
		var address2 = $('txt_prac_address2');
		var suburb = $('txt_prac_suburb');
		var postcode = $('txt_prac_postcode');
		var phone = $('txt_prac_phone');

		var o_address1 = $('txt_address1');
		var o_address2 = $('txt_address2');
		var o_suburb = $('txt_suburb');
		var o_postcode = $('txt_postcode');
		var o_phone = $('txt_phone');

		if (e.element().checked)
			address1.disabled = address2.disabled = suburb.disabled = postcode.disabled = phone.disabled = true;
		else
			address1.disabled = address2.disabled = suburb.disabled = postcode.disabled = phone.disabled = false;
	},

	validate_payment : function(e) {

		if (!$('radio_card').checked)
			return true;

		valid = true;

		var name = $('txt_name');
		var number = $('txt_number');
		var security = $('txt_security');

		var li_name = $('li_name');
		var li_number = $('li_number');
		var li_security = $('li_security');

		var lbl_name = $('lbl_name');
		var lbl_number = $('lbl_number');
		var lbl_security = $('lbl_security');

		var type = $('opt_type');
		var type_name = type.options[type.selectedIndex].value;

		var alert = $('alert_payment');

		if (name.value == '') {
			li_name.style.display = 'block';
			lbl_name.style.fontWeight = 'bold';
			valid = false;
		}
		else {
			li_name.style.display = 'none';
			lbl_name.style.fontWeight = 'normal';
		}

		if (number.value == '' || (type_name == 'Visa' && (number.value.length != 16 && number.value.length != 13)) 
				|| (type_name == 'Mastercard' && number.value.length != 16)){
			li_number.style.display = 'block';
			lbl_number.style.fontWeight = 'bold';
			valid = false;
		}
		else {
			li_number.style.display = 'none';
			lbl_number.style.fontWeight = 'normal';
		}

		if (security.value == '') {
			li_security.style.display = 'block';
			lbl_security.style.fontWeight = 'bold';
			valid = false;
		}
		else {
			li_security.style.display = 'none';
			lbl_security.style.fontWeight = 'normal';
		}

		if (!valid) {
			alert.style.display = 'block';
			$('a_focus').focus();
			Event.stop(e);
			return false;
		}

		return true;
	}
}

