/**
	JS Control Module
**/

function SubMenuFunc(objid,action)
{
	if(action=='over') document.getElementById(objid).style.display = ""; else
	document.getElementById(objid).style.display = "none"; 
}

var PaymentMethods = {};
PaymentMethods['WMZ'] = {title: 'Webmoney WMZ', method: 'WMZ', icon: 'wm.png'};
PaymentMethods['WMU'] = {title: 'Webmoney WMU', method: 'WMU', icon: 'wm.png'};
PaymentMethods['WME'] = {title: 'Webmoney WME', method: 'WME', icon: 'wm.png'};
PaymentMethods['WMR'] = {title: 'Webmoney WMR', method: 'WMR', icon: 'wm.png'};
PaymentMethods['PRIVATBANKUSD'] = {title: 'ПриватБанк USD', method: 'PRIVATBANKUSD', icon: 'privatbank.png'};
PaymentMethods['PRIVATBANKUAH'] = {title: 'ПриватБанк UAH', method: 'PRIVATBANKUAH', icon: 'privatbank.png'};
PaymentMethods['PRIVATMONEYUAH'] = {title: 'Privat Money UAH', method: 'PRIVATMONEYUAH', icon: 'privatbank.png'};
PaymentMethods['MONEYGRAMUSD'] = {title: 'MoneyGram USD', method: 'MONEYGRAMUSD', icon: 'moneygram.png'};
PaymentMethods['WESTERNUNIONUSD'] = {title: 'Western Union USD', method: 'WESTERNUNIONUSD', icon: 'wu.png'};
PaymentMethods['CASHMONEYUAH'] = {title: 'Наличные UAH', method: 'CASHMONEYUAH', icon: 'cashmoney.png'};
PaymentMethods['VISAMASTERCARDUSD'] = {title: 'Visa / Mastercard USD', method: 'VISAMASTERCARDUSD', icon: 'visamc.png'};
PaymentMethods['VISAMASTERCARDUAH'] = {title: 'Visa / Mastercard UAH', method: 'VISAMASTERCARDUAH', icon: 'visamc.png'};

/** Select payment method **/
function SelectMethod(objid)
{
	var v = document.getElementById(objid).style.display
	if(objid=="ObjSelectOut") document.getElementById('ObjSelectIn').style.display = "none";
	if(objid=="ObjSelectIn") document.getElementById('ObjSelectOut').style.display = "none";
	if(v=='none')
	{ 
	 $("#"+objid).slideToggle("slow");
	 document.getElementById(objid).style.display = ""; 
	} 
	else 
	{
	 $("#"+objid).slideToggle("slow");
	 document.getElementById(objid).style.display = "none"; 
	}
}

function SelectPayOutMethod(method)
{
	document.getElementById('PaymentOutIcon').src = "../images/exform/ico/" + PaymentMethods[method]['icon'];
	document.getElementById('PaymentOutText').textContent = PaymentMethods[method]['title'];
	document.getElementById('PaymentOutText').innerText = PaymentMethods[method]['title'];
	document.getElementById('PayOutMethod').value = PaymentMethods[method]['method'];
	LockNoValidPaymentIn(method);
	SelectDefaultMethodIn(method);
	CalculateTotalPrice();

	if(method == "WMZ" || method == "WMR" || method == "WME" || method == "WMU")
	{
		$("#TotalPriceArea").animate({ opacity: "show" }, "slow");
	}
	else $("#TotalPriceArea").animate({ opacity: "hide" }, "slow");
	/** Recalculate price out **/
	ChangePriceOut();
}

function SelectPayInMethod(method)
{
	document.getElementById('PaymentInIcon').src = "../images/exform/ico/"+PaymentMethods[method]['icon'];
	document.getElementById('PaymentInText').textContent = PaymentMethods[method]['title'];
	document.getElementById('PaymentInText').innerText = PaymentMethods[method]['title'];
	document.getElementById('PayInMethod').value = PaymentMethods[method]['method'];
	CalculateTotalPrice();
}

