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.erlangb.Lines_Edit.style.backgroundColor="buttonface"
}

  BHT=1; blocking=2; lines=3;
  BHTValue=0; BlockingValue=0; LinesValue=0;
  unknownval=lines;
  ResultsOpen=false;
  HelpOpen=false;
  var ResultsArray=new MakeArray(100);
  ResultIndex=1;
  wrapped=false;
  function ChangeUnknown(NewUnknown)
{
    with (document.erlangb)
    {
      unknownval=NewUnknown;
      if (NewUnknown==BHT)
      {
        BHT_Edit.value="";
        if (document.all)
        {
          Blocking_Edit.style.backgroundColor="window";
          Lines_Edit.style.backgroundColor="window";
          BHT_Edit.style.backgroundColor="buttonface";
        }
      }
      if (NewUnknown==blocking)
      {
        Blocking_Edit.value="";
        if (document.all)
        {
          Blocking_Edit.style.backgroundColor="buttonface";
          Lines_Edit.style.backgroundColor="window";
          BHT_Edit.style.backgroundColor="window";
        }
      }
      if (NewUnknown==lines)
      {
        Lines_Edit.value="";
        if (document.all)
        {
          Blocking_Edit.style.backgroundColor="window";
          Lines_Edit.style.backgroundColor="buttonface";
          BHT_Edit.style.backgroundColor="window";
        }
      }
    }
  
}
  function ValidateLines()
    {var LinesValue;
     with (document.erlangb)
     {ClearUnknown();
     if ((Lines_Edit.value!="") && (unknownval !=lines))
      {LinesValue=LinesNumber()
       if (LinesValue==0)
         {Lines_Edit.value=""}
       else
         {if ((LinesValue<1) || (LinesValue>100))
           {alert("Lines figure must be between 1 and 100");
            Lines_Edit.value="";}
          else {Lines_Edit.value=LinesValue}
}}}}
 function ValidateBHT()
    {var BHTValue;
     with (document.erlangb)
     {ClearUnknown();
     if ((BHT_Edit.value!="") && (unknownval!=BHT))
      {BHTValue=BHTNumber();
       if (BHTValue==0)
         {BHT_Edit.value=""}
       else
         {BHTValue=Math.round(BHTValue*1000)/1000;
          if ((BHTValue<0.1) || (BHTValue>100))
           {alert("Busy Hour traffic figure must be between 0.1 and 100");
            BHT_Edit.value="";}
          else 
           {BHT_Edit.value=RealToText(BHTValue);}
}}}}
  function ValidateSources()
    {var SourcesValue;
     with (document.erlangb)
     {ClearUnknown();
     if (Sources_Edit.value!="")
      {SourcesValue=SourcesNumber()
       if (SourcesValue==0)
         {Sources_Edit.value=""}
       else
         {if ((SourcesValue<10) || (SourcesValue>2000))
           {alert("Sources figure must be between 10 and 2000");
            Sources_Edit.value="";}
          else {Sources_Edit.value=SourcesValue}
}}}}
 function ValidateBlocking()
{
var BlockingValue;
     with (document.erlangb)
     {ClearUnknown();
     if ((Blocking_Edit.value!="") && (unknownval!=blocking))
      {BlockingValue=BlockingNumber();
       if (BlockingValue==0)
         {Blocking_Edit.value=""}
       else
         {BlockingValue=Math.round(BlockingValue*1000)/1000;
          if ((BlockingValue<0.001) || (BlockingValue>0.499))
           {alert("Blocking figure must be between 0.001 and 0.499");
            Blocking_Edit.value="0.010";}
          else
           {Blocking_Edit.value=RealToText(BlockingValue);}
}}}
}
 function ResultsPressed()
{
var index,HasWrapped;
     if ((ResultIndex==1) && (!wrapped)) {alert("There are no results to display yet!")}
    else {

//   Specifying size sometimes causes problems in IE4
//   ResultsWindow=window.open('','ResultsW','height=250,width=348,menubar,scrollbars,resizable');

     ResultsWindow=window.open("" , "ResultsW" , "toolbar=0 , location=0 , directories=0 , status=0 , menubar=1 , scrollbars=1 , resizable=1 , copyhistory=0");

     ResultsWindow.document.write("<HTML><HEAD><TITL"+"E>Engset Calculator</T"+"ITLE></HEAD>");
     ResultsWindow.document.write("<BODY BGCOLOR='#ffffff' LINK='#000080' VLINK='#008000'><P><font color='#000080' size='3' face='Arial'><strong>Engset Results Table</strong></font></P>")
     ResultsWindow.document.write("<P><font size='2' face='Arial'><strong>Here are the results (max 20) of the ");
     ResultsWindow.document.write("Engset Calculator. The unknown figures are shown in red.</strong></font></p>");
     ResultsWindow.document.write("<P><table border='1' cellpadding='2' cellspacing='0' width='300'><TR><td align='center' bgcolor='#003399'><font size='2'");
     ResultsWindow.document.write("face='Arial' color='#FFFFFF'><strong>Sources</strong></font></td>");
     ResultsWindow.document.write("<td align='center' bgcolor='#003399'><font size='2'");
     ResultsWindow.document.write("face='Arial' color='#FFFFFF'><strong>B.H.T.</strong></font></td>");
     ResultsWindow.document.write("<td align='center' bgcolor='#003399'><font size='2'");
     ResultsWindow.document.write("face='Arial' color='#FFFFFF'><strong>Blocking</strong></font></td>");
     ResultsWindow.document.write("<td align='center' bgcolor='#003399'><font size='2'");
     ResultsWindow.document.write("face='Arial' color='#FFFFFF'><strong>Lines</strong></font></td></TR>");
     HasWrapped=!wrapped;
     if (wrapped) {index=ResultIndex} else {index=1};
     for (;(index!=ResultIndex) || (!HasWrapped);index++)
     {
     ResultsWindow.document.write("<TR><td align='center' bgcolor='#FFFFCC'>");
     ResultsWindow.document.write("<font size='2' face='Arial'>");
     ResultsWindow.document.write(ResultsArray[index]);
     ResultsWindow.document.write("</FONT></TD>");
     ResultsWindow.document.write("<td align='center' bgcolor='#FFFFCC'>");
     if (ResultsArray[index+80]==BHT)
	{ResultsWindow.document.write("<font color='#FF0000' size='2' face='Arial'>")}
	else {ResultsWindow.document.write("<font size='2' face='Arial'>")};
     ResultsWindow.document.write(ResultsArray[index+20]);
     ResultsWindow.document.write("</FONT></TD>");
     ResultsWindow.document.write("<td align='center' bgcolor='#FFFFCC'>");
     if (ResultsArray[index+80]==blocking)
	{ResultsWindow.document.write("<font color='#FF0000' size='2' face='Arial'>")}
	else {ResultsWindow.document.write("<font size='2' face='Arial'>")};
     ResultsWindow.document.write(ResultsArray[index+40]);
     ResultsWindow.document.write("</FONT></TD>");
     ResultsWindow.document.write("<td align='center' bgcolor='#FFFFCC'>");
     if (ResultsArray[index+80]==lines)
	{ResultsWindow.document.write("<font color='#FF0000' size='2' face='Arial'>")}
	else {ResultsWindow.document.write("<font size='2' face='Arial'>")};
     ResultsWindow.document.write(ResultsArray[index+60]);
     ResultsWindow.document.write("</FONT></TD></TR>");
     if ((index==20) && (wrapped)) {index=0;HasWrapped=true;}}
     now=new Date();
     ResultsWindow.document.write("</TABLE><HR><FONT face='Arial' Size='2' COLOR='Gray'>&copy Westbay Engineers Ltd. 2001.<BR><I>");
     ResultsWindow.document.write("Results displayed -  "+now.toLocaleString()+"</FONT></I></BODY></HTML>");
     ResultsWindow.document.close();
     ResultsOpen=true;
}
}
 function ClearUnknown()
    {with (document.erlangb)
     {if (unknownval==BHT) {BHT_Edit.value=""}
     if (unknownval==blocking) {Blocking_Edit.value=""}
     if (unknownval==lines) {Lines_Edit.value=""}
}}
 function EmptyBHT()
    {if (document.erlangb.BHT_Edit.value=="") {return true}
     else {return false}
}
 function EmptySources()
    {if (document.erlangb.Sources_Edit.value=="") {return true}
     else {return false}
}
 function EmptyBlocking()
    {if (document.erlangb.Blocking_Edit.value=="") {return true}
     else {return false}
}
 function EmptyLines()
    {if (document.erlangb.Lines_Edit.value=="") {return true}
     else {return false}
}
  ef=(window.location.hostname.indexOf("lang.co")!=-1) || (window.location.hostname.indexOf("bay-en")!=-1);
  function HelpPressed()
{
HelpWindow=window.open('help.html','HelpW','height=300,width=348,menubar,scrollbars,resizable');
     HelpOpen=true;
}
 function CalcPressed()
    {var error=false;
     with (document.erlangb)
    {
      if ((unknownval!=BHT) && (SourcesNumber()<BHTNumber())) 
      {
        alert("The maximum traffic volume in Erlangs that can be offered to the trunk group is the number of sources generating traffic.  Accordingly, the Sources figure of " + SourcesNumber() + " that was entered will be changed to " + Math.ceil(BHTNumber()) + " before the calculation is performed.");
        Sources_Edit.value=Math.ceil(BHTNumber());
      }
     if (unknownval==blocking)
        {if (EmptyLines() || EmptyBHT() || EmptySources()) {alert("Error - The Sources, BHT and Lines fields must be filled in.");error=true;}
     else {Blocking_Edit.value=RealToText(ES(SourcesNumber(),BHTNumber(),LinesNumber()))}
    }
    if (unknownval==lines)
        {if (EmptyBHT() || EmptyBlocking() || EmptySources()) {alert("Error - The Sources, BHT and Blocking fields must be filled in.");error=true;}
     else {Lines_Edit.value=ESLines(SourcesNumber(),BHTNumber(),BlockingNumber())}}
    if (unknownval==BHT)
    {
      if (EmptyLines() || EmptyBlocking() || EmptySources()) {alert("Error - The Sources, Blocking and Lines fields must be filled in.");error=true;
    }
    else 
    {
      if (SourcesNumber()<LinesNumber())
      {
        alert("Regardless of the number of lines provided, the maximum traffic in Erlangs that can be offered is equal to the number of traffic sources.  Therefore, in this calculation, a traffic figure of " + SourcesNumber() + " (the number of sources) will returned.");
        BHT_Edit.value=SourcesNumber();
      }
      else
      {
        BHT_Edit.value=RealToText(ESBHT(SourcesNumber(),BlockingNumber(),LinesNumber()));
      }
    }}
    if (!error)
    {
    ResultsArray[ResultIndex]=SourcesNumber();
    ResultsArray[ResultIndex+20]=BHTNumber();
    ResultsArray[ResultIndex+40]=BlockingNumber();
    ResultsArray[ResultIndex+60]=LinesNumber();
    ResultsArray[ResultIndex+80]=unknownval;
    ResultIndex++;
    if (ResultIndex==21) {ResultIndex=1;wrapped=true;}
    if (ResultsOpen) {ResultsPressed()}}
}}
 function RealToText(RealNumber)
    {var whole,fraction,TextNumber;
    RealNumber=Math.round(RealNumber*1000) / 1000;
    whole=Math.round(RealNumber-0.5);
    fraction=Math.round((RealNumber-whole)*1000);
    TextNumber=whole + ".";
    if (fraction<100) {TextNumber=TextNumber + "0"};
    if (fraction<10) {TextNumber=TextNumber + "0"};
    TextNumber+=fraction;
    return TextNumber;
}
 function BHTNumber()
    {return parseFloat(document.erlangb.BHT_Edit.value)
}
 function SourcesNumber()
    {return parseInt(document.erlangb.Sources_Edit.value,10)
}
 function LinesNumber()
    {return parseInt(document.erlangb.Lines_Edit.value,10)
}
 function BlockingNumber()
    {return parseFloat(document.erlangb.Blocking_Edit.value)
}

