function take(id) {return document.getElementById(id);}
function take(id) {return document.getElementById(id);}
function parseRecords(xml, nastepny_select, ost_wybor) {
if (take(nastepny_select)){
nast_sel = take(nastepny_select);
record = xml.getElementsByTagName("record");
result = "";
for (i = 0; i < record.length; i++) {
 option = document.createElement("option");			
 nast_sel.appendChild(option);
 for (j = 0; j < 3; j++) { 
	if (j==0){option.setAttribute('value', record[i].childNodes[j].childNodes[0].nodeValue);}
	else if(j==1){option.innerHTML = record[i].childNodes[j].childNodes[0].nodeValue;}}
	//else if(j==2){if(record[i].childNodes[j]){if(record[i].childNodes[j].childNodes[0].nodeValue=='selected') option.setAttribute('selected','"selected"');}}}
}return result;}}
function getDost(link_1, id_czesci){
advAJAX.get({
	url : link_1,		
    onSuccess : function(obj) {parseDost(obj.responseText, id_czesci);},										
	onError : function(obj) { alert("Error: " + obj.status);}});		
}
function  parseDost(tekst, id_czesci){
img = document.getElementById(id_czesci);
img.setAttribute("src",tekst);}
function getRecords(parametr, nastepny_select, ost_wybor){
var ost = parseInt(ost_wybor);  
 if(nastepny_select == 'select1'){
	if (take("select1")){usun(take("select1"));}
	if (take("select2")){take("select2").disabled=true;}
}
if(nastepny_select == 'select2'){
	if (take("select2")){usun(take("select2"));}} 
	advAJAX.get({url : parametr,		
		onInitialization : function(){
		 if (nastepny_select == 'select') take("select").disabled=false;
	     if (take("info")) {take("info").innerHTML = '<div id="info" class="progress"><img src="images/progress.gif" /></div>';}
	     if (take("info")) {take("info").style.visibility = "visible";}
		 if (take("auto")) {take("auto").style.visibility="hidden";};  
		},
		onSuccess : function(obj) {parseRecords(obj.responseXML, nastepny_select, ost_wybor);
			if (take("info")) {take("info").innerHTML="&nbsp;";}
			if(nastepny_select != "select3"){if (take("auto")){take("auto").style.visibility="visible";}}  										
		}
		//onError : function(obj) { alert("Proszę czekać, trwa przetwarzanie danych...");}
	});	
}
//function initDynamicTableIndex(link, customer_discount, zmiana, mark) {
function initDynamicTableIndex(link, customer_discount, zmiana) {
if(zmiana == true){if (take("wybranyNaglowek")){take("wybranyNaglowek").innerHTML = '';}}	
if (take("wybrany")){if(zmiana == true )
	// if (take("wybrany")) {take("wybrany").innerHTML = '&nbsp Wybierz samochód, do którego poszukujesz części';}
if (take("wybrany")) {take("wybrany").innerHTML = '&nbsp';}
else take("wybrany").innerHTML = take("wybrany").innerHTML;
}
if (take("wybrany")){take("wybrany").innerHTML = take("wybrany").innerHTML;} //b. ważna funkcja dla prawidłowego działania pod IE !!!	
 //if (take("marka")){getRecords(link +"ajax.php?default=default", "select", 0);}
//if(mark=='') 
	getRecords(link +"ajax.php?default=default", "select", 0); 
//else{
//	getRecords(link +"ajax.php?default=default&mark="+mark, "select", 0); 
//	take("select1").disabled=false;
//	take("select1").focus();	
//	getRecords(link +"ajax.php?select=select&marka="+mark, 'select1', 0);	
//	take("select1").style.zIndex="40";	
//}	
}
function changeSelect(wybor, select, link){
if(select == 'select1'){	
	//take("select").setAttribute("size","1");
	//take("select").style.visibility = "hidden";
	//if (take("model")) { take("model").style.visibility = "visible";}
	take("select1").disabled=false;
	take("select1").focus();
	getRecords(link +"ajax.php?select=select&klucz="+wybor, 'select1', 0);
	// take("select1").setAttribute("size","14");
	take("select1").style.zIndex="40";	
	//take("select1").style.width="250";
}else if(select == 'select2'){
	//take("select1").setAttribute("size","1");
	take("select2").style.zIndex="40";
	take("select2").disabled=false;
	take("select2").focus();
	if (take("typ")) { take("typ").style.visibility = "visible";}
	getRecords(link +"ajax.php?select=select1&klucz="+wybor, 'select2', 0);
	//take("select2").setAttribute("size","14");		
}	
}
function usun(lista) {
 while (lista.length>0){lista.remove(0);}
}	
function onFoc(obj, ile){
 obj.style.width=ile;	
}
function onBlu(obj, ile){
 obj.style.width=ile;	
}
function getProfil(evt){
 var e_out_link;
 var e_out_id_typu;
 var e_out_customer_id;
 var ie_var = "srcElement"; // dla IE
 var moz_var = "target";  // dla mozilli
 var prop_var = "myflag";
 var typ = "typ"; 
 var link = "link";
 var customer_id = "customer_id";
 evt[moz_var] ? e_out_link = evt[moz_var][link] : e_out_link = evt[ie_var][link];
 evt[moz_var] ? e_out_id_typu = evt[moz_var][typ] : e_out_id_typu = evt[ie_var][typ];
 evt[moz_var] ? e_out_customer_id = evt[moz_var][customer_id] : e_out_customer_id = evt[ie_var][customer_id];
 getCar(e_out_link, e_out_id_typu, e_out_customer_id);	 
}	
function getCar(link ,id_typu, customer_id) {
 alert("Dodałeś samochód do swojego profilu. \nMożesz używać go przy wyszukiwaniu części z naszej oferty\n\nZa chwilę zostaniesz przekierowany do strony głównej\ngdzie będziesz mógł wybierać części do \nTwojego samochodu w okienku 'kategorie'");
 advAJAX.get({
  url : link + "ajax.php?usercar&customer_id=" + customer_id + "&id_typu=" + id_typu,			
  onSuccess: function(){window.location = link +'index.php';},
  onError : function(obj) { alert("Error: " + obj.status); }
 });
}
function Zapotrzebowanie(customer_id, link){
 var typ;
 var part;
 typ = take("select2");
 part = take("select4");
 advAJAX.get({
	url : link + "ajax.php?needs&customer_id=" + customer_id + "&id_typu="+typ.value + "&subcategory_id=" + part.value,
	onSuccess : function(obj){
	 alert(obj.responseText);
	}
 });}
