﻿var _started = 0;
var temp_id_wina = 0;
var temp_id_procesu = 0;

function pokazDane(id_wina,id_procesu) {
	
		temp_id_wina = id_wina;
		temp_id_procesu = id_procesu;
		document.location.hash = "#wino_"+id_wina+"&proc_"+id_procesu;
		
	advAJAX.get({
		onInitialization : function() {
	        //nic
	    },
	    url: "ajax/karta_dane.php?id_wina="+id_wina+"&id_procesu="+id_procesu,
	    onSuccess : function(obj) { 
	    document.getElementById('id_tekstu').innerHTML= obj.responseText;
	    if(_started == 0) { nasluch(); }
	    skroll(id_wina,id_procesu);
	    
	     },
	    onError : function(obj) { alert("Error: " + obj.status); }
	});
}

function wyslijLink(id_wina) {
		//var _email;
		
		_email = document.getElementById('link_email').value;
		_email_nad = document.getElementById('link_email_nad').value;
		_link = document.getElementById('link_adres').value;

	advAJAX.get({
		onInitialization : function() {
	        //nic
	    },
	    url: "ajax/wyslij_link.php?email="+_email+"&adres="+_link+"&email_nad="+_email_nad,
	    onSuccess : function(obj) { 
	    
	    	switch(obj.responseText) {
	    		case('1') : tresc = 'Adres został wysłany na '+_email+'.'; break;
	    		case('2') : tresc = 'Wpisz właściwy adres email.'; 
	    			setTimeout("pokazDane("+id_wina+",6)",3000);
	    		break;
	    	}
	   		document.getElementById('wyslij_link').innerHTML= tresc;
	   		
	     },
	    onError : function(obj) { alert("Error: " + obj.status); }
	});
}

function nasluch() {

		_started = 1;
		
		if(window.location.hash) {
			response = window.location.hash;
			response = response.substr (response.indexOf('#') + 1, response.length);
			
			wino_str = response.substr (0, response.indexOf('&'));
			wino_id =  wino_str.substr (wino_str.indexOf('_')+1, wino_str.length);
		
			proces_str = response.substr (response.indexOf('&')+1, response.length);
			proces_id = proces_str.substr (proces_str.indexOf('_')+1, proces_str.length);
		
			//document.getElementById('pole_temp').innerHTML= wino_id+" "+proces_id;
			
			if(temp_id_wina != wino_id || temp_id_procesu != proces_id) {
				//document.getElementById('pole_temp').innerHTML= wino_id+" "+proces_id+ " nierowne/zmiana";
				temp_id_wina = wino_id;
				temp_id_procesu = proces_id;
				pokazDane(wino_id,proces_id);
			}
			
			setTimeout("nasluch()",500);
		}

}




function pokazWino(_id) {
	advAJAX.get({
		onInitialization : function() {
	        //nic
	    },
	    url: "tpl_karta.php?id_wina="+_id+"&alone_in_the_dark=1",
	    onSuccess : function(obj) {
		   	
		   	
		   	document.getElementById('id_karta').innerHTML= obj.responseText;
	    },
	    onError : function(obj) { alert("Error:  " + obj.status); }
	});
}

function changeUserData(_id) {
	advAJAX.get({
		onInitialization : function() {
	        //nic
	    },
	    url: "ajax/change_user_data.php?id="+_id,
	    onSuccess : function(obj) { 
		    if(_id == 1) {
		   		document.getElementById('id_dane_osobowe').innerHTML= obj.responseText;
		   	} else {
		   		document.getElementById('id_dane_firmy').innerHTML= obj.responseText;
		   	}
	    },
	    onError : function(obj) { alert("Error:  " + obj.status); }
	});
}


function showOrder(_id) {
	advAJAX.get({
		onInitialization : function() {
	        //nic
	    },
	    url: "ajax/show_order.php?id="+_id,
	    onSuccess : function(obj) { 
		   		document.getElementById('transakcje').innerHTML= obj.responseText;
	    },
	    onError : function(obj) { alert("Error: " + obj.status); }
	});
}


function skroll() {

    scroller.docH = document.getElementById("content").offsetHeight;
    scroller.contH = document.getElementById("container").offsetHeight;
    scroller.scrollAreaH = document.getElementById("scrollArea").offsetHeight;
      
    if(scroller.docH > scroller.scrollAreaH) {
  		document.getElementById("scrollArea").style.visibility = 'visible';
	    scroller.scrollH = (scroller.contH * scroller.scrollAreaH) / scroller.docH;
	    document.getElementById("scroller").style.height = Math.round(scroller.scrollH) + "px";
	    scroller.scrollDist = Math.round(scroller.scrollAreaH-scroller.scrollH);
	    Drag.init(document.getElementById("scroller"),null,0,0,-1,scroller.scrollDist);
	    
	    document.getElementById("scroller").onDrag = function (x,y) {
	      var scrollY = parseInt(document.getElementById("scroller").style.top);
	      var docY = 0 - (scrollY * (scroller.docH - scroller.contH) / scroller.scrollDist);
	      document.getElementById("content").style.top = docY + "px";
	    }
    
 	 } else {
	 	 document.getElementById("scrollArea").style.visibility = 'hidden';
 	 }
  
}
  
  
  
