var towarXmlHttpRequest=createXmlHttpRequestObject();var towarXmlHttpRequestTimeoutId=-1;function towarXmlHttpRequestHandleResponse(){if(towarXmlHttpRequest.readyState==4)
{if(towarXmlHttpRequest.status==200){try{var xmlRoot=towarXmlHttpRequest.responseXML.documentElement;var response=xmlRoot.getElementsByTagName('odpowiedz').item(0);var dodano=parseInt(response.getAttribute('dodano'));if(dodano>0){window.location=MAIN_URL+'?action=koszyk';return;var towar_nazwa=response.getAttribute('nazwa');var lp=parseInt(response.getAttribute('liczba_poz'));var wn=parseFloat(response.getAttribute('wartosc_netto'));var wb=parseFloat(response.getAttribute('wartosc_brutto'));update_stan_koszyka(lp,wn,wb);var dane=new Object();for(var i=0;i<response.attributes.length;i++){dane[response.attributes.item(i).nodeName]=response.attributes.item(i).nodeValue;}
updateTowar(dane);}else{var blad=response.getAttribute('error');alert('Nie dodano towaru do koszyka! \n'+blad);}}
catch(e)
{alert(e.toString());}}else{alert("Wystąpił problem podczas pobierania danych:\n"+towarXmlHttpRequest.statusText);}}}
function towarXmlHttpRequestDoKoszyka(towar_id,ilosc){if(towarXmlHttpRequest){try{if(towarXmlHttpRequest.readyState==4||towarXmlHttpRequest.readyState==0){towarXmlHttpRequest.open("GET",MAIN_URL+'?action=koszyk&mode=dodaj&ajax=1&ID='+towar_id+'&SZT='+ilosc,true);towarXmlHttpRequest.onreadystatechange=function(){towarXmlHttpRequestHandleResponse();};towarXmlHttpRequest.send(null);}else{if(towarXmlHttpRequestTimeoutId!=-1)
clearTimeout(towarXmlHttpRequestTimeoutId);towarXmlHttpRequestTimeoutId=setTimeout(function(){towarXmlHttpRequestDoKoszyka(towar_id,ilosc);},500);}}catch(e){alert("Wystąpił błąd w trakcie nawiązywania połączenia z serwerem:\n"+e.toString());window.location=MAIN_URL+'?action=koszyk&mode=dodaj&ID='+towar_id+'&SZT='+ilosc;}}else{alert("Coś nie tak z towarXmlHttpRequest:\n"+e.toString());window.location=MAIN_URL+'?action=koszyk&mode=dodaj&ID='+towar_id+'&SZT='+ilosc;}}
function updateTowar(dane){var tab=document.getElementById('koszyk_table');var tab_body=tab.getElementsByTagName('tbody');if(tab_body.length){tab_body=tab_body.item(0);}else{tab_body=tab;}
var oldTR=null;var lastTR=null;var lp=0;var cena=new Number();if(parseInt(dane['w_koszyku'])==1){oldTR=document.getElementById('towar'+dane['id']);if(!oldTR)return false;var secTD=oldTR.getElementsByTagName('td').item(1);lp=secTD.innerHTML.replace(/<[^>]+>/g,'');lp=parseInt(lp);}else{var tab_rows=tab_body.getElementsByTagName('TR');lastTR=tab_rows.item(tab_rows.length-1);if(lastTR.getAttribute("id")=='koszyk_rabat_row'){lastTR=tab_rows.item(tab_rows.length-2);lp=tab_rows.length-3;}else{lastTR=tab_rows.item(tab_rows.length-1);lp=tab_rows.length-2;}}
var newTR=document.createElement('tr');newTR.setAttribute('class','nvplain');newTR.setAttribute('id','towar'+dane['id']);var newTD=document.createElement('td');var newEL=document.createElement('input');newEL.setAttribute('type','checkbox');newEL.setAttribute('name','usun[]');newEL.setAttribute('value',dane['id']);newTD.appendChild(newEL);newTR.appendChild(newTD);newTD=document.createElement('td');newTD.setAttribute('align','right');newTD.appendChild(document.createTextNode(lp+'. '));newTR.appendChild(newTD);newTD=document.createElement('td');newTD.appendChild(document.createTextNode(dane['skrot']));newTR.appendChild(newTD);newTD=document.createElement('td');newEL=document.createElement('a');newEL.setAttribute('href',MAIN_URL+'?action=towar&mode=show&ID='+dane['id']);newEL.appendChild(document.createTextNode(dane['nazwa']));newTD.appendChild(newEL);newTR.appendChild(newTD);newTD=document.createElement('td');newTD.setAttribute('class','plhi');newEL=document.createElement('input');newEL.setAttribute('type','text');newEL.setAttribute('name','koszyk['+dane['id']+']');newEL.setAttribute('value',dane['ilosc']);newEL.setAttribute('size',2);newTD.appendChild(newEL);newTR.appendChild(newTD);newTD=document.createElement('td');newTD.setAttribute('align','right');newTD.appendChild(document.createTextNode(currency_format(dane['cena'])));newTR.appendChild(newTD);newTD=document.createElement('td');newTD.setAttribute('align','right');newTD.setAttribute('class','plhi');newTD.appendChild(document.createTextNode(currency_format(dane['cena_brutto'])));newTR.appendChild(newTD);newTD=document.createElement('td');newTD.setAttribute('align','right');newTD.appendChild(document.createTextNode(currency_format(dane['ilosc']*dane['cena'])));newTR.appendChild(newTD);newTD=document.createElement('td');newTD.setAttribute('align','right');newTD.setAttribute('class','plhi');newTD.appendChild(document.createTextNode(currency_format(dane['ilosc']*dane['cena_brutto'])));newTR.appendChild(newTD);if(parseInt(dane['w_koszyku'])==1){tab_body.replaceChild(newTR,oldTR);}else{tab_body.insertBefore(newTR,lastTR);}}
var o_towar_ilosc_czastkowa=null;var b_pokaz_dostepnosc=true;function towar_cecha_onchange(o_sel,o_num){if(b_pokaz_dostepnosc&&!o_towar_ilosc_czastkowa){o_towar_ilosc_czastkowa=document.getElementById('towar_ilosc_czastkowa');if(!o_towar_ilosc_czastkowa){b_pokaz_dostepnosc=false;}}
var i=0;var o_form=o_sel.form;var tid=new String('');while(i<5&&o_form.elements['towar_cecha['+i+']']){tid+='-'+o_form.elements['towar_cecha['+i+']'].value;i++;}
tid=tid.substring(1);if(towar_matryca_cech[tid]){server_connect(towarXmlHttpRequest,MAIN_URL+'?action=towar&mode=ajax&stan=1&ID='+towar_matryca_cech[tid],towar_stan_response);}else{alert('Uwaga! Towar nie występuje z wybranym zestawem cech! Proszę wybrać inne cechy inaczej towar niezostanie dodany do koszyka!');}}
function towar_stan_response(o_xml){var towar_xml=o_xml.getElementsByTagName('towar').item(0);if(towar_xml){var stan=towar_xml.getAttribute('stan');if(b_pokaz_dostepnosc)o_towar_ilosc_czastkowa.innerHTML=stan;document.forms['towar2koszUp'].elements['ID'].value=document.forms['towar2kosz'].elements['ID'].value=towar_xml.getAttribute('id');}else{}}
