speciesArray = new Array();

function Species(nm,ppf,price,orderID,imgURL){
  this.name = nm;
  this.ppf = ppf;
  this.price = price;
  this.orderID = orderID;
  this.imageURL = imgURL;
  speciesArray[speciesArray.length] = this;
}

function getSpecies(){
  outHTML = "<select class='calcForm' name='plantType' onChange='runCalc()'>";
  for(var i=0;i<speciesArray.length;i++)
    outHTML += "<option value='"+i+"'>"+speciesArray[i].name;
  return outHTML+"</select>";
}

var noCols = 7;
var noRects = 5;
var isNN = 1;

function init(){
  isNN = (navigator.appName=="Netscape");
  document.calcForm.totalArea.focus();
  document.calcForm.Product_Code.value = speciesArray[0].orderID;
}

function fmt(val,isCash){
  if(val+""=="NaN")
    return "0";
  else{
    if(isCash){
      newVal = "$"+(Math.round(val*100)/100);
      if(newVal.indexOf(".")>newVal.length-3) newVal+="0"; 
      return newVal;
    }
    return Math.round(val*100)/100;
  }
}

function toNo(str){
  if(str=="")
    return 0;
  else
    return parseInt(str);
}

function keyDown(fieldObj){
  if(isNN) return true;
  if(event.keyCode==9 || event.keyCode==13){
    objNo = (/\d+/).exec(fieldObj.name);
    if(fieldObj.name.indexOf("areaWidth")>-1)
      document.calcForm.elements["areaLength"+objNo].focus();
    else{
      if(objNo==noCols) objNo=0;
      if(objNo<noRects)
        document.calcForm.elements["areaWidth"+(parseInt(objNo)+1)].focus();
      else
        document.calcForm.elements["areaDia"+(parseInt(objNo)+1)].focus();
    }
    return false;
  }
}

function calcArea(inputObj,isCircle){
  objNo = (/\d+/).exec(inputObj.name);
  areaObj = document.calcForm.elements["areaSq"+objNo];
  if(isCircle)
    areaObj.value = fmt(Math.PI*Math.pow(inputObj.value/2,2));
  else{
    wObj = document.calcForm.elements["areaWidth"+objNo];
    hObj = document.calcForm.elements["areaLength"+objNo];
    areaObj.value = fmt(wObj.value*hObj.value);
  }
  runCalc(1);
}

function runCalc(updateTA){
  with(document.calcForm){
    if(updateTA){
      areaSqV=0;
      for(var x=1;x<=noCols;x++)
        areaSqV+=toNo(elements["areaSq"+x].value);
      totalArea.value = areaSqV;
    }
    speciesObj = speciesArray[plantType.selectedIndex];
    plantTypeOut.value = speciesObj.name;
    plantsReq.value = plantsReqV = fmt(totalArea.value * density.options[density.selectedIndex].value);
    flatsReq.value = flatsReqV = fmt(plantsReqV/speciesObj.ppf);
    Quantity.value = flatsReqCV = Math.ceil(flatsReqV);
    cost.value = fmt(flatsReqCV*speciesObj.price,1);
    Product_Code.value = speciesObj.orderID;
  }
}

var infoWin=-1;

function plantInfo(){
  if(infoWin!=-1 && !infoWin.closed)
    infoWin.focus();
  else
    infoWin = window.open("","plntinfo","titlebar=no,scrollbars=no,width=140,height=250");
  speciesObj = speciesArray[document.calcForm.plantType.selectedIndex];
  infoWin.document.write("<html><body leftmargin=5 topmargin=5><font face='Arial, Helvetica, sans-serif' size=3 color='#006600'>");
  infoWin.document.write("<b>"+speciesObj.name+"</b><br><br>");
  if((/.+\..{2}/).test(speciesObj.imageURL))
    infoWin.document.write("<center><img alt='photograph' src='"+speciesObj.imageURL+"'></center><br>");
  infoWin.document.write("Plants per flat: "+speciesObj.ppf+"<br><br>");
  infoWin.document.write("Price per flat: "+fmt(speciesObj.price,1)+"</body></html>");
  infoWin.document.close();
  infoWin.document.title = "Plant Info";
}
