
function password(){
	$('pwd_notice').innerHTML = "Password is ... ";
}

function setup_register() {
	if(watchables != null) {
		$A(watchables).each(function(el) {
			Event.observe(el, 'mouseup', update_register);
			Event.observe(el, 'change', update_register);
		});
		
		$A(hide).each(function(i) {
			$(i).hide();
		});
		
		update_register();
	}
}

function update_register() {
	$A(watchables).each(function(el) {
		if(el.selectedIndex != 0) {
			$A(hide).each(function(i) {
				$(i).show();
			});
		}
		else {
			$A(hide).each(function(i) {
				$(i).hide();
			});
		}
	});
}

// The functions above were created by Kieren, and I don't know
// whether they have side effects, depend on external calls, global
// variables or whatever.
// I'm writing my functions in the closed environment below,
// the way I now like doing it

(function () {

Event.observe(window, 'load', function () {
	var reffields = $A(['register[referral_code1]', 'register[referral_code2]', 'register[referral_code3]']);
	reffields.each(function (e) {
		var node = $(e);
		add_check_button(node);
	});
});

function add_check_button(node) {
	var paragraph = Element.up(node, 'P');

	new Insertion.Bottom(paragraph, '<input type="button" class="button" style="font-size: 0.8em; float: right; margin-top: -2em; margin-right: 1.5em;" value="Check validity" />');
	var inserted = Element.down(paragraph, '.button');
	Event.observe(inserted, 'click', function (event) {
		update_referral_code(node.id);
	});
}

function update_referral_code(field_id) {
	var field = $(field_id);
	var code = $F(field);

	new Ajax.Request('/json/login/check_referral_code/' + code, {
		onComplete: callback_referral_codes.bind(field)
	});
}

var codes_status = {};

function callback_referral_codes(response) {
	var res = eval('(' + response.responseText + ')');
	codes_status[this.id] = res;
	recreate_referral_messages();
}

function recreate_referral_messages() {
	recreate_messages_beside_fields();
	recreate_bottom_list_messages();
}

function recreate_messages_beside_fields() {
	$$('.code-description').each(function (n) {
		Element.remove(n);
	});

	$H(codes_status).each(function (e) {
		var field = $(e[0]);
		var data = e[1];

		if ($F(field) != '' && data) {
			var paragraph = Element.up(field, 'P');
			var osm = document.createElement('DIV');
			osm.className = 'code-description';
			osm.innerHTML = '<div style="color: red; font-size: 10px; text-align: center;">' + data.OnScreenMessage + '</div>';
			paragraph.appendChild(osm);

			// If IE behaved itself, I would use the next two lines instead of the 5 above (sigh)
// 			var paragraph = Element.up(field, 'P');
// 			new Insertion.Bottom(paragraph, '<div style="color: red; font-size: 10px; text-align: center;" class="code-description">' + data.OnScreenMessage + '</div>');
		}
	});
}

function recreate_bottom_list_messages() {
	var free_sms = 0;
	var discount = 0;
	var network_seen = false;

	resetAppliedCodes();
	$H(codes_status).each(function (e) {
		var data = e[1];
		if ( ! codeApplied(data.Code) && data.from != "network" && (1*data.DiscountPercent == 0 || (discount < 1*data.DiscountPercent))) {
			discount = discount < 1*data.DiscountPercent ? 1*data.DiscountPercent : discount;
			free_sms += 1*data.SmsCount;
			markAsApplied(data.Code);
		}
		
		if(data.from == "network") {
			network_seen = true;
		}
	});
	
	if(network_seen) {
		$('associate_signup').hide();
		$('associate_info').hide();
		$('master_info').show();
	}
	else {
		$('associate_signup').show();
		if($('associate_signup').checked) {
			$('associate_info').show();
		}
		$('master_info').hide();
	}

	$$('.advantage-description').each(function (n) {
		Element.remove(n);
	});

	$$('.free-sms').each(function (n) {
		if (free_sms > 0) {
			var tmp_free_sms = free_sms;
			if(n.className.match('totallyfree')) {
				$H(codes_status).each(function (e) {
					var data = e[1];
					if (codeApplied(data.Code) && data.from != "network" && (1*data.AffiliateOwner > 0)) {
						tmp_free_sms -= 1*data.SmsCount;
					}
				});
			}
			if (tmp_free_sms > 0) {
				new Insertion.Bottom(n, '<span style="color: red; font-size: 0.8em;" class="advantage-description"><br/>-- plus ' + tmp_free_sms + ' extra added from referral code</span>');
			}
		}
	});
	$$('.upfront-payment').each(function (n) {
		if (discount > 0 && !n.className.match('free')) {
			new Insertion.Bottom(n, '<span style="color: red; font-size: 0.8em;" class="advantage-description"><br/>-- minus ' + discount + '% discount applied from referral code</span>');
		}
	});
}

var applied_codes = [];
function codeApplied(code) {
	var ret = false;
	$A(applied_codes).each(function (c) {
		if (c == code) {
			ret = true;
		}
	});

	return ret;
}
function markAsApplied(code) {
	if ( ! codeApplied(code)) {
		applied_codes.push(code);
	}
}
function resetAppliedCodes(code) {
	applied_codes = [];
}

})();
