

var CalculatorObject = function()
{
	this.title		= 'Calculator';
	this.phrases	= new Array();
	this.accounts	= new Array();
};


CalculatorObject.prototype.setTitle = function(title)
{
	if(title == '' || typeof(title) != 'string')
	{
		return false;
	}

	this.title = title;
	return true;
};


CalculatorObject.prototype.getPhrase = function(phraseKey)
{
	if(!this.phrases[phraseKey])
	{
		return false;
	}

	return this.phrases[phraseKey];
};


CalculatorObject.prototype.addPhrases = function(phrases)
{
	if(! typeof( phrases) == 'object')
	{
		return false;
	}

	var counter = 0;
	for(var index in phrases)
	{
		this.phrases[index] = phrases[index];
		counter++;
	}
	this.phrases['amount'] = counter;

	return true;
};


CalculatorObject.prototype.addAccounts = function(accounts)
{
	if(! typeof(accounts) == 'object')
	{
		return false;
	}

	for(var index in accounts)
	{
		this.accounts[index] = new Array();
		this.accounts[index]['id']				= accounts[index]['id'];
		this.accounts[index]['name']			= (accounts[index]['name'] ? accounts[index]['name'] : 'Unknown');
		this.accounts[index]['price']			= (accounts[index]['price'] ? accounts[index]['price'] : 0);
		this.accounts[index]['price_minutes']	= (accounts[index]['price_minutes'] ? accounts[index]['price_minutes'] : 0);
		this.accounts[index]['max_viewer']		= (accounts[index]['max_viewer'] ? accounts[index]['max_viewer'] : 0);
		this.accounts[index]['max_minutes']		= (accounts[index]['max_minutes'] ? accounts[index]['max_minutes'] : 0);
	}

	return true;
};


CalculatorObject.prototype.render = function()
{
	if
	(
		this.accounts.length == 0 ||
		this.phrases['amount'] == 0
	)
	{
		return false;
	}

	HTMLCode = this.getHTMLCode();

	var  win_calculator = new RequestWindow();
	with(win_calculator) 
	{ 
		title		= this.title;
		description	= this.getPhrase('calculator_description');
		id			= 'calculator_window';

		addHTML(HTMLCode);
		addButton('Close', false, false, 'CLOSE');
		render();
	}

	return true;
};


CalculatorObject.prototype.getHTMLCode = function()
{
	var HTMLCode = '';

	HTMLCode	+= '<div class="requestWindowFieldContainer">';

	HTMLCode	+= '<div style="margin:20px auto;width:350px;text-align:left;">';	

	HTMLCode	+= '<div class="float_left">'+ this.getPhrase('calculator_amount_shows') +'</div>';
	HTMLCode	+= '<div class="float_right"><input onkeyup="Calculator.calculateStreamMinutes();" value="0" type="text" style="padding:3px;width:90px;text-align:right" id="amount_shows" onchange="javascript:Calculator.calculateStreamMinutes()" /></div>';
	HTMLCode	+= '<div class="clear_float" style="height:10px"></div>';

	HTMLCode	+= '<div class="float_left">'+ this.getPhrase('calculator_length_shows') +'</div>';
	HTMLCode	+= '<div class="float_right"><input onkeyup="Calculator.calculateStreamMinutes();" value="0" type="text" style="padding:3px;width:90px;text-align:right" id="length_shows" onchange="javascript:Calculator.calculateStreamMinutes()" /></div>';
	HTMLCode	+= '<div class="clear_float" style="height:10px"></div>';

	HTMLCode	+= '<div class="float_left">'+ this.getPhrase('calculator_amount_viewer') +'</div>';
	HTMLCode	+= '<div class="float_right"><input onkeyup="Calculator.calculateStreamMinutes();" value="0" type="text" style="padding:3px;width:90px;text-align:right" id="amount_viewer" onchange="javascript:Calculator.calculateStreamMinutes()" /></div>';
	HTMLCode	+= '<div class="clear_float" style="height:20px"></div>';

	HTMLCode	+= '<div class="float_left">'+ this.getPhrase('calculator_needed_minutes') +'</div>';
	HTMLCode	+= '<div class="float_right" style="width:97px;text-align:right;background:#707070;padding:2px;" id="needed_minutes">0</div>';
	HTMLCode	+= '<div class="clear_float"></div>';

	HTMLCode	+= '<div style="font-style:bold;font-size:14px;margin:40px 0px 20px 0px;"><u>'+ this.getPhrase('calculator_cost_header') +'</u></div>';

	var counter = this.accounts.length -1 ;

	for(var index in this.accounts)
	{
		HTMLCode	+= '<div class="float_left">'+ this.accounts[counter]['name'] +'</div>';
		HTMLCode	+= '<div class="float_right" style="white-space:nowrap;overflow:hidden;width:190px;text-align:right;background:#707070;padding:2px;" id="price_field_'+ this.accounts[counter].id +'">'+ this.accounts[counter].price +' &euro;</div>';
		HTMLCode	+= '<div class="clear_float" style="height:5px;"></div>';
		counter--;
	}

	HTMLCode	+= '<br /><br />';
	HTMLCode	+= '<div>'+ this.getPhrase('calculator_footer') +'</div>';

	HTMLCode += '</div></div>';

	return HTMLCode;
};


