var pole;
var obwod;
var ilosc;
var suma = 0;
var nazwa_tech;
var dodatki_tab = new Array();
var nazwy = new Array();
var czas = 0;
var wymx = 0;
var wymy = 0;
var zmienna = 0;
function spr(){
	zmienna = 0;
}

function spr1(){
	if(zmienna == 0) {
		oblicz();
		alert("Nie obliczono kosztu zamówienia. Koszt zostanie automatycznie obliczony.");
		return;
	}
}

function clear(){
	for(var i=62; i < 72; i++)
		if(document.getElementById('d_'+i).checked) document.getElementById('d_'+i).checked = !document.getElementById('d_'+i).checked;
}

function oblicz(){
	zmienna = 1;
	daj_wymiar(sprawdz(document.kalkulator.material));
	x1 = parseFloat(document.kalkulator.dlugosc.value)/100;
		y2 = parseFloat(document.kalkulator.szerokosc.value)/100;
		p2 = x1*y2;
		
		x2 = document.kalkulator.dlugosc.value;
		y1 = document.kalkulator.szerokosc.value;
		
	if(x2>y1)
		{
			temp = x2;
			x2 = y1;
			y1 = temp;
			//alert("Zamień długość z szerokością.");
		}	
	//alert(wymx);alert(document.kalkulator.szerokosc.value);
	//alert(wymy);alert(document.kalkulator.dlugosc.value);
	if(document.kalkulator.dlugosc.value == "" || document.kalkulator.szerokosc.value == "" || document.kalkulator.ilosc.value == ""){
		alert('Musisz podać długość, szerokość i ilość!');
		return;
	} 
	
	else if(((x2 > wymx) || (y1 > wymy) )&&(p2>1)){
		//alert("document.kalkulator.dlugosc.value "+document.kalkulator.dlugosc.value+"  wymx  "+ wymx +" document.kalkulator.szerokosc.value "+document.kalkulator.szerokosc.value+" wymy  "+wymy);
		//msg = 'Maksymalny wymiar wybranego przez Ciebie materiału to '+wymx+' / '+wymy+' cm zmniejsz wymiar swojej pracy.';
		alert('Maksymalny wymiar wybranego przez Ciebie materiału to '+wymx+' / '+wymy+' cm. Zmniejsz wymiar swojej pracy.');
		return;
	}else {
		
		suma = 0;
		dodatki_tab = new Array();
		nazwy = new Array();
		
		x = parseFloat(document.kalkulator.dlugosc.value)/100;
		y = parseFloat(document.kalkulator.szerokosc.value)/100;
		ilosc = document.kalkulator.ilosc.value;
		
		pole = x*y;
		obwod = (2*x)+(2*y);
		
		technologia = sprawdz(document.kalkulator.technologie);
		//alert(technologia);
		material = sprawdz(document.kalkulator.material);

		ustaw_czas(material);
		podloze = sprawdz(document.kalkulator.podloze);
		//alert(podloze);
		dodatki = sprawdz(document.kalkulator.dodatki);
		if(technologia != 3){
	/**/	if((podloze == 55) && (document.kalkulator.szerokosc.value > 60) )//&& (technologia != 3))
		{
			alert('Maksymalna szerokość podkładu to: 60 cm');
			suma = "(Zmniejsz szerokość) ";
		}else if(pole < (document.getElementById('pole_'+material).value) && (document.getElementById('pole_'+material).value) == 1 )// && (technologia != 3))
		{
			alert('Minimalny wymiar zamówienia to: 1 m2, zwiększ wymiar swojej pracy. Jeżeli potrzebujesz mniejszy użytek to zduplikuj go aby uzyskać min. 1 m2');
			suma = "(Zwiększ wymiar pracy) ";
		}
		else if(pole < (document.getElementById('pole_'+material).value))//  && (technologia != 3))
	{
		if((pole * ilosc) < (document.getElementById('pole_'+material).value))
		alert('Dla tego wymiaru obowiązuje cena minimalna. Możesz zwiększyć wymiar pracy lub ilość sztuk');
		//daj_cene_tech(technologia);
		//alert(ilosc);
		if(ilosc>1)
		{
			//alert((pole*ilosc));
		if((pole * ilosc) < (document.getElementById('pole_'+material).value))
		{
			
			pole = (document.getElementById('pole_'+material).value);
		daj_cene(material);
			daj_cene(podloze);
		//alert(suma);
			cena_podstawowa = suma;
			cena_podstawowa = Math.round(cena_podstawowa*100)/100;
		
			daj_cene_dodatkow(dodatki_tab);
			
		}else{
			daj_cene(material);
			daj_cene(podloze);
		//alert(suma);
			cena_podstawowa = suma*ilosc;
			cena_podstawowa = Math.round(cena_podstawowa*100)/100;
		
			daj_cene_dodatkow(dodatki_tab);
			suma = suma * ilosc;
		}
		
		}
		else
		{
			pole = (document.getElementById('pole_'+material).value);
		daj_cene(material);
		
		//suma += Math.round((cena*pole)*100)/100;
		daj_cene(podloze);
		
		cena_podstawowa = suma * ilosc;
		cena_podstawowa = Math.round(cena_podstawowa*100)/100;
		
		daj_cene_dodatkow(dodatki_tab);
		suma = suma * ilosc;
		}
		suma = Math.round(suma*100)/100;
		
		cena_dodatkow = suma - cena_podstawowa;
		cena_dodatkow = Math.round(cena_dodatkow*100)/100;
	}
	else
	{				
		daj_cene_tech(technologia);
		daj_cene(material);
		daj_cene(podloze);
		
		cena_podstawowa = suma * ilosc;
		cena_podstawowa = Math.round(cena_podstawowa*100)/100;
		
		daj_cene_dodatkow(dodatki_tab);
		
		suma = suma * ilosc;
		suma = Math.round(suma*100)/100;
		
		cena_dodatkow = suma - cena_podstawowa;
		cena_dodatkow = Math.round(cena_dodatkow*100)/100;
	}
		}
	else
	{				
		daj_cene_tech(technologia);
		daj_cene(material);
		daj_cene(podloze);
		
		cena_podstawowa = suma * ilosc;
		cena_podstawowa = Math.round(cena_podstawowa*100)/100;
		
		daj_cene_dodatkow(dodatki_tab);
		
		suma = suma * ilosc;
		suma = Math.round(suma*100)/100;
		
		cena_dodatkow = suma - cena_podstawowa;
		cena_dodatkow = Math.round(cena_dodatkow*100)/100;
	}
		var dpi = 0;
		if(technologia == 1) $dpi = 540;
		if(technologia == 2) $dpi = 720;
		if(technologia == 3) $dpi = 1440;
		
		document.getElementById('razem_cena').innerHTML = cena_podstawowa+'zł';
		document.getElementById('razem_dodatki').innerHTML = cena_dodatkow+'zł';
		document.getElementById('razem_brutto').innerHTML = suma+'zł';
		
		document.getElementById('send_cena').value = suma;
		document.getElementById('send_szerokosc').value = document.kalkulator.szerokosc.value;
		document.getElementById('send_dlugosc').value = document.kalkulator.dlugosc.value;
		document.getElementById('send_nazwy').value = nazwy;
		document.getElementById('send_tech').value = technologia;
		document.getElementById('send_sztuki').value = ilosc;
		document.getElementById('send_czas').value = czas;
		//Math.round((x*y)*100)/100;
	}

}

