/**
 * Game Currency Payment Calculator
 */
function paymentCalculator(srv_prices, ccourses, gm_const, gm_const_formated, _srvr_min_max){
	this._server_prices = srv_prices;
	this._currency_courses = ccourses;
	
	this._id_send_money_box = 'txt_pay_amount';
	this._id_receive_money_box = 'txt_receive_amount';
	this._id_server_list = 'sclt_srvers';
	this._id_curency_list = 'current_currency_id';
	this._id_curency_list_prev_val = 'cur_list_prev_value';
	this._id_discount_email_box = 'id_discount_email';
	this._id_calc_info_text = 'calc_info_text';
	
	this._game_money_const = gm_const;
	this._game_money_const_formated = gm_const_formated;
	this._game_srvr_min_max = _srvr_min_max;
	
	this._current_discount_amount = parseFloat(0);
	
	ua = navigator.userAgent;
	this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
	this.isMSIE5 = this.isMSIE && (ua.indexOf('MSIE 5') != -1);
	this.isMSIE5_0 = this.isMSIE && (ua.indexOf('MSIE 5.0') != -1);
	this.isMSIE7 = this.isMSIE && (ua.indexOf('MSIE 7') != -1);
	this.isGecko = ua.indexOf('Gecko') != -1; // Will also be true on Safari
	this.isSafari = ua.indexOf('Safari') != -1;
	this.isOpera = window['opera'] && opera.buildNumber ? true : false;
	this.isMac = ua.indexOf('Mac') != -1;
	this.isNS7 = ua.indexOf('Netscape/7') != -1;
	this.isNS71 = ua.indexOf('Netscape/7.1') != -1;
	
	var that=this;
	
	
	/**
	 * class methods
	 */
	this.formatMoney = function(Curr) {
		if( Curr == '' ) {
			Curr = 0;
		} else {
			Curr = parseFloat(Curr);
		}
		return Curr.toFixed(2);
	}
	
	this.getCurrencyPrice = function(current_amount){
		server_id = $(this._id_server_list).value;
		
		current_amount = parseFloat(current_amount);
		var ret = 0;
		var prices = this._server_prices[server_id];
		for(var i = 0; i < prices.length; i++){
			if(i+1 == prices.length){
				ret = prices[i].item_price;
				break;
			} else {
				if(current_amount >= prices[i].buy_more_then && current_amount < prices[i+1].buy_more_then){
					ret = prices[i].item_price;
					break;
				}
			}
			
		}
		return ret;
	}
	
	this.getCurrencyBonus = function(current_amount){
		server_id = $(this._id_server_list).value;
		
		current_amount = parseFloat(current_amount);
		var ret = 0;
		var prices = this._server_prices[server_id];
		for(var i = 0; i < prices.length; i++){
			if(i+1 == prices.length){
				ret = prices[i].item_bonus;
				break;
			} else {
				if(current_amount >= prices[i].buy_more_then && current_amount < prices[i+1].buy_more_then){
					ret = prices[i].item_bonus;
					break;
				}
			}
			
		}
		return ret;
	}
	
	this.getCurrentDiscountCategory = function(current_amount){
		server_id = $(this._id_server_list).value;
		
		current_amount = parseFloat(current_amount);
		var ret = 0;
		var prices = this._server_prices[server_id];
		for(var i = 0; i < prices.length; i++){
			if(i+1 == prices.length){
				ret = prices[i].buy_more_then;
				break;
			} else {
				if(current_amount >= prices[i].buy_more_then && current_amount < prices[i+1].buy_more_then){
					ret = prices[i].buy_more_then;
					break;
				}
			}
			
		}
		return ret;
	}
	
	this.ifAmountInDiscountCategory = function(current_amount){
		server_id = $(this._id_server_list).value;
		
		current_amount = parseFloat(current_amount);
		var ret = false;
		var prices = this._server_prices[server_id];
		for(var i = 0; i < prices.length; i++){
			if(i+1 == prices.length){
				ret = true;
				break;
			} else {
				if(current_amount >= prices[i].buy_more_then && current_amount < prices[i+1].buy_more_then){
					ret = true;
					break;
				}
			}
			
		}
		return ret;
	}
	
	this.updateReceiveMoney = function(e){
		if(e.type == 'blur'){
			this.updateReceiveMoneyValue();
		} else {
			wkey = e.which?e.which:window.event.keyCode;
			//alert(e.type + ': ' + wkey);
			if((wkey >= 48 && wkey <= 57) || (wkey >= 96 && wkey <= 105) || wkey == 8 || wkey == 13  || wkey == 37 || wkey == 39 || wkey == 46){
				//alert(wkey);
				this.updateReceiveMoneyValue();
			} else {
				return false;
			}
		}
	}
	
	this.updateReceiveMoneyValue = function(){
		var _parsed_send_money = parseFloat(this.getValidMoneyValue($(this._id_send_money_box).value));
		//alert(_parsed_send_money);
		_parsed_send_money = this.converMoneyFromAnotherCurrency(_parsed_send_money);

		$(this._id_receive_money_box).value = this.formatMoney(_parsed_send_money / this.getCurrencyPrice(_parsed_send_money  + parseFloat(this._current_discount_amount)));
		this.updateAdditionalInfoBoxes();
	}
	
	this.updateSendMoney = function(e){
		if(e.type == 'blur'){
			this.updateSendMoneyValue();
		} else {
			wkey = e.which?e.which:window.event.keyCode;
			if((wkey >= 48 && wkey <= 57) || (wkey >= 96 && wkey <= 105) || wkey == 8 || wkey == 13  || wkey == 37 || wkey == 39 || wkey == 46){
				this.updateSendMoneyValue();
			} else {
				return false;
			}
		}
	}
	
	this.updateSendMoneyValue = function(){
		var _parsed_receive_money = parseFloat(this.getValidMoneyValue($(this._id_receive_money_box).value));
		var _pay_money = _parsed_receive_money * parseFloat(this._server_prices[$(this._id_server_list).value][0].item_price) + parseFloat(this._current_discount_amount);
		//alert(this._server_prices[$(this._id_server_list).value][0].item_price);
		var _curr2 = _parsed_receive_money * this.getCurrencyPrice(_pay_money);
		
		$(this._id_send_money_box).value = this.formatMoney(this.converMoneyToAnotherCurrency(_curr2));
		this.updateAdditionalInfoBoxes();
	}
	
	this.getSendMoneyAmount = function(discount_amt){
		var _parsed_receive_money = parseFloat(this.getValidMoneyValue($(this._id_receive_money_box).value));
		var _pay_money = _parsed_receive_money * parseFloat(this._server_prices[$(this._id_server_list).value][0].item_price) + parseFloat(discount_amt);
		var _curr2 = _parsed_receive_money * this.getCurrencyPrice(_pay_money);
		return _curr2;
	}
	
	this.getNoDiscountedPayMoney = function(){
		var _parsed_receive_money = parseFloat(this.getValidMoneyValue($(this._id_receive_money_box).value));
		var _parsed_receive_bonus = parseFloat(this.getReceiveBonusAmount() / this._game_money_const);
		var _pay_money = (_parsed_receive_money + _parsed_receive_bonus) * parseFloat(this._server_prices[$(this._id_server_list).value][0].item_price);
		return _pay_money;
	}
	
	this.getCurrencyItemPrice = function(){
		if($(this._id_receive_money_box).value != 0){
			return $(this._id_send_money_box).value / $(this._id_receive_money_box).value;
		} else {
			return 0;
		}
	}
	
	this.converMoneyFromAnotherCurrency = function(mo){
		switch($(this._id_curency_list).value){
			case 'wmz':				mo = mo/1; break;
			case 'wmz-card':		mo = mo/1; break;
			case 'wmr':				mo = mo/parseFloat(this._currency_courses['usd2rur']); break;
			case 'wmr-card':		mo = mo/parseFloat(this._currency_courses['usd2rur']); break;
			case 'wme':				mo = mo/parseFloat(this._currency_courses['usd2eur']); break;
			case 'wme-card':		mo = mo/parseFloat(this._currency_courses['usd2eur']); break;
			case 'wmu':				mo = mo/parseFloat(this._currency_courses['usd2uah']); break;
			case 'wmu-card':		mo = mo/parseFloat(this._currency_courses['usd2uah']); break;
			case 'ya':				mo = mo/parseFloat(this._currency_courses['usd2rur']); break;
			case 'elecs':			mo = mo/parseFloat(this._currency_courses['usd2rur']); break;
			case 'robo.mm':
			case 'robo.rbc':
			case 'robo.ek':
			case 'robo.wc':
			case 'robo.ep':			mo = mo/parseFloat(this._currency_courses['usd2rur']); break;
			case 'qiwi':			mo = mo/parseFloat(this._currency_courses['usd2rur']); break;
			case 'liqpay':			mo = mo/parseFloat(this._currency_courses['usd2rur']); break;
		}
		//alert(mo);
		return parseFloat(mo);
	}
	
	this.selectedCurrencySign = function(mo){
		switch($(this._id_curency_list).value){
			case 'wmz':				
			case 'wmz-card':		return '$';
			case 'wmr':				
			case 'wmr-card':		return 'руб.';
			case 'wme':				
			case 'wme-card':		return '&euro;';
			case 'wmu':				
			case 'wmu-card':		return 'грв.';
			case 'ya':				
			case 'elecs':			
			case 'robo.mm':
			case 'robo.rbc':
			case 'robo.ek':
			case 'robo.wc':
			case 'robo.ep':			
			case 'qiwi':			
			case 'liqpay':			return 'руб.';
		}
		return 'руб.';
	}
	
	this.converMoneyToAnotherCurrency = function(mo){
		switch($(this._id_curency_list).value){
			case 'wmz':				mo = mo*1; break;
			case 'wmz-card':		mo = mo*1; break;
			case 'wmr':				mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'wmr-card':		mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'wme':				mo = mo*parseFloat(this._currency_courses['usd2eur']); break;
			case 'wme-card':		mo = mo*parseFloat(this._currency_courses['usd2eur']); break;
			case 'wmu':				mo = mo*parseFloat(this._currency_courses['usd2uah']); break;
			case 'wmu-card':		mo = mo*parseFloat(this._currency_courses['usd2uah']); break;
			case 'ya':				mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'elecs':			mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'robo.mm':
			case 'robo.rbc':
			case 'robo.ek':
			case 'robo.wc':
			case 'robo.ep':			mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'qiwi':			mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'liqpay':			mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
		}
		
		return parseFloat(mo);
	}
	
	this.getCurrencyMultiplierBySource = function(src){
		var ret = 1;
		switch(src){
			case 'wmr':				ret = parseFloat(this._currency_courses['usd2rur']); break;
			case 'wmr-card':		ret = parseFloat(this._currency_courses['usd2rur']); break;
			case 'wme':				ret = parseFloat(this._currency_courses['usd2eur']); break;
			case 'wme-card':		ret = parseFloat(this._currency_courses['usd2eur']); break;
			case 'wmu':				ret = parseFloat(this._currency_courses['usd2uah']); break;
			case 'wmu-card':		ret = parseFloat(this._currency_courses['usd2uah']); break;
			case 'ya':				ret = parseFloat(this._currency_courses['usd2rur']); break;
			case 'elecs':			ret = parseFloat(this._currency_courses['usd2rur']); break;
			case 'robo.mm':
			case 'robo.rbc':
			case 'robo.ek':
			case 'robo.wc':
			case 'robo.ep':			ret = parseFloat(this._currency_courses['usd2rur']); break;
			case 'qiwi':			ret = parseFloat(this._currency_courses['usd2rur']); break;
			case 'liqpay':			ret = parseFloat(this._currency_courses['usd2rur']); break;
		}
		
		return ret;
	}
	
	this.paymentCurrencyChanged = function(){
		var _parsed_send_money = parseFloat(this.getValidMoneyValue($(this._id_send_money_box).value));
		
		_parsed_send_money = _parsed_send_money / this.getCurrencyMultiplierBySource($(this._id_curency_list_prev_val).value);
		$(this._id_curency_list_prev_val).value = $(this._id_curency_list).value;

		_parsed_send_money = this.converMoneyToAnotherCurrency(_parsed_send_money);
		$(this._id_send_money_box).value = this.formatMoney(_parsed_send_money);
		
		this.updateReceiveMoneyValue();
	}
	
	this.getValidMoneyValue = function(m){
		return m.replace(',', '.').replace(/[^\d\.]/, '');
	}
	
	this.updateAdditionalInfoBoxes = function(){
		this.updateCommissionText();
		this.updateReceiveInGameText();
		this.updateBonusInfoText();
		this.updateDiscountInfoText();
	}
	
	this.updateCommissionText = function(){
		var _parsed_send_money = parseFloat(this.getValidMoneyValue($(this._id_send_money_box).value));
		var comLevel = 0;
		
		switch($(this._id_curency_list).value){
			case 'wmz':				
			case 'wmr':				
			case 'wme':				
			case 'wmu':				comLevel = 1.008; break;
			case 'wmz-card':		
			case 'wmr-card':		
			case 'wme-card':		
			case 'wmu-card':		comLevel = 0; break;
			case 'ya':				comLevel = 1.005; break;
			case 'elecs':			comLevel = 1.078; break;
		}
		if(comLevel > 0){
			$(this._id_calc_info_text).update('С учетом комиссии: ' + this.formatMoney(_parsed_send_money * comLevel) + this.selectedCurrencySign());
		} else {
			$(this._id_calc_info_text).update('');
		}
		
		
	}
	
	this.updateReceiveInGameText = function(){
		var _parsed_receive_money = parseFloat(this.getValidMoneyValue($(this._id_receive_money_box).value)) * this._game_money_const;
		var _parsed_receive_bonus = parseFloat(this.getReceiveBonusAmount());
		$(this._id_calc_info_text).innerHTML += '<br/>В игре вы получите ' + this.prettyFormatNumber(Math.round(_parsed_receive_money + _parsed_receive_bonus)) + ' единиц валюты';
	}
	
	this.getReceiveBonusAmount = function(){
		var current_bonus = 0;
		var discount_amount = this.getSendMoneyAmount(this._current_discount_amount);
		var current_bonus_multiplier = this.getCurrencyBonus(discount_amount);
		
		if(current_bonus_multiplier > 0){
			
			if(current_bonus_multiplier < 1){
				//% value
				var _parsed_receive_money = parseFloat(this.getValidMoneyValue($(this._id_receive_money_box).value)) * this._game_money_const;
				current_bonus = current_bonus_multiplier * _parsed_receive_money;
			} else {
				//plain multiplier value
				current_bonus = current_bonus_multiplier * this._game_money_const;
			}
		}
		
		return Math.round(current_bonus);
	}
	
	this.updateBonusInfoText = function(){	
		var currency_bonus = this.getReceiveBonusAmount();
		if(currency_bonus > 0){
			$(this._id_calc_info_text).innerHTML += '<br/>С учетом Вашего бонуса ' + this.prettyFormatNumber(currency_bonus) + ' единиц валюты';
		}
	}
	
	this.updateDiscountInfoText = function(){
		var discount_category_used = false;
		
		if(this.ifAmountInDiscountCategory()){
			discount_category_used = true;
			var discount_amount = this.getSendMoneyAmount(0)+this._current_discount_amount;
			//$(this._id_calc_info_text).innerHTML += '<br/>Категория скидки "от ' + this.formatMoney(this.getCurrentDiscountCategory(discount_amount)) + '$"';
		}
		
		var price_per_currency_item = this.formatMoney(this.getCurrencyItemPrice());
		$(this._id_calc_info_text).innerHTML += '<br/>Цена за ' + this._game_money_const_formated + ' ';
		if(discount_category_used){
		 	$(this._id_calc_info_text).innerHTML += 'с учетом категории скидки ';
		}
		
		$(this._id_calc_info_text).innerHTML += '<span class="discount_add_info_major">' + this.formatMoney(price_per_currency_item) + this.selectedCurrencySign() + '</span>';
		
		var no_discount_amount = this.getNoDiscountedPayMoney();
		var discount_amount = this.getSendMoneyAmount(this._current_discount_amount);
		
		var safe_natural = no_discount_amount - discount_amount;
		if(safe_natural != 0){
			//alert(no_discount_amount + "-" + discount_amount);
			if(no_discount_amount > 0){
				var safe_percent = Math.floor(safe_natural * 100 / no_discount_amount);
			} else {
				var safe_percent = 0;
			}
			
			$(this._id_calc_info_text).innerHTML += '<br/>Вы экономите <span class="discount_add_info_major">' + this.formatMoney(this.converMoneyToAnotherCurrency(safe_natural)) + this.selectedCurrencySign() + ' (' + safe_percent + '%)</span>';
		}
	}
	
	this.prettyFormatNumber = function(number){
		var tmp = String(number).split('.');
		formattedNumberString = tmp[0];
		var f = "";
		for(var i = formattedNumberString.length - 1; i >= 0; i--){
			f += formattedNumberString.charAt(i);
		}
		f = f.replace(/(\d{3,3})/gi, "$1 ");
		formattedNumberString = "";
		for(var i = f.length - 1; i >= 0; i--){
			if(i == f.length - 1){
				if(f.charAt(i) != ' '){
					formattedNumberString += f.charAt(i);
				}
			} else {
				formattedNumberString += f.charAt(i);
			}
		}
		
		if(tmp.length > 1){
			formattedNumberString += " . " + tmp[1];
		}
		
		return formattedNumberString;
	}
	
	this.updateDiscountInfo = function(e){
		wkey = e.which?e.which:window.event.keyCode;
		if(wkey >= 45 || wkey == 8){
			//this._id_discount_email_box
			var _discount_email = $(this._id_discount_email_box).value;
			if(_discount_email.length > 4 && _discount_email.match(/^[\da-z\-\_\.]{1,}\@[\da-z\-\_]+\.[\da-z\-\_\.]{1,}$/i)){
				$('currency_calc_loader_id').show();
				new Ajax.Request('/games/',
			    {
			        method:'post',
			            parameters: { 
			            				'email': _discount_email, 
			            				'action': 'personalDiscountInfo'
			            			},
			        onSuccess:
			            function(transport){
			                that._current_discount_amount = parseFloat(transport.responseText);
			                if(isNaN(that._current_discount_amount)){
			                	that._current_discount_amount = 0;
			                }
			                //alert(that._current_discount_amount);
			                that.updateReceiveMoneyValue();
			                $('currency_calc_loader_id').hide();
			            },
			        onFailure:
			            function(){ 
			            	alert('Something went wrong...'); 
			            	$('currency_calc_loader_id').hide();
			            }
			    });
			} else {
				if($(this._id_discount_email_box).value == ''){
					this._current_discount_amount = 0;
					this.updateReceiveMoneyValue();
				}
			}
			//this._current_discount_amount
		} else {
			if($(this._id_discount_email_box).value == ''){
				this._current_discount_amount = 0;
				this.updateReceiveMoneyValue();
			}
		}
	}
	
	this.preSubmitCheck = function(){
		this.updateReceiveMoneyValue();
		
		server_id = $(this._id_server_list).value;
		
		var _discount_email = $(this._id_discount_email_box).value;
		
		if(_discount_email.length <= 4 || !_discount_email.match(/^[\da-z\-\_\.]{1,}\@[\da-z\-\_]+\.[\da-z\-\_\.]{1,}$/i)){
			alert('Укажите верный Email!');
			return false;
		}
		//alert(parseFloat($(this._id_receive_money_box).value) < parseFloat(this._game_srvr_min_max[server_id].bmin)); return false;
		if(parseFloat($(this._id_receive_money_box).value) * this._game_money_const < parseFloat(this._game_srvr_min_max[server_id].bmin)){
			alert('Минимальная сумма покупки на этом сервере составляет ' + this._game_srvr_min_max[server_id].bmin + ' единиц валюты');
			return false;
		}
		
		return true;
	}
}