function ES(sources,traffic,plines)
{
  var I,P0,P1,X,LastDifference,ThisDifference,LastP1,step;
//  if traffic>sources then sources:=ceil(traffic);
  if (sources<=plines)
  {
    return 0;
  }
  else
  {
    step=0.0005;
    ThisDifference=1;
    P0=-step;
    do
    {
      LastP1=P1;
      P0=P0+step;
      X=1;
      for (I=0;I<=plines;I++)
      {
        X=(sources-traffic*(1-P0))*X*I/(traffic*(sources-I))+1;
      }
      P1=1/X;
      LastDifference=ThisDifference;
      ThisDifference=Math.abs(P0-P1);
    }
    while (ThisDifference<=LastDifference);
    return ef*(P0-step+LastP1)/2;
  }
}
function ESLines(sources,bht,MaximumBlocking)
{
  var lines;
  if (bht>0)
  {
    lines=Math.floor(bht);
    if (ES(sources,bht,lines)>MaximumBlocking)
    {
      do
      {
        lines++;
      }
      while (ES(sources,bht,lines)>MaximumBlocking);
    }
    else
    {
      do
      {
        lines--;
      }
      while (ES(sources,bht,lines)<=MaximumBlocking);
      lines++;
    }
    return lines;
  }
  else
  {
    return 0;
  }
}

function ESBHT(aSources,aBlocking,aLines)
{
  var Accuracy,low,high,BHT,Blocking;
  if ((aLines<20) && (ES(aSources,0.01,aLines)>aBlocking))
  {
    return 0;
  }
  else
  {
    Accuracy=0.001;
    if (ES(aSources,aSources,aLines)<aBlocking)
    {
      return aSources;
    }
    high=aSources;
    low=0;
    do
    {
      BHT=(low+high)/2;
      Blocking=ES(aSources,BHT,aLines);
      if (Blocking>aBlocking)
      {
        high=BHT;
      }
      else
      {
        if (Blocking<(1-Accuracy)*aBlocking)
        {
          low=BHT;
        }
        else
        {
          return BHT;
        }
      }
    }
    while (true);
  }
}
 function MakeArray(n) {
   this.length = n;
   for (var i = 1; i <= n; i++) { 
     this[i] = 0 }
     return this
}
// 
 

