$(document).ready(function() {
	
	// Ajax-Formcheck
	$.include('jquery/jquery.ajaxformcheck.js', function() {$("#registration").doAjaxFormCheck()});
	
	// Adressdaten
	if (!$("#address-business").attr("checked")) $("#address-firmname-container").hide();
	$("#address-business").click(function() {
		$("#address-firmname-container").show();
	});
	$("#address-private").click(function() {
		$("#address-firmname-container").hide();
	});
	
	// wenn E-Mailadresse als Benutzername verwendet werden soll, dann EMail ins benutzername-Feld schreiben
	$("#registration").find("#select-username-box").show();
	var usernameField = $("#registration #username");
	$('#select-username').click(function() {
	    if ($('#select-username').attr('checked')) {
	    	var email = $("#registration").find("#email-address").val();
	    	usernameField.val(email).attr('readonly', true).css('background', 'transparent');
	    } else {
	    	usernameField.val('').attr('readonly', false).css('background', '#FFF');
	    }
	});

	// topbonus-Felder ausblenden, wenn tb-registration nicht ausgewaehlt	
	if (!$("#registration").find("#tb-registration").attr("checked")) $("#tb-additionals").hide();
	
	// tb registration
	$("#registration").find("#tb-registration").click(function() {
		$("#tb-additionals").toggle();
		if (!$(this).attr("checked")) $("#sc-registration").attr("checked", false);
	});

	// Zusendung der Partnerangebote vorbelegen, wenn post ausgewaehlt
	$("#registration").find("input[name=tb-sendaccountstatus]").click(function() {
		var doCheck = $(this).val() == "email" ? "" : "checked";
		$("#registration").find("#tb-partneroffers").attr("checked", doCheck);
	});

	// sc registration
	$("#registration").find("#sc-registration").click(function() {
		if ($(this).attr("checked")) $("#tb-registration").attr("checked", true);
		if ($("#tb-registration").attr("checked")) $("#tb-additionals").show();
	});
	
	// Auswahlfenster fuer Newsletter, toggle Container fuer partner
	$.include('functions-jquery.js', function() {
		var toggleContainer = $("#tb-bargain-Newsletter-select");	
		// alle container zuklappen
		toggleContainer.nextAll().doToggle(".tb-which-partner-content");
		// Toggle bei Click Layer Button anzeigen 
		toggleContainer.find(".tb-which-partner-headline img").click(function() {
			$(this).parent().parent().doToggle(".tb-which-partner-content");
		});
	});
	
	// Checkboxen Post oder E-Mail kann nur eines ausgewaehlt werden (Erstauswahl)
	$("#tb-sendaccountstatus-select-post, #tb-sendaccountstatus-select-email").click(function() {
		if (!$(this).attr("checked")) return;
		$("#tb-sendaccountstatus-select-post, #tb-sendaccountstatus-select-email").not(this).each(function() {
			$(this).attr("checked", false);
		});
	});

	// Layer: Checkboxen Neuigkeiten und Vorteilsangebote auf disabled setzen und Partner-Container verstecken
	var permissionTypeBoxes = $("#tb-sendaccountstatus-post, #tb-sendaccountstatus-email");
	$("#tb-sendaccountstatus-box-nostatement input[type=checkbox]").not(permissionTypeBoxes).attr("disabled", true).attr("checked", false);
	$(".tb-which-partner-content").hide();
	
	// Layer: Checkboxen Post oder E-Mail kann nur eines ausgewaehlt werden
	permissionTypeBoxes.click(function() {
		if (!$(this).attr("checked")) {
			// alle verbleibenden Checkboxen deaktivieren
			$("#tb-sendaccountstatus-box-nostatement input[type=checkbox]").not(permissionTypeBoxes).attr("disabled", true).attr("checked", false);
			$(".tb-which-partner-content").hide();
			return;
		}
		permissionTypeBoxes.not(this).each(function() {$(this).attr("checked", false)});
		// alle verbleibenden Checkboxen aktivieren
		$("#tb-sendaccountstatus-box-nostatement input[type=checkbox]").not(permissionTypeBoxes).removeAttr("disabled").attr("checked", true);
	});
	
	// Layer: Vorteilsangebote von partner checkboxen abhaengig machen
	var bargainCheckboxes = $("#tb-bargain-Newsletter-select input[type=checkbox]");
	bargainCheckboxes.click(function() {
		$("#tb-partner-bargain").attr("checked", true);
		bargainCheckboxes.each(function() {
			if (!$(this).attr("checked")) {
				$("#tb-partner-bargain").attr("checked", false);
				return;
			}
		});
	});
	
	// wenn Vorteilsangebote, dann alle partner checkboxen aktivieren
	$("#tb-partner-bargain").click(function() {
		if ($(this).attr("checked")) {
			bargainCheckboxes.attr("checked", true);
		} else {
			bargainCheckboxes.attr("checked", false);
		}
	});
	
	// Titel: Checkbox-Magister kann nur aktiviert werden, wenn Doktor und Professor deaktiviert sind
	$("#status-title-magister").click(function() {
		if ($(this).attr("checked")) {
			$("#status-title-professor").attr("checked", false).attr("disabled", "disabled");
			$("#status-title-doctor").attr("checked", false).attr("disabled", "disabled");
		} else {
			$("#status-title-professor").removeAttr("disabled", "disabled");
			$("#status-title-doctor").removeAttr("disabled");
		}
	});
	
	$("#status-title-professor").click(function() {
		if ($(this).attr("checked")) {
			$("#status-title-magister").attr("checked", false);
			$("#status-title-magister").attr("disabled", "disabled");
		} else if ($("#status-title-doctor:checked").val() === undefined) {
			$("#status-title-magister").removeAttr("disabled");
		}
	});
	
	$("#status-title-doctor").click(function() {

		if ($(this).attr("checked")) {
			$("#status-title-magister").attr("checked", false);
			$("#status-title-magister").attr("disabled", "disabled");
		} else if ($("#status-title-professor:checked").val() === undefined) {
			$("#status-title-magister").removeAttr("disabled");
		}
	});

});