function calForm(){
  this.per = calForm.arguments[0];
  this.length = 0;

  if (calForm.arguments.length % 2 == 1){
     this.rates = new Array;
     var i=1; var j=0
     while(i < calForm.arguments.length){
       this.rates[j] = {value:0, mult:0};
       this.rates[j].value = calForm.arguments[i]; 
       this.rates[j].mult  = calForm.arguments[i+1];
       i++;i++;j++;
     }

     this.length = this.rates.length;

     this.charges = new Array();
     for(var i=0;i < this.length; i++){
       this.charges[i] = {rate:0, fee:0, mult:0};
       
       if(i==0){
         this.charges[i].rate = 0;
         this.charges[i].fee  = 0;
       }
       else{
         this.charges[i].rate = this.charges[i-1].rate + this.rates[i-1].value;
         this.charges[i].fee  = this.charges[i-1].fee  + (this.rates[i-1].value * this.rates[i-1].mult)/this.per;
       }
       
       this.charges[i].mult = this.rates[i].mult
     }

     this.rating = toRate;
     this.feeing = toFee;

     // ***************************************************************************

     this.display = toShow;
  }
  else
    alert ("Error: the number of arguments must be odd!")
}

// to determine the rating value
function toRate(num){
  var rFlag = false; 
  var rIndex = this.length;

  while(!rFlag){
    rIndex--
    if (num > this.charges[rIndex].rate)
       rFlag = true 
  }
  
  return(rIndex)
}

// determine the fee
function toFee(num){
  rInd = this.rating(num); // determining the rating index

  basFee = this.charges[rInd].fee; // determining the basic fee due to rating index

  pNum  = num - this.charges[rInd].rate; 
  pFee  = (pNum * this.charges[rInd].mult)/this.per;
  // BUG: Netscape cannot count the integer value of numbers less then 1
  // FIX:
  if((basFee + pFee)*100 >=1)
    return parseInt((basFee + pFee)*100)/100
  else
    return 0 
}