function onC(t){
 t.style.width = '240px';
 take("kat").style.width = '280px';
 take("podk").style.width = '155px';
}
function ofC(t){
 t.style.width = '150px';
 take("kat").style.width = '160px';
 take("podk").style.width = '145px';
}
function onS(t){
 t.style.width = '240px';
 take("kat").style.width = '175px';
 take("podk").style.width = '255px';
}
function ofS(t){
 t.style.width = '150px';
 take("kat").style.width = '160px';
 take("podk").style.width = '145px';
}
function stan(ilosc, id, link, dostawca){
 sprStan(link,id, ilosc, dostawca);		
}
function sprStan(link, id, ilosc, dostawca){
 var ksa = id.toString();
 linki = link+'ssl-f-parser.php?dostawca='+dostawca+'&id='+id+'&ilosc='+ilosc;
 advAJAX.get({
  url : linki,
  onInitialization : function() {
	if (take("info")) {take("info").style.visibility = "visible";}			 
	if (take("info")) {take("info").innerHTML = '<div id="info" class="progress"><img src="images/progress.gif" /></div>';}
	if (take(ksa)) {
		take(ksa).setAttribute("src","images/clessidra.gif");
	}},		
  onSuccess : function(obj) {parseStan(obj.responseText);
  if (take("info")) {take("info").style.visibility="hidden";}
  if (take("info")) {take("info").innerHTML = '&nbsp;';}
	pobierz_ikone(link, id);},										
  onError : function(obj) { alert("Error: " + obj.status); }
 });}
function  parseStan(tekst){alert(tekst);}
function pobierz_ikone(link, id){
 var ksa = id.toString();
 linki = link+'ajax.php?stan_id='+id;
 advAJAX.get({
  url : linki,
  onSuccess : function(obj) {
	var jest = 0;
	var stan24 = 0;
	var stan48 = 0;
	xml = obj.responseXML;
	record = xml.getElementsByTagName("record");
	for (i = record.length - 1; i > -1; i--){
	 if (i == 2 && record[i].childNodes[0].childNodes[0].nodeValue > 0 ) stan48 = 1;
	 if (i == 1 && record[i].childNodes[0].childNodes[0].nodeValue > 0 ) stan24 = 1;
	 if (i == 0 && record[i].childNodes[0].childNodes[0].nodeValue > 0 ) jest = 1;
	}		
	if(jest == 1){if (take(ksa)) { take(ksa).setAttribute("src",'http://e-autoparts.pl/catalog/images/jest.gif');}}
	else if(jest == 0 && stan24 == 1){if (take(ksa)) { take(ksa).setAttribute("src",'http://e-autoparts.pl/catalog/images/24h.gif');}}
	else if(jest == 0 && stan24 == 0 && stan48 == 1){if (take(ksa)) { take(ksa).setAttribute("src",'http://e-autoparts.pl/catalog/images/48h.gif');}}
	else if(jest == 0 && stan24 == 0 && stan48 == 0){if (take(ksa)) { take(ksa).setAttribute("src",'http://e-autoparts.pl/catalog/images/ask.gif');}}						
  },
  onError : function(obj) { alert("Error: " + obj.status); }
 });} 
 