function SelectDefaultMethodIn(value)
{
	var setup_method;
	if(value == "WMZ" || value == "WMU" || value == "WMR" || value == "WME")
	{
		setup_method = "PRIVATBANKUAH";
		SelectPayInMethod(setup_method);
	}
	else
	if(value == "PRIVATBANKUSD" || value == "PRIVATBANKUAH" || value == "PRIVATMONEYUAH" || value == "CASHMONEYUAH" || value == "MONEYGRAMUSD" || value == "WESTERNUNIONUSD" || value == "VISAMASTERCARDUSD" || value == "VISAMASTERCARDUAH")
	{
		setup_method = "WMZ";
		SelectPayInMethod(setup_method);
	}
	else
	{
		alert("Error of default payment method -OUT mark.");
	}
}

function LockNoValidPaymentIn(value)
{
	if(value == "WMZ" || value == "WMR")
	{
		document.getElementById('PIN-PRIVATBANKUSD').style.display = "";
		document.getElementById('PIN-PRIVATBANKUAH').style.display = "";
		document.getElementById('PIN-PRIVATMONEYUAH').style.display = "none";
		document.getElementById('PIN-CASHMONEYUAH').style.display = "";
		document.getElementById('PIN-WMZ').style.display = "none";
		document.getElementById('PIN-WMR').style.display = "none";
		document.getElementById('PIN-WMU').style.display = "none";
		document.getElementById('PIN-WME').style.display = "none";
		document.getElementById('PIN-MONEYGRAMUSD').style.display = "none";
		document.getElementById('PIN-WESTERNUNIONUSD').style.display = "none";
		document.getElementById('PriceIn').readOnly = true;
	}
	else
	if(value == "WMU")
	{
		document.getElementById('PIN-PRIVATBANKUSD').style.display = "none";
		document.getElementById('PIN-PRIVATBANKUAH').style.display = "";
		document.getElementById('PIN-PRIVATMONEYUAH').style.display = "none";
		document.getElementById('PIN-CASHMONEYUAH').style.display = "";
		document.getElementById('PIN-WMZ').style.display = "none";
		document.getElementById('PIN-WMR').style.display = "none";
		document.getElementById('PIN-WMU').style.display = "none";
		document.getElementById('PIN-WME').style.display = "none";
		document.getElementById('PIN-MONEYGRAMUSD').style.display = "none";
		document.getElementById('PIN-WESTERNUNIONUSD').style.display = "none";
		document.getElementById('PriceIn').readOnly = true;
	}
	else
	if(value == "WME")
	{
		document.getElementById('PIN-PRIVATBANKUSD').style.display = "none";
		document.getElementById('PIN-PRIVATBANKUAH').style.display = "";
		document.getElementById('PIN-PRIVATMONEYUAH').style.display = "none";
		document.getElementById('PIN-CASHMONEYUAH').style.display = "";
		document.getElementById('PIN-WMZ').style.display = "none";
		document.getElementById('PIN-WMR').style.display = "none";
		document.getElementById('PIN-WMU').style.display = "none";
		document.getElementById('PIN-WME').style.display = "none";
		document.getElementById('PIN-MONEYGRAMUSD').style.display = "none";
		document.getElementById('PIN-WESTERNUNIONUSD').style.display = "none";
		document.getElementById('PriceIn').readOnly = true;
	}
	else
	if( value == "PRIVATBANKUAH" || value == "PRIVATMONEYUAH" || value == "CASHMONEYUAH" )
	{
		document.getElementById('PIN-WMZ').style.display = "";
		document.getElementById('PIN-WMR').style.display = "none";
		document.getElementById('PIN-WMU').style.display = "none";
		document.getElementById('PIN-WME').style.display = "none";
		document.getElementById('PIN-MONEYGRAMUSD').style.display = "none";
		document.getElementById('PIN-WESTERNUNIONUSD').style.display = "none";
		document.getElementById('PIN-PRIVATBANKUSD').style.display = "none";
		document.getElementById('PIN-PRIVATBANKUAH').style.display = "none";
		document.getElementById('PIN-PRIVATMONEYUAH').style.display = "none";
		document.getElementById('PIN-CASHMONEYUAH').style.display = "none";
		document.getElementById('PriceIn').readOnly = false;
	}
	else
	if(value == "PRIVATBANKUSD" || value == "MONEYGRAMUSD" || value == "WESTERNUNIONUSD" || value == "VISAMASTERCARDUSD" || value == "VISAMASTERCARDUAH")
	{
		document.getElementById('PIN-WMZ').style.display = "";
		document.getElementById('PIN-WMR').style.display = "none";
		document.getElementById('PIN-WMU').style.display = "none";
		document.getElementById('PIN-WME').style.display = "none";
		document.getElementById('PIN-MONEYGRAMUSD').style.display = "none";
		document.getElementById('PIN-WESTERNUNIONUSD').style.display = "none";
		document.getElementById('PIN-PRIVATBANKUSD').style.display = "none";
		document.getElementById('PIN-PRIVATBANKUAH').style.display = "none";
		document.getElementById('PIN-PRIVATMONEYUAH').style.display = "none";
		document.getElementById('PIN-CASHMONEYUAH').style.display = "none";
		document.getElementById('PriceIn').readOnly = false;
	}
	else
	{
		alert("Error of hide payment methods -OUT mark.");
	}
}

