
var reqx;
var currPriceID=0;
var currcartkey='';
var currorder='';
var axaction='';

function updateProductPrice(id) {
		currPriceID=id;
		axaction='setprice';
		var ckey=getCartKey(id);
		posaljiZahtjev('opt/shop/ajx/getprice','key='+ckey);
}
function favorite(id) {
		axaction='setfavorite';
		posaljiZahtjev('opt/shop/ajx/setfavorite','id='+id);
}
function favoriteremove(id) {
		axaction='favoriteremoved';
		posaljiZahtjev('opt/shop/ajx/favoriteremove','id='+id);
}

function setfavorite(txt) {
	alert(txt);
}
function favoriteremoved(txt) {
	var newel=document.getElementById(txt);
	if (newel) {
		var par=newel.parentNode;
		par.removeChild(newel);
	}
	alert("Proizvod je uklonjen iz liste favorita");
}

function shOO(id,t) {
		axaction='axshoworder';
		currorder=id;
		posaljiZahtjev('opt/shop/ajx/showorder','type='+t+'&id='+id);
}
function shOC(id) {
		var ordimg=document.getElementById('oi_'+id);
		var ordimgm=document.getElementById('oim_'+id);
		ordimgm.style.display='none';
		ordimg.style.display='block';
		var ordblock=document.getElementById('ord_'+id);
		ordblock.innerHTML='';
}
function axshoworder(txt) {
		var ordimg=document.getElementById('oi_'+currorder);
		var ordimgm=document.getElementById('oim_'+currorder);
		ordimg.style.display='none';
		ordimgm.style.display='block';
		var ordblock=document.getElementById('ord_'+currorder);
		ordblock.innerHTML=txt;
}
function getCartKey(id) {
		var nfrm=document.getElementById('shform'+id);
		var vals=new Array();
		for(var i=0;i<nfrm.elements.length;i++) {
			if ((nfrm.elements[i].type=='select') || (nfrm.elements[i].type=='select-one')) {
				var n=nfrm.elements[i].name.replace('opt','');
				vals.push(n+'-'+nfrm.elements[i].options[nfrm.elements[i].selectedIndex].value);
			}
		}
		return id+'_'+vals.join('|');
}
function getSelectedValue(boxid) {
    var obj=document.getElementById(boxid);
    return obj.options[obj.selectedIndex].value;
}

function setprice(txt) {
		var ispis=document.getElementById('ispis_cijene'+currPriceID);
		var ispisr=document.getElementById('ispis_cijener'+currPriceID);
		var ispisc=document.getElementById('ispis_code'+currPriceID);
		var tmparr=txt.split("|");
		ispis.innerHTML=tmparr[0];
		ispisr.innerHTML=tmparr[1];
		ispisc.innerHTML=tmparr[2];
}
function setcart(txt) {
		var carttotal=document.getElementById('carttotal');
		if (carttotal) {
			var itm=document.getElementById(currcartkey);		
			if (!itm) { 
				var tmp=document.createElement('li');	
				tmp.id=currcartkey;
				var clist=document.getElementById('cartlist');
				itm=clist.appendChild(tmp);	
			}
			var ret=txt.split("#$#");
			itm.innerHTML=ret[0];
			carttotal.innerHTML=ret[1];
		} else document.location.reload();
}
function cart(id) {
		axaction='setcart';	
		var ck=getCartKey(id);
		currcartkey='crt_'+ck;
		if (document.getElementById('qty'+id))
			posaljiZahtjev('opt/shop/ajx/cart','qt='+document.getElementById('qty'+id).value+'&key='+ck);
		else posaljiZahtjev('opt/shop/ajx/cart','key='+ck);
}
function mon2US(str) {
	return Number(str.replace(".","").replace(",","."));
}
function mon2HR(str) {
	var num=str.replace(".",",");
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+'.'+num.substring(num.length-(4*i+3));

	return num.replace(".,",",");
}
function calcQt(k) {
		var qt=document.getElementById('qt_'+k);
		var oqt=document.getElementById('oldqt_'+k);
		var opo=document.getElementById('oldpo_'+k);
		if (Number(qt.value)<1) qt.value=oqt.value;
		else {
			var uw=document.getElementById('uw_'+k);
			var am=document.getElementById('am_'+k);
			var po=document.getElementById('po_'+k);
			var tam=document.getElementById('tam_'+k);
			var tpo=document.getElementById('tpo_'+k);
			var total=document.getElementById('total');
			var discount=document.getElementById('discountfld');
			
			var totalpoints=document.getElementById('totalpoints');
			var totalfld=document.getElementById('totalfld');
			var totalpointsfld=document.getElementById('totalpointsfld');
			var nam=qt.value*mon2US(am.value);
			var npo=Math.min(qt.value*po.value,SHP_MAXPOINTS);
			tam.innerHTML=mon2HR(nam.toFixed(2));
			tpo.innerHTML=npo;
			var tot=mon2US(totalfld.value)-oqt.value*mon2US(am.value)+nam;
			var totp=Math.min(totalpointsfld.value-oqt.value*po.value+npo,SHP_MAXPOINTS);
			total.innerHTML=mon2HR(tot.toFixed(2));
			if (discount.value>0) {
				var totald=document.getElementById('totald');
				var shipping=document.getElementById('shippingfld');
				var totd=(mon2US(totalfld.value)-oqt.value*mon2US(am.value)+nam+mon2US(shipping.value))*(100-discount.value)/100;
				totald.innerHTML=mon2HR(totd.toFixed(2));
			}
			totalfld.value=mon2HR(tot.toFixed(2));
			totalpoints.innerHTML=totp;
			totalpointsfld.value=totp;
			axaction='setcart';	
			currcartkey='crt_'+k;	
			posaljiZahtjev('opt/shop/ajx/cart','qt='+qt.value+'&key='+k);
			oqt.value=qt.value;
		}
}