// ***************************************************************************
// displaying function for charges, charging formula
function toShow(){
  wodWindow = open("","", 
    "width="+screen.width+",height=250,top=-5,left=0,directories=no,location=no,resizable=yes,scrollbars=yes,toolbar=no,status=no,menubar=no");

  htm_code = "";
  htm_code += "<html><head><title>actual charge rates</title></head>";
  htm_code += "<body bgcolor='#efefef'>"
    htm_code += "<table align='center' cellpadding='3'>"
      htm_code += "<tr><td colspan=2 align ='center'>";
          htm_code += "<font face='arial'>" 
          htm_code += "<b>Actual Charge Rates</b>"
          htm_code += "</font>" 
      htm_code += "</td></tr>"
      htm_code += "<tr>";
        htm_code += "<td align ='left' valign ='top'>";
          htm_code += "<font face='arial' size='-1'>" 
          htm_code += "<b>rate </b>";
          htm_code += "</font>" 
        htm_code += "</td>"
        htm_code += "<td align ='left' valign ='top'>";
          htm_code += "<font face='arial' size='-1'>" 
          htm_code += "<b>charge<br>/100 cubic feet</b>";
          htm_code += "</font>" 
        htm_code += "</td>"
      htm_code += "</tr>";
    for(var i=0; i<this.length; i++){
      rate = this.rates[i].value;
      mult = "$" + this.rates[i].mult;

      htm_code += "<tr>";
        htm_code += "<td align ='left' valign ='top'>";
          htm_code += "<font face='arial' size='-1'>"  
          htm_code += rate;
          htm_code += "</font>" 
        htm_code += "</td>"
        htm_code += "<td align ='left' valign ='top'>";
          htm_code += "<font face='arial' size='-1'>" 
          htm_code += mult;
          htm_code += "</font>" 
        htm_code += "</td>"
      htm_code += "</tr>";
    }
    htm_code += "</table><br>"

    htm_code += "<table align='left' cellpadding='3'>"
      htm_code += "<tr><td align ='left'>";
          htm_code += "<font face='arial' size='-1'>" 
          htm_code += "<b>Charging Formula</b>"
          htm_code += "</font>" 
      htm_code += "</td></tr>"
      htm_code += "<tr>";
        htm_code += "<td align ='left' valign ='top'>";
          htm_code += "<font face='arial' size='-1'>" 
          htm_code += "For the first " + this.rates[0].value;
          htm_code += " cubic feet the charge (per 100 feet) is $" + this.rates[0].mult + ".<br>";
          htm_code += " This means you pay $" + this.rates[0].mult + " for " + this.per + " cubic feet"  
          if(2*this.per < this.rates[0].value){
            htm_code += ", $" + 2*this.rates[0].mult + " for " + 2*this.per + " cubic feet"
            if(3*this.per < this.rates[0].value){
              htm_code += ", $" + 3*this.rates[0].mult + " for " + 3*this.per + " cubic feet, etc."
            }
            else
              htm_code += "."
          }  
          else
            htm_code += "."
          htm_code += "<br><br>"
          htm_code += "For the second " + this.rates[1].value;
          htm_code += " cubic feet the charge (per 100 feet) is $" + this.rates[1].mult + ".<br>";
          htm_code += " This means if the amount of water exceeds " + this.rates[0].value + " cubic feet"
          htm_code += " then you pay for the surplus (per 100 feet) only $" + this.rates[1].mult + "."
          htm_code += " Therefore " + this.per + " cubic feet surplus costs $" +  this.rates[1].mult    
          if(2*this.per < this.rates[1].value){
            htm_code += ", " + 2*this.per + " cubic feet surplus costs $" +  2*this.rates[1].mult    
            if(3*this.per < this.rates[1].value){
              htm_code += ", " + 3*this.per + " cubic feet surplus costs $" +  3*this.rates[1].mult + ", etc."
            }
            else
              htm_code += "."
          }  
          else
            htm_code += "."
          htm_code += "<br><br>"
          htm_code += "For the third " + this.rates[2].value;
          htm_code += " cubic feet the charge (per 100 feet) is $" + this.rates[2].mult + ".<br>";
          htm_code += " This means if the amount of water exceeds " + (parseInt(this.rates[0].value) + parseInt(this.rates[1].value)) + " cubic feet"
          htm_code += " then you pay for the surplus (per 100 feet) only $" + this.rates[2].mult + "."
          htm_code += " Therefore " + this.per + " cubic feet surplus costs $" +  this.rates[2].mult    
          if(2*this.per < this.rates[2].value){
            htm_code += ", " + 2*this.per + " cubic feet surplus costs $" +  2*this.rates[2].mult    
            if(3*this.per < this.rates[2].value){
              htm_code += ", " + 3*this.per + " cubic feet surplus costs $" +  3*this.rates[2].mult + ", etc."    
            }
            else
              htm_code += "."
          }  
          else
            htm_code += "."
          htm_code += "<br><br>"
          htm_code += "And so on..."
          htm_code += "<br><br>"

          htm_code += "<b>Example</b><br>" 
          val0 = this.rates[0].value; mult0 = this.rates[0].mult;
          val1 = this.rates[1].value; mult1 = this.rates[1].mult;          
          val2 = this.rates[2].value; mult2 = this.rates[2].mult;
          val = this.per*parseInt(val1/(2*this.per)); mult = mult1;
          htm_code += val0 + val + " = " + val0 + " + " + val + " cubic feet costs:<br> ";
          htm_code += "(" + val0 + " / " + this.per + ") * $" + mult0; 
          htm_code += " + "; 
          htm_code += "(" + val  + " / " + this.per + ") * $" + mult; 
          htm_code += " = ";           
          htm_code += (val0/this.per) + " * $" + mult0; 
          htm_code += " + "; 
          htm_code += (val/this.per)  + " * $" + mult; 
          htm_code += " = $" + ((val0*mult0)/this.per + (val*mult)/this.per);
          htm_code += "<br><br>"
          val = this.per*parseInt(val2/(3*this.per)); mult = mult2;
          htm_code += val0 + val1 + val;
          htm_code += " = ";
          htm_code += val0 + " + " + val1 + " + " + val + " cubic feet costs:<br> "
          htm_code += "(" + val0 + " / " + this.per + ") * $" + mult0 
          htm_code += " + " 
          htm_code += "(" + val1 + " / " + this.per + ") * $" + mult1 
          htm_code += " + " 
          htm_code += "(" + val  + " / " + this.per + ") * $" + mult 
          htm_code += " = "           
          htm_code += (val0/this.per) + " * $" + mult0 
          htm_code += " + " 
          htm_code += (val1/this.per) + " * $" + mult1 
          htm_code += " + "
          htm_code += (val/this.per)  + " * $" + mult 
          htm_code += " = $" + ((val0*mult0)/this.per + (val1*mult1)/this.per +(val*mult)/this.per)
          htm_code += "</font>" 
        htm_code += "</td>"
      htm_code += "</tr>";
    htm_code += "</table>"



  htm_code += "</body></html>";
 
  wodWindow.document.open();
  wodWindow.document.write(htm_code);
  wodWindow.document.close();
 
}