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);

}



function Set(c_p,app,cp,hr,d,tc,dc,pc,ri,rf,t)

{

       

	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;

		

	Recalculate();

}



function Reset()

{

        $('#ifxElement').show();  

	ElemByID('copy_price').value = 1;

	ElemByID('approaches').value = 6;

	ElemByID('copies').value = 3;

	ElemByID('hours').value = 8;

	ElemByID('days').value = 21;

	ElemByID('toner_cost').value = 500;



	ElemByID('toner_recycle_cost').value = 100;

	ElemByID('def_toner_recycle_cost').value = 100;

	ElemByID('drum_cost').value = 450;

	ElemByID('paper_cost').value = 35;

	ElemByID('electricity_cost').value = 0;

	ElemByID('rent_interest').value = 0;

	ElemByID('rent_flat').value = 0;

	ElemByID('tax').value = 0;

	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 MonthlyCopies = ElemByID('copies').value * MonthlyApproaches;

        var CopySelfCost = ElemByID('drum_cost').value/25000 + (parseInt(ElemByID('toner_cost').value) + ElemByID('toner_recycle_cost').value * 2)/25000 + (ElemByID('paper_cost').value/500);

//      alert (25000/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 Profit = MonthlyCopies * CopyProfit - ElectricityCost - ElemByID('rent_flat').value;

	Profit = Profit * (100 - ElemByID('rent_interest').value) / 100;

	Profit = Profit * (100 - ElemByID('tax').value) / 100;

	ElemByID('calcresult2').innerHTML = Math.round(CopySelfCost*100)/100 + ' грн.';

        ElemByID('calcresult').innerHTML = Math.round(Profit)*12 + ' грн.';

        ElemByID('calcresult1').innerHTML = Math.round(Profit)*12 + ' грн.';

	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);