var CenaZdobienia = new Array();

function Miejsca(id,miejsce)
{
 this.id = id;
 this.miejsce = miejsce; 
}

function Metody(id,metoda)
{
 this.id = id;
 this.metoda = metoda;
}

function Kolory(id,kolor,hex)
{
 this.id = id;
 this.kolor = kolor;
 this.hex = hex;
}

function MiejscaMetody(miejscaId,metoda)
{
 this.miejscaId = miejscaId;
 this.metoda = metoda;
}


function SetKolory() 
{
	for (var i = 0; i <KoloryProdukt.length; i++) 
	{
	 document.kalkulator.Kolor.options[i] = new Option(KoloryProdukt[i].kolor);
	 document.kalkulator.Kolor.options[i].value = KoloryProdukt[i].id;

         /*
         document.kalkulator.Kolor.options[i].style.background="#"+KoloryProdukt[i].hex;
         if(h2d(KoloryProdukt[i].hex) > 13619151) document.kalkulator.Kolor.options[i].style.color ="#000000";
         else document.kalkulator.Kolor.options[i].style.color ="#ffffff";
         */
	}
}


function SetListaKolor(element)
{
  var x=document.getElementById(element);

        for (var i = 0; i <KoloryLista.length; i++)
	{
         x.options[i] = new Option(KoloryLista[i].kolor);
	 x.options[i].value = KoloryLista[i].id;
	 
         /*
         x.options[i].style.background="#"+KoloryLista[i].hex;
         if(h2d(KoloryLista[i].hex) > 13619151) x.options[i].style.color ="#000000";
	 else x.options[i].style.color ="#ffffff";
         */
         //x.options[i].style = 'background-color: #EDEDED';
        }
}



function SetMiejscaMetodyKolory(idMiejsca)
{

        for (var i=0; i<Miejsca[idMiejsca].metoda.length; i++)
	 {
			for(var j=0; j<MetodyZdobienia.length;j++)
		   {
				//test(Miejsca[1].metoda[0]);
				if(MetodyZdobienia[j].id == Miejsca[idMiejsca].metoda[i])
				 {
 			   var x=document.getElementById('Metoda'+idMiejsca);
				 // jako pierwszy element dodajemy puste pole
				 if(!x.length){x.options[i] = new Option('...'); x.options[i].value = 0;}

				 x.options[i+1] = new Option(MetodyZdobienia[j].metoda);
				 x.options[i+1].value = MetodyZdobienia[j].id;
				 break;
				 }
			 }
	 }

}

function SetIloscKolorow(idMiejsca,idMetody)
{
 //tutaj trzeba ustwiac pola z warstw - po zmianie wszystkie chowamy 
 //SetWarstwaKolorow(idMiejsca,0);
 var tmp = document.getElementById('Metoda'+idMiejsca).value;
 //if(tmp == 0) 
 HideWarstwaKolorow(idMiejsca);
 
 var x=document.getElementById('IloscKolor'+idMiejsca); 
 x.length=0;
 x.options[0] = new Option('brak');
 x.options[0].value = 0;

 for (var i=1; i<=MiejscaKolory[idMiejsca][idMetody]; i++)
  {    
   x.options[i] = new Option(i);
	 x.options[i].value = i;
	} 
 
 x.selectedIndex = 1;
 SetWarstwaKolorow(idMiejsca,1);
}

function test(i)
{
  window.alert(i);
}

function HideWarstwaKolorow(id)
{
document.getElementById('WarstwaKolorow'+id).style.display = "none";


document.getElementById('TextKolor'+id+'_1').style.display = "none";
document.getElementById('TextKolor'+id+'_2').style.display = "none";
document.getElementById('TextKolor'+id+'_3').style.display = "none";
document.getElementById('TextKolor'+id+'_4').style.display = "none";
}

