function label($for, $input){

	$("label").each(function(){
		if ($(this).attr("for") == $for ){ 
		wartosc = $(this).text()+" "+$input;
		}
	});
return wartosc;
}

function wal(produkt){

var zawartosc = ("<b>Miejsce nadania i doręczenia</b><br/>");
	$("#miejsce").find("select").each(function(){	
			zawartosc += label($(this).attr("id"), $(this).val())+"<br/>";
			
	});

	$("#miejsce").find("input:filled").each(function(){	
		if ($(this).attr("type") != "radio"){
			zawartosc += label($(this).attr("id"), $(this).val())+"<br/>";
		}
		
	});
	zawartosc += ("<br/><b>Co nadajesz?</b><br/>");
	$("#zawartosc").find("select").each(function(){	
			zawartosc += label($(this).attr("id"), $(this).val())+"<br/>";
	});
	$("#zawartosc").find("input:filled").each(function(){	
		if ($(this).attr("type") != "radio"){
			zawartosc +=  label($(this).attr("id"), $(this).val())+"<br/>";
		}
		
	});
	zawartosc += ("<br/><b>Czy zależy Ci na szybkiej dostawie?</b><br/>");
		$("#czas").find("input:radio[checked]").each(function(){
		zawartosc +=  label($(this).attr("id"), $(this).val())+"<br/>";
	});
	zawartosc += ("<br/><b>Wybrany produkt DHL</b><br/>"+produkt+"<br/><br/><b>Wybrane usługi dodatkowe</b><br/>");
	$("#listaProduktow").find("input:checkbox[checked]").each(function(){	
			zawartosc += $(this).attr("id")+"<br/>";
	});

	
	
	window.location.href = 'kontakt.php?zawartosc='+zawartosc+'&produkt='+produkt;

}
var rulK = {
	rules: {
        numerklienta:{
            minLength: 7,
            maxLength: 7,
            digits: true
        },
        email:{
			required: true,
			minLength: 4,
			maxLength: 255,
			email: true
		},
		imie:{
			required: true,
			minLength: 2,
			maxLength: 32,
			remote: "Wulgaryzmy.php"
		},
		nazwisko:{
			required: true,
			minLength: 2,
			maxLength: 64,
			remote: "Wulgaryzmy.php"
		},
		nazwafirmy:{
			required: "#usertype:checked",
			minLength: 2,
			maxLength: 64,
			remote: "wulgaryzmy.php"
		},
		nip:{
            required: "#usertype:checked",
            maxLength: 13,
			remote: "nip.php"
		},
		ulica:{
			required: true,
			minLength: 4,
			maxLength: 78,
			remote: "wulgaryzmy.php"
		},
		nrdomu:{
            required: true,
			minLength: 1,
			maxLength: 10
		},
		nrlokalu:{
            digits: true,
			maxLength: 8
		},
		kodpocztowy:{
			required: true,
			remote: "postcode.php"
		},
		miejscowosc:{
			required: true,
			minLength: 2,
			maxLength: 100
		},
		phone:{
			required: true
		},
		zgodanaprzetwarzanie:{
            required: true
        }
	},
	
	messages: {
        numerklienta:{
            minLength: "Podaj minimum 7 cyfr",
            maxLength: "Podaj maksimum 7 cyfr",
            digits: "Podaj tylko cyfry"
        },
        email:{
			required: "Podaj adres e-mail",
			minLength: "Podaj minimum 4 znaki",
			maxLength: "Podaj maksimum 255 znaków",
            email: "Podaj poprawny adres e-mail"
		},
		imie:{
			required: "Podaj imię",
			minLength: "Podaj minimum 2 znaki",
			maxLength: "Podaj maksimum 32 znaki",
			remote: "Niedozwolone imię"
		},
		nazwisko:{
			required: "Podaj nazwisko",
			minLength: "Podaj minimum 2 znaki",
			maxLength: "Podaj maksimum 64 znaki",
			remote: "Niedozwolone nazwisko"
		},
		nazwafirmy:{
			required: "Podaj nazwę firmy",
			minLength: "Podaj minimum 2 znaki",
			maxLength: "Podaj maksimum 64 znaki",
			remote: "Niedozwolone imię"
		},
		nip:{
            required: "Podaj numer NIP",
            maxLength: "Wpisz maksimum 13 znaków",
            remote: "Zły format numeru nip"
		},
		ulica: {
            required: "Wprowad nazwę ulicy",
            minLength: "Wpisz minimum 4 znaki",
            maxLength: "Wpisz maksimum 78 znaków",
            remote: "Niedozwolona ulica"
        },
		nrdomu: {
            required: "Wprowad numer domu",
            minLength: "Wpisz minimum 1 znak",
            maxLength: "Wpisz maksimum 10 znaków"
        },
        nrlokalu: {
            digits: "Wprowad tylko cyfry",
            maxLength: "Wpisz maksimum 8 znaków"
        },
		kodpocztowy:{
			required: "Podaj kod pocztowy",
			remote: "Niepoprawny kod pocztowy"
		},
		miejscowosc:{
			required: "Podaj miejscowoć",
			minLength: "Podaj minimum 2 znaki",
			maxLength: "Podaj maksimum 100 znaków"
		},
		phone:{
			required: "Podaj numer telefonu"
		},
		zgodanaprzetwarzanie:{
            required: "Zgoda jest wymagana"
        }
	},
	
    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.next("span") );
    },
	submitHandler: function(form) {
		if(this.remoteCalls==0) {
			this.currentForm.submit();
		}
    },
	success: "success",
	afterRemoteCallback:function(){
		return;
		}
};


$(document).ready(function(){   
	validatorK = $("#frm").validate(rulK);
	
	$("#kodpocztowy").mask("99-999",{placeholder:" "});
    $("#cellphone").mask("999 999 999",{placeholder:" "});
    $("#phone").mask("999 999 999",{placeholder:" "});
	
	$("#nazwafirmy").attr( "disabled", "disabled" );
    $("#nazwafirmydiv").hide();
    $("#nip").attr( "disabled", "disabled" );
    $("#nipdiv").hide();
    
    $("#miejscowosc").autocomplete("search.php", { minChars:0, matchSubset:1, matchContains:1, selectOnly:1, depend:"kodpocztowy" } );

    $("input[name=usertype]").click( function() {
        if( $(this).val() == "N" ) {
            $("#nazwafirmy").attr( "disabled", "disabled" );
            $("#nazwafirmydiv").hide();
            $("#nip").attr( "disabled", "disabled" );
            $("#nipdiv").hide();
        } else {
            $("#nazwafirmy").removeAttr( "disabled" );
            $("#nazwafirmydiv").show();
            $("#nip").removeAttr( "disabled" );
            $("#nipdiv").show();
        }
    });
});

