var EntryValue,PeakHour;

if (document.all)
{
  document.styleSheets[0].addRule(".RightAlign","text-align: right");
  document.styleSheets[0].addRule(".CalcTable","background-color: buttonface");
  document.styleSheets[0].addRule(".CalcCaptionBack","background-color: activecaption");
  document.styleSheets[0].addRule(".CalcCaptionText","color: captiontext");
  document.styleSheets[0].addRule(".RightAlignGrey","background-color: buttonface");
  document.styleSheets[0].addRule(".Agents","background-color: buttonface");
  document.styleSheets[0].addRule(".Header","background-color: threedshadow");
  document.styleSheets[0].addRule(".HeaderText","color: window");
}

function CalculatePressed()
{
with (document.CallCentreForm)
{
	for (var EditIndex=5 ; EditIndex!=33 ; EditIndex=EditIndex+4)
	{
		if ((elements[EditIndex].value!="-") && (elements[EditIndex].value!="") && (elements[EditIndex+1].value=="-"))
		{CalculateHour(EditIndex)}
	}
}
}
function RemoveResults()
{
var ElementCounter;
with (document.CallCentreForm)
{
	for (ElementCounter=6;ElementCounter!=38;ElementCounter=ElementCounter+4)
	{elements[ElementCounter].value="-"}
	for (ElementCounter=7;ElementCounter!=39;ElementCounter=ElementCounter+4)
	{elements[ElementCounter].value="-"}
	for (ElementCounter=8;ElementCounter!=40;ElementCounter=ElementCounter+4)
	{elements[ElementCounter].value="-"}
	elements[38].value="-";
	elements[39].value="-";
}

}
function ErlangCPODelayTime(Traffic,Lines,HoldTime,DelayTime)
{
var Probability;
Probability=ErlangC(Traffic,Lines)*Math.exp(-(Lines-Traffic)*DelayTime/HoldTime);
if (Probability>1) {return 1} else {return Probability};
}
function ErlangB(Traffic,pLines)
{
var PBR,index;
if (Traffic>0) 
{
	PBR=(1+Traffic)/Traffic;
	for (index=2;index!=pLines+1;index++)
	{
		PBR=index/Traffic*PBR+1;
		if (PBR>10000) {return 0;}
	}
	return 1/PBR;
}
else {return 0;}
}
function ErlangC(Traffic,pLines)
{
var EBResult,Probability;
EBResult=ErlangB(Traffic,pLines);
Probability=EBResult/(1-(Traffic/pLines)*(1-EBResult));
if (Probability>1) {return 1} else {return Probability};
}
function CallDurationChanged()
{
var DurationValue;
with (document.CallCentreForm.CallDurationEdit)
{
	DurationValue=parseInt(value);
	if ((DurationValue>=10) & (DurationValue<=1200))
	{
		value=DurationValue;
		RemoveResults();
	}
	else
	{
		alert("Average call duration must be between 10 and 1200 seconds");
		value=EntryValue;
	}
}
}
function BlockingEditChanged()
{
var BlockingValue,whole,fraction,TextNumber;
with (document.CallCentreForm.BlockingEdit)
{
	BlockingValue=parseFloat(value);
	BlockingValue=Math.round(BlockingValue*1000)/1000;
	if ((BlockingValue>=0.001) & (BlockingValue<=0.500))
	{
		whole=Math.round(BlockingValue-0.5);
		fraction=Math.round((BlockingValue-whole)*1000);
		TextNumber=whole + ".";
		if (fraction<100) {TextNumber=TextNumber + "0"};
		if (fraction<10) {TextNumber=TextNumber + "0"};
		TextNumber+=fraction;
		value=TextNumber;
		RemoveResults();
	}
	else
	{
		alert("Blocking figure must be between 0.001 and 0.500");
		value=EntryValue;
	}
}
}
function AnsweredInEditChanged()
{
var AnsweredInValue;
with (document.CallCentreForm.AnsweredInEdit)
{
	AnsweredInValue=parseInt(value);
	if ((AnsweredInValue>=5) & (AnsweredInValue<=300))
	{
		value=AnsweredInValue;
		RemoveResults();
	}
	else
	{
		alert("Target must be between 5 and 300");
		value=EntryValue;
	}
}
}
ef=window.location.hostname.indexOf("int.co")!=-1;
function PercentageEditChanged()
{
var PercentageValue;
with (document.CallCentreForm.PercentageEdit)
{
		PercentageValue=parseInt(value);
	if ((PercentageValue>=10) & (PercentageValue<=95))
	{
		value=PercentageValue;
		RemoveResults();
	}
	else
	{
		alert("Target percentage must be between 10 and 95");
		value=EntryValue;
	}
}
}
function WrapTimeEditChanged()
{
var WrapTimeValue;
with (document.CallCentreForm.WrapTimeEdit)
{
	WrapTimeValue=parseInt(value);
	if ((WrapTimeValue>=0) & (WrapTimeValue<=300))
	{
		value=WrapTimeValue;
		RemoveResults();
	}
	else
	{
		value=WrapTimeValue;
		RemoveResults();
		alert("Wrap up time must be between 0 and 300 seconds");
		value=EntryValue;
	}
}
}
function CalculateHour(ElementChanged)
{
var CallsPerHourValue,AgentCounter,AgentBusyTime,AverageDelayAll,ECTraffic,EBTraffic,Lines;
with (document.CallCentreForm)
{
	CallsPerHourValue=parseInt(elements[ElementChanged].value);
	if ((CallsPerHourValue>=10) & (CallsPerHourValue<=5000))
	{
		AgentBusyTime=parseInt(CallDurationEdit.value)+parseInt(WrapTimeEdit.value);
		ECTraffic=ef*AgentBusyTime*elements[ElementChanged].value/3600;
		AgentCounter=Math.floor(ECTraffic)+1;
		while (ErlangCPODelayTime(ECTraffic,AgentCounter,AgentBusyTime,AnsweredInEdit.value)>(100-PercentageEdit.value)/100)
		{AgentCounter++}
		AverageDelayAll=Math.floor(ErlangC(ECTraffic,AgentCounter)*AgentBusyTime/(AgentCounter-ECTraffic))+1;
		EBTraffic=elements[ElementChanged].value*(AverageDelayAll+parseInt(CallDurationEdit.value))/3600;
		Lines=Math.floor(CallDurationEdit.value*elements[ElementChanged].value/3600)+1;
		if (EBTraffic>0)
		{
			while (ErlangB(EBTraffic,Lines)>BlockingEdit.value) {Lines++}
		}
		elements[ElementChanged+1].value=AverageDelayAll;
		elements[ElementChanged+2].value=AgentCounter;
		elements[ElementChanged+3].value=Lines;
		if (LinesRequiredEdit.value=="-") {LinesRequiredEdit.value=Lines}
		else {LinesRequiredEdit.value=Math.max(LinesRequiredEdit.value,Lines)}		
		if (MaximumAgentsEdit.value=="-") {MaximumAgentsEdit.value=AgentCounter}
		else {MaximumAgentsEdit.value=Math.max(MaximumAgentsEdit.value,AgentCounter)}		
		if (PeakHourEdit.value=="-")
		{
			PeakHour=(ElementChanged-1)/4;
			PeakHourEdit.value="Hour "+PeakHour;
		}
		else
		{
			if (Math.max(elements[ElementChanged].value,elements[(PeakHour*4)+1].value)==elements[ElementChanged].value)
			{
				PeakHour=(ElementChanged-1)/4;
				PeakHourEdit.value="Hour "+PeakHour;
			}
		}
	}
	else
	{
		alert("Calls per hour must be between 10 and 5000");
		elements[ElementChanged].value=EntryValue;
	}
}
}