function SetWarstwaKolorow(id,ilosc)
{
 //test('ListaKolorow'+id+'_1');
 document.getElementById('WarstwaKolorow'+id).style.display = "block";
 
 if(ilosc == 1)
  {

	 document.getElementById('TextKolor'+id+'_1').style.display = "inline";
	 document.getElementById('TextKolor'+id+'_2').style.display = "none";
	 document.getElementById('TextKolor'+id+'_3').style.display = "none";
	 document.getElementById('TextKolor'+id+'_4').style.display = "none";
	}

 if(ilosc == 2)
  {


	 document.getElementById('TextKolor'+id+'_1').style.display = "inline";
	 document.getElementById('TextKolor'+id+'_2').style.display = "inline";
	 document.getElementById('TextKolor'+id+'_3').style.display = "none";
	 document.getElementById('TextKolor'+id+'_4').style.display = "none";
	}

 if(ilosc == 3)
  {

	 document.getElementById('TextKolor'+id+'_1').style.display = "inline";
	 document.getElementById('TextKolor'+id+'_2').style.display = "inline";
	 document.getElementById('TextKolor'+id+'_3').style.display = "inline";
	 document.getElementById('TextKolor'+id+'_4').style.display = "none";
	}

 if(ilosc == 4)
  {

	 document.getElementById('TextKolor'+id+'_1').style.display = "inline";
	 document.getElementById('TextKolor'+id+'_2').style.display = "inline";
	 document.getElementById('TextKolor'+id+'_3').style.display = "inline";
	 document.getElementById('TextKolor'+id+'_4').style.display = "inline";
	}

	if(ilosc == 0)
  {

	 document.getElementById('TextKolor'+id+'_1').style.display = "none";
	 document.getElementById('TextKolor'+id+'_2').style.display = "none";
	 document.getElementById('TextKolor'+id+'_3').style.display = "none";
	 document.getElementById('TextKolor'+id+'_4').style.display = "none";

	 HideWarstwaKolorow(id);
	}
}

function h2d(h) {return parseInt(h,16);}

/* funkcje zwiazane z obliczeniami */

function zaokraglenie(liczba) {
liczba = liczba * 100;
return Math.round(liczba)/100;
}


function SprawdzPola()
{

 if(!parseInt(document.kalkulator.Ilosc.value)) {window.alert('Ilość musi być liczbą'); return false; }
   else
      if (document.kalkulator.Ilosc.value < MinIlosc) {window.alert('Ilość minimalna wynosi '+MinIlosc+' szt.'); document.kalkulator.Ilosc.value = MinIlosc;  return false; }
//         else
//            if(document.kalkulator.Kolor.value == 0) {window.alert('Wybierz kolor produktu'); return false; }
	       else return true;
}

function KosztyStale(metoda,ilosc_kolorow,matryce,przygotowanie)
{
 this.metoda = metoda;
 this.ilosc_kolorow = ilosc_kolorow;
 this.matryce = matryce;
 this.przygotowanie = przygotowanie;
}

function CennikProdukt(ilosc,cena)
{
 this.ilosc = ilosc;
 this.cena = cena;
}

function CennikZdobienia(metoda,cena,cena_prev,ilosc_od,ilosc_do,ilosc_kolorow,cena_kolor,ryczalt)
{
 this.metoda=metoda;
 this.cena=cena;
 this.cena_prev=cena_prev;
 this.ilosc_od=ilosc_od;
 this.ilosc_do=ilosc_do;
 this.ilosc_kolorow=ilosc_kolorow;
 this.cena_kolor=cena_kolor;
 this.ryczalt=ryczalt;  
}

function matryca_kolorow(kolor_produktu, kolor_zdobienia, bonus)
{
this.kolor_produktu = kolor_produktu;
this.kolor_zdobienia = kolor_zdobienia;
this.bonus = bonus;
}

function koszt_koloru(ilosc,koszt) 
{
this.ilosc = ilosc;
this.koszt = koszt;
}

function ObliczCenaProdukt()
{
var ilosc = parseInt(document.kalkulator.Ilosc.value);
for(var i=0; i<CProdukt.length; i++)
 {
 if(ilosc >= parseInt(CProdukt[i].ilosc)) break; 
 }	
if(i<CProdukt.length-1) return CProdukt[i].cena; else return CProdukt[CProdukt.length-1].cena; 	
}

function CzyscWyniki()
{
 document.kalkulator.Wynik.value = "";
}

