function Toggle1(form) {

	var  formId = document.getElementById(form);
	if (formId == null) return;
	
	if (formId.style.display == '') {
		formId.style.display = 'none';		
	}
	else {
		formId.style.display = '';
	}
		
}

function PodzespolZmiana(podzespol, nr) {

	switch(podzespol) {
		case "element":
		
			kod = eval('form_konfigurator.select_element'+nr+'.value');
			
			if (kod == 0) {
				while (eval('form_konfigurator.select'+nr+'.length') > 0) {
					eval('form_konfigurator.select'+nr+'.options[0] = null');
				}
				eval('form_konfigurator.select'+nr+'.options[0] = new Option(komunikaty[1],\'0\')');
				
				while (eval('form_konfigurator.select_okno'+nr+'.length') > 0) {
					eval('form_konfigurator.select_okno'+nr+'.options[0] = null');
				}
				eval('form_konfigurator.select_okno'+nr+'.options[0] = new Option(komunikaty[1],\'0\')');
			}
			else {
				while (eval('form_konfigurator.select_okno'+nr+'.length') > 0) {
					eval('form_konfigurator.select_okno'+nr+'.options[0] = null');
				}
				eval ('form_konfigurator.select_okno'+nr+'.options[0] = new Option(komunikaty[1],\'0\')');
				
				while (eval('form_konfigurator.select'+nr+'.length') > 0) {
					eval('form_konfigurator.select'+nr+'.options[0] = null');
				}
				eval ('form_konfigurator.select'+nr+'.options[0] = new Option(komunikaty[0],\'0\')');
				i_options = 1;
				for (k = 1; k < produkt[0]; k++) {
					if(podzespoly[produkt[k].id].ref == kod && podzespoly[produkt[k].id].color == 0) {
						eval ('form_konfigurator.select'+nr+'.options[i_options] = new Option(podzespoly[produkt[k].id].nazwa,produkt[k].id)');
						i_options++;
					}
				}
			}
			break;

		case "drugie_okno":
			kod1 = eval('form_konfigurator.select'+nr+'.value');
			kod2 = eval('form_konfigurator.select_element'+nr+'.value');	
			if (kod1 == 20 || kod1 == 22 || kod1 == 23 || kod1 == 47 || kod1 == 48 || kod1 == 49 || kod1 == 50 || kod1 == 51 || kod1 == 52 || kod1 == 53 || kod1 == 54 || 685 == kod1 || 1003 == kod1 || 1004 == kod1|| kod1 == 1379 || kod1 == 1031 || kod1 == 1032 || kod1 == 1033 || kod1 == 1034 || kod1 == 1035 || kod1 == 1054 || kod1 == 1055 || kod1 == 1501 || kod1 == 1502 || kod1 == 1503) {
				while (eval('form_konfigurator.select_okno'+nr+'.length') > 0) {
					eval('form_konfigurator.select_okno'+nr+'.options[0] = null');
				}
				eval ('form_konfigurator.select_okno'+nr+'.options[0] = new Option(komunikaty[2],\'0\')');				
			}
			else if (kod1 == 0) {
				while (eval('form_konfigurator.select_okno'+nr+'.length') > 0) {
					eval('form_konfigurator.select_okno'+nr+'.options[0] = null');
				}
				eval ('form_konfigurator.select_okno'+nr+'.options[0] = new Option(komunikaty[1],\'0\')');
			}
			else if(kod2 && kod1 != 0) {
				while (eval('form_konfigurator.select_okno'+nr+'.length') > 0) {
					eval('form_konfigurator.select_okno'+nr+'.options[0] = null');
				}
				eval('form_konfigurator.select_okno'+nr+'.options[0] = new Option(komunikaty[0],\'0\')');
				i_options = 1;
				for (k = 1; k < produkt[0]; k++) {
					if(podzespoly[produkt[k].id].ref == kod1 && podzespoly[produkt[k].id].color == 0) {
						eval('form_konfigurator.select_okno'+nr+'.options[i_options] = new Option(podzespoly[produkt[k].id].nazwa, produkt[k].id)');
						i_options++;
					}
				}
			}		
			break;	
	}
	showDiscount();
}

function WypiszElementy(selectedElement) {
	//funkcja wpisujaca wartosci podczas ladowania wizarda do odpowiednich list wyborow
    document.write('<option value=\"0\" >'+komunikaty[0]+'</option>');   

    for (i = 1; i < produkt[0]; i++) 
    {
		if (podzespoly[produkt[i].id].ref == 0) 
		{
		    var selected = '';
		    if(selectedElement==podzespoly[produkt[i].id].id)
	        {
		        selected = ' selected="selected" ';
	        }
		    
       		document.write('<option value="' + podzespoly[produkt[i].id].id + '" ' + selected + ' >' + podzespoly[produkt[i].id].nazwa + '</option>');
		}
     }      		
}

