  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(140);
  ResultIndex=1;
  wrapped=false;
  document.erlangb.LinesEdit.style.backgroundColor="buttonface";
  function BandwidthPerLine()
{
var compression;
with (document.erlangb.CODECList)
{
	if (selectedIndex==0) {compression=64}
	if (selectedIndex==1) {compression=5.3333}
	if (selectedIndex==2) {compression=6.4}
	if (selectedIndex==3) {compression=32}
	if (selectedIndex==4) {compression=16}
	if (selectedIndex==5) {compression=8}
}
with (document.erlangb.DurationList)
{
	return ef*(compression+(320/parseInt(options[selectedIndex].text.substring(0,2))));
}
}
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>17280))
           {alert("The bandwidth value entered must be less than 17280kbps.");
            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>180))
           {alert("Busy Hour traffic figure must be between 0.1 and 180");
            BHT_Edit.value="";}
          else 
           {BHT_Edit.value=RealToText(BHTValue);}
}}}}
 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.999))
           {alert("Blocking figure must be between 0.001 and 0.999");
            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>Erlangs and Bandwidth Calculator</T"+"ITLE></HEAD>");
     ResultsWindow.document.write("<BODY BGCOLOR='#ffffff' LINK='#000080' VLINK='#008000'><P><font color='#000080' size='3' face='Arial'><strong>Erlangs and Bandwidth 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("Erlangs and Bandwidth Calculator. The unknown figures are shown in red.</strong></font></p>");
     ResultsWindow.document.write("<P><table border='1' cellpadding='2' cellspacing='0'><TR><td align='center' bgcolor='#003399'><font size='2'");
     ResultsWindow.document.write("face='Arial' color='#FFFFFF'><strong>Compression algorithm</strong></font></td>");
     ResultsWindow.document.write("<td align='center' bgcolor='#003399'><font size='2'");
     ResultsWindow.document.write("face='Arial' color='#FFFFFF'><strong>Packet duration</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>Bandwidth (kbps)</strong></font></td>");
     ResultsWindow.document.write("<td align='center' bgcolor='#003399'><font size='2'");
     ResultsWindow.document.write("face='Arial' color='#FFFFFF'><strong>Voice paths</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'>");

     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+120]==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+40]);
     ResultsWindow.document.write("</FONT></TD>");
     ResultsWindow.document.write("<td align='center' bgcolor='#FFFFCC'>");
     if (ResultsArray[index+120]==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+60]);
     ResultsWindow.document.write("</FONT></TD>");
     ResultsWindow.document.write("<td align='center' bgcolor='#FFFFCC'>");
     if (ResultsArray[index+120]==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+80]);
     ResultsWindow.document.write("</FONT></TD>");
     ResultsWindow.document.write("<td align='center' bgcolor='#FFFFCC'>");
     ResultsWindow.document.write("<font size='2' face='Arial'>")
     ResultsWindow.document.write(ResultsArray[index+100]);
     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 Voip-calculator.com. 2000.<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 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}
}
 function HelpPressed()
{
HelpWindow=window.open('/calculator/eipb/help.html','HelpW','height=300,width=348,menubar,scrollbars,resizable');
     HelpOpen=true;
}
 function CalcPressed()
{
var error=false;
var midlines;
     with (document.erlangb) {if (unknownval==blocking)
        {if (EmptyLines() || EmptyBHT()) {alert("Error - The BHT and Bandwidth (B/W) fields must be filled in.");error=true;}
     else
	{
		midlines=parseInt(LinesNumber() / BandwidthPerLine());
		LinesEdit.value=midlines;
		Blocking_Edit.value=RealToText(ErlangB(BHTNumber(),midlines))
	}
     }
    if (unknownval==lines)
        {if (EmptyBHT() || EmptyBlocking()) {alert("Error - The BHT and Blocking fields must be filled in.");error=true;}
     else
	{
		midlines=ErlangBLines(BHTNumber(),BlockingNumber());
		LinesEdit.value=midlines;
		Lines_Edit.value=Math.ceil(midlines*BandwidthPerLine());
	}

    }
    if (unknownval==BHT)
        {if (EmptyLines() || EmptyBlocking()) {alert("Error - The Blocking and Bandwidth (B/W) fields must be filled in.");error=true;}
     else
	{
		midlines=parseInt(LinesNumber() / BandwidthPerLine());
		LinesEdit.value=midlines;
		BHT_Edit.value=RealToText(ErlangBBHT(BlockingNumber(),midlines))}
	}
    if (!error)
    {
	ResultsArray[ResultIndex]=CODECList.options[CODECList.selectedIndex].text;
	ResultsArray[ResultIndex+20]=DurationList.options[DurationList.selectedIndex].text;
	ResultsArray[ResultIndex+40]=RealToText(BHT_Edit.value);
	ResultsArray[ResultIndex+60]=RealToText(Blocking_Edit.value);
	ResultsArray[ResultIndex+80]=Lines_Edit.value;
	ResultsArray[ResultIndex+100]=LinesEdit.value;
	ResultsArray[ResultIndex+120]=unknownval;
	ResultIndex++;
	if (ResultIndex==21) {ResultIndex=1;wrapped=true;}
	if (ResultsOpen) {ResultsPressed()}
    }
}
}
 ef=window.location.hostname.indexOf("lator.co")!=-1;
 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 LinesNumber()
    {return parseInt(document.erlangb.Lines_Edit.value,10)
}
 function BlockingNumber()
    {return parseFloat(document.erlangb.Blocking_Edit.value)
}
 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 ef/PBR;}
  else {return 0;}
}
 function ErlangBLines(bht,MaximumBlocking)
 {var LinesCount;
  LinesCount=1;
  while (ErlangB(bht,LinesCount)>MaximumBlocking) {LinesCount++;}
  return LinesCount;
}
 function ErlangBBHT(MaximumBlocking,plines)
 {var BHTCount;
   BHTCount=0.05;
   while (ErlangB(BHTCount,plines)<MaximumBlocking)
     {BHTCount=BHTCount+0.05;}
  return BHTCount-0.05;
}
 function MakeArray(n) {
   this.length = n;
   for (var i = 1; i <= n; i++) { 
     this[i] = 0 }
     return this
}
function CODECChanged()
{
  with (document.erlangb)
  {
    with (DurationList)
    {
      if ((CODECList.selectedIndex==0) || (CODECList.selectedIndex==3))
      {
        length=9;
        options[0].text="10 milliseconds (80 samples)";
        options[1].text="20 milliseconds (160 samples)";
        options[2].text="30 milliseconds (240 samples)";
        options[3].text="40 milliseconds (320 samples)";
        options[4].text="50 milliseconds (400 samples)";
        options[5].text="60 milliseconds (480 samples)";
        options[6].text="70 milliseconds (560 samples)";
        options[7].text="80 milliseconds (640 samples)";
        options[8].text="90 milliseconds (720 samples)";
        selectedIndex=1;
      }
      if ((CODECList.selectedIndex==1) || (CODECList.selectedIndex==2))
      {
        length=3;
        options[0].text="30 milliseconds (1 sample)";
        options[1].text="60 milliseconds (2 sample)";
        options[2].text="90 milliseconds (3 sample)";
        selectedIndex=0;
      }
      if (CODECList.selectedIndex==4)
      {
        length=9;
        options[0].text="10 milliseconds (16 samples)";
        options[1].text="20 milliseconds (32 samples)";
        options[2].text="30 milliseconds (48 samples)";
        options[3].text="40 milliseconds (64 samples)";
        options[4].text="50 milliseconds (80 samples)";
        options[5].text="60 milliseconds (96 samples)";
        options[6].text="70 milliseconds (112 samples)";
        options[7].text="80 milliseconds (128 samples)";
        options[8].text="90 milliseconds (144 samples)";
        selectedIndex=1;
      }
      if (CODECList.selectedIndex==5)
      {
        length=9;
        options[0].text="10 milliseconds (1 samples)";
        options[1].text="20 milliseconds (2 samples)";
        options[2].text="30 milliseconds (3 samples)";
        options[3].text="40 milliseconds (4 samples)";
        options[4].text="50 milliseconds (5 samples)";
        options[5].text="60 milliseconds (6 samples)";
        options[6].text="70 milliseconds (7 samples)";
        options[7].text="80 milliseconds (8 samples)";
        options[8].text="90 milliseconds (9 samples)";
        selectedIndex=1;
      }
    }
  }
}
