var bannfull = true;
var bannintl = 1000;
function banflash ()
{//return;
bannfull = !bannfull;

if (bannfull)
{
	$("#bottombanner_timg").attr("src",STATIC_BASE_PATH + "/images/ew_banner_"+GLOBAL_LANGUAGE+".png");
}
else
{
	$("#bottombanner_timg").attr("src",STATIC_BASE_PATH + "/images/ew_banner_2_"+GLOBAL_LANGUAGE+".png");
}

setTimeout ("banflash ();",bannintl);
}

payblocks = 6;
paycurrent = 0;
function openPayBlock(id)
{
	$('.payblock_hider').hide();
	$('#payblock_'+id).show();
	$('#cancel_container').show();
	if (id == 2 || id == 7)
	{
		$('#moneyfield_container').hide();
	}
	else
	{
		$('#moneyfield_container').show();
	}
	$('.paycurrency_hider').hide();
	$('#moneycurrency_'+id).show();
	paycurrent = id;
	recalculate_money();
}

function cancelPayment()
{
	$('.payblock_hider').hide();
	for (var i = 1; i <= payblocks; i++)
	{
		$('#bibit_radio'+i).attr('checked',false);
	}
	$('#cancel_container').hide();
	$('#moneyfield_container').hide();
	paycurrent = 0;
}

function reconstruct_linx (amount,ratio)
{
	amount = number_format(amount,2,".","");
	// Reconstructing Portmone
	var pm_desc = pm_btndes.replace("%%",amount).replace("@@",number_format((amount*ratio),2,".",""));
	/*if (document.getElementById('portm_bm'))
	{
		document.getElementById('portm_bm').value = amount;
	}
	if(document.getElementById('portm_dsc'))
	{
		document.getElementById('portm_dsc').value = pm_desc;
	}
	if (document.getElementById('portmone_pay_button'))
	{
		document.getElementById('portmone_pay_button').value = pm_desc;
	}
	if (document.getElementById('w1_pay_button'))
	{
		document.getElementById('w1_pay_button').value = w1_btndes.replace("%%",amount).replace("@@",number_format((amount*ratio),2,".",""));
	}
	if(document.getElementById('lp_pay_button'))
	{
		document.getElementById('lp_pay_button').value = lp_btndes.replace("%%",amount).replace("@@",number_format((amount*ratio),2,".",""));
	}*/
	$('#portm_bm').val(amount);
	$('#portm_dsc').val(pm_desc);
	$('#portmone_pay_button').val(pm_desc);
	$('#w1_pay_button').val(w1_btndes.replace("%%",amount).replace("@@",number_format((amount*ratio),2,".","")));
	$('#lp_pay_button').val(lp_btndes.replace("%%",amount).replace("@@",number_format((amount*ratio),2,".","")));
	$('#tp_pay_button').val(tp_btndes.replace("%%",amount).replace("@@",number_format((amount*ratio),2,".","")));
	$('#ik_pay_button').val(ik_btndes.replace("%%",amount).replace("@@",number_format((amount*ratio),2,".","")));
	
	// Reconstructing WMU
	//var ratio = w_ratio;//WMU_ratio;
	var curre = "WMU";
	var am2 = number_format((amount*ratio),2,".","");
	var pm_desc = texto.replace("%%",amount).replace("##",curre).replace("@@",am2);
	/*if(document.getElementById('wmul_trg'))
	{
		$('#wmul_trg').html(pm_desc);
	}*/
	$('#wmul_trg').html(pm_desc);
	
	var pm_desc2 = wmlink
				.replace("|||",(textowm.replace("%%",amount).replace("##",curre).replace("@@",am2)))
				.replace("$$$",amount);
				//.replace(">>>",purse);
	/*if(document.getElementById('wmul_trg'))
	{
		document.getElementById('wmul_trg').href = pm_desc2;
	}*/
	$('#wmul_trg').attr('href',pm_desc2);
	
	// Reconstructing WalletOne
	/*if (document.getElementById('w1_pm'))
	{
		document.getElementById('w1_pm').value = amount;
	}*/
	$('#w1_pm').val(amount);
	
	//document.getElementById('w1_desc').value = w1_btndes.replace("%%",amount).replace("@@",number_format((amount*ratio),2,".",""));;
	
	// Reconstructing LiqPay
	/*if(document.getElementById('lp_pm'))
	{
		document.getElementById('lp_pm').value = amount;
	}*/
	$('#lp_pm').val(amount);
	
	// Reconstructing ToPay
	/*if(document.getElementById('lp_pm'))
	{
		document.getElementById('lp_pm').value = amount;
	}*/
	$('#tp_pm').val(amount);
	
	// Reconstructing InterKassa
	$('#ik_pm').val(amount);
	
	// WebMoneyMerchant reconstruction
	$('#pay_webmoney_btn').val(texto.replace("%%",amount).replace("##",curre).replace("@@",am2));
	$('#wmm_d').val(Base64.encode(wmmdsc.replace('##',pm_desc)));
	$('#wmm_a').val(amount);
}