function WypiszOpcje(param){
	//funkcja wpisujaca wartosci podczas ladowania wizarda do odpowiednich list wyborow
	document.write('<option value=\"0\" selected>'+komunikaty[1]+'</option>'+"\n");
}

function dodaj_element(id, nazwa, opis, ref, color) {
	//dodaje nowe elementy
	podzespoly[id] = new p_e(id, nazwa, opis, ref, color);
	produkt[produkt[0]] = new p_p(id, ref, color);
	produkt[0] += 1;
}

function p_p (id, ref, color) {
	//obiekt tabeli wszystko
 	this.id = id;
	this.ref = ref;
	this.color = color;	
}

function p_e (id, nazwa, opis, ref, color) {
	//obiekt elementy
	this.id = id;
	this.nazwa = nazwa;
	this.opis = opis;
	this.ref = ref;
	this.color = color;	
}

function test(form_konfigurator)  {
    var error = false;
    productDiscount = numberOfProductDiscount()
    if(productDiscount > 0  && productDiscount < 7)
    {
    	if(!confirm("Przypominamy o wyższym rabacie przy 7 szt. okien. \n\nKliknij 'Anuluj' żeby wrócić do zamówienia.\nKliknij Ok, żeby kontynuować zamówienie."))
    	{
    		return false;
    	}
    	
    }else if(productDiscount >= 7  && productDiscount < 21)
    {
    	if(!confirm ("Przypominamy o wyższym rabacie przy 21 szt. okien. \n\nKliknij 'Anuluj' żeby wrócić do zamówienia.\nKliknij Ok, żeby kontynuować zamówienie."))
    	{
    		return false;
    	}
    }
    /*
    else if(productDiscount >= 21  && productDiscount < 51)
    {
        if(!confirm ("Przypominamy o wyższym rabacie przy 51 szt. okien. \n\nKliknij 'Anuluj' żeby wrócić do zamówienia.\nKliknij Ok, żeby kontynuować zamówienie."))
        {
            return false;
        }
    }
    */

    if (!checkMail(form_konfigurator.email2.value)) {
    	alert ("Podaj adres e-mail.");
    	return false;
    }
     
    for (i = 1; i < rows; i++) {
    	kod1 = eval('form_konfigurator.select'+i+'.value'); //typ
		kod2 = eval('form_konfigurator.select_element'+i+'.value'); //prod.
		kod3 = eval('form_konfigurator.select_okno'+i+'.value'); // rozmiar
		if (kod2 != 0 || kod1 != 0 || kod3 != 0) {
			if (kod2 == 0 || kod1 == 0 || (kod3 == 0 && !(kod1 == 20 || kod1 == 22 || kod1 == 23 || kod1 == 47 || kod1 == 48 || kod1 == 49 || kod1 == 50 || kod1 == 51 || kod1 == 52 || kod1 == 53 || kod1 == 54 || 685 == kod1 || 1003 == kod1 || 1004 == kod1|| kod1 == 1379 || kod1 == 1031 || kod1 == 1032 || kod1 == 1033 || kod1 == 1034 || kod1 == 1035 || kod1 == 1054 || kod1 == 1055 || kod1 == 1501 || kod1 == 1502 || kod1 == 1503))) {
				alert ("Nie wybrałeś wszystkich dostepnych opcji");
	        	return false;
			}
		}
		if (!checkColor(i)) {
	        //alert ("Nie podałeś prawidłowych kolorów.");
	        return false;
		}
    }
	document.form_konfigurator.submit();
}

function checkMail(email) {
	var re = /^([A-Za-z0-9_]|\-|\.)+@(([A-Za-z0-9_]|\-)+\.)+[A-Za-z]{2,4}$/;
	return re.test(email);
}


function checkBlank(value) {
	var re = /^\s$/;
	return re.test(value);
}

function getAutocomArray1(number) {
	var num = eval('form_konfigurator.select'+number+'.value');
	
	return 'colors.php?id='+num+'&pol='+number;
}

