function TradeCalculator(type) {
	this.type = type;
	
	// Should be able to use comma
	// Remove illegal characters
	// Create for offers as well, owned percentage and no account balance limit

	this.initialize = function() {
		this.creditsPerEuro = 100;
		this.sharesPerPercentage = 1000;
		
		if(this.type == 'bid') this.maximumPercentage = 100;
		this.accountBalance = parseFloat($('#accountBalance').html()) - parseFloat($('#accountBalanceLocked').html());
		this.errorMessages = {}
	
		this.numShares = $('#numShares');
		this.numPercentage = $('#numPercentage');
		this.totalPrice = $('#totalPrice');
		this.tuneValue = $('#tuneValue');
		this.percentagePrice = $('#percentagePrice');
		this.tradingFee = $('#tradingFee');
	}
	
	this.setFromNumShares = function() {
		this.calculate(['numPercentage','percentagePrice','tuneValue']);
		this.numPercentage.val(this.numPercentageValue);
		this.percentagePrice.val(this.percentagePriceValue);
		this.tuneValue.val(this.tuneValueValue);
		this.setErrorMessage();
	}
	
	this.setFromNumPercentage = function() {
		this.calculate(['numShares','percentagePrice','tuneValue']);
		this.numShares.val(this.numSharesValue);
		this.percentagePrice.val(this.percentagePriceValue);
		this.tuneValue.val(this.tuneValueValue);
		this.setErrorMessage();
	}
	
	this.setFromTotalPrice = function() {
		this.errorMessages.maximumPrice = " ";
		this.calculate(['percentagePrice','tuneValue', 'tradingFee']);
		this.percentagePrice.val(this.percentagePriceValue);
		this.tuneValue.val(this.tuneValueValue);
		this.tradingFee.val(this.tradingFeeValue);
		this.setErrorMessage();
	}
	
	this.setFromTuneValue = function() {
		this.calculate(['percentagePrice','totalPrice', 'tradingFee']);
		this.percentagePrice.val(this.percentagePriceValue);
		this.totalPrice.val(this.totalPriceValue);
		this.tradingFee.val(this.tradingFeeValue);
		this.setErrorMessage();
	}
	
	this.setFromPercentagePrice = function() {
		this.calculate(['totalPrice','tuneValue', 'tradingFee']);
		this.totalPrice.val(this.totalPriceValue);
		this.tuneValue.val(this.tuneValueValue);
		this.tradingFee.val(this.tradingFeeValue);
		this.setErrorMessage();
	}
	
	this.calculate = function(options) {
	
		if(this.type == 'offer') this.maximumPercentage = $('#ownedPercentage').html();
		
		this.numSharesValue = this.convertForbiddenCharacters(this.numShares);
		this.numPercentageValue = this.convertForbiddenCharacters(this.numPercentage);
		this.totalPriceValue = this.convertForbiddenCharacters(this.totalPrice);
		this.tuneValueValue = this.convertForbiddenCharacters(this.tuneValue);
		this.percentagePriceValue = this.convertForbiddenCharacters(this.percentagePrice);
		this.tradingFeeValue = this.convertForbiddenCharacters(this.tradingFee);
		
		if((jQuery.inArray('numShares',options) + 1) > 0) {
			this.numSharesValue = this.validNumber(parseFloat(this.numPercentageValue)*this.sharesPerPercentage);
			this.allowedPercentage();
		}
				
		if((jQuery.inArray('numPercentage',options) + 1) > 0) {
			this.numPercentageValue = this.validNumber(parseFloat(this.numSharesValue) / this.sharesPerPercentage);
			this.allowedPercentage();
		}
	
		if((jQuery.inArray('totalPrice',options) + 1) > 0) {
			this.totalPriceValue = this.round(this.validNumber(this.percentagePriceValue * this.numPercentageValue));
			if(this.type == 'bid') this.allowedSum();
		}
	
		if((jQuery.inArray('tuneValue',options) + 1) > 0) {
			this.tuneValueValue = this.round(this.validNumber(((this.totalPriceValue / parseFloat(this.numSharesValue)) * this.sharesPerPercentage * 100)));
		}
		
		if((jQuery.inArray('percentagePrice',options) + 1) > 0) {
			this.percentagePriceValue = this.round(this.validNumber(((this.totalPriceValue / parseFloat(this.numSharesValue)) * this.sharesPerPercentage)));
			if(this.type == 'bid') this.allowedSum();
		}
		
		if((jQuery.inArray('tradingFee',options) + 1) > 0) {
			if((this.totalPriceValue * 2) <= 25) this.tradingFeeValue = 0.25
			else this.tradingFeeValue = this.round(this.validNumber(this.totalPriceValue * 0.02));
		}
		
	}
	
	this.allowedSum = function() {
		if((parseFloat(this.totalPriceValue) + parseFloat(this.tradingFeeValue)) > parseFloat(this.accountBalance)) {
			this.totalPriceValue = parseFloat(this.accountBalance) - parseFloat(this.tradingFeeValue);
			this.totalPrice.val(this.totalPriceValue);
			this.calculate(['tuneValue','percentagePrice']);
			this.percentagePrice.val(this.percentagePriceValue);
			this.tuneValue.val(this.tuneValueValue);
			this.errorMessages.maximumPrice = "Account balance limit reached. Bid at " + this.totalPriceValue + " with a trading fee of " + this.tradingFeeValue + ".";
		}
	}
	
	this.allowedPercentage = function() {
		if(this.numPercentageValue > this.maximumPercentage) {
			this.numPercentage.val(this.maximumPercentage);
			this.calculate(['percentagePrice','numShares','tuneValue']);
			this.percentagePrice.val(this.percentagePriceValue);
			this.numShares.val(this.numSharesValue);
			this.tuneValue.val(this.tuneValueValue);
			this.errorMessages.maximumPercentage = "You can't purchase more than 100%";
		}
	}
	
	this.setErrorMessage = function() {
		if(this.errorMessages.maximumPrice) $('#totalPriceSpan').html(this.errorMessages.maximumPrice);
		if(this.errorMessages.maximumPercentage) $('#numPercentageSpan').html(this.errorMessages.maximumPercentage);
	}
	
	this.round = function(value) {
		return Math.round(value * 100) / 100;
	}
	
	this.validNumber = function(value) {
		if(isNaN(value)) return 0;
		else if(!isFinite(value)) return 0;
		else return value;
	}
	
	this.convertForbiddenCharacters = function(value) {
	
		newValue = value.val().replace(/\,/,'.');
		newValue = newValue.replace(/[a-z]/g,'');
		value.val(newValue);
		return newValue;

	}
	
	this.initialize();
}

var tradeCalculator = {};
$(document).ready(function() {
	if($('#bidCalculatorEnabler').length > 0) tradeCalculator = new TradeCalculator('bid');
	else if($('#offerCalculatorEnabler').length > 0) tradeCalculator = new TradeCalculator('offer');
});

