// Hedging Plant Calculator (c) Buckingham Nurseries & Garden Centre. All rights reserved.
//
// Buckingham Nurseries, Tingewick Road, Buckingham, MK18 4AE, UK.  
// Web: www.buckingham-nurseries.co.uk  Tel.: 01280 822133  E-Mail: enquires@hedging.co.uk
// ---------------------------------------------------------------------------------------

function RndCur(ttl)
{  
  ttl = "" + (Math.round(ttl * 100)/100);  
  dec1 = ttl.substring(ttl.length-3, ttl.length-2);
  dec2 = ttl.substring(ttl.length-2, ttl.length-1);
  if (dec1 != '.') 
  if (dec2 == '.') ttl += "0";
  else ttl += ".00";
  return(ttl);
}
function CalcNo(curForm)
{
  noPlantsVal = new Array (curForm.NoPlantsA, curForm.NoPlantsB, curForm.NoPlantsC);
  priceVal = new Array(18)

  i=0;
  priceVal[0] = curForm.Price0a;
  if (maxDist>=2) priceVal[++i] = curForm.Price0b;
  if (maxDist>=3) priceVal[++i] = curForm.Price0c;
  priceVal[++i] = curForm.Price1a;
  if (maxDist>=2) priceVal[++i] = curForm.Price1b;
  if (maxDist>=3) priceVal[++i] = curForm.Price1c;
  priceVal[++i] = curForm.Price2a;
  if (maxDist>=2) priceVal[++i] = curForm.Price2b;
  if (maxDist>=3) priceVal[++i] = curForm.Price2c;
  priceVal[++i] = curForm.Price3a;
  if (maxDist>=2) priceVal[++i] = curForm.Price3b;
  if (maxDist>=3) priceVal[++i] = curForm.Price3c;
  priceVal[++i] = curForm.Price4a;
  if (maxDist>=2) priceVal[++i] = curForm.Price4b;
  if (maxDist>=3) priceVal[++i] = curForm.Price4c;
  
  if (CGplants == true) {
  	priceVal[16] = curForm.PriceCGa;
  	if (maxDist>=2) priceVal[17] = curForm.PriceCGb;
  	if (maxDist>=3) priceVal[18] = curForm.PriceCGc;	
	}
	
  Lfactor=curForm.lUnits.options[curForm.lUnits.selectedIndex].value;

  hedgeLength = parseFloat(curForm.Hlength.value);
  if (!isNaN(hedgeLength)) {
    for (i=0; i < maxDist; i++) {
      noPlants = Math.round(hedgeLength * Lfactor * distFactor[i]);
      noPlantsVal[i].value = noPlants;
      for (j=0; j < maxSize; j++) {
        if (noPlants > maxNo)
          priceVal[(j*maxDist)+i].value = "POA Please ask for a quote."
        else if (noPlants < 5)
          priceVal[(j*maxDist)+i].value = "£" + RndCur(noPlants * priceArray[0+(j*5)]);
        else if (noPlants < 50)
          priceVal[(j*maxDist)+i].value = "£" + RndCur(noPlants * priceArray[1+(j*5)]);
        else if (noPlants < 250)
          priceVal[(j*maxDist)+i].value = "£" + RndCur(noPlants * priceArray[2+(j*5)]);
        else if (noPlants < 1000)
          priceVal[(j*maxDist)+i].value = "£" + RndCur(noPlants * priceArray[3+(j*5)]);
        else 
          priceVal[(j*maxDist)+i].value = "£" + RndCur(noPlants * priceArray[4+(j*5)]);
		if (priceVal[(j*maxDist)+i].value == '£0.00')
		  priceVal[(j*maxDist)+i].value = "POA Please ask for a quote.";
      }
	  if (CGplants == true) {	  
	  	if (noPlants >= 1000)		
			priceVal[16+i].value = "POA Please ask for a quote."
        else if (priceArrayCG[0] > 0) {
				if (noPlants < 10)
					priceVal[16+i].value = "£" + RndCur(noPlants * priceArrayCG[0]);
				else if (noPlants < 50)
					priceVal[16+i].value = "£" + RndCur((noPlants%10 * priceArrayCG[0])+((noPlants-noPlants%10) * priceArrayCG[1]));				
				else if (noPlants < 500)	
					priceVal[16+i].value = "£" + RndCur((noPlants%10 * priceArrayCG[0])+((noPlants-noPlants%10) * priceArrayCG[2]));
				else
					priceVal[16+i].value = "£" + RndCur((noPlants%10 * priceArrayCG[0])+((noPlants-noPlants%10) * priceArrayCG[3]));
				}
			else {
				noPlantsT = Math.round(noPlants/10 + 0.4) * 10;
				if (noPlants < 10)
					priceVal[16+i].value = "£" + RndCur(10 * priceArrayCG[1]);
				else if (noPlantsT < 50)
					priceVal[16+i].value = "£" + RndCur(noPlantsT * priceArrayCG[1]);
				else if (noPlants < 500)	
					priceVal[16+i].value = "£" + RndCur(noPlantsT * priceArrayCG[2]);
				else
					priceVal[16+i].value = "£" + RndCur(noPlantsT * priceArrayCG[3]);
				}
		}
    }
   }
   else {
     for (i=0; i < maxDist; i++) {
     noPlantsVal[i].value = "";
     for (j=0; j < maxSize; j++) {
       priceVal[(j*maxDist)+i].value = "";
       }
     }
   }
  return false;
}

function CalcDist(curForm, dist) {
  noPlantsVal = new Array (curForm.NoPlantsA, curForm.NoPlantsB, curForm.NoPlantsC);
  curForm.Hlength.value = Math.round(10*noPlantsVal[dist].value / (curForm.lUnits.value * distFactor[dist]))/10;
  return false;
}

function updateParent(newLocation) {
	if (opener) {
		opener.window.focus();
		opener.window.location=newLocation;
	}
	else
		window.location=newLocation;	
}