function zmianaSamochodu(select, link, tablica, sele1, sele2){
 if(select = 'select'){ // zmiana modelu
  getmarka(tablica[0], link, tablica, sele1, sele2);
  getRecords(link + "ajax.php?select=select&klucz="+tablica[0], 'select1', tablica[0]);}
}
// funkcja sedn wysyła id_typu wybranego pojazdu do index.php
function send(id_typu, link, cat, subcat){
 window.location = link+"\index.php?id_typu="+id_typu;
 if(cat != 0 && subcat !=0) window.location = link+"\index.php?cPath="+cat+"_"+subcat+"&id_typu="+id_typu;
 else if(cat == 0) window.location = link+"\index.php?id_typu="+id_typu;
 else if(cat != 0	&& subcat == 0) window.location = link+"\index.php?cPath="+cat+"&id_typu="+id_typu;	
}	
function addprofil(customer_id, typ, link){zapiszTyp(customer_id, typ, link);}	
function zapiszTyp(customer_id, typ, link){
 advAJAX.get({
	url : link + "ajax.php?zapiszTyp=" + typ +"&customer_id=" +customer_id,
	onSuccess : function(obj){ take("dodany").innerHTML = " ! pojazd zapamiętany w Twoim profilu !";}
 });}
function wylacz_profil(link){
 advAJAX.get({
 url : link + "ajax.php?usunTyp=ok",
 onSuccess : function(obj){ take("profil").innerHTML = "";}
});}	
function photo(){take("info").innerHTML = '';}
function laduj(link, products_id){
 advAJAX.get({
  url : link + "ajax.php?oe=ok&products_id="+products_id,
  onSuccess: function(obj){ take("oe").innerHTML = parseOE(obj.responseXML);} 
 });
 advAJAX.get({
  url : link + "ajax.php?zamienniki=ok&products_id="+products_id,
  onInitialization : function() {
	if (take("info")) {take("info").style.visibility = "visible";}			 
	if (take("info")) {take("info").innerHTML = '<div id="info" class="progress"><img src="images/progress.gif" /></div>';}},
  onSuccess: function(obj){ take("zamienniki1").innerHTML = parseZam1(obj.responseXML);
    take("zamienniki2").innerHTML = parseZam2(obj.responseXML); 
	take("zamienniki3").innerHTML = parseZam3(obj.responseXML);
	if (take("info")) {take("info").style.visibility="hidden";}
	if (take("info")) {take("info").innerHTML = '&nbsp;';}} 
 });}	
function parseOE(xml){
 str = '';
 record = xml.getElementsByTagName("record");
 if(record.length > 0){ 
  str =  str + '<b>numery OE:</b><br>';
  for (i = 0; i < record.length; i++) {	
	for (j = 0; j < 2; j++) {if(j == 1) str = str + record[i].childNodes[j].childNodes[0].nodeValue + "<br>";}	
  }}
 return str;
}
function parseZam1(xml){
 str = '';
 record = xml.getElementsByTagName("record");
 if(record.length > 0){ 
	str =  str + '<b>zamienniki:</b><br>';
	for (i = 0; i < record.length; i++) {	
		for (j = 0; j < 3; j++) {if(j == 0)str = str + record[i].childNodes[j].childNodes[0].nodeValue + "<br>";}	
	}}
return str;
}
function parseZam2(xml){
    str = '';
    str =  str + '<b>cena&nbsp;&nbsp;&nbsp;&nbsp;</b><br><b>';
	record = xml.getElementsByTagName("record");
	  if(record.length > 1){ 
	  //alert(record.length);
		for (i = 0; i < record.length; i++) {	
			for (j = 0; j < 3; j++) {
				
			    if(j == 1)
					str = str + record[i].childNodes[j].childNodes[0].nodeValue + "<br>";
				//if(j == 1)
				    //str2 = str2 + record[i].childNodes[j].childNodes[0].nodeValue + "<br>";
					//str = str + "/n";
			}	
		}
	 }
	return str;
}
function parseZam3(xml){
    str = '';
	str =  str + '<b>producent</b><br>';
	record = xml.getElementsByTagName("record");
	  if(record.length > 1){ 
	   // str =  str + '<b>zamienniki:</b><br>';
	   //alert(record.length);
		for (i = 0; i < record.length; i++) {	
			for (j = 0; j < 3; j++) {
				
			   // if(j == 0)
					//str = str + record[i].childNodes[j].childNodes[0].nodeValue + "<br>";
				if(j == 2)
				    str = str + record[i].childNodes[j].childNodes[0].nodeValue + "<br>";
					//str = str + "/n";
			}	
		}
	 }
	return str;
}
function datacopy(){
	take("company_street").value=take("street_address").value;
	take("company_code").value=take("postcode").value;
	take("company_city").value=take("city").value;
}

