// apply inline-box only for mozilla
if( jQuery.browser.mozilla ) {
	// do when DOM is ready
	$( function() {
		// search form, hide it, search labels to modify, filter classes nocmx and error
		$( 'form.cmxform' ).hide().find( 'p>label:not(.nocmx):not(.error)' ).each( function() {
			var $this = $(this);
			var labelContent = $this.html();
			var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
			// create block element with width of label
			var labelSpan = $("<span>")
				.css("display", "block")
				.width(labelWidth)
				.html(labelContent);
			// change display to mozilla specific inline-box
			$this.css("display", "-moz-inline-box")
				// remove children
				.empty()
				// add span element
				.append(labelSpan);
		// show form again
		}).end().show();
	});
};


//$.validator.setDefaults({
//	submitHandler: function() { alert("submitted!"); }
//});


$().ready(function() {
	// validate the comment form when it is submitted
	$("#commentForm").validate();
	
	// validate signup form on keyup and submit
	$("#signupForm").validate({
		rules: {

			captcha: { 
       			required: true,
				remote: {
        				url: "http://www.jewishinstitute.org.pl/check.php",
        				type: "post"
						}
    
   				}, 
 	


			KlientImie: {
				required: true,
				minlength: 2
			},
			KlientNazwisko: {
				required: true,
				minlength: 3
			},
			KlientUlica: "required",
			KlientDom: "required",
			KlientLokal: "required",
			KlientPostcode: {
				required: true,
				KlientPostcode: true
			},
			KlientMiasto: "required",
			KlientTelefon: "required",
			
			temat: {
				required: true,
				minlength: 5
			},
			tresc: {
				required: true,
				minlength: 15
			},
			opis: {
				required: true,
				minlength: 15
			},
			KlientMail: {
				required: true,
				KlientMail: true
			},
			url: {
				required: true,
				url: true
			},
			password: {
				required: true,
				minlength: 6,
				maxlength: 12
			},
			confirm_password: {
				required: true,
				minlength: 6,
				maxlength: 12,
				equalTo: "#password"
			},
			loginemail: {
				required: true,
				loginemail: true
			},
			haslo: {
				required: true,
				minlength: 6,
				maxlength: 12
			},
			Tytul: {
				required: true,
				minlength: 5
			},
			TytulKsiazki: {
				required: true,
				minlength: 5
			},
			Nazwa: {
				required: true,
				minlength: 5
			},			
			Kategoria: "required",
			Wojew: "required",
			Dzial: "required",
			country: "required",
			agree: "required"
			
			
		},
		messages: {
			captcha: { 
  				required: "Wpisz znaki widoczne obok na orazku (zachowaj wielkość):",
 				remote: jQuery.format("Wpisz poprawnie znaki.") 
  			 }, 
			KlientImie: {
				required:"Proszę wypełnić popranie pole Imię",
				minlength: "Imię wymaga minimum 2 znaków"
			},
			KlientNazwisko: {
				required:"Proszę wypełnić popranie pole Nazwisko",
				minlength: "Nazwisko wymaga minimum 3 znaków"
			},
			KlientUlica: "Proszę wypełnić pole Ulica",
			KlientDom: " ",
			KlientLokal: "Proszę wypełnić zaznaczone pole",
			KlientPostcode: "Proszę wypełnić popranie Kod pocztowy",
			KlientMiasto: "Proszę wypełnić pole Miejscowość",
			KlientTelefon: "Proszę wypełnić pole Telefon",
			url: "Proszę o porane wypełninie",
			KlientMail: "Proszę poprawnie wypełnić pole E-mail",
			temat: {
				required: "Proszę wypełnić pole Temat",
				minlength: "Temat wymaga minimum 5 znaków"
			},
			tresc: {
				required: "Proszę wypełnić pole Pytanie",
				minlength: "Pytanie wymaga minimum 15 znaków"
			},
			opis: {
				required: "Proszę wypełnić pole Opis",
				minlength: "Opis wymaga minimum 15 znaków"
			},
			password: {
				required: "Proszę wypełnić pole Hasło",
				minlength: "Hasło wymaga minimum 6 znaków",
				maxlength: "Hasło nie może przekraczać 12 znaków"
			},
			confirm_password: {
				required: "Proszę wypełnić pole Powtórz hasło",
				minlength: "Hasło wymaga minimum 6 znaków",
				maxlength: "Hasło nie może przekraczać 12 znaków",
				equalTo: "Wpisane hasła nie są takie same!"
			},
			loginemail: "Proszę poprawnie wypełnić pole E-mail",
			haslo: {
				required: "Proszę wypełnić pole Hasło",
				minlength: "Hasło wymaga minimum 6 znaków",
				maxlength: "Hasło nie może przekraczać 12 znaków"
			},
			Tytul: {
				required: "Proszę wypełnić Tytuł artykułu",
				minlength: "Tytuł artykułu wymaga minimum 5 znaków"
			},
			TytulKsiazki: {
				required: "Proszę wpisać Tytuł",
				minlength: "Tytuł artykułu wymaga minimum 5 znaków"
			},
			Nazwa: {
				required: "Proszę wpiasć nazwę",
				minlength: "Nazwa wymaga minimum 5 znaków"
			},
			Kategoria: "Proszę wybrać kategorję informacji",
			Wojew: "Proszę wybrać Województwo",
			Dzial: "Proszę wybrać dział",
			country: "Proszę wybrać Państwo",
			osPrzesylka: "Proszę wypełnić poprawnie wagę produktu",
			agree: "Akceptacja obowiązkowa"
			
		}
	});
	
	// 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;
		}
	});
	
	// check if confirm password is still valid after password changed
	$("#password").blur(function() {
		$("#confirm_password").valid();
	});
	
	//code to hide topic selection, disable for demo
	var addres = $("#addres");
	// addres topics are optional, hide at first
	var inital = addres.is(":checked");
	var topics = $("#addres_topics")[inital ? "removeClass" : "addClass"]("gray");
	var topicInputs = topics.find("input").attr("disabled", !inital);
	// show when addres is checked
	addres.click(function() {
		topics[this.checked ? "removeClass" : "addClass"]("gray");
		topicInputs.attr("disabled", !this.checked);

	});

