var xhpobProb;
var xhZamProb;
var nrZam;
var nazwaKolekcji_last;
var liczbaProbek = 0;

function urlEncode(str)
{
	return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
    	replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');

}

function zmienZakladkeProbek(id)
{
	var aTabs = new Array('firany', 'rolety_i_zaslony', 'rolety');
	
	for (i = 0, cnt = aTabs.length; i < cnt; i++)
	{
		var tab = document.getElementById('tab_'+aTabs[i]);
		var container = document.getElementById('probki_'+aTabs[i]);
		
		if (aTabs[i] == id)
		{
			tab.style.backgroundColor = '#EAEAEA';
			container.style.display = '';
		}
		else
		{
			tab.style.backgroundColor = '#DADADA';
			container.style.display = 'none';
		}
	}
	
}

function pobierzProbki(nazwaKolekcji, id, firany)
{
	var kolekcje = document.getElementsByName('kolekcje');
	for (var i = 0; i < kolekcje.length; i++)
	{
		kolekcje[i].style.fontWeight = 'normal';
	}
	if (firany == 1) {
		document.getElementById(nazwaKolekcji+'_fir').style.fontWeight = 'bold';
	} else if (firany == 2) {
		document.getElementById(nazwaKolekcji+'_rol').style.fontWeight = 'bold';
	} else {
		document.getElementById(nazwaKolekcji).style.fontWeight = 'bold';
	}
	nazwaKolekcji_last = nazwaKolekcji;
	xhpobProb = GetXmlHttpObject();
	if (xhpobProb == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	document.getElementById('loaderProbek').style.display = '';
	document.getElementById('kontenerProbek').style.display = 'none';
	id = id > 0 ? id : 0;
	firany = firany > 0 ? firany : 0;
	var url="/tpl/view/pobierzProbki.php?nazwaKolekcji=" + urlEncode(nazwaKolekcji) + "&id=" + id + "&firany="+firany;
	//alert(url);
	xhpobProb.onreadystatechange = pobierzProbki_SC;
	xhpobProb.open("GET", url, true);
	xhpobProb.send(null);
}

function pobierzProbki_SC()
{
	if (xhpobProb.readyState == 4) {
		document.getElementById('loaderProbek').style.display = 'none';
		document.getElementById('kontenerProbek').style.display = '';
		document.getElementById('kontenerProbek').innerHTML = xhpobProb.responseText;
		
		// zaznaczenie wybranych probek
		var probkiWKoszyku = document.getElementById('koszyk_probek').childNodes;
		for (var i = 0; i < probkiWKoszyku.length; i++)
		{
			try
			{
				var nodeId = probkiWKoszyku.item(i).id;
				nodeId = nodeId.substring(7);
				document.getElementById('probka_'+nodeId).style.border = '2px solid #8D512C';
			}
			catch (ex){}
		}
	}
}

function dodajProbke(nazwaProbkiSave, nazwaProbkiDisp, imgSrc)
{
	if (liczbaProbek >= 5)
	{
		alert(lang['probki']['limitProbek']);
		return;
	}
	// czy probka jest na liscie
	if (document.getElementById('koszyk_'+nazwaProbkiSave) != null)
	{
		alert(lang['probki']['probkaJuzWZam']);
		return;
	}
	var item = '<a href="javascript:void(0);" name="koszyk_'+nazwaProbkiSave+'" id="koszyk_'+nazwaProbkiSave+'" onclick="usunProbke(\''+nazwaProbkiSave+'\');" title="'+nazwaProbkiDisp+'">'
		+'<div style="background: url(\'http://images.stylus.pl/'+imgSrc+'\') no-repeat -4px -4px; margin: 0 1px 2px 1px; float: left; border: 2px solid #8D512C; width: 97px; height: 110px; overflow: hidden; text-align: center;">'
		+'</div></a>';

	//alert(item);
	document.getElementById('probka_'+nazwaProbkiSave).style.border = '2px solid #8D512C';
	
	var koszyk_probek = document.getElementById('koszyk_probek');
	if (!liczbaProbek)
	{
		koszyk_probek.innerHTML = '';
	}
	document.getElementById('koszyk_probek').innerHTML += item;
	liczbaProbek++;
}

function usunProbke(nazwaProbki)
{
	var del_item = document.getElementById('koszyk_'+nazwaProbki);
	
	try
	{
		document.getElementById('probka_'+nazwaProbki).style.border = '2px solid #FFF';
	} catch (ex){}
	
	del_item.parentNode.removeChild(del_item);
	
	liczbaProbek--;
	if (!liczbaProbek)
	{
		document.getElementById('koszyk_probek').innerHTML = lang['probki']['zamowieniePuste'];
	}
}

function zamowProbki()
{
	// sprawdzenie czy zamowienie nie jest puste
	if (!liczbaProbek)
	{
		alert(lang['probki']['brakProbek']);
		return;
	}
	// powiadomienie o maksymalnej liczbie probek
	if (liczbaProbek < 5)
	{
		var result = confirm(lang['probki']['limitProbekZam']);
		if (!result)
		{
			return;
		}
	}
	// sprawdzenie formularza
	
	//imie
	var reString = /.{1,50}/;
	var reKodPocztowy = /\d{2}-\d{3}/;
	var reTelefon = /[\d- ]{6,50}/;
	var reEmail = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var formPass = true;
	if (!document.probki_form.imie.value.match(reString))
	{
		document.probki_form.imie.style.border = '2px solid #FF0000';
		document.getElementById('errorImie').style.display = '';
		formPass = false;
	} 
	else
	{
		document.probki_form.imie.style.border = '1px solid #7F9DB9';
		document.getElementById('errorImie').style.display = 'none';
	}
	// nazwisko
	if (!document.probki_form.nazwisko.value.match(reString))
	{
		document.probki_form.nazwisko.style.border = '2px solid #FF0000';
		document.getElementById('errorNazwisko').style.display = '';
		formPass = false;
	}
	else
	{
		document.probki_form.nazwisko.style.border = '1px solid #7F9DB9';
		document.getElementById('errorNazwisko').style.display = 'none';
	}
	// ulica
	if (!document.probki_form.ulica.value.match(reString))
	{
		document.probki_form.ulica.style.border = '2px solid #FF0000';
		document.getElementById('errorUlica').style.display = '';
		formPass = false;
	}
	else
	{
		document.probki_form.ulica.style.border = '1px solid #7F9DB9';
		document.getElementById('errorUlica').style.display = 'none';
	}
	// nr_domu
	if (!document.probki_form.nr_domu.value.match(reString))
	{
		document.probki_form.nr_domu.style.border = '2px solid #FF0000';
		document.getElementById('errorNrDomu').style.display = '';
		formPass = false;
	}
	else
	{
		document.probki_form.nr_domu.style.border = '1px solid #7F9DB9';
		document.getElementById('errorNrDomu').style.display = 'none';
	}
	// kod_pocztowy
	if (!document.probki_form.kod_pocztowy.value.match(reKodPocztowy))
	{
		document.probki_form.kod_pocztowy.style.border = '2px solid #FF0000';
		document.getElementById('errorKodPocztowy').style.display = '';
		formPass = false;
	}
	else
	{
		document.probki_form.kod_pocztowy.style.border = '1px solid #7F9DB9';
		document.getElementById('errorKodPocztowy').style.display = 'none';
	}
	// miejscowosc
	if (!document.probki_form.miejscowosc.value.match(reString))
	{
		document.probki_form.miejscowosc.style.border = '2px solid #FF0000';
		document.getElementById('errorMiejscowosc').style.display = '';
		formPass = false;
	}
	else
	{
		document.probki_form.miejscowosc.style.border = '1px solid #7F9DB9';
		document.getElementById('errorMiejscowosc').style.display = 'none';
	}
	// telefon
	if (!document.probki_form.telefon.value.match(reTelefon))
	{
		document.probki_form.telefon.style.border = '2px solid #FF0000';
		document.getElementById('errorTelefon').style.display = '';
		formPass = false;
	}
	else
	{
		document.probki_form.telefon.style.border = '1px solid #7F9DB9';
		document.getElementById('errorTelefon').style.display = 'none';
	}
	// email
	if (!document.probki_form.email.value.match(reEmail))
	{
		document.probki_form.email.style.border = '2px solid #FF0000';
		document.getElementById('errorEmail').style.display = '';
		formPass = false;
	}
	else
	{
		document.probki_form.email.style.border = '1px solid #7F9DB9';
		document.getElementById('errorEmail').style.display = 'none';
	}
	if (formPass)
	{
		// dodaj probki
		var probkiWKoszyku = document.getElementById('koszyk_probek').childNodes;
		var probkiToInput = '';
		for (var i = 0; i < probkiWKoszyku.length; i++)
		{
			try
			{
				var nodeId = probkiWKoszyku.item(i).id;
				var image = probkiWKoszyku.item(i).firstChild;
				while(image.nodeName != 'DIV')
				{
					image = image.nextSibling;
				}
				regexImg = new RegExp("([A-Za-z0-9])+\.jpg");
				match = regexImg.exec(image.style.background);
				image = match[0];
				name = nodeId.substring(7);
				name_lang = probkiWKoszyku.item(i).title;
				probkiToInput += name + '@' + name_lang + '@' + image + ';';
			} catch (ex) {}
		}
		document.probki_form.probki.value = probkiToInput;
		document.probki_form.submit();
	}
}

function GetXmlHttpObject()
{
        var xmlHttpRequest = false;
        try {
			if(window.ActiveXObject) {
				for(var i = 5; i; i--) {
					try {
						if(i == 2) {
							xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");       
						} else {
							xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP." + i + ".0");
						}
					break;
					}
					catch(excNotLoadable) {
						xmlHttpRequest = false;
					}
				}
			} else if(window.XMLHttpRequest) {
				xmlHttpRequest = new XMLHttpRequest();
			}
		}
        catch(excNotLoadable) {
			xmlHttpRequest = false;
        }
        return xmlHttpRequest; 
}