function recalculate_eba()
{
	$('#informer').hide();
	$('#informer_eba').hide();
	$('#payblock_'+paycurrent).show();
	if (!$('#ebaamount').length)
	{
		return;
	}
	// cleaning amount
	
	var amount = $('#ebaamount').val()+"";
	amount = amount.replace(",",".").replace(" ","");
	amount = parseFloat(amount);
	amount = number_format(amount,2,".","");

	var ratio = 1;
	switch (paycurrent)
	{
		case 1:
			ratio = p_ratio;
			break;
		case 3:
		case 6:
			ratio = w_ratio;
			break;
		case 4:
			ratio = w1ratio;
			break;
		case 5:
			ratio = lpratio;
			break;
		case 7:
			ratio = tpratio;
			break;
		case 8:
			ratio = ikratio;
			break;
		default:
			return;
			break;
	}
	
	var tmp = number_format(amount/ratio,2,".","");

	$('#amount').val(tmp);
	
	if (tmp < minimal_amount)
	{
		$('#informer').show();
		$('#payblock_'+paycurrent).hide();
		return;
	}
	
	if (amount<0.1)
	{
		$('#informer_eba').show();
		$('#payblock_'+paycurrent).hide();
		return;
	}
	
	reconstruct_linx (tmp,ratio);
}

function recalculate_money()
{
	$('#informer').hide();
	$('#informer_eba').hide();
	$('#payblock_'+paycurrent).show();
	if (!$('#amount').length)
	{
		return;
	}
	// cleaning amount
	
	var amount = $('#amount').val()+"";
	amount = amount.replace(",",".").replace(" ","");
	amount = parseFloat(amount);
	amount = number_format(amount,2,".","");

	var ratio = 1;
	switch (paycurrent)
	{
		case 1:
			ratio = p_ratio;
			break;
		case 3:
		case 6:
			ratio = w_ratio;
			break;
		case 4:
			ratio = w1ratio;
			break;
		case 5:
			ratio = lpratio;
			break;
		case 7:
			ratio = tpratio;
			break;
		case 8:
			ratio = ikratio;
			break;
		default:
			return;
			break;
	}
	// Reconstructing eba field
	$('#ebaamount').val(number_format(amount*ratio,2,".",""));
	
	if (amount<minimal_amount)
	{
		$('#informer').show();
		$('#payblock_'+paycurrent).hide();
		return;
	}
	
	reconstruct_linx (amount,ratio);
}

function number_format( number, decimals, dec_point, thousands_sep ) {
    var n = number, prec = decimals;
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep == "undefined") ? ',' : thousands_sep;
    var dec = (typeof dec_point == "undefined") ? '.' : dec_point;
 
    var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
 
    var abs = Math.abs(n).toFixed(prec);
    var _, i;
 
    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;
 
        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
 
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }
 
    return s;
}

var Base64 = {
		 
		// private property
		_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	 
		// public method for encoding
		encode : function (input) {
			var output = "";
			var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
			var i = 0;
	 
			input = Base64._utf8_encode(input);
	 
			while (i < input.length) {
	 
				chr1 = input.charCodeAt(i++);
				chr2 = input.charCodeAt(i++);
				chr3 = input.charCodeAt(i++);
	 
				enc1 = chr1 >> 2;
				enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
				enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
				enc4 = chr3 & 63;
	 
				if (isNaN(chr2)) {
					enc3 = enc4 = 64;
				} else if (isNaN(chr3)) {
					enc4 = 64;
				}
	 
				output = output +
				this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
				this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
	 
			}
	 
			return output;
		},
	 
		// public method for decoding
		decode : function (input) {
			var output = "";
			var chr1, chr2, chr3;
			var enc1, enc2, enc3, enc4;
			var i = 0;
	 
			input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	 
			while (i < input.length) {
	 
				enc1 = this._keyStr.indexOf(input.charAt(i++));
				enc2 = this._keyStr.indexOf(input.charAt(i++));
				enc3 = this._keyStr.indexOf(input.charAt(i++));
				enc4 = this._keyStr.indexOf(input.charAt(i++));
	 
				chr1 = (enc1 << 2) | (enc2 >> 4);
				chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
				chr3 = ((enc3 & 3) << 6) | enc4;
	 
				output = output + String.fromCharCode(chr1);
	 
				if (enc3 != 64) {
					output = output + String.fromCharCode(chr2);
				}
				if (enc4 != 64) {
					output = output + String.fromCharCode(chr3);
				}
	 
			}
	 
			output = Base64._utf8_decode(output);
	 
			return output;
	 
		},
	 
		// private method for UTF-8 encoding
		_utf8_encode : function (string) {
			string = string.replace(/\r\n/g,"\n");
			var utftext = "";
	 
			for (var n = 0; n < string.length; n++) {
	 
				var c = string.charCodeAt(n);
	 
				if (c < 128) {
					utftext += String.fromCharCode(c);
				}
				else if((c > 127) && (c < 2048)) {
					utftext += String.fromCharCode((c >> 6) | 192);
					utftext += String.fromCharCode((c & 63) | 128);
				}
				else {
					utftext += String.fromCharCode((c >> 12) | 224);
					utftext += String.fromCharCode(((c >> 6) & 63) | 128);
					utftext += String.fromCharCode((c & 63) | 128);
				}
	 
			}
	 
			return utftext;
		},
	 
		// private method for UTF-8 decoding
		_utf8_decode : function (utftext) {
			var string = "";
			var i = 0;
			var c = c1 = c2 = 0;
	 
			while ( i < utftext.length ) {
	 
				c = utftext.charCodeAt(i);
	 
				if (c < 128) {
					string += String.fromCharCode(c);
					i++;
				}
				else if((c > 191) && (c < 224)) {
					c2 = utftext.charCodeAt(i+1);
					string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
					i += 2;
				}
				else {
					c2 = utftext.charCodeAt(i+1);
					c3 = utftext.charCodeAt(i+2);
					string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
					i += 3;
				}
	 
			}
	 
			return string;
		}
	 
	}
