
function init_elfmoney_form(){
	selectTransferType();
	recount(price_per, qty_price_text);

	$('#buy-gold-form-qty ,#buy-gold-form-server, #buy-payment-method').change(function(){
		recount(price_per, qty_price_text);
	});

	$('#buy-gold-form-qty, #buy-gold-form-server').keyup(function(){
		recount(price_per, qty_price_text);
	});

	$('#transfer-type').change(function(){
		selectTransferType();
	});

	$('#buy-gold-form-qty-price').change(function() {
		count_amount_from_price();
		recount(price_per, qty_price_text, true);
	});

	$('#buy-gold-form-qty-price').keyup(function() {
		 count_amount_from_price();
		 recount(price_per, qty_price_text, true);
	});

	$("form").submit(function () {
		if (product_id == 24) {
			if ($("#buy-gold-form-qty").val()%5 != 0) {
				alert ("Приносим извинения, но, в данный момент мы можем продать только количество, кратное 5 (5, 10, 15...).");
				return false;
			}
		}
	});
}

function count_amount_from_price() {
	qty_price = $('#buy-gold-form-qty-price').attr('value');
	method = $("#buy-payment-method").attr('value');
	price = prices[$("#buy-gold-form-server").attr('value')]/price_per*payment_coefs[method];

	iqty = qty_price/price;

	for (var i=0; i<wholesale.length; i++) {
		if (wholesale[i]['min'] <= iqty && wholesale[i]['max'] > iqty || (wholesale[i]['min'] <= iqty && wholesale[i]['max'] == 0)) {
			coef = wholesale[i]['coef'];
			break;
		}
	}

	ret = parseFloat(qty_price/(price*coef)).toFixed(2);
	$('#buy-gold-form-qty').attr('value', ret);
}

function selectTransferType() {
	selectedType = $('#transfer-type').attr('value');
	$('#id-auction-alert').hide();
	if(selectedType == 'account') {
		$('#login-block').show();
		$('#id-account-alert').show();
		$('#CharacterCommentLabel').text('Время, когда оператор может зайти на ваш аккаунт:');
	}
	else {
		if (selectedType == 'auction') {
			$('#id-auction-alert').show();
			$('#CharacterCommentLabel').text('Название предмета:');
		}
		else {
			$('#CharacterCommentLabel').text('Комментарий:');
		}
		$('#login-block').hide();
	}
}

function recount(price_per, qty_price_text, call_from_count_amount) {
	price_per = price_per == undefined ? 1000 : price_per;
	post_text = '';

	id = $("#buy-gold-form-server").attr('value');
	method = $("#buy-payment-method").attr('value');

	qty = $("#buy-gold-form-qty").attr('value');

	coef = 1;
	//Расчет скидки за опт
	iqty = parseInt(qty);
	for (var i=0; i<wholesale.length; i++) {
		if (wholesale[i]['min'] <= iqty && wholesale[i]['max'] > iqty || (wholesale[i]['min'] <= iqty && wholesale[i]['max'] == 0)) {
			coef = wholesale[i]['coef'];
			break;
		}
	}

	if (id != 0) {
		qty = parseFloat(qty*payment_coefs[method]);
		total = parseFloat(qty * prices[id]/price_per*coef).toFixed(2);

		total_text = total + " " + payment_types[method]['currency'];

		if(coef != 1) {
			post_text += "<br>Включая скидку " + (100-coef*100) + "%";
		}

		if (call_from_count_amount == undefined)
			$("#buy-gold-form-qty-price").attr('value', total);
		$("#buy-gold-form-sub-count").html("Итого: <b>" + total_text + "</b>"+post_text);
		$("#span-server-price").html("&nbsp;Стоимость: <b>" + prices[id] + "</b>р. ("+qty_price_text+")");
	}
}