﻿		function roundNumber(num, dec) {
			var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
			return result;
		}
	
		function przelicz(ilosc) {
		var cena=0;
		if(isNaN(ilosc) || (ilosc < 1)){
			window.alert("podano złą wartość");
			return false;
			}
		else {
			if ((ilosc > 0) && (ilosc <= 2)) cena = roundNumber(ilosc * 572.95,2);
			if ((ilosc >= 3) && (ilosc <= 5)) cena = roundNumber(ilosc * 544.26,2);
			if ((ilosc >= 6) && (ilosc <= 10)) cena = roundNumber(ilosc * 515.57,2);
			if ((ilosc >= 11) && (ilosc <= 20)) cena = roundNumber(ilosc * 486.89,2);
			if ((ilosc >= 21) && (ilosc <= 50)) cena = roundNumber(ilosc * 458.20,2);
			if (ilosc >= 50) cena = roundNumber(ilosc * 429.51,2);

			document.getElementById('kwotaNetto').value = cena;
			document.getElementById('kwota').value = roundNumber((cena*22/100) + cena,2);
			document.getElementById('wartoscNetto').innerHTML = cena;
			document.getElementById('wartosc').innerHTML = roundNumber((cena*22/100) + cena,2);
			
			}
			return true;
		}

		function nextStep(step) {
			if (!przelicz(document.f.ilosc.value)) {
				return false;
			};

			if (step == 1) {
				if (validateForm(document.getElementById('f')) == false) {
					return false;
				};
			}
		
			if (step == 0) {
				if (document.f.zgoda.checked == false) {
					window.alert("Aby złożyć zamówienia należy zakceptować regulamin sklepu.");
					return false;
				};
			}
			
			document.getElementById('krok').value = parseInt(step)+1;
			document.f.submit();
			
			
		}

		function prevStep(step) {
			document.getElementById('krok').value = parseInt(step)-1;
		}
		
		function checkField(field, desc) {
			if (field.value == "") { //This checks to make sure the field is not empty
			   alert("Pole '" + desc +"' nie może być puste."); //Informs user of empty field
			   field.focus( ); //This focuses the cursor on the empty field
			   return false; //This prevents the form from being submitted
			}
			return true;   
		}
		
		function validateFirmForm(form) { //This is the name of the function
			if (!checkField(form.F_firm		, "Firma")) {return false;};
			if (!checkField(form.F_nipp		, "NIP")) {return false;};
			if (!checkField(form.F_forename	, "imię")) {return false;};
			if (!checkField(form.F_surname	, "nazwisko")) {return false;};
			if (!checkField(form.F_street	, "ulica")) {return false;};
			if (!checkField(form.F_street_n1	, "numer domu")) {return false;};
			if (!checkField(form.F_city		, "miejscowość")) {return false;};
			if (!checkField(form.F_postcode	, "kod pocztowy")) {return false;};
			return true;
		}
		
		function validateForm(form) { //This is the name of the function
			if (form.qType[1].checked == true) {
				if (!checkField(form.firmm		, "Firma")) {return false;};
				if (!checkField(form.NIPP		, "NIP")) {return false;};
			};
			if (!checkField(form.forename	, "imię")) {return false;};
			if (!checkField(form.surname	, "nazwisko")) {return false;};
			if (!checkField(form.street		, "ulica")) {return false;};
			if (!checkField(form.street_n1	, "numer domu")) {return false;};
			if (!checkField(form.city		, "miejscowość")) {return false;};
			if (!checkField(form.postcode	, "kod pocztowy")) {return false;};
			if (!checkField(form.city		, "miejscowość")) {return false;};
			if (!checkField(form.email		, "e-mail")) {return false;};
			if (form.qType[1].checked == true) {
				if (!(validateFirmForm(form))) {return false;};
			}
		}