$j.validator.setDefaults({
	//submitHandler: function() { alert("submitted!"); }
});

$j().ready(function() {
	// validate the comment form when it is submitted
	$j("#preformularz").validate();

	// validate signup form on keyup and submit
	$j("#formularz").validate({
		rules: {
                        tytul: {
                                required: true,
                                znaki: true
                                },
                        tresc: {
                                required:true,
                                maxlength: 2000,
                                znaki: true
                                },
                        mailaddr: {
				required: true,
				email: true
			},
                        mailaddr2: {
				required: true,
				email: true,
                                equalTo: "#mailaddr"
			},
                        haslo: {
				required: true,
				minlength: 5,
                                znaki: true
			},
			haslo2: {
				required: true,
				minlength: 5,
                                znaki: true,
				equalTo: "#haslo"
			},
			imie: "required",
			//nazwisko: "required",
                        //data_ur: "required",
                        firma: "required",
                        nip: {
				//required: true,
				minlength: 10
			},
                        wojewodztwo: "required",
                        miasto: "required",
                        //kod_pocztowy: "required",
			akceptacja: "required",
                        c_temat: {
                                required: true,
                                znaki: true
                                },
                        c_tresc: {
                                required:true,
                                maxlength: 2000,
                                znaki: true
                                },
                        c_mail: {
				required: true,
				email: true
			},
			c_imie: "required",
                        c_telefon: "required"
		},
		messages: {
                        tytul: {
                                required: "Podaj tytuł"
                                },
                        tresc: {
                                required: "Podaj treść",
                                maxlength: "Maksymalna długość: 2000 znaków"
                                },
                        mailaddr: "Podaj prawidłowy adres e-mail",
                        mailaddr2: {
				required: "Powtórz e-mail",
                                email: "Podaj prawidłowy adres e-mail",
				equalTo: "E-maile się nie zgadzają!"
			},
                        haslo: {
				required: "Podaj hasło",
				minlength: "Twoje hasło musi być dłuższe niż 5 znaków"
			},
			haslo2: {
				required: "Powtórz hasło",
				minlength: "Twoje hasło musi być dłuższe niż 5 znaków",
				equalTo: "Hasła się nie zgadzają!"
			},
                        firma: "Podaj nazwę firmy",
                        //nip: "Pofaj NIP firmy (bez pauz)",
                        wojewodztwo: "Wybierz województwo",
                        miasto: "Wybierz miasto lub podaj miejscowość",
                        //kod_pocztowy: "Podaj kod pocztowy",
			imie: "Podaj imię",
			//nazwisko: "Podaj nazwisko",
                        //data_ur: "Podaj datę urodzenia",
			akceptacja: "Zaakceptuj regulamin!",
                        c_temat: {
                                required: "Podaj temat"
                                },
                        c_tresc: {
                                required: "Podaj treść",
                                maxlength: "Maksymalna długość: 2000 znaków"
                                },
                        c_imie: "Podaj imię i nazwisko",
                        c_mail: "Podaj prawidłowy adres e-mail",

                        c_telefon: "Podaj telefon"
		}
	});

	// check if confirm password is still valid after password changed
	$j("#haslo").blur(function() {
		$j("#haslo2").valid();
	});

	//code to hide topic selection, disable for demo
	var kto = $j("#kto");
	// newsletter topics are optional, hide at first
	var inital = kto.is(":checked") ;
	var topics = $j("#dlafirma")[inital ? "removeClass" : "addClass"]("gray");
	var topicInputs = topics.find("input").attr("disabled", !inital);
        var topics2 = $j("#dlauser")[inital ? "removeClass" : "addClass"]("gray");
	var topicInputs2 = topics2.find("input").attr("disabled", inital);
	// show when newsletter is checked
	kto.click(function() {
		topics[this.checked ? "removeClass" : "addClass"]("gray");
		topicInputs.attr("disabled", !this.checked);
		topics2[this.checked ? "removeClass" : "addClass"]("gray");
		topicInputs2.attr("disabled", this.checked);


/*        	//code to hide topic selection, disable for demo
	var newsletter = $j("#newsletter");
	// newsletter topics are optional, hide at first
	var inital = newsletter.is(":checked");
	var topics = $j("#newsletter_topics")[inital ? "removeClass" : "addClass"]("gray");
	var topicInputs = topics.find("input").attr("disabled", !inital);
	// show when newsletter is checked
	newsletter.click(function() {
		topics[this.checked ? "removeClass" : "addClass"]("gray");
		topicInputs.attr("disabled", !this.checked);
*/
	});
});




// apply inline-box only for mozilla
if( jQuery.browser.mozilla ) {
	// do when DOM is ready
	$j( function() {
		// search form, hide it, search labels to modify, filter classes nocmx and error
		$j( 'form.cmxform' ).hide().find( 'p>label:not(.nocmx):not(.error)' ).each( function() {
			var $jthis = $j(this);
			var labelContent = $jthis.html();
			var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
			// create block element with width of label
			var labelSpan = $j("<span>")
				.css("display", "block")
				.width(labelWidth)
				.html(labelContent);
			// change display to mozilla specific inline-box
			$jthis.css("display", "-moz-inline-box")
				// remove children
				.empty()
				// add span element
				.append(labelSpan);
		// show form again
		}).end().show();
	});
};