function wypisz_wyniki()
{
 if(SprawdzPola())
 {
 var cena_produkt =  ObliczCenaProdukt();
 var koszt_zdobienia = ObliczCenaZdobienie();
 var koszt_przygotowania = ObliczKosztPrzygotowania();
 var koszt_matryc = 0;
 
 if(document.kalkulator.SaMatryce.checked == false) koszt_matryc = ObliczKosztMatryc(); 
 
 document.kalkulator.Wynik.value =  "Produkt : "+(cena_produkt)+" zł. x "+document.kalkulator.Ilosc.value+" szt. = "+(zaokraglenie(cena_produkt*document.kalkulator.Ilosc.value))+ " zł";
 document.kalkulator.Wynik.value += "\r\nJednorazowe koszty przygotowania : "+koszt_matryc+" zł";
 document.kalkulator.Wynik.value += "\r\Każdorazowe koszty uruchomienia produkcji : "+koszt_przygotowania+" zł";
 document.kalkulator.Wynik.value += "\r\nKoszt zdobienia : "+zaokraglenie(koszt_zdobienia)+" zł";
 document.kalkulator.Wynik.value += "\r\nSuma kosztów zdobienia : "+koszt_matryc+" + "+koszt_przygotowania+" + "+zaokraglenie(koszt_zdobienia)+" = "+(zaokraglenie(koszt_matryc+koszt_przygotowania+koszt_zdobienia))+" zł";
 document.kalkulator.Wynik.value += "\r\n-----------------------";
 document.kalkulator.Wynik.value += "\r\nRazem : "+(zaokraglenie(cena_produkt*document.kalkulator.Ilosc.value))+" + "+(zaokraglenie(koszt_matryc+koszt_przygotowania+koszt_zdobienia))+" = "+(zaokraglenie(koszt_matryc+koszt_przygotowania+koszt_zdobienia+cena_produkt*document.kalkulator.Ilosc.value))+" zł + VAT 22% ";
 document.kalkulator.Wynik.value += "("+zaokraglenie((koszt_matryc+koszt_przygotowania+koszt_zdobienia+cena_produkt*document.kalkulator.Ilosc.value)/document.kalkulator.Ilosc.value)+" zł szt. + VAT 22% )"
 return 1;
 }
}


function ObliczKosztExtra(id)
{
 var ilosc = document.getElementById('IloscKolor'+id).value;
 var tmp = 0;
 
  for(var i=1; i<=ilosc; i++) 
  {
   //test(document.getElementById('ListaKolor'+id+'_'+i).value);
	 for(var j=0; j<Matryca.length; j++)
		 {
		  if(Matryca[j].kolor_produktu == document.kalkulator.Kolor.value) 
			  
			  if(Matryca[j].kolor_zdobienia == document.getElementById('ListaKolor'+id+'_'+i).value) 
  																		      
					if(Matryca[j].bonus > 0 ) tmp += Matryca[j].bonus;
		 } 
	} 

  return tmp;
}


function checkForm()
{
/*     	if(zamowienie.adres.value == '')
			{
			window.alert('Wypełnij pole adres ');
			zamowienie.adres.focus();
			return false;
      }
*/

			if( (zamowienie.tel.value == '') && (zamowienie.tel2.value == '')){
			window.alert('Musisz podać przynajmniej jeden numer telefonu');
			zamowienie.tel.focus();
			return false;
      }

      if(zamowienie.osoba.value == ''){
			window.alert('Wpisz dane osoby kontaktowej');
			zamowienie.osoba.focus();
			return false;
                }

     if(zamowienie.email.value == '')
                {
			window.alert('Wpisz swój e-mail');
			zamowienie.email.focus();
			return false;
                }

                else
                {
                  Wzor=/^[0-9a-zA-Z_.]+\@[0-9a-zA-Z]+\.[0-9a-zA-Z.]+$/;
                  OK=(Wzor.test(zamowienie.email.value)==true);
                 // alert(OK ? "POPRAWNIE." : "Błędny adres e-mail.");
                  if (!OK)
                  {
                  alert("Wprowadzony e-mail jest nieprawidłowy");
                  zamowienie.email.focus();
                  return false
                  }
                }

     	if(zamowienie.akceptacja.checked != 1)
			{
			window.alert('Musisz zaakceptować Ogólne Warunki Współpracy ');
			zamowienie.akceptacja.focus();
			return false;
      }

}