function getAutocomArray(number) {
	var num = eval('form_konfigurator.select'+number+'.value');
	if (currn_tab == num && currn_num == number) {
		return;
	}
	//DelNode(number);
	name = null;
	//alert(currn_tab);
	if (kolory[num]) {
		/*if (currn_tab[number]) {
			var id = 'kolor'+number+''+currn_tab[number];
		}
		else {
			var id = 'kolor'+number;
		}
		var node1 = document.getElementById(id);	
		//alert(id);
		if (node1) {
			node1.id = 'kolor'+number+''+num;
			//alert(node1.id);
		}*/
	
		name = new Autocompleter.Local('kolor'+number, 'ac4update'+number, kolory[num], {});
		
		/*name.initialize(node1.id,'ac4update'+number, new Array(), {});
		name.options.array = new Array();
		name.setOptions(name.options);	
		name.updateChoices();*/
		
		name.initialize('kolor'+number,'ac4update'+number, kolory[num], {});
		name.options.array = kolory[num];
		name.setOptions(name.options);
		name.updateChoices();
		
		currn_tab = num;
		currn_num = number;		
		return;
	}
	name = new Autocompleter.Local('kolor'+number,'ac4update'+number, new Array(), {});
/*	name.initialize(node1.id,'ac4update'+number+''+num, new Array(), {});
	name.options.array = new Array();
	name.setOptions(name.options);*/
	currn_tab = num;
	currn_num = number;
}

function DelNode(number) {
	var sAgent = navigator.userAgent.toLowerCase();
	var IE = (sAgent.indexOf("msie") != -1 );
	if (IE) {
		//var node = document.getElementById('ac4update'+number+''+number_tab+'_iefix');
		/*var node = document.getElementById('ac4update'+number+'_iefix');
		if (node) {
			node.removeNode(true);
		}*/
	}
	/*var node1 = document.getElementById('ac4update'+number+''+number_tab);*/
	var node1 = document.getElementById('ac4update'+number);
	if (node1) {
		node1.innerHTML  = '';
		//node1.style.zidex = 1;
	}
	return true;
}

function checkColor(number) {
	var num = eval('form_konfigurator.select'+number+'.value');
	var kolor = document.getElementById('kolor'+number);
	var kolory_prod = kolory[num];
	var jest = false;
	var ile = 0;
	
	if (!kolory_prod) {
		return true;
	}
	
	ile = kolory_prod.length;
	for (var i = 0 ; i < ile; i++) {
		if (kolory_prod[i] == kolor.value) {
			jest = true;
			return true;
		}
	}
	if (!jest) {
		alert('Proszę podać prawidłowy kolor w produkcie numer: '+number+'.');
		return false;
	}
}


function numberOfProductDiscount()
{
	var windowCount = 0;
	kod1 = '';
	var kod2;
	for(i = 1; i < rows; i++)
	{
		product = " " + eval('form_konfigurator.select_element'+i+'.value'); //typ
		//kod1 += product;
		if(product == 1 || product == 2 || product == 732 || product == 1011  || product == 733  || product == 3 || product == 4  || product == 1500)
		{
			if(product == 1011 || product == 3 || product == 733)
			{
				type = eval('form_konfigurator.select'+i+'.value')
				//kod1 += " " + type + " " ;

				if(type == 1364 || type == 1003 || type == 1004 || type == 1379 || type == 1743 || type == 1745 || type == 1418 || type == 1417 || type == 1747 || type == 1420 || type == 1419 || type == 1749 )
				{
					//kod1 += "tego nie liczę! \n";
					continue;
				}
			}
			count = parseInt(eval('form_konfigurator.ilosc'+i+'.value')); //ilosc
			windowCount = windowCount + count; 
			//kod1 += "  -  " + count; 
			
		}
		//kod1 += "\n";

	}
	//alert(kod1 + "\nsum: " + windowCount);
	return windowCount;
}


function showDiscount()
{
    var infoBox = document.getElementById('discountBox');
    var productDiscount = numberOfProductDiscount();
    infoBox.innerHTML = '&nbsp;';

    if(productDiscount > 0  && productDiscount < 7)
    {
        infoBox.innerHTML = '<span style="color:#ff0000;">Przypominamy o wyższym rabacie przy 7 szt. okien!</span>';
        
    }else if(productDiscount >= 7  && productDiscount < 21)
    {
        infoBox.innerHTML = '<span style="color:#ff0000;">Przypominamy o wyższym rabacie przy 21 szt. okien!!</span>';
    }
    /*else if(productDiscount >= 21  && productDiscount < 51)
    {
        infoBox.innerHTML = '<span style="color:#ff0000;">Przypominamy o wyższym rabacie przy 51 szt. okien!!!</span>';
    }
    */
    
    
}


var produkt = new Array();
	produkt[0] = 1;
var podzespoly = new Array();
	podzespoly[0] = new p_e ("Zerowy towar", "", "", "0");
var komunikaty = new Array();
	komunikaty[0] = 'Wybierz pozycję z listy';
	komunikaty[1] = '<<';
	komunikaty[2] = 'Opcja niedostępna';
var kolory = new Array();
var currn_tab = new Array();
var currn_num = 0;
var name;
