/*
 * Update 2010-03-07 by Lukasz Perec for d4u.pl
 * Created 2006-11-23 by Lukasz Perec.  
 * 
 * */

$(function() {
	$("#warunki").change(function() {

		if (!($(this).attr('checked'))) {
			$("#platnosci-all").css('display', "none");
			$("#platnosci-none").css('display', "block");
		} else {
			$("#platnosci-all").css('display', "block");
			$("#platnosci-none").css('display', "none");
		}
	});
});

$(function() {
	$("#newsletterop").change(function() {

		if (!($(this).attr('checked'))) {
			$("#box_newsletter").css('display', "none");
		} else {
			$("#box_newsletter").css('display', "block");
		}
	});

	$('#email2').bind('paste', function(e) {
		e.preventDefault();

		$("#box_email2").addClass("blad-form");
		$("#email2_komunikat").css("display", "inline");
		$("#email2_komunikat").html('Proszę wpisać email... nie kopiować');

	});
	$('#email2').bind('keypress', function() {
		$("#box_email2").removeClass("blad-form");
		$("#email2_komunikat").css("display", "none");

	});

});

$(function() {
	$('#form_kod').submit(
			function() {
			 	
				if ($.trim($('#email').val()) == $.trim($('#email2').val())) {
						
					$("#box_email").removeClass("blad-form");
					$("#email_komunikat").css("display", "none");
					$("#box_email2").removeClass("blad-form");
					$("#email2_komunikat").css("display", "none");
					

					$.ajax( {
						type : $(this).attr('method'),
						url : $(this).attr('action'),
						data : $(this).serialize(),
						cache : false,
						dataType : 'json',

						beforeSend : function() {

							/*
							 * Mozna by dodac jakies info o wysylaniu zapytania
							 * o poprawnosc danych ...
							 */

						},
						complete : function() {

							/*
							 * a tu poprzednie info ukryc ;)
							 */

						},
						success : function(obj) {

							if (obj.kod.s) {
								/*
								 * niepoprawny kod
								 */

								$("#box_kod").addClass("blad-form");
								$("#kod_komunikat").css("display", "inline");
								$("#kod_komunikat").html(obj.kod.desc);

							} else {
								$("#box_kod").removeClass("blad-form");
								$("#kod_komunikat").css("display", "none");

							}

							if (obj.email.s) {
								/*
								 * niepoprawny email
								 */
								$("#box_email").addClass("blad-form");
								$("#email_komunikat").css("display", "inline");
								$("#email_komunikat").html(obj.email.desc);

							} else {
								$("#box_email").removeClass("blad-form");
								$("#email_komunikat").css("display", "none");

							}

							if (obj.imie.s) {
								/*
								 * trzeba wpisac imie!
								 */
								$("#box_newsletter").addClass("blad-form");
								$("#imie_komunikat").css("display", "inline");
								$("#imie_komunikat").html(obj.imie.desc);

							} else {
								$("#box_newsletter").removeClass("blad-form");
								$("#imie_komunikat").css("display", "none");

							}

							/*
							 * a tu obsluga pozostalych danych!
							 */

							if (obj.tpl.status > 0) {

								$("#step1").fadeOut('fast');
								$("#step2").fadeOut('fast');
								$("#showURL").html(
										(obj.tpl.linkURL ? obj.tpl.linkURL
												+ obj.tpl.msg : obj.tpl.msg));
								$("#step3").fadeIn('slow');

							}

						}

					});

				} else {

					$("#box_email").addClass("blad-form");
					$("#email_komunikat").css("display", "inline");
					$("#email_komunikat").html(
							'Adresy email muszą sie zgadzać!');

					$("#box_email2").addClass("blad-form");
					$("#email2_komunikat").css("display", "inline");
					$("#email2_komunikat").html(
							'Adresy email muszą sie zgadzać!');

				}

				return false;

			});

});