CalculatorObject.prototype.calculateStreamMinutes = function()
{
	var resultField = getObject('needed_minutes');
	if(! typeof(resultField) == 'object')
	{
		return false;
	}

	var showsAmount		= getObject('amount_shows').value;

	if(! showsAmount.match(/^[0-9]+$/))
	{
		showsAmount = 0;
		getObject('amount_shows').value = '';
	}
	var showLength		= getObject('length_shows').value;
	if(! showLength.match(/^[0-9]+$/))
	{
		showLength = 0;
		getObject('length_shows').value = '';
	}
	var viewerAmount	= getObject('amount_viewer').value;
	if(! viewerAmount.match(/^[0-9]+$/))
	{
		viewerAmount = 0;
		getObject('amount_viewer').value = '';
	}

	var resultValue = parseInt(showsAmount) * parseInt(showLength) * parseInt(viewerAmount);

	this.calculateAccountPrice(resultValue, viewerAmount);

	resultField.innerHTML = resultValue;
	return true;
};


CalculatorObject.prototype.calculateAccountPrice = function(viewedMinutes, viewerAmount)
{
	if
	(
		this.accounts.length == 0
	)
	{
		return false;
	}

	var counter 		= 0;
	var accountPrice	= 0;
	var account			= false;

	for(var index in this.accounts)
	{
		account = this.accounts[index];

		switch(account.id)
		{
			case '2': // private
				if(viewerAmount > account.max_viewer)
				{
					getObject('price_field_' + account.id).innerHTML = this.getPhrase('calculator_viewer_limit_exceed');
				}
				else if(viewedMinutes > account.max_minutes)
				{
					getObject('price_field_' + account.id).innerHTML = this.getPhrase('calculator_minutes_limit_exceed');
				}
				else
				{
					getObject('price_field_' + account.id).innerHTML = Math.round(account.price * 100)/100 + ' &euro;';
				}
			break;

			default:
				if(viewedMinutes <= account.max_minutes)
				{
					getObject('price_field_' + account.id).innerHTML = account.price+ ' &euro;';
				}
				else
				{
					getObject('price_field_' + account.id).innerHTML = Math.round( (account.price +  ((viewedMinutes - account.max_minutes) / 1000) * account.price_minutes) * 100) /100 + ' &euro;';
				}
			break;
		}
		
		
		if(viewerAmount > this.accounts[index]['max_viewer'])
		{
			//
		}
		else
		{
			accountPrice = this.accounts[index]['pirce'];
		
			if(this.accounts[index]['price_minutes'] != 0)
			{
				accountPrice = accountPrice + (this.accounts[index]['price_minutes'] * viewedMinutes);
			}
		}
	}
};




