function calculateShares(){
	var ids	= $("#pak_sale_ids").val().split(",");
	$(ids).each(function(i){
		var field	= $("#input_pak_start_amount_"+ids[i]);
		var price	= field.val();

		if( price <= 0 || isNaN(price) )
			field.value(price = 1);
		var sum = $("#fld_pak_pre_total_start_amount").html();
		sum	= sum.replace( /^([0-9.,]+).*$/, "$1" );
		while( sum.indexOf('.') != -1 )
			sum = sum.replace(".","");
		sum = sum.replace(",",".");
		var share = price / sum * 100;
		share = number_format(share.toFixed(0),0,',', '.');
		field = $("#fld_pak_pre_price_share_"+ids[i]);
		field.html(share+"&nbsp;%");
	});
}

function calculatePricesInPackage(){
	var ids	= $("#pak_sale_ids").val().split(",");
	//var currency = $("#currency").val();
	var currency = "";
	var oldPackagePrice = getPackagePrice();
	var newPackagePrice = $("#input_pak_total_price_in_package").val();
	$(ids).each(function(i){
		price	= $("#input_pak_start_amount_"+ids[i]).val();
		price	= Math.round( newPackagePrice * ( price / oldPackagePrice ) );
		price	= number_format( price, 0, ',', '.' );
		$("#fld_pak_pre_price_in_package_"+ids[i]).html( price+"&nbsp;"+currency );
	});
}

function getPackagePrice(){
	var ids	= $("#pak_sale_ids").val().split(",");
	var sum	= 0;
	$(ids).each(function(i){
		var field	= $("#input_pak_start_amount_"+this);
		price	= field.val();
		if( price <= 0 || isNaN(price) )
			field_id.val(price = 1);
		sum		+= parseFloat( price );
	});
	return sum;
}

function initalisePackagePrice()
{
	var sum		= getPackagePrice();
	// var currency = $("#currency").val();
	var currency = "";
	var number	= number_format(sum,0,',', '.');
	$("#fld_pak_pre_total_start_amount").html(number+"&nbsp;" +currency);
	calculateShares();
	calculatePricesInPackage();
}

function convertCurrency( initiator )
{
	 var currencyIdFirst	= $("#currencyIdFirst");
	 var currencyIdSecond	= $("#currencyIdSecond");
	 var firstCurrency		= document.getElementById("firstCurrency");
	 var secondCurrency		= document.getElementById("secondCurrency");
	 var priceFirst			= firstCurrency.value;
	 var priceSecond		= secondCurrency.value;
	 var rateFirst			= currencies[currencyIdFirst.val()];
	 var rateSecond			= currencies[currencyIdSecond.val()];

	 priceSecond	= priceSecond.replace( /\./g,'', priceSecond );
	 priceSecond	= priceSecond.replace( /\,/g,'.', priceSecond );

	 priceFirst		= priceFirst.replace( /\./g,'', priceFirst );
	 priceFirst		= priceFirst.replace( /\,/g,'.', priceFirst );

	 if ( initiator == 1 )
	 {
	 	 priceSecond			= exchangeValue( priceFirst, rateFirst, rateSecond );
	 	 secondCurrency.value	= number_format(priceSecond, 2, ',','.');
	 }
	 else
	 {
	 	 priceFirst				= exchangeValue( priceSecond, rateSecond, rateFirst );
	 	 firstCurrency.value	= number_format(priceFirst, 2, ',','.');
	 }
}

function exchangeValue( from, rate1, rate2 )
{
	if ( rate1 == rate2)
		return from;

	if ( rate1 == 1.0 && rate2 != 1.0 )
		return from*rate2;
	else if ( rate1 != 1.0 && rate2 == 1.0 )
		return from/rate1;
	else if ( rate1 != 1.0 && rate2 != 1.0 )
		return (from*rate2)/rate1;
	else
		return from;
}

function exchange(price_field, target_field,  currency_rate, currency_label) {
	if (currency_rate != 0)
	{
		var field	= $("#"+price_field);
		var priceInEuro	= field.val();
		var start_amount_in_my_currency = priceInEuro * currency_rate;
		start_amount_in_my_currency = Math.round( start_amount_in_my_currency );
		start_amount_in_my_currency = number_format( start_amount_in_my_currency,0,',','.' );

		document.getElementById(target_field).firstChild.innerHTML = start_amount_in_my_currency + " " + currency_label;
	}
}

