jQuery(document).ready(function(){
	jQuery("input[id^='ile_']").keypress(function (e) {
	if ((e.which>47 && e.which<58)||e.which==8) 
	{
		
		return true; 
	}else return false;
    });
	
	jQuery("input[id^='ile_']").keyup(function (e) {
		var broken=jQuery(this).attr('id').split('_'); 
		zmien(broken[(broken.length-1)]);
	});
	
		
		
});

var needToConfirm=false;

	function ch(what,id)
	{
		var ile;
		ile = jQuery('#ile_'+id).attr('value');
		ile = parseInt(ile);
		if(what=='plus')
		{
			ile = ile+1;
			jQuery('#ile_'+id).attr('value',ile);
			var broken=id.split('_'); 
			zmien(broken[(broken.length-1)]);
		}
		else if(ile>0){
			ile = ile-1;
			jQuery('#ile_'+id).attr('value',ile);
			var broken=id.split('_'); 
			zmien(broken[(broken.length-1)]);
		}
	}
	var bind = 0 ;
	
	function zmien(id)
	{

		var cena = jQuery('#cena_'+id).attr('value');
		var ile = jQuery('#ile_'+id).attr('value');
		cena = parseFloat(cena);
		ile = parseInt(ile);
		var koszt = cena * ile;
		jQuery('#cena_ile_'+id).html(zaokraglaj(koszt));
		
		var cena2 = jQuery('#cena_small_'+id).attr('value');
		var ile2 = jQuery('#ile_small_'+id).attr('value');
		cena2 = parseFloat(cena2);
		ile2 = parseInt(ile2);
		koszt2 = cena2 * ile2;
		jQuery('#cena_ile_small_'+id).html(zaokraglaj(koszt2));
		
		jQuery('#cena_suma_'+id).html(zaokraglaj(koszt+koszt2));

		
		
		var al = 0 ;
		jQuery("[id^='cena_suma_']").each(function(i){
			ile = jQuery(this).html();
			ile = parseFloat(ile);
			 al=al+ile;
 		});
		jQuery('#cena_wszystko').html(zaokraglaj(al));
		
		if(bind!='1')
		{
			bind = '1';
			needToConfirm = true;
			window.onbeforeunload = confirmExit
		}
	}
	
	function zaokraglaj(co)
	{
		if(co)
		{
		co = co * 100;
		var wynik = Math.round(co)/100;
		return wynik;
		}
		else return '0';
	}
	
  function confirmExit()
  {
	  if (needToConfirm)
    return "You have selected a product but did not put it to your cart. If you want to put the product to the cart please select Cacnel and then click the button: Add to cart (at the bottom of the site, under the table with products).";
  }
  
  function zamowienie()
  {
	  ile = jQuery('#cena_wszystko').html();
	  ile = parseFloat(ile);
	  if(ile && ile>150){
		  if(!jQuery('#akceptuje').attr('checked')) 
		  {
			   alert('Accept terms!');
			  return false;
		  }
		  return true
	  }else{
		  alert('Minimal order = 150 Pounds');
		  return false;
	  }
  }
