<?php
$waluta = $currencies->currencies[$currency]['symbol_right'];
$miejsca_po_przecinku = $currencies->currencies[$currency]['decimal_places'];
?>

<script type="text/javascript" language="javascript">
function FormatNumber(num)
{
	if(isNaN(num)) { num = "0"; }
	//
	<?php if ($miejsca_po_przecinku > 2) { ?>
	miejsce = (num - parseInt(num));
	miejsce = miejsce.toFixed(<?php echo $miejsca_po_przecinku; ?>);
	grosze = miejsce.substring(2, miejsce.length);
	<?php } ?>
	//
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	<?php if ($miejsca_po_przecinku > 2) { ?>
	cents = grosze;
	<?php } else { ?>
	if(cents<10) { cents = "0" + cents; }
	<?php } ?>
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	{
		num = num.substring(0,num.length-(4*i+3))+'<?php echo $currencies->get_thousands_point($currency); ?>'+ num.substring(num.length-(4*i+3));
	}
	return (((sign)?'':'-') + num + '<?php echo $currencies->get_decimal_point($currency); ?>' + cents);
}

function wyczysc_HTML(tekst) {
var re = /(<([^>]+)>)/gi;
ciag=tekst.replace(re, "");
ciag=ciag.replace('<?php echo $currencies->get_thousands_point($currency); ?>','');
ciag=ciag.replace('<?php echo $currencies->get_decimal_point($currency); ?>','.');
return ciag;
}

function showPrice(form)
{
	var myTotalPrice = 0;
	var myOldPrice = 0;
	var myKatalogPrice = 0;	
	var showUP = 0;
    var myPrice = 0;
	var myMathProblem = "";
	var myWaluta = 0;
	var myItemPrice = parseFloat(form.nuPrice.value);
	if (document.getElementById("staraCena_bz")) {
		var oldPrice = wyczysc_HTML(document.getElementById("staraCena_bz").innerHTML);
        oldPrice = parseFloat(oldPrice.replace(/<?php echo $waluta; ?>/gi,""));
	}
	if (document.getElementById("katalCena_bz")) {
		var KatalogPrice = wyczysc_HTML(document.getElementById("katalCena_bz").innerHTML);
        KatalogPrice = parseFloat(KatalogPrice.replace(/<?php echo $waluta; ?>/gi,""));
	}	
	var myStawkaVat = parseFloat(form.vat_stawka.value);
	for (var i = 0; i < form.elements.length; i++)
	{
		var e = form.elements[i];
		if ( e.type == 'select-one' )
		{
			showUP = 1;
			Item = e.selectedIndex;
			myPrice = e.options[Item].text;
	    } else if (e.type == 'radio' && e.checked) {
	      showUP = 1;
	      myPrice = e.getAttribute('price');
	    } else {showUP = 0}

    if (showUP == 1) {
			var myDollarSign = myPrice.indexOf("[",0)
			if ( myDollarSign != "-1" )
			{
				myParSign = myPrice.indexOf("]", myDollarSign);
				myWaluta = myPrice.indexOf(" ", myDollarSign);
				myAttributeString = myPrice.substring(myWaluta + 1, myParSign);
				tysiac = myAttributeString.lastIndexOf(",");
				if ( tysiac != "-1" ) {
					myAttributeString = myAttributeString.replace(".","");
				}
				myAttributeString = myAttributeString.replace(/,/,"."); 
                myAttributeString = myAttributeString.replace(/<?php echo $waluta; ?>/gi,""); 
				myAttributePrice = parseFloat(myAttributeString);             
				myMathProblem = myPrice.charAt(myDollarSign + 1);
			} else { myAttributePrice = 0; }
			if (myMathProblem == "-")
			{
				myTotalPrice = myTotalPrice - myAttributePrice;
				if (document.getElementById("staraCena_bz")) {				
					myOldPrice = myOldPrice - myAttributePrice;
				}
				if (document.getElementById("katalCena_bz")) {				
					myKatalogPrice = myKatalogPrice - myAttributePrice;
				}				
			} else {
				myTotalPrice = myTotalPrice + myAttributePrice;              
				if (document.getElementById("staraCena_bz")) {					
					myOldPrice = myOldPrice + myAttributePrice;
				}
				if (document.getElementById("katalCena_bz")) {				
					myKatalogPrice = myKatalogPrice + myAttributePrice;
				}				
			}
		}
	}
	myStawkaVat = (myStawkaVat/100)+1;
	myTotalPrice_Netto = FormatNumber((myTotalPrice + myItemPrice)/myStawkaVat)
	myTotalPrice = FormatNumber(myTotalPrice + myItemPrice);
	if (document.getElementById("staraCena_bz")) {	
        myOldPrice_Netto = FormatNumber((parseFloat(myOldPrice) + oldPrice)/myStawkaVat);	
		myOldPrice = FormatNumber(parseFloat(myOldPrice) + oldPrice);	
	}
	if (document.getElementById("katalCena_bz")) {	
		myKatalogPrice = FormatNumber(parseFloat(myKatalogPrice) + KatalogPrice);	
	}	
	<?php
	if (DISPLAY_PRICE_BRUTTO_NETTO == 'true') {
        echo 'document.getElementById("nowaCena").innerHTML = "<span class=\'kwot_netto\'>" + myTotalPrice_Netto + "' . ' '. $waluta .'</span><span class=\'netbru_text\'>'.TEXT_NETTO . '</span><br><span class=\'kwot_brutto\'>" + myTotalPrice + " '. $waluta .'</span><span class=\'netbru_text\'>'.TEXT_BRUTTO.'</span>";';
		echo 'if (document.getElementById("staraCena_bz")) {';
		echo 'document.getElementById("staraCena").innerHTML = myOldPrice+ "' . ' '. $waluta . '";';
        echo 'document.getElementById("staraCena").innerHTML = "<span class=\'kwot_netto\'>" + myOldPrice_Netto + "' . ' '. $waluta .'</span><span class=\'netbru_text\'>'.TEXT_NETTO . '</span><br><span class=\'kwot_brutto\'>" + myOldPrice + " '. $waluta .'</span><span class=\'netbru_text\'>'.TEXT_BRUTTO.'</span>";';
		echo '}';
		echo 'if (document.getElementById("katalCena_bz")) {';
		echo 'document.getElementById("katalCena").innerHTML = myKatalogPrice+ "' . ' '. $waluta . '";';
		echo '}';		
	  } else {
	    echo 'document.getElementById("nowaCena").innerHTML = myTotalPrice+ "' . ' '. $waluta . '";';
		echo 'if (document.getElementById("staraCena_bz")) {';		
		echo 'document.getElementById("staraCena").innerHTML = myOldPrice+ "' . ' '. $waluta . '";';
		echo '}';	
		echo 'if (document.getElementById("katalCena_bz")) {';
		echo 'document.getElementById("katalCena").innerHTML = myKatalogPrice+ "' . ' '. $waluta . '";';
		echo '}';		
    }
	?>
}
</script>