function AddToCart(itemid, maxitems) {
quantity = document.getElementById("item_"+itemid).value;
if (quantity > 0 && quantity <= maxitems) {
  if (window.encodeURIComponent)
    oldurl = encodeURIComponent(location);
  else if (window.escape)
    oldurl = escape(location);
  window.location="index.php?action=add2cart&itemid="+itemid+"\&quantity="+quantity+"\&oldurl="+oldurl;
  } else {
  alert("Wpisałeś błędną liczbę przedmiotów!");
  };
}

function CheckOrderForm() {
  var emailfilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

  if (document.forms["order_form"].elements["first_name"].value == "") {
    alert("Nie podałeś imienia zamawiającego!");
    return false;
  }
  if (document.forms["order_form"].elements["last_name"].value == "") {
    alert("Nie podałeś nazwiska zamawiającego!");
    return false;
  }
  if (!emailfilter.test(document.forms["order_form"].elements["email"].value)) {
    alert("Nie podałeś poprawnego adresu e-mail zamawiającego!");
    return false;
  }
  if (document.forms["order_form"].elements["phone"].value == "") {
    alert("Nie podałeś numeru telefonu zamawiającego!");
    return false;
  }
  if (document.forms["order_form"].elements["address"].value == "") {
    alert("Nie podałeś swojego adresu!");
    return false;
  }
  if (document.forms["order_form"].elements["postcode"].value == "") {
    alert("Nie podałeś kodu pocztowego!");
    return false;
  }
  if (document.forms["order_form"].elements["city"].value == "") {
    alert("Nie podałeś miasta!");
    return false;
  }
  if (document.forms["order_form"].elements["rules"].checked == false) {
    alert('Musisz zaakceptować regulamin!');
    return false;
  }
  
  myLen = document.forms["order_form"].elements["paymentType"].length;
  paymentChecked = false;
  for ($i=0; $i<myLen; $i++) {
  	if (document.forms["order_form"].elements["paymentType"][$i].checked==true) paymentChecked = true;
  }
  if (paymentChecked==false) {
  	alert('Nie wybrano sposobu płatności');
  	return false;
  }
return true;
}

function setDefaults($newTotalSum, $newShippingCost, $newPaymentCost) {
	var $totalSum = $newTotalSum;
	var $shippingCost = $newShippingCost;
	var $paymentCost = $newPaymentCost;
}

function refreshValues() {
	sc = document.getElementById('inlinePaymentCost');
	sc.innerHTML = number_format(($totalSum+$shippingCost)*$platnosciCommission) + ' zł';
	sc = document.getElementById('summaryPaymentCost');
	sc.innerHTML = number_format($paymentCost) + ' zł';
	sc = document.getElementById('summaryShippingCost');
	sc.innerHTML = number_format($shippingCost) + ' zł';
	tc = document.getElementById('summaryTotalCost');
	tc.innerHTML = number_format($totalSum+$shippingCost+$paymentCost) + ' zł';
}

function updatePaymentCost($commission) {
	$paymentCost = ($totalSum+$shippingCost)*$commission;
	$currentCommission = $commission;
	refreshValues();
}

function updateShippingCost($newShippingCost) {
	$shippingCost = $newShippingCost;
	$paymentCost = ($totalSum+$shippingCost)*$currentCommission;
	refreshValues();
}

function number_format(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	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 (((sign)?'':'-') + num + ',' + cents);
}
