$(document).ready(function() {
	// validate signup form on keyup and submit
	var validator = $("#signupform").validate({
		rules: {
			firma: "required",
			osoba: "required",
			miejscowosc: {
				required: true
			},
			telefon_kontaktowy: {
				required: true,
				minlength: 9
			},

			e_mail: {
				required: true,
				email: true
			},
			tresc_wiadomosci: "required"
		},
		messages: {
			firma: "Pole Firma jest wymagane.",
			osoba: "Pole Osoba jest wymagane.",
			miejscowosc: {
				required: "Pole Miejscowość jest wymagane."
			},
			telefon_kontaktowy: {
				required: "Pole Telefon kontaktowy jest wymagane.",
				minlength: jQuery.format("Pole Telefon kontaktowy musi zawierać conajmniej {0} znaków.")
			},

			e_mail: {
				required: "Pole E-mail jest wymagane.",
				email: "To nie jest poprawny adres e-mail.",
				remote: jQuery.format("{0} is already in use")
			},
			tresc_wiadomosci: "Pole Treść wiadomości jest wymagane."
		},
		// 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().next() );
		},
		// specifying a submitHandler prevents the default submit, good for the demo
		submitHandler: function() {
			 document.signupform.submit();
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
	});
	
	// propose username by combining first- and lastname
	$("#username").focus(function() {
		var firstname = $("#firstname").val();
		var lastname = $("#lastname").val();
		if(firstname && lastname && !this.value) {
			this.value = firstname + "." + lastname;
		}
	});

});
