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);
	});
	$('#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;
	
	var x_out;
	var str = "";
	switch(method) {
		case 'webmoney_r':
		case 'paymer_wmr':
			qty_price/=(1.008);
		break;
		case 'webmoney_z':
		case 'paymer_wmz':
			qty_price/=payment_coefs[method];
			qty_price/=(1.008);
		break;
		default:
			qty_price/=payment_coefs[method];
		break;
	}
	for (var i=0; i<wholesale.length; i++) {
		x_out = qty_price;
		x_out /= price;
		x_out /= wholesale[i]['coef'];
		x_out = parseFloat(x_out).toFixed(0);
		x_out = x_out-0;
		//str = str + "x_out=" + x_out + " " +wholesale[i]['coef'] + " " + " m" + payment_coefs[method] + " price="+price+ " min="+ wholesale[i]['min'] + " max="+wholesale[i]['max'] + "<br />";
		//$("#err_text").html(str);
		//$("#err_text").show();
		if (x_out < wholesale[i]['min']) {
			x_out = qty_price;
			x_out /= price;
			x_out /= wholesale[i-1]['coef'];
			x_out = parseFloat(x_out).toFixed(0);
			break;
		}
	}
	$('#buy-gold-form-qty').attr('value', x_out);
}

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) {
			coef = wholesale[i]['coef'];
			break;
		}
	}

	if (id != 0) {
		qty = parseFloat(qty*payment_coefs[method]);
		total = parseFloat(qty * prices[id]/price_per*coef).toFixed(2);
		if (total > 1000000) total=1000000;
		total_text = total + " " + payment_types[method]['currency'];

		if(coef != 1) {
			post_text += "<br>включая скидку " + (100-coef*100) + "%";
		}
		cstt=0;
		$("#buy-mobile").attr('style',"display: none;");
		$("#buy-paymer").attr('style',"display: none;");
		komis_text="";
		if (payment_coefs[method]>1) {
			komis = ((payment_coefs[method]-1)*100).toFixed(0);
			komis_text = "<br /></b> с комиссией "+komis+"%";
		}				
		if (($("#buy-payment-method").attr('value') == "webmoney_r") || ($("#buy-payment-method").attr('value') == "webmoney_z")) {
			cstt=parseFloat(total*0.008).toFixed(2);
			total=total-0;cstt=cstt-0;
			cstt=parseFloat(total+cstt).toFixed(2);
			total = cstt;
			total_text = "<b>"+cstt+" "+payment_types[method]['currency']+"<br /></b> c учетом комиссии WebMoney (0.8%)";
			//post_text = "<br />С учетом комиссии WebMoney (0.8%) : <b>"+cstt+" "+payment_types[method]['currency']+"</b>";
		} else if ($("#buy-payment-method").attr('value') == "yandex_manual") {
			//cstt=parseFloat(total*(payment_coefs['yandex_manual_comm']-1)).toFixed(5);
			//total=total-0;cstt=cstt-0;
			//cstt=parseFloat(total+cstt).toFixed(2);
			total_text = "<b>"+total+"</b> "+payment_types[method]['currency'];
			komis_text = "<br />с комиссией RoboxChange (~10.8%)";
		} else if ($("#buy-payment-method").attr('value') == "yandex_man") {
			total_text = "<b>"+total+" "+payment_types[method]['currency']+"</b>";
		} else if ($("#buy-payment-method").attr('value') == "liqpay") {
			total_text = "<b>"+total+" "+payment_types[method]['currency']+"</b>";
		} else if ($("#buy-payment-method").attr('value') == "qiwi") {
			total_text = "<b>"+total+" "+payment_types[method]['currency']+"</b>";
			$("#buy-mobile").attr('style',"display: table-row;");
			
		} else if (($("#buy-payment-method").attr('value') == "paymer_wmr") || ($("#buy-payment-method").attr('value') == "paymer_wmz")) {
			total_text = "<b>"+total+" "+payment_types[method]['currency']+"</b>";
			$("#buy-paymer").attr('style',"display: table-row;");
		} else if ($("#buy-payment-method").attr('value') == "robox") {
			post_text = " + комиссия RoboxChange.</b>";
		}
		if (call_from_count_amount == undefined)
			$("#buy-gold-form-qty-price").attr('value', total);

		$("#buy-gold-form-sub-count").html("Итого: " + total_text + komis_text + post_text);
		if ($("#buy-gold-form-qty").attr('value') == '') {
			$("#span-server-price").html("&nbsp;Стоимость: от <b>" + (prices[id]*wholesale[wholesale.length-1]['coef']).toFixed(1) + "</b>р. до <b>"+(prices[id]).toFixed(1)+"</b>р. ("+qty_price_text+")");	
		} else $("#span-server-price").html("&nbsp;Стоимость: <b>" + (prices[id]*coef).toFixed(1) + "</b>р. ("+qty_price_text+")");		
	}
}