function GetExchangeRatio(Price)
{
	var ExchangeRatioValue;
	if (Price>0 && Price<=80) ExchangeRatioValue = ExchangeRates['ExchangeRatio']['UAH']['0-80']/1;
	if (Price>80 && Price<=400) ExchangeRatioValue = ExchangeRates['ExchangeRatio']['UAH']['80-400']/1;
	if (Price>400 && Price<=800) ExchangeRatioValue = ExchangeRates['ExchangeRatio']['UAH']['400-800']/1;
	if (Price>800) ExchangeRatioValue = ExchangeRates['ExchangeRatio']['UAH']['over800']/1;
	return ExchangeRatioValue;
}

function CalculateTotalPrice()
{
	var PayOutMethod = document.getElementById('PayOutMethod').value;
	var PayInMethod = document.getElementById('PayInMethod').value;
	var TransactionType = PayOutMethod + PayInMethod;
	var PriceOut = document.getElementById('PriceOut').value;
	var PriceIn;
	var ExchangeRatio, ExchangeRatioUAH;
	var Rate;
	
	if (PriceOut>0 && PriceOut<=10) ExchangeRatio = ExchangeRates['ExchangeRatio']['USD']['0-10']/1;
	if (PriceOut>10 && PriceOut<=50) ExchangeRatio = ExchangeRates['ExchangeRatio']['USD']['10-50']/1;;
	if (PriceOut>50 && PriceOut<=100) ExchangeRatio = ExchangeRates['ExchangeRatio']['USD']['50-100']/1;;
	if (PriceOut>100 && PriceOut<=1000) ExchangeRatio = ExchangeRates['ExchangeRatio']['USD']['100-1000']/1;;
	if (PriceOut>1000) ExchangeRatio = ExchangeRates['ExchangeRatio']['USD']['over1000']/1;;
	ExchangeRatioUAH = GetExchangeRatio(PriceOut);
	
	if(TransactionType =="WMZPRIVATBANKUSD")
	{
	Rate = ExchangeRates['leadout']['WMZ']*1;
	PriceIn= Math.floor((PriceOut - (PriceOut*ExchangeRatio))*100)/100;
	if(PriceOut<=10) PriceIn = Math.floor((PriceIn - BankCharges['USD'])*100)/100;
	document.getElementById('PriceIn').value = PriceIn;
	}
	else
	if(TransactionType =="WMZPRIVATBANKUAH")
	{
		Rate = ExchangeRates['leadout']['WMZ']*1;
		ExchangeRatio = GetExchangeRatio(PriceOut*Rate);
		PriceIn= Math.floor(((PriceOut*Rate) - (PriceOut*Rate*ExchangeRatio))*100)/100;
		if(PriceOut<=10) PriceIn = Math.floor((PriceIn - BankCharges['UAH'])*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else
	if(TransactionType =="WMRPRIVATBANKUAH")
	{
		Rate = ExchangeRates['leadout']['WMR']*1;
		ExchangeRatio = 0;
		PriceIn= Math.floor(((PriceOut*Rate) - (PriceOut*Rate*ExchangeRatio))*100)/100;
		if(PriceOut<=10) PriceIn = Math.floor((PriceIn -BankCharges['UAH'])*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else
	if(TransactionType =="WMEPRIVATBANKUAH")
	{
		Rate = ExchangeRates['leadout']['WME']*1;
		ExchangeRatio = GetExchangeRatio(PriceOut*Rate);
		PriceIn= Math.floor(((PriceOut*Rate) - (PriceOut*Rate*ExchangeRatio))*100)/100;
		if(PriceOut<=10) PriceIn = Math.floor((PriceIn -BankCharges['UAH'])*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else
	if(TransactionType =="WMUPRIVATBANKUAH")
	{
		Rate = ExchangeRates['leadout']['WMU']*1;
		PriceIn= Math.floor(((PriceOut*Rate) - (PriceOut*Rate*ExchangeRatioUAH))*100)/100;
		if(PriceOut<=10) PriceIn = Math.floor((PriceIn - BankCharges['UAH'])*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else
	if(TransactionType =="WMRPRIVATBANKUSD")
	{
		Rate = ExchangeRates['leadout']['WMR'] / ExchangeRates['leadout']['WMZ'];
		PriceIn= Math.floor((PriceOut*Rate - (PriceOut*Rate*ExchangeRatio))*100)/100;
		if(PriceOut<=10) PriceIn = Math.floor((PriceIn - BankCharges['USD'])*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="WMZPRIVATMONEYUAH")
	{
		Rate = ExchangeRates['leadout']['WMZ'];
		ExchangeRatio = GetExchangeRatio(PriceOut*Rate);
		PriceIn= Math.floor(((PriceOut*Rate) - (PriceOut*Rate*ExchangeRatio))*100)/100;
		PriceIn = PrivatMoneyFee(PriceIn);
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="WMEPRIVATMONEYUAH")
	{
		Rate = ExchangeRates['leadout']['WME'];
		ExchangeRatio = GetExchangeRatio(PriceOut*Rate);
		PriceIn= Math.floor(((PriceOut*Rate) - (PriceOut*Rate*ExchangeRatio))*100)/100;
		PriceIn = PrivatMoneyFee(PriceIn);
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="WMRPRIVATMONEYUAH")
	{
		Rate = ExchangeRates['leadout']['WMR'];
		ExchangeRatio = 0;
		PriceIn= Math.floor(((PriceOut*Rate) - (PriceOut*Rate*ExchangeRatio))*100)/100;
		PriceIn = PrivatMoneyFee(PriceIn);
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="WMUPRIVATMONEYUAH")
	{
		Rate = ExchangeRates['leadout']['WMU'];
		PriceIn= Math.floor((PriceOut*Rate - (PriceOut*Rate*ExchangeRatioUAH))*100)/100;
		PriceIn = PrivatMoneyFee(PriceIn);
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="WMZCASHMONEYUAH")
	{
		Rate = ExchangeRates['leadout']['WMZ'];
		ExchangeRatio = GetExchangeRatio(PriceOut*Rate);
		PriceIn= Math.floor(((PriceOut*Rate) - (PriceOut*Rate*ExchangeRatio))*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="WMRCASHMONEYUAH")
	{
		Rate = ExchangeRates['leadout']['WMR'];
		ExchangeRatio = 0;
		PriceIn= Math.floor(((PriceOut*Rate) - (PriceOut*Rate*ExchangeRatio))*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="WMECASHMONEYUAH")
	{
		Rate = ExchangeRates['leadout']['WME'];
		ExchangeRatio = GetExchangeRatio(PriceOut*Rate);
		PriceIn= Math.floor(((PriceOut*Rate) - (PriceOut*Rate*ExchangeRatio))*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="WMUCASHMONEYUAH")
	{
		Rate = ExchangeRates['leadout']['WMU'];
		PriceIn= Math.floor((PriceOut*Rate - (PriceOut*Rate*ExchangeRatioUAH))*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="PRIVATBANKUSDWMZ")
	{
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/(1+ExchangeRatio/1))*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="MONEYGRAMUSDWMZ")
	{
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/(1+ExchangeRatio/1))*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="WESTERNUNIONUSDWMZ")
	{
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/(1+ExchangeRatio/1))*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="CASHMONEYUAHWMZ")
	{
		Rate = ExchangeRates['leadin']['WMZ'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/Rate)*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="CASHMONEYUAHWME")
	{
		Rate = ExchangeRates['leadin']['WME'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/Rate)*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="CASHMONEYUAHWMR")
	{
		Rate = ExchangeRates['leadin']['WMR'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/Rate)*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="CASHMONEYUAHWMU")
	{
		Rate = ExchangeRates['leadin']['WMU'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/Rate)*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="PRIVATBANKUAHWMZ")
	{
		Rate = ExchangeRates['leadin']['WMZ'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/Rate)*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="PRIVATBANKUAHWME")
	{
		Rate = ExchangeRates['leadin']['WME'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/Rate)*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="PRIVATBANKUAHWMR")
	{
		Rate = ExchangeRates['leadin']['WMR'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/Rate)*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="PRIVATBANKUAHWMU")
	{
		Rate = ExchangeRates['leadin']['WMU'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/Rate)*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="PRIVATMONEYUAHWMZ")
	{
		Rate = ExchangeRates['leadin']['WMZ'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/Rate)*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="PRIVATMONEYUAHWME")
	{
		Rate = ExchangeRates['leadin']['WME'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/Rate)*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="PRIVATMONEYUAHWMR")
	{
		Rate = ExchangeRates['leadin']['WMR'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/Rate)*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="PRIVATMONEYUAHWMU")
	{
		Rate = ExchangeRates['leadin']['WMU'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceIn= Math.floor((PriceOut/Rate)*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else if(TransactionType =="VISAMASTERCARDUSDWMZ")
	{
		Rate = ExchangeRates['VisaMastercard']['commission']/1;
		PriceIn= Math.floor((PriceOut - PriceOut*Rate)*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}	
	else if(TransactionType =="VISAMASTERCARDUAHWMZ")
	{
		Rate = ExchangeRates['VisaMastercard']['commission']/1;
		ExchangeRate = ExchangeRates['VisaMastercard']['USDUAH']/1;
		PriceIn= Math.floor((PriceOut - PriceOut*Rate)/ExchangeRate*100)/100;
		document.getElementById('PriceIn').value = PriceIn;
	}
	else alert(TransactionType);
}
function ChangePriceOut()
{
	var PriceOut = document.getElementById('PriceOut').value;
	var WebmoneyCommis = PriceOut * 0.008;
	var PayOutMethod = document.getElementById('PayOutMethod').value;
	
	if(PayOutMethod=="WMZ" && WebmoneyCommis>50) WebmoneyCommis = 50;
	if(PayOutMethod=="WME" && WebmoneyCommis>50) WebmoneyCommis = 50;
	if(PayOutMethod=="WMR" && WebmoneyCommis>1500) WebmoneyCommis = 1500;
	if(PayOutMethod=="WMU" && WebmoneyCommis>250) WebmoneyCommis = 250;
	
	var TotalPrice = Math.round((PriceOut/1 + WebmoneyCommis)*100)/100;
	document.getElementById('TotalPrice').innerText = TotalPrice;
	document.getElementById('TotalPrice').textContent = TotalPrice;
	
	if ( /\s/.test(PriceOut) ) 
	{
		alert('Вы можете вводить только цифры, без пробелов.');
		document.getElementById('PriceOut').value = "";
		document.getElementById('PriceIn').value = "";
		return false;
	} 
	else if ( /[a-zA-ZА-Яа-я]/.test(PriceOut) ) 
	{
	alert('Вы можете вводить только цифры.');
	document.getElementById('PriceOut').value = "";
	document.getElementById('PriceIn').value = "";
	return false;
	} else
	if ( PriceOut<0 ) 
	{
		alert('Число не должно быть отрицательным.');
		document.getElementById('PriceOut').value = "";
		document.getElementById('PriceIn').value = "";
		return false;
	} 
	else 
	{
		CalculateTotalPrice();
		return true;
	}
}

function PrivatMoneyFee(Price)
{
	if(Price>0 && Price<=50) Price = Price - 2;
	else if(Price>50 && Price<=100) Price = Price - Price * 0.045;
	else if(Price>100 && Price<=200) Price = Price - Price * 0.04;
	else if(Price>200 && Price<=500) Price = Price - Price * 0.035;
	else if(Price>500 && Price<=600) Price = Price - Price * 0.03;
	else if(Price>600 && Price<=800) Price = Price - Price * 0.025;
	else if(Price>800 && Price<=1000) Price = Price - Price * 0.02;
	else if(Price>1000 && Price<=10000) Price = Price - Price * 0.015;
	else if(Price>10000 && Price<=80000) Price = Price - Price * 0.01;
	Price = Math.floor(Price);
	return Price;
}

function CheckExchangeDataForm()
{
	if(ChangePriceOut()!=false)
	{
		var PriceOut = document.getElementById('PriceOut').value;
		var PriceIn = document.getElementById('PriceIn').value;
		var PayOutMethod = document.getElementById('PayOutMethod').value;
		var PayInMethod = document.getElementById('PayInMethod').value;
		var TransactionType = PayOutMethod + PayInMethod;
		if(PriceOut!="" && PriceOut>1 && PriceIn!="" && PriceIn>0)
		{
			if(TransactionType!="")
			{
				if((PayOutMethod=="MONEYGRAMUSD" ||PayOutMethod=="WESTERNUNIONUSD") && PriceOut<100)
				{
					alert("При данном обмене минимальная сумма операции составляет 100 USD.");
					document.getElementById('PriceOut').value = 100;
					ChangePriceOut();
				}
				if(PayOutMethod=="PRIVATMONEYUAH" && PriceOut<800)
				{
					alert("При данном обмене минимальная сумма операции составляет 800 UAH.");
					document.getElementById('PriceOut').value = 800;
					ChangePriceOut();
				}
				
				if((PayOutMethod=="PRIVATBANKUSD" || PayOutMethod=="PRIVATBANKUAH" || PayOutMethod=="CASHMONEYUAH") && PriceIn < 5)
				{
					alert("При данном обмене минимальная сумма ввода составляет 5 WMZ.");
					document.getElementById('PriceIn').value = 5;
					ChangePriceIn();
				}				
				
				if((PayOutMethod=="PRIVATBANKUSD" || PayOutMethod=="PRIVATBANKUAH" || PayOutMethod=="CASHMONEYUAH") && PriceIn > 500)
				{
					alert("При данном обмене максимальная сумма ввода составляет 500 WMZ.");
					document.getElementById('PriceIn').value = 500;
					ChangePriceIn();
				}		
				
				if((PayOutMethod=="VISAMASTERCARDUAH" && PriceOut>250*8) || (PayOutMethod=="VISAMASTERCARDUSD" && PriceOut>250) )
				{
					alert("При пополнении кошелька действует ограничение на сумму операции в размере 250 долларов или 2000 гривен.");
					if(PayOutMethod=="VISAMASTERCARDUSD") document.getElementById('PriceOut').value = 250;
					if(PayOutMethod=="VISAMASTERCARDUAH") document.getElementById('PriceOut').value = 2000;
					ChangePriceOut();
				}
				if((PayOutMethod=="VISAMASTERCARDUAH" && PriceIn<10) || (PayOutMethod=="VISAMASTERCARDUSD" && PriceIn<10) )
				{
					alert("При пополнении кошелька действует ограничение на минимальну сумму ввода в размере 10 WMZ");
					ChangePriceOut();
				}
				else
				{
					document.getElementById('CalcExForm').submit();
				}
			}
			else
			{
				alert("Ошибка типа операции.");
			}
		}
		else
		{
			alert("Сумма обмена указана неверно.");
		}
	}
}

function CheckExchangeOperation(PayOutMethod,PriceOut,PayInMethod,PriceIn)
{
	document.getElementById("checkbutton").style.display="none";
	document.getElementById("loadingimg").style.display="";
	var Order = {};
	Order['client_surname'] = document.getElementById('client_surname').value; 
	Order['client_name'] = document.getElementById('client_name').value;
	Order['client_patr'] = document.getElementById('client_patr').value;
	Order['client_wmid'] = document.getElementById('client_wmid').value;
	Order['client_wmpurse'] = document.getElementById('client_wmpurse').value;
	Order['client_passport'] = document.getElementById('client_passport').value;
	Order['client_passport_detail'] = document.getElementById('client_passport_detail').value;
	Order['client_cardnumber'] = document.getElementById('client_cardnumber').value;
	Order['client_email'] = document.getElementById('client_email').value;
	Order['client_phone'] = document.getElementById('client_phone').value;
	Order['client_city'] = document.getElementById('client_city').value;
	var html,result;
	var PostData = "client_wmid="+Order['client_wmid']+"&client_surname="+Order['client_surname']+"&client_name="+Order['client_name']+"&client_patr="+Order['client_patr']+"&client_wmpurse="+Order['client_wmpurse']+"&client_email="+Order['client_email']+"&client_phone="+Order['client_phone']+"&client_passport="+Order['client_passport']+"&client_passport_detail="+Order['client_passport_detail']+"&client_city="+Order['client_city']+"&client_cardnumber="+Order['client_cardnumber']+"&payment_method[out]="+PayOutMethod+"&PriceOut="+PriceOut+"&payment_method[in]="+PayInMethod+"&PriceIn="+PriceIn;
	
	html = $.ajax({
	url: "checkorder.html",
	async: false,
	type: "POST",
	dataType: "json",
	data: PostData,
	success: function(msg)
	{
	}
	}).responseText;
	
	try {
	result = eval('(' + html + ')');
	} catch(err) {
	result = {code: '5',text: 'При выполнении запроса возникла критическая ошибка'};
	alert(html);
	}
	document.getElementById("loadingimg").style.display="none";
	if(result['code']!=1)
	{
		document.getElementById('msgbox').innerHTML = result['text'];
		$("#msgbox").animate({ opacity: "show" }, "slow");
		$("#msgbox").delay(3500).animate({ opacity: "hide" }, "slow");
		$("#checkbutton").delay(5000).animate({opacity: "show"}, "slow");
	}
	else
	{
		$("#formbox").animate({ opacity: "hide" }, "slow");
		$("#order_ruls01").animate({ opacity: "hide" }, "slow");
		html = htmlspecialchars_decode(result['text'],'ENT_QUOTES');
		$("#resbox").delay(500).html(html);
		$("#resbox").animate({ opacity: "show" }, "slow");
		$(".main-box").show().animate({height:'470px'}, "slow");
	}
}

function ClickToPay()
{
	document.getElementById('payForm').submit();
}

function SendMsg()
{
	$("#FeedbackForm").submit();
}
function htmlspecialchars_decode(string, quote_style) 
{
	string = string.toString(); 
	string = string.replace(/&amp;/g, '&');
	string = string.replace(/&lt;/g, '<');
	string = string.replace(/&gt;/g, '>');
	 
	if (quote_style == 'ENT_QUOTES') {
		string = string.replace(/"/g, '"');
		string = string.replace(/'/g, '\'');
		string = string.replace(/&quot;/g, '\"'); 
	} else if (quote_style != 'ENT_NOQUOTES') { 
		string = string.replace(/"/g, '"');
	}
	return string;
}

function ShowMobilePhone()
{
	$("#mobilephone_button").animate({ opacity: "hide" }, "slow");
	if(document.getElementById('mobilephone').style.display=="")
	$("#mobilephone").delay(500).animate({ opacity: "hide" }, "slow");
	else
	$("#mobilephone").delay(500).animate({ opacity: "show" }, "slow");
}

function CalculateTotalPriceIn()
{
	var PayOutMethod = document.getElementById('PayOutMethod').value;
	var PayInMethod = document.getElementById('PayInMethod').value;
	var TransactionType = PayOutMethod + PayInMethod;
	var PriceIn = document.getElementById('PriceIn').value;
	var PriceOut;
	var ExchangeRatio;
	var Rate;

	if(TransactionType =="PRIVATBANKUSDWMZ")
	{
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil(PriceIn*(1+ExchangeRatio/1)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="MONEYGRAMUSDWMZ")
	{
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil(PriceIn*(1+ExchangeRatio/1)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="WESTERNUNIONUSDWMZ")
	{
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil(PriceIn*(1+ExchangeRatio/1)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="CASHMONEYUAHWMZ")
	{
		Rate = ExchangeRates['leadin']['WMZ'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil((PriceIn*Rate)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="CASHMONEYUAHWME")
	{
		Rate = ExchangeRates['leadin']['WME'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil((PriceIn*Rate)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="CASHMONEYUAHWMR")
	{
		Rate = ExchangeRates['leadin']['WMR'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil((PriceIn*Rate)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="CASHMONEYUAHWMU")
	{
		Rate = ExchangeRates['leadin']['WMU'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil((PriceIn*Rate)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="PRIVATBANKUAHWMZ")
	{
		Rate = ExchangeRates['leadin']['WMZ'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil((PriceIn*Rate)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="PRIVATBANKUAHWME")
	{
		Rate = ExchangeRates['leadin']['WME'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil((PriceIn*Rate)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="PRIVATBANKUAHWMR")
	{
		Rate = ExchangeRates['leadin']['WMR'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil((PriceIn*Rate)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="PRIVATBANKUAHWMU")
	{
		Rate = ExchangeRates['leadin']['WMU'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil((PriceIn*Rate)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="PRIVATMONEYUAHWMZ")
	{
		Rate = ExchangeRates['leadin']['WMZ'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil((PriceIn*Rate)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="PRIVATMONEYUAHWME")
	{
		Rate = ExchangeRates['leadin']['WME'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil((PriceIn*Rate)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="PRIVATMONEYUAHWMR")
	{
		Rate = ExchangeRates['leadin']['WMR'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil((PriceIn*Rate)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="PRIVATMONEYUAHWMU")
	{
		Rate = ExchangeRates['leadin']['WMU'];
		ExchangeRatio = ExchangeRates['leadin']['ratio'];
		PriceOut = Math.ceil((PriceIn*Rate)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
	else if(TransactionType =="VISAMASTERCARDUSDWMZ")
	{
		Rate = 1 - ExchangeRates['VisaMastercard']['commission']/1;
		PriceOut = Math.ceil((PriceIn / Rate)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}	
	else if(TransactionType =="VISAMASTERCARDUAHWMZ")
	{
		Rate = 1 - ExchangeRates['VisaMastercard']['commission']/1;
		ExchangeRate = ExchangeRates['VisaMastercard']['USDUAH']/1;
		PriceOut = Math.ceil((PriceIn / Rate * ExchangeRate)*100)/100;
		document.getElementById('PriceOut').value = PriceOut;
	}
}

function ChangePriceIn()
{
	var PriceIn = document.getElementById('PriceIn').value;
	if ( /\s/.test(PriceIn) ) 
	{
	alert('Вы можете вводить только цифры, без пробелов.');
	document.getElementById('PriceOut').value = "";
	document.getElementById('PriceIn').value = "";
	} else
		if ( /[a-zA-ZА-Яа-я]/.test(PriceIn) ) 
	{
	alert('Вы можете вводить только цифры.');
	document.getElementById('PriceOut').value = "";
	document.getElementById('PriceIn').value = "";
	} else
	if ( PriceIn<0 ) 
	{
	alert('Число не должно быть отрицательным.');
	document.getElementById('PriceOut').value = "";
	document.getElementById('PriceIn').value = "";
	} 
	else 
	CalculateTotalPriceIn();
}
