/* Made by Mathias Bynens <http://mathiasbynens.be/> */
function number_format(a, b, c, d) {
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if (!f[0]) {
		f[0] = '0';
	}
	if (!f[1]) {
		f[1] = '';
	}
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}
function formatPrice(price) {
	price_out=number_format(price,2,',',' ');
	price_out = price_out+"&nbsp;&euro;";
	return price_out;
}

function checkConfirmObchodne() {
	check = document.getElementById("confirm_obchodne");
	if (check.checked==true) {
		return true;
	} else {
		alert('Je nám ľúto, ale pokiaľ neakceptujete naše obchodné a reklamačné podmienky\nnie je možné pokračovať v objednávke!');
		return false;
	}
}




function updateBasketCount(request,mnozstvo,cena) {
	count = document.getElementById("basket_count").innerHTML;
	count = parseInt(count) + parseInt(mnozstvo);
	document.getElementById("basket_count").innerHTML = count;

	price = document.getElementById("basket_value").innerHTML;
	price = parseFloat(price) + parseFloat(cena);
	document.getElementById("basket_value").innerHTML = price;
	document.getElementById("basket_value_out").innerHTML = formatPrice(price);
}

function addToBasket(uid,sid,id,amount,price) {
	var url = "ajax_basket.php?action=addtobasket&sid="+sid+"&uid="+uid+"&pid="+id+"&amt="+amount;
	var params = "";
	var ajax = new Ajax.Request(
		url,
		{
			method: 'get',
			parameters: params,
			onSuccess: function(transport) {
				updateBasketCount(transport,amount,price)
				document.getElementById("img_"+id).src = "imgs/btn_vlozdokosikafull.gif";
				alert('Tovar bol pridaný do košíka.',3);
			}, 
			onFailure: function(request) {/*alert('Chyba pri pridavaní do kosíka!')*/}
		}
	);
}

