var electronPayments = new Array();
electronPayments['wmz'] = {'code': '1', 'currency': 'WMZ'};
electronPayments['wmr'] = {'code': '2', 'currency': 'WMR'};
electronPayments['wme'] = {'code': '3', 'currency': 'WME'};
electronPayments['yandex'] = {'code': '7', 'currency': 'YAD'};
electronPayments['rbk'] = {'code': '9', 'currency': 'RBK'};
electronPayments['moneymail'] = {'code': '13', 'currency': 'RUB'};
electronPayments['visa'] = {'code': '8', 'currency': 'RUB'};

function generateSelects(){
	var countries ='<select id=\'countries\' onchange=\'showSelectedCountryBrands();\'>';
	var brands = '';
	countries += '<option value=\'default\'>Выбор страны</option>';
	for (i = 0; i < jsonSMS.country.length; i++) {
		countries += '<option value=\''+i+'\'>'+jsonSMS.country[i].Name+'</option>';
		brands += '<select id=\'brands'+i+'\' style=\'display: none;\' class=\'brandSelect\' onchange=\'generateSmsText()\'>';
		brands += '<option value=\'default\'>Выбор оператора</option>';
		for (j = 0; j < jsonSMS.country[i].Brands.length; j++) {
			brands += '<option value=\''+j+'\'>'+jsonSMS.country[i].Brands[j].Name+'</option>';
		}
		brands +='</select>';
	}
	countries +='</select>';
	$("#selects")[0].innerHTML = countries;
	$("#selects")[0].innerHTML += brands;
}
function showSelectedCountryBrands() {
	$("#code_fields").hide();
	$(".brandSelect").hide();
	var country_id = $("#countries")[0].value;
	if (country_id != 'default') {$("#brands"+country_id+"").show()};
}
function generateSmsText(){
	var dynamic_text = '';
	var country_id = $("#countries")[0].value;
	var brand_id = 	$("#brands"+country_id+"")[0].value;
	if (country_id != 'default' && brand_id !='default'){
		// сервисный номер
		var number = jsonSMS.country[country_id].Brands[brand_id].ISNN;

		// текст с префиксом
		var prefix_text = jsonSMS.country[country_id].Brands[brand_id].prefix+project_id;

		// получаем полную цену с учетом НДС
		var default_cost = jsonSMS.country[country_id].Brands[brand_id].cost;
		var tax_procent = jsonSMS.country[country_id].Tax;
		//parseInt(default_cost,10) +
		var tax = default_cost*tax_procent/100;
		var cost = parseFloat(default_cost) + parseFloat(tax.toFixed(2));
		var cost = cost.toFixed(2);
		var currency = jsonSMS.country[country_id].Brands[brand_id].currency;

		// лимит по кол-ву смс в период 
		for (var id in limits){
			if (limits[id]["number"] == number) {
				var count = limits[id]["amount"];
				var period = limits[id]["period"];
			} else {
				var count = limits[0]["amount"];
				var period = limits[0]["period"];
			}	
		}
		limitStr = 'Частота посылки sms на этот номер ограничена: amount sms в period мин. с одного номера.';
		limitStr = limitStr.replace ('amount', count);
		limitStr = limitStr.replace ('period', period);

		/*вывод всего бреда*/
		// динамический текст 
		if (vip_status == 0){
			prefix_text = prefix_text+' '+dinamic;
		}
		if (ext_text) {
			prefix_text += ' '+ext_text;
		}	
		$("#sms_text")[0].innerHTML = prefix_text;
		$("#sms_number")[0].innerHTML = number;
		$("#sms_cost")[0].innerHTML = cost+' '+currency;
		if (jsonSMS.country[country_id].Name == "Украина"){
			$("#sms_cost_additional")[0].innerHTML = '(Тариф в гривнах с учетом НДС. Дополнительно удерживается сбор в пенсионный фонд в размере 7,5% от стоимости услуги без учета НДС)';
		}

		if (vip_status == 0 && type == 1){
			// лимит по действю ключа
			var timelimit_text = 'Время действия ключа '+timelimit+' минут';
			$("#sms_timelimit")[0].innerHTML = timelimit_text;
		}

		$("#sms_count_limit")[0].innerHTML = limitStr;

		//показываем див с интсрукцией
		$("#code_fields").show();
	} else {
		$("#code_fields").hide();
	}	
}
function selectPaymentType(type) {
	$(".instructionArea").hide();
	$("#wmOpts").hide();
	$("#code_fields").hide();
	$(".brandSelect").hide();
	$(".sl").css("color","#000");
	$("#"+type+"_sl").css("color", "#b90000");
	switch (type){
		case 'sms':
			$("#sms").show();
			generateSelects();
			break;
		case 'wmr':
			$("#wmr2_sl").css("color", "#b90000");
		case 'wmz':
		case 'wme':
			$("#wmOpts").show();
			$("#wmr_sl").css("color", "#b90000");
		case 'yandex':
		case 'rbk':
		case 'moneymail':
		case 'visa':
			for (var i in jsonELECTRON){
				if (jsonELECTRON[i].Name == type){
					cost_in_rub = jsonELECTRON[i].Cost;
				}
			}
			$("#mode_type")[0].value = electronPayments[type].code;
			var cost_in_curr = cost_in_rub*currencies[electronPayments[type].currency];
			$("#amount")[0].value = cost_in_curr.toFixed(2)
			$("#electron").show();
			break;
	}	
}