var faktura = $("#faktura");
	// faktura topics are optional, hide at first
	var inital2 = faktura.is(":checked");
	var topics2 = $("#faktura_topics")[inital2 ? "removeClass" : "addClass"]("fakt");
	var topicInputs2 = topics2.find("input").attr("disabled", !inital2);
	// show when faktura is checked
	faktura.click(function() {
		topics2[this.checked ? "removeClass" : "addClass"]("fakt");
		topicInputs2.attr("disabled", !this.checked);

	});






	var kontakt = $("#kontakt");
	// faktura topics are optional, hide at first
	var inital3 = faktura.is(":checked");
	var topics3 = $("#kontakt_topics")[inital3 ? "removeClass" : "addClass"]("fakt");
	var topicInputs3 = topics3.find("input").attr("disabled", !inital3);
	// show when faktura is checked
	kontakt.click(function() {
		topics3[this.checked ? "removeClass" : "addClass"]("fakt");
		topicInputs3.attr("disabled", !this.checked);

	});


	$("#signupForm2").validate({
		rules: {
			
			KlientImie: {
				required: true,
				minlength: 2,
				KlientImie: true
				
			},
			KlientNazwisko: {
				required: true,
				minlength: 2,
				KlientNazwisko: true
				
			},
			
			KlientMail: {
				required: true,
				KlientMail: true
			},
			password: {
				required: true,
				minlength: 6,
				maxlength: 12
			},
			confirm_password: {
				required: true,
				minlength: 6,
				maxlength: 12,
				equalTo: "#password"
			},

			
			agree: "required"
			
			
		},
		messages: {
			KlientImie: "Proszę wypełnić popranie pole Imię",
			KlientNazwisko: "Proszę wypełnić popranie pole Nazwisko",
			KlientMail: "Proszę poprawnie wypełnić pole E-mail",
			password: {
				required: "Proszę wypełnić pole Hasło",
				minlength: "Hasło wymaga minimum 6 znaków",
				maxlength: "Hasło nie może przekraczać 12 znaków"
			},
			confirm_password: {
				required: "Proszę wypełnić pole Powtórz hasło",
				minlength: "Hasło wymaga minimum 6 znaków",
				maxlength: "Hasło nie może przekraczać 12 znaków",
				equalTo: "Wpisane hasła nie są takie same!"
			},
			agree: "Akceptacja obowiązkowa"
			
		}
	});
	


});


   

$(document).ready(
	function()
	{
		$('a#form_open').click(function()
			{
				$('div.kontakt:hidden').show();
				$('a#form_open:visible').hide();
				$('a#form_close:hidden').show();
				return false;
			}
		);
		$('a#form_close').click(function()
			{
				$('div.kontakt:visible').hide();
				$('a#form_close:visible').hide();
				$('a#form_open:hidden').show();
				return false;
			}
		);
	}
);
