// JavaScript Document

var SSL_webshop = "https://www.finder.hr";


function dodajUkosaricu(sifra,n,proizvod){
	
	
	
		document.getElementById('loader').style.display = "inline";
		document.getElementById('basket').style.display = "none";
		var komada=document.getElementById('komada_'+proizvod).value;
		
		
		if(!IsNumeric(komada) || komada==0 || komada==''){
			alert("Broj komada mora biti broj veći od nule!");
			document.getElementById('loader').style.display = "none";
			return;
		}
	
		xmlhttp=GetXmlHttpObject();
			
			//xmlhttp=new XMLHttpRequest();
			xmlhttp.onreadystatechange= function() { 
				if (xmlhttp.readyState==4){
					if (xmlhttp.status==200){
					
						
					
						//punimo desni stupac koji prikazuje stanje u košarici
						document.getElementById('kosarica').innerHTML = xmlhttp.responseText;
						document.getElementById('loader').style.display = "none";
						
						
						
							
						// definiranje odnosa - traženo dobiveno... javljamo ako je dobio manje nego je tražio...
						var dodano_komada_hid = document.getElementById('dodano_komada_hid');
						var error_hid = document.getElementById('error_hid');
						
						//ako je došlo do greške
						if(error_hid.value!=0){
							alert("Došlo je do greške prilikom unosa proizvoda u košaricu!\nProizvod nije dodan!");
							document.getElementById('komada_'+proizvod).value = 1;
							window.location.reload();
							return;
						}
						
						// ako nije ništa uneseno
						if(dodano_komada_hid.value==0){
							alert("Proizvod nije dodan u košaricu jer na skladištu trenutno nema ovog proizvoda. \nUmjesto kupnje možete zatražiti ponudu.");
							document.getElementById('komada_'+proizvod).value = 1;
							window.location.reload();
							return;
						}
						
						// ako je uneseno manje nego je tražio
						if(dodano_komada_hid.value<komada){
							mojalert="";
							//mojalert+="U košaricu je uneseno "+dodano_komada_hid.value+" dostupnih komada proizvoda.\n\n"; 
							mojalert+="Na skladištu nije dostupna tražena količina proizvoda!\n\nTražili ste "+komada+" komada\n";
							mojalert+="Dostupno je "+dodano_komada_hid.value+" komada\n\n"; 
							mojalert+="Kupnju možete obaviti samo za dostupnu količinu proizvoda, a za preostalu količinu molimo zatražite ponudu.";
							document.getElementById('komada_'+proizvod).value = dodano_komada_hid.value;
							alert(mojalert);
						}
						
						if(n==2){
							
							window.location.href=SSL_webshop+"/pregled_kosarice.cfm";
						}
						
						
						
						document.getElementById('basket').style.display = "inline";
						
							var pero="<a href='"+SSL_webshop+"/pregled_kosarice.cfm'  style='text-transform:uppercase;color:#fd0100' ><strong>Pregled košarice</strong></a>";
							document.getElementById('basket_kontrole_'+proizvod).innerHTML = pero;
						
		
						
						
					}
					else{
						document.getElementById('loader').style.display = "none";
						alert("Došlo je do greške prilikom unosa proizvoda u košaricu!\nProizvod nije dodan!");
						document.getElementById('komada_'+proizvod).value = 1;
						
					
					}
				}
					
			}
			
			
		
			var url = "/webshop/dodajUkosaricu.cfm?sifra="+sifra+"&komada="+komada+"&dummy=" + new Date().getTime();
			
			
			
			
			
			xmlhttp.open("GET",url,true);
			xmlhttp.send(null);
		}
		
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		
// prikaz statusa košarice u desnom stupcu****************************************************************************************************************

function PregledKosarice(){
	
		if(document.getElementById('loader2')){
		document.getElementById('loader2').style.visibility = "visible";
		}
	
		xmlhttp=GetXmlHttpObject();
			
			//xmlhttp=new XMLHttpRequest();
			xmlhttp.onreadystatechange= function() { 
				if (xmlhttp.readyState==4){
					if (xmlhttp.status==200){
						
						//document.getElementById('kosarica').innerHTML = '';
						writeHTML(xmlhttp, 'kosarica');	
						//alert("Proizvod je dodan u košaricu")
						if(document.getElementById('loader2')){
							document.getElementById('loader2').style.visibility = "hidden";
							
							}
					}
					else{
					alert("Došlo je do greške! (7)");
					}
				}
					
			}
			
			
			
			var url = "/webshop/dodajUkosaricu.cfm?pregled=true&dummy=" + new Date().getTime();
			
			xmlhttp.open("GET",url,true);
			xmlhttp.send(null);
		}