/**
 * Powerleveling calculator
 */
function pwlCalculator(price_and_time, ccourses){
	this._price_table = price_and_time;
	this._currency_courses = ccourses;
	
	this._id_server_list = 'sclt_srvers_pwl';
	this._id_curency_list = 'current_currency_id_pwl';
	this._id_curency_list_prev_val = 'cur_list_prev_value_pwl';
	this._id_lvl_from_slct = 'pwl_from_lvl';
	this._id_lvl_to_slct = 'pwl_to_lvl';
	this._id_result_days = 'result_days';
	this._id_result_payment = 'result_payment';
	
	ua = navigator.userAgent;
	this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
	this.isMSIE5 = this.isMSIE && (ua.indexOf('MSIE 5') != -1);
	this.isMSIE5_0 = this.isMSIE && (ua.indexOf('MSIE 5.0') != -1);
	this.isMSIE7 = this.isMSIE && (ua.indexOf('MSIE 7') != -1);
	this.isGecko = ua.indexOf('Gecko') != -1; // Will also be true on Safari
	this.isSafari = ua.indexOf('Safari') != -1;
	this.isOpera = window['opera'] && opera.buildNumber ? true : false;
	this.isMac = ua.indexOf('Mac') != -1;
	this.isNS7 = ua.indexOf('Netscape/7') != -1;
	this.isNS71 = ua.indexOf('Netscape/7.1') != -1;
	
	this._result_payment = 0;
	this._result_days_count = 0;
	
	var that=this;
	
	/**
	 * class methods
	 */
	this.formatMoney = function(Curr) {
		if( Curr == '' ) {
			Curr = 0;
		} else {
			Curr = parseFloat(Curr);
		}
		return Curr.toFixed(2);
	}
	
	this.populateFromAndToFields = function(){
		$(this._id_result_payment).value = '';
		$(this._id_result_days).value = '';
		
		var serv_id = $(this._id_server_list).value;
		var max_lvl = this._price_table[serv_id].length;
		
		$(this._id_lvl_from_slct).options.length = 0;
		$(this._id_lvl_to_slct).options.length = 0;
		
		for(var i = 0; i < max_lvl-1; i++){
			$(this._id_lvl_from_slct).options[i] = new Option(i+1, i+1);
			$(this._id_lvl_to_slct).options[i] = new Option(i+2, i+2);
		}
	}
	
	this.recalcResultValues = function(){
		var _parsed_send_money = this.getTotalPaymentValue();
		_parsed_send_money = this.converMoneyToAnotherCurrency(_parsed_send_money);
		$(this._id_result_payment).value = this.formatMoney(_parsed_send_money);
		$(this._id_result_days).value = Math.ceil(this._result_days_coun);
	}
	
	this.getTotalPaymentValue = function(){
		var serv_id = $(this._id_server_list).value;
		var result = 0;
		var days_result = 0;
		
		var i = parseInt($(this._id_lvl_from_slct).value);
		var count = parseInt($(this._id_lvl_to_slct).value);
		if(count <= i){
			$(this._id_lvl_to_slct).selectedIndex = $(this._id_lvl_from_slct).selectedIndex;
			count = parseInt($(this._id_lvl_to_slct).value);
		}
		for(i; i < count; i++){
			result += parseFloat(this._price_table[serv_id][i].lvl_price);
			days_result += parseFloat(this._price_table[serv_id][i].lvl_time);
		}
		
		this._result_payment = result;
		this._result_days_coun = days_result;
		
		return this._result_payment;
	}
	
	this.converMoneyFromAnotherCurrency = function(mo){
		switch($(this._id_curency_list).value){
			case 'wmz':				mo = mo/1; break;
			case 'wmz-card':		mo = mo/1; break;
			case 'wmr':				mo = mo/parseFloat(this._currency_courses['usd2rur']); break;
			case 'wmr-card':		mo = mo/parseFloat(this._currency_courses['usd2rur']); break;
			case 'wme':				mo = mo/parseFloat(this._currency_courses['usd2eur']); break;
			case 'wme-card':		mo = mo/parseFloat(this._currency_courses['usd2eur']); break;
			case 'wmu':				mo = mo/parseFloat(this._currency_courses['usd2uah']); break;
			case 'wmu-card':		mo = mo/parseFloat(this._currency_courses['usd2uah']); break;
			case 'ya':				mo = mo/parseFloat(this._currency_courses['usd2rur']); break;
			case 'elecs':			mo = mo/parseFloat(this._currency_courses['usd2rur']); break;
			case 'robo.mm':
			case 'robo.rbc':
			case 'robo.ek':
			case 'robo.wc':
			case 'robo.ep':			mo = mo/parseFloat(this._currency_courses['usd2rur']); break;
			case 'qiwi':			mo = mo/parseFloat(this._currency_courses['usd2rur']); break;
			case 'liqpay':			mo = mo/parseFloat(this._currency_courses['usd2rur']); break;
		}
		//alert(mo);
		return parseFloat(mo);
	}
	
	this.converMoneyToAnotherCurrency = function(mo){
		switch($(this._id_curency_list).value){
			case 'wmz':				mo = mo*1; break;
			case 'wmz-card':		mo = mo*1; break;
			case 'wmr':				mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'wmr-card':		mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'wme':				mo = mo*parseFloat(this._currency_courses['usd2eur']); break;
			case 'wme-card':		mo = mo*parseFloat(this._currency_courses['usd2eur']); break;
			case 'wmu':				mo = mo*parseFloat(this._currency_courses['usd2uah']); break;
			case 'wmu-card':		mo = mo*parseFloat(this._currency_courses['usd2uah']); break;
			case 'ya':				mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'elecs':			mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'robo.mm':
			case 'robo.rbc':
			case 'robo.ek':
			case 'robo.wc':
			case 'robo.ep':			mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'qiwi':			mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'liqpay':			mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
		}
		
		return parseFloat(mo);
	}
	
	this.getCurrencyMultiplierBySource = function(src){
		var ret = 1;
		switch(src){
			case 'wmr':				ret = parseFloat(this._currency_courses['usd2rur']); break;
			case 'wmr-card':		ret = parseFloat(this._currency_courses['usd2rur']); break;
			case 'wme':				ret = parseFloat(this._currency_courses['usd2eur']); break;
			case 'wme-card':		ret = parseFloat(this._currency_courses['usd2eur']); break;
			case 'wmu':				ret = parseFloat(this._currency_courses['usd2uah']); break;
			case 'wmu-card':		ret = parseFloat(this._currency_courses['usd2uah']); break;
			case 'ya':				ret = parseFloat(this._currency_courses['usd2rur']); break;
			case 'elecs':			ret = parseFloat(this._currency_courses['usd2rur']); break;
			case 'robo.mm':
			case 'robo.rbc':
			case 'robo.ek':
			case 'robo.wc':
			case 'robo.ep':			ret = parseFloat(this._currency_courses['usd2rur']); break;
			case 'qiwi':			ret = parseFloat(this._currency_courses['usd2rur']); break;
			case 'liqpay':			ret = parseFloat(this._currency_courses['usd2rur']); break;
		}
		
		return ret;
	}
}

