function onContent(f){//(C)webreflection.blogspot.com
	var a,b=navigator.userAgent,d=document,w=window,
	c="__onContent__",e="addEventListener",o="opera",r="readyState",
	s="<scr".concat("ipt defer src='//:' on",r,"change='if(this.",r,"==\"complete\"){this.parentNode.removeChild(this);",c,"()}'></scr","ipt>");
	w[c]=(function(o){return function(){w[c]=function(){};for(a=arguments.callee;!a.done;a.done=1)f(o?o():o)}})(w[c]);
	if(d[e])d[e]("DOMContentLoaded",w[c],false);
	if(/WebKit|Khtml/i.test(b)||(w[o]&&parseInt(w[o].version())<9))
	(function(){/loaded|complete/.test(d[r])?w[c]():setTimeout(arguments.callee,1)})();
	else if(/MSIE/i.test(b))d.write(s);
};
function ElemByID(name)
{	if (document.layers)
	return document.layers[name];
	else if (document.all)
	return document.all[name];
	else if (document.getElementById)
	return document.getElementById(name);
};
/* Set(
стоимость_копии,
количество_пользователей_в_час,
количество_копий_сделанных_одним_пользователем,
количество_рабочих_часов_в_день,
количество_рабочих_дней_в_месяце,
стоимость_тонера,
стоимость_барабана,
стоимость_бумаги_(уп.500_листов),
стоимость_аренды_в_%_от_дохода,
стоимость_аренды_фиксированная_сумма,
налог
) */
function Set(c_p,app,cp,hr,d,tc,dc,pc,ri,rf,t,trc)
{	ElemByID('copy_price').value = c_p;
	ElemByID('approaches').value = app;
	ElemByID('copies').value = cp;
	ElemByID('hours').value = hr;
	ElemByID('days').value = d;
	ElemByID('toner_cost').value = tc;
	ElemByID('drum_cost').value = dc;
	ElemByID('paper_cost').value = pc;
	ElemByID('rent_interest').value = ri;
	ElemByID('rent_flat').value = rf;
	ElemByID('tax').value = t;
	ElemByID('toner_recycle_cost').value = trc;
	Recalculate();
};
/* We doesn't need this in future
function Reset()
{	$('#ifxElement').show();
	ElemByID('copy_price').value = 5;
	ElemByID('approaches').value = 6;
	ElemByID('copies').value = 3;
	ElemByID('hours').value = 8;
	ElemByID('days').value = 21;
	ElemByID('toner_cost').value = 2600;
	ElemByID('toner_recycle_cost').value = 600;
	ElemByID('def_toner_recycle_cost').value = 600;
	ElemByID('drum_cost').value = 2300;
	ElemByID('paper_cost').value = 100;
	ElemByID('electricity_cost').value = 0;
	ElemByID('rent_interest').value = 20;
	ElemByID('rent_flat').value = 0;
	ElemByID('tax').value = 6;
	ElemByID('recycle').checked = true;
	Recalculate();
};
*/

// Check input. Only numbers and <.> allowed.
function testKey(e)
{	// Make sure to use event.charCode if available
	var key = (typeof e.charCode == 'undefined' ? e.keyCode : e.charCode);
	// Ignore special keys
	if (e.ctrlKey || e.altKey || key < 32) return true;
	key = String.fromCharCode(key);
	return /[\d\.]/.test(key);
};
function Recalculate()
{	var MonthlyApproaches = ElemByID('approaches').value * ElemByID('hours').value * ElemByID('days').value;
	var DayCopies = ElemByID('approaches').value * ElemByID('hours').value * ElemByID('copies').value;
	var MonthlyCopies = ElemByID('copies').value * MonthlyApproaches;
	var CopySelfCost = ElemByID('drum_cost').value/20000 + (parseInt(ElemByID('toner_cost').value) + ElemByID('toner_recycle_cost').value * 2)/18000 + (ElemByID('paper_cost').value/500);
	CopySelfCost = Math.round(CopySelfCost*100)/100;
	//      alert (20000/MonthlyCopies);
	//	var CopyProfit = ElemByID('copy_price').value - (ElemByID('toner_cost').value / 8000) - (ElemByID('drum_cost').value / 20000) - (ElemByID('paper_cost').value / 500);
	var CopyProfit = ElemByID('copy_price').value - CopySelfCost;
	var ActiveHours = (MonthlyApproaches * 10/3600) + (MonthlyCopies * 5/3600);
	var ElectricityCost = ((ActiveHours * 340/1000) + ((720 - ActiveHours) * 16/1000)) * ElemByID('electricity_cost').value;
	var MonthlyProfit = MonthlyCopies * ElemByID('copy_price').value;
	var MonthlyCosts = CopySelfCost * MonthlyCopies;
	var Profit = MonthlyProfit - ElectricityCost - ElemByID('rent_flat').value;
	var ProfitRent = MonthlyProfit * ElemByID('rent_interest').value / 100;
	var ProfitTax = MonthlyProfit * ElemByID('tax').value / 100;
	Profit = Profit - ProfitRent - ProfitTax - MonthlyCosts;
	ElemByID('calcresult2').innerHTML = Math.round(CopySelfCost*100)/100 + ' руб.';
	ElemByID('calcresult').innerHTML = Math.round(Profit)*12 + ' руб.';
	ElemByID('calcresult1').innerHTML = Math.round(Profit)*12 + ' руб.';
	ElemByID('daycopies').innerHTML = DayCopies + ' шт.';
	ElemByID('monthcopies').innerHTML = MonthlyCopies + ' шт.';
	if(Profit < 0)
	{
		ElemByID('calcresult').style.color = '#CC0000';
	}
	else if(Profit < 200)
	{
		ElemByID('calcresult').style.color = '#CC9900';
	}
	else
	{
		ElemByID('calcresult').style.color = '#009900';
	}
	if(Profit < 0)
	{
		ElemByID('calcresult1').style.color = '#CC0000';
	}
	else if(Profit < 200)
	{
		ElemByID('calcresult1').style.color = '#CC9900';
	}
	else
	{
		ElemByID('calcresult1').style.color = '#009900';
	}
};
function Init()
{	ElemByID('calcblock').style.display = "block";
	Recalculate();
};
onContent(Init);
