$(document).ready(function(){
	//hide bottom part of form at start
	//$("#sub_form").hide();
	if ($("input#user_type_1").is(':checked')) {
		$("#sub_form").show();
	}else if ($("input#user_type_2").is(':checked')) {
		$("#sub_form").show();
	}else if ($("input#user_type_0").is(':checked')) {
		$("#sub_form").hide();
	}
	$("#register_tweets").hide();
	if ($("input#furthereducationopt_1").is(':checked')) {
		$("#further_education_type").hide();	
	}
	// validate signup form on keyup and submit
	$("#signupForm").validate({
		rules: {
			user_firstname: "required",
			user_surname: "required",
			username: {
				required: true,
				minlength: 2
			},
			password: {
				required: true,
				minlength: 5
			},
			confpass: {
				required: true,
				minlength: 5,
				equalTo: "#password"
			},
			useremail: {
				required: true,
				email: true
			},
			confirmemail: {
				required: true,
				email: true
			},
			topic: {
				required: "#newsletter:checked",
				minlength: 2
			},
			agree: "required",

		},
		messages: {
			user_firstname: "Please enter your firstname",
			user_surname: "Please enter your surname",
			username: {
				required: "Please enter a username",
				minlength: "Your username must consist of at least 2 characters"
			},
			password: {
				required: "Please create a password",
				minlength: "Your password must be at least 5 characters long"
			},
			confpass: {
				required: "Please confirm the login password",
				minlength: "Your password must be at least 5 characters long",
				equalTo: "Please enter the same password as above"
			},
			useremail: "Please enter a valid email address",
			confirmemail: "Please re-enter email address",
			agree: "Please accept our policy",

		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo( element.parent().next().next() );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.next() );
			else
				error.appendTo( element.parent());
			}
	});
	
	// toggle optional billing address
	var subTableDiv = $("#sub_form");
	/*
	var toggleCheck = $("input#emailoptin");
	toggleCheck.is(":checked")
	? subTableDiv.hide()
	: subTableDiv.show();
	$("input#emailoptin").click(function() {
		if (this.checked == true) {
			subTableDiv.slideUp("medium");
		$("#signupForm").valid();
		} else {
			subTableDiv.slideDown("medium");
		}
	});
	*/
	$("input#user_type_0").click(function() {
		if ($(this).is(':checked')) {
			subTableDiv.slideUp("medium");
			$("#signupForm").valid();
		}
	});
	$("input#user_type_1").click(function() {
		if ($(this).is(':checked')) {
			subTableDiv.slideDown("medium");
			$("#signupForm").valid();
		}
	});
	$("input#user_type_2").click(function() {
		if ($(this).is(':checked')) {
			subTableDiv.slideDown("medium");
			$("#signupForm").valid();
		}
	});
	$("input#furthereducationopt_1").click(function() {
		if ($(this).is(':checked')) {
			$("#further_education_type").slideUp("medium");
			$("#signupForm").valid();
		}
	});
	$("input#furthereducationopt_0").click(function() {
		if ($(this).is(':checked')) {
			$("#further_education_type").slideDown("medium");
			$("#signupForm").valid();
		}
	});
	
	//hide publish tweets option
	//$("input#twitter_url").click(function() {
		if ($("input#twitter_url").value!="") {
			$("#register_tweets").slideDown("medium");
			$("#signupForm").valid();
		}
//	});
	/*
	$("input#user_type").click(function() {
		if (this.checked == true || $("input#user_type_2").checked ==true) {
			subTableDiv.slideDown("medium");
			$("#signupForm").valid();
		} 
	});
	$("input#user_type").click(function() {
		if (this.checked == true || $("input#user_type_1").checked ==true) {
			subTableDiv.slideDown("medium");
			$("#signupForm").valid();
		} 
	});
*/
});