function sprawdz(pole){
	len = pole.length;
	chosen = "";
	
	if(pole == document.kalkulator.dodatki){
		
	 		for (i = 0; i <len; i++) {
				if (pole[i].checked) {
					dodatki_tab.push(pole[i].value);
				}
			}
			
		return dodatki_tab;
		
	} else {
	 		
			for (i = 0; i <len; i++) {
				if (pole[i].checked) {
					chosen = pole[i].value;
				}
			}
		if(chosen == '') chosen = 0;

		return chosen;
	 }

}

function daj_cene(id){
	if(id == 'inne' || id == 0){
		nazwa = 'inne';
		cena = 0;
		jm = 'szt';
	} else {
		nazwa = document.getElementById('nazwa_'+id).value;
		cena = document.getElementById('cena_'+id).value;
		jm = document.getElementById('jm_'+id).value;
	}

	switch(jm){
		case 'm2': suma += Math.round((cena*pole)*100)/100; break;
		case 'mb': suma += Math.round((cena*obwod)*100)/100; break;
		case 'szt': suma += Math.round(cena*100)/100;
	}
	nazwy.push(nazwa);
	
}

function ustaw_czas(id){
	if(id == 'inne' || id == 0) {
		czas = 0;
	} else {
		czas = document.getElementById('czas_'+id).value;
	}
}

function daj_cene_tech(id){
	if(id == 'inne' || id == 0){
		nazwa = 'inne';
		cena = 0;
	} else {
		nazwa = document.getElementById('nazwa_'+id).value;
		cena = document.getElementById('cena_'+id).value;
	}

	suma += Math.round(cena*100)/100;
	
	nazwa_tech = nazwa;
	
}