/**
 * Card Price Calculator
 */
function cardpriceCalculator(card_price, ccourses, discounts, purses){
	this._card_price = parseFloat(card_price);
	this._currency_courses = ccourses;
	this._discounts = discounts;
	this._purses = purses;
	
	this._id_send_money_box = 'txt_pay_amount';
	this._id_curency_list = 'current_currency_id';
	this._id_curency_list_prev_val = 'cur_list_prev_value';
	this._id_discount_email_box = 'id_discount_email';
	this._id_discount_val_box = 'txt_discoun_percent';
	
	this._current_discount_amount = parseFloat(0);
	
	ua = navigator.userAgent;
	this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
	this.isMSIE5 = this.isMSIE && (ua.indexOf('MSIE 5') != -1);
	this.isMSIE5_0 = this.isMSIE && (ua.indexOf('MSIE 5.0') != -1);
	this.isMSIE7 = this.isMSIE && (ua.indexOf('MSIE 7') != -1);
	this.isGecko = ua.indexOf('Gecko') != -1; // Will also be true on Safari
	this.isSafari = ua.indexOf('Safari') != -1;
	this.isOpera = window['opera'] && opera.buildNumber ? true : false;
	this.isMac = ua.indexOf('Mac') != -1;
	this.isNS7 = ua.indexOf('Netscape/7') != -1;
	this.isNS71 = ua.indexOf('Netscape/7.1') != -1;
	
	var that=this;
	
	
	/**
	 * class methods
	 */
	this.formatMoney = function(Curr) {
		if( Curr == '' ) {
			Curr = 0;
		} else {
			Curr = parseFloat(Curr);
		}
		return Curr.toFixed(2);
	}
	
	this.getWmPurse = function(){
		switch($(this._id_curency_list).value){
			case 'wmz':				
			case 'wmz-card':		return this._purses.wmz;
			case 'wmr':				
			case 'wmr-card':		return this._purses.wmr;
			case 'wme':				
			case 'wme-card':		return this._purses.wme;
			case 'wmu':				
			case 'wmu-card':		return this._purses.wmu;
			case 'ya':				return this._purses.ya;
			case 'elecs':			return this._purses.elecs;
		}
	}
	
	this.converMoneyToAnotherCurrency = function(mo){
		switch($(this._id_curency_list).value){
			case 'wmz':				mo = mo*1; break;
			case 'wmz-card':		mo = mo*1; break;
			case 'wmr':				mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'wmr-card':		mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'wme':				mo = mo*parseFloat(this._currency_courses['usd2eur']); break;
			case 'wme-card':		mo = mo*parseFloat(this._currency_courses['usd2eur']); break;
			case 'wmu':				mo = mo*parseFloat(this._currency_courses['usd2uah']); break;
			case 'wmu-card':		mo = mo*parseFloat(this._currency_courses['usd2uah']); break;
			case 'ya':				mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'elecs':			mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'robo.mm':
			case 'robo.rbc':
			case 'robo.ek':
			case 'robo.wc':
			case 'robo.ep':			mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'qiwi':			mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
			case 'liqpay':			mo = mo*parseFloat(this._currency_courses['usd2rur']); break;
		}
		
		return parseFloat(mo);
	}
	
	this.paymentCurrencyChanged = function(){
		var new_price = this.converMoneyToAnotherCurrency(this._card_price * this.getCardMultPrice(this._card_price + this._current_discount_amount));
		new_price = new_price.toFixed(2);
		/**
		 * update lmi field
		 */
		$('lmi_amount_id').value = new_price;
		$('lmi_purse_id').value = this.getWmPurse();
		
		switch($(this._id_curency_list).value){
			case 'wmz':				new_price = '$' + new_price; break;
			case 'wmz-card':		new_price = '$' + new_price; break;
			case 'wmr':				new_price = new_price + 'руб'; break;
			case 'wmr-card':		new_price = new_price + 'руб'; break;
			case 'wme':				new_price = new_price + '&euro;'; break;
			case 'wme-card':		new_price = new_price + '&euro;'; break;
			case 'wmu':				new_price = new_price + 'ukr'; break;
			case 'wmu-card':		new_price = new_price + 'ukr'; break;
			case 'ya':				new_price = new_price + 'руб'; break;
			case 'elecs':			new_price = new_price + 'руб'; break;
			case 'robo.mm':
			case 'robo.rbc':
			case 'robo.ek':
			case 'robo.wc':
			case 'robo.ep':			new_price = new_price + 'руб'; break;
			case 'qiwi':			new_price = new_price + 'руб'; break;
			case 'liqpay':			new_price = new_price + 'руб'; break;
		}
		
		$(this._id_send_money_box).update(new_price);
	}
	
	this.getCardMultPrice = function(current_amount){
		current_amount = parseFloat(current_amount);
		var ret = 0;
		var prices = this._discounts;
		for(var i = 0; i < prices.length; i++){
			prices[i].bp = parseFloat(prices[i].bp);
			if(i+1 == prices.length){
				ret = prices[i].dp;
				break;
			} else {
				if(i == 0 && current_amount < prices[i].bp){
					ret = 0;
					break;
				} else {
					if(current_amount >= prices[i].bp && current_amount < prices[i+1].bp){
						ret = parseFloat(prices[i].dp);
						break;
					}
				}
			}
			
		}
		
		if(ret == '' || isNaN(parseInt(ret))){
			ret = 0;
		}
		
		if(ret == 0){
			$(this._id_discount_val_box).update('');
		} else {
			$(this._id_discount_val_box).update(' ( -' + ret + '%)');
		}
		var rVal = parseFloat((1 - ret/100)).toFixed(2);
		
		return rVal;
	}
	
	this.updateDiscountInfo = function(e){
		wkey = e.which?e.which:window.event.keyCode;
		if(wkey >= 45 || wkey == 8){
			//this._id_discount_email_box
			var _discount_email = $(this._id_discount_email_box).value;
			if(_discount_email.length > 4 && _discount_email.match(/^[\da-z\-\_\.]{1,}\@[\da-z\-\_]+\.[\da-z\-\_\.]{1,}$/i)){
				$('cards_comm_work_info').update('<img src="/img/ajax-loader.gif" />');
				new Ajax.Request('/games/',
			    {
			        method:'post',
			            parameters: { 
			            				'email': _discount_email, 
			            				'action': 'personalDiscountInfo'
			            			},
			        onSuccess:
			            function(transport){
			            	$('cards_comm_work_info').update('');
			                that._current_discount_amount = parseFloat(transport.responseText);
			                if(isNaN(that._current_discount_amount)){
			                	that._current_discount_amount = 0;
			                }
			                that.paymentCurrencyChanged();
			            },
			        onFailure:
			            function(){ alert('Something went wrong...') }
			    });
			}
			//this._current_discount_amount
		}
	}
}