function zmianaRegionu(_id) {

	document.frm.id_regionu.value = _id;

	//kombo apleacji
	advAJAX.get({
		onInitialization : function() {
	        document.getElementById('apelacja').innerHTML = "Trwa ładowanie...";
	    },
	    url: "ajax/apelacja.php?seq=1&id_regionu="+_id,
	    onSuccess : function(obj) { 
		   		document.getElementById('apelacja').innerHTML= obj.responseText;
	    },
	    onError : function(obj) { alert("Error: " + obj.status); }
	});
	
	//kombo producenta
	advAJAX.get({
		onInitialization : function() {
	        document.getElementById('producent').innerHTML = "Trwa ładowanie...";
	    },
	    url: "ajax/producent.php?seq=1&id_regionu="+_id,
	    onSuccess : function(obj) { 
		   		document.getElementById('producent').innerHTML= obj.responseText;
	    },
	    onError : function(obj) { alert("Error: " + obj.status); }
	});
	odznaczBtWszystkie();
	
}

function wstawIdRegionu(_id) {
	document.frm.id_regionu.value = _id;

}

function zmianaApelacji() {

	 _id = document.getElementById('id_apelacji').value;
	//alert(_id+' '+document.fm.id_regionu.value);
	
	//kombo apleacji
	advAJAX.get({
	    url: "ajax/apelacja.php?seq=2&id_apelacji="+_id,
	    onSuccess : function(obj) { 
		   		document.getElementById('apelacja').innerHTML= obj.responseText;
	    },
	    onError : function(obj) { alert("Error: " + obj.status); }
	});
	
	//kombo producenta
	advAJAX.get({
		onInitialization : function() {
	        document.getElementById('producent').innerHTML = "Trwa ładowanie...";
	    },
	    url: "ajax/producent.php?seq=2&id_apelacji="+_id,
	    onSuccess : function(obj) { 
		   		document.getElementById('producent').innerHTML= obj.responseText;
	    },
	    onError : function(obj) { alert("Error: " + obj.status); }
	});
	
	

	
	//do id regionu
	advAJAX.get({
	    url: "ajax/id_regionu.php?seq=1&id_apelacji="+_id,
	    onSuccess : function(obj) { 
			document.frm.id_regionu.value = obj.responseText;
			document.getElementById('flash_regiony').SetVariable('id_regionu', obj.responseText);
	    },
	    onError : function(obj) { alert("Error: " + obj.status); }
	});
	
}
  
  
function zmianaProducenta() {

	 _id = document.getElementById('id_producenta').value;
	// alert(_id);
	 
	 _id_apelacji = document.getElementById('id_apelacji').value;
	//  alert(_id_apelacji);

	//kombo apleacji
	/*
	advAJAX.get({
	    url: "ajax/apelacja.php?seq=3&id_producenta="+_id+"&id_apelacji="+_id_apelacji,
	    onSuccess : function(obj) { 
		   		document.getElementById('apelacja').innerHTML= obj.responseText;
	    },
	    onError : function(obj) { alert("Error: " + obj.status); }
	});
	*/
	
	//kombo producenta
	advAJAX.get({
	    url: "ajax/producent.php?seq=3&id_producenta="+_id,
	    onSuccess : function(obj) { 
		   		document.getElementById('producent').innerHTML= obj.responseText;
	    },
	    onError : function(obj) { alert("Error: " + obj.status); }
	});
	
	
	//do id regionu
	advAJAX.get({
	    url: "ajax/id_regionu.php?seq=2&id_producenta="+_id,
	    onSuccess : function(obj) { 
			document.frm.id_regionu.value = obj.responseText;
			document.getElementById('flash_regiony').SetVariable('id_regionu', obj.responseText);

	    },
	    onError : function(obj) { alert("Error: " + obj.status); }
	});
	
}
  
function zmienPunkty(_pkt) {
	document.frm.punktacja.value = _pkt;
	odznaczBtWszystkie();
}

function pokazPrzechowalnie() {
	document.getElementById('id_przechowalni').style.display = 'block';
}

function ukryjPrzechowalnie() {
	document.getElementById('id_przechowalni').style.display = 'none';
}


function zaznaczPrezent(_id,_seq) {
	advAJAX.get({
		onInitialization : function() {
	        //nic
	    },
	    url: "ajax/zmien_opakowanie.php?&id="+_id+"&seq="+_seq,
	    onSuccess : function(obj) { 
	    	document.getElementById('img_prezent_'+_id).innerHTML = obj.responseText;
	    	zmienCene(_id,_seq);
	    },
	    onError : function(obj) { alert("Error: " + obj.status); }
	});
	
	
}

function zmienCene(_id,_seq) {

	_cena = document.getElementById('cena_pierwotna_'+_id).value;
	_ilosc = document.getElementById('ilosc_'+_id).value;
	
	//alert(_cena+' '+_ilosc);

	advAJAX.get({
	
		onInitialization : function() {
	        //nic
	    },
	    url: "ajax/zmien_opakowanie_ceny.php?&id="+_id+"&seq="+_seq+"&cena="+_cena+"&ilosc="+_ilosc,
	    onSuccess : function(obj) {
	    
	    	_str = obj.responseText;
	    	cena_ilosc = _str.substr (0, _str.indexOf('#'));
	    	cena_koniec = _str.substr (_str.indexOf('#')+1, _str.length);
	    	
	    	document.getElementById('cena_ilosc_'+_id).innerHTML = cena_ilosc+' zł';
	    	document.getElementById('cena_koniec').innerHTML = '= <b>'+cena_koniec+' zł</b>';
	    	
	    	
	    },
	    onError : function(obj) { alert("Error: " + obj.status); }
	});
	
}

  