function daj_wymiar(id){
	if(id == 'inne' || id == 0){
		wymx = 0;
		wymy = 0;
	} else {
		var tmp = document.getElementById('wymiar_'+id).value;
		wymx = tmp.substr(0, 3);
		wymy = tmp.substr(4, 3);
		
		
	}	
}

function daj_cene_dodatkow(tablica){
	if(tablica.length > 0){
		
		for(var i = 0; i < tablica.length; i++){
			daj_cene(tablica[i]);
		}
	} 
}

function showDIV1(id){
	
	if(id == 1){
		document.getElementById('spec_podloza').style.display = 'block';
					
	} 
	if(id == 0){
		document.getElementById('spec_podloza').style.display = 'none';
		document.getElementsByName('podloze').item(13).checked = 'checked';
	} 
	
}

function showDIV(id){
	
	if(id == 3){
		clear();
		document.getElementById('spec_material').style.display = 'none';
		document.getElementById('spec_mat_720').style.display = 'none';	
		document.getElementById('spec_mat_1440').style.display = 'block';
		document.getElementById('spec_podloza').style.display = 'none';
		document.getElementById('spec_dodatki').style.display = 'none';
		document.getElementById('spec_dodatki_1440').style.display = 'block';
					
	} 
	if(id == 2){
		clear();
		document.getElementById('spec_mat_720').style.display = 'block';
		document.getElementById('spec_material').style.display = 'none';		
		document.getElementById('spec_mat_1440').style.display = 'none';
		document.getElementById('spec_podloza').style.display = 'block';
		document.getElementById('spec_dodatki').style.display = 'block';
		document.getElementById('spec_dodatki_1440').style.display = 'none';
	} 
	if(id == 1){
		clear();
		document.getElementById('spec_mat_1440').style.display = 'none';
		document.getElementById('spec_mat_720').style.display = 'none';
		document.getElementById('spec_material').style.display = 'block';
		document.getElementById('spec_podloza').style.display = 'block';
		document.getElementById('spec_dodatki_1440').style.display = 'none';
		document.getElementById('spec_dodatki').style.display = 'block';
		
		//document.kalkulator.material[0].checked = true;
	}
}
//walidacja formularzu
function waliduj(){
	
	var bad = false;
	var formularz = document.forms[0];
	var napis = "";
	
	if(formularz.nazwa_firma.value.length < 3 || formularz.nazwa_firma.value.length > 32){
		napis += 'Nazwa firmy musi mieć min. 3 znaki i maksimum 32 znaki\n';
		bad = true;
	}
	
	if(formularz.tel_firma.value.length < 3 || formularz.tel_firma.value.length > 15){
		napis += 'Telefon firmy musi mieć min. 3 znaki i maksimum 15 znaków\n';
		bad = true;
	}
	
	if(formularz.ulica_firma.value.length < 3 || formularz.ulica_firma.value.length > 20){
		napis += 'Nazwa ulicy firmy musi mieć min. 3 znaki i maksimum 20 znaków\n';
		bad = true;
	}	

	if(formularz.mail_firmy.value.length < 3 || formularz.mail_firmy.value.length > 40){
		napis += 'Adres mail firmy musi mieć min. 3 znaki i maksimum 40 znaków\n';
		bad = true;
	}
	
	if(formularz.kod_pocz_firma.value.length != 6 ){
		napis += 'Kod pocztowy firmy powinien mieć format 00-000\n';
		bad = true;
	}	
	
	if(formularz.miej_firma.value.length < 3 || formularz.miej_firma.value.length > 24){
		napis += 'Miescowość  musi mieć min. 3 znaki i maksimum 24 znaków\n';
		bad = true;
	}
	
	if(formularz.imie_zam_firma.value.length < 3 || formularz.imie_zam_firma.value.length > 15){
		napis += 'Imię zamawiającego  musi mieć min. 3 znaki i maksimum 15 znaków\n';
		bad = true;
	}
	
	if(formularz.NIP_firma.value.length > 13){
		napis += 'Pole NIP jest zbyt długie\n';
		bad = true;
	}
	
	if(formularz.nazw_zam_firma.value.length < 3 || formularz.nazw_zam_firma.value.length > 15){
		napis += 'Nazwisko zamawiającego  musi mieć min. 3 znaki i maksimum 15 znaków\n';
		bad = true;
	}
	
	if(!bad){
		return true;
	} else {
		alert('Wystąpiły błędy w formularzu:\n'+napis);
		return false;
	}
}
