// This file is copyright 2007 by Horsley Witten Group.





//Defining the first page so it can be visible

var currentLayer = 'page1';




function calculateBudget(a){



//septic flow
var shf=((a.houses.value*1 - a.seweredHouses.value*1 + a.units.value*1 - a.seweredUnits.value*1) * a.peopleInUnit.value * a.resFlow.value) / 1000000;
var scf=(a.totalIndWater.value*1*(1- a.seweredIndWater.value/100)+a.totalMunWater.value*1*(1-a.seweredMunWater.value/100))/1000000;
var sf = shf + scf;

// septic N load
var shN=shf*a.septicHouseNConc.value*3.785*2.205*365;
var scN=scf*a.septicIndNConc.value*3.785*2.205*365;
var sN=shN*1+scN*1;

//wwtp effluent flow (in M gpd)
var wwtpf=a.sewerFlow.value / 1000000// need to divide by a million to get M gpd 

//wwtp Nitrogen (in lbs)
var wwtpN=a.sewerNConc.value*wwtpf*3.785*2.205*365;

//sewer leakage in M gpd
var slf= (a.seweredHouses.value*a.resFlow.value*a.peopleInUnit.value*1+ a.totalIndWater.value*a.seweredIndWater.value*1 +a.totalMunWater.value*a.seweredMunWater.value*1)*0.1/1000000;//10% of sewer flow leaks 

//sewer leakage N (assumes 40 mg/L)
slN=slf*40*3.785*2.205*365;

//Agriculture Flow (acre-feet/year to M gpd)
var agfA=a.acresCropA.value*a.irrigCropA.value*325851/365/1000000;//from acre-feet/year to million gallons per day
var agfB=a.acresCropB.value*a.irrigCropB.value*325851/365/1000000;
var agfC=a.acresCropC.value*a.irrigCropC.value*325851/365/1000000;
var agfP=a.acresPast.value*a.irrigPast.value*325851/365/1000000;
var agf=(agfA*1+agfB*1+agfC*1+agfP*1)*a.rechIrrig.value/100; // the recharge from crop irrigation is in %

//Agriculture N 
var agnA=a.acresCropA.value*a.fertCropA.value*a.fertleachCropA.value/100;//the leach rates are in %
var agnB=a.acresCropB.value*a.fertCropB.value*a.fertleachCropB.value/100;
var agnC=a.acresCropC.value*a.fertCropC.value*a.fertleachCropC.value/100;
var agnP=a.acresPast.value*a.fertPast.value*a.fertleachPast.value/100;
var animN=(a.dairyCount.value*a.dairyNRate.value*a.dairyLeachRate.value*1 + a.horseCount.value*a.horseNRate.value*a.horseLeachRate.value*1 + a.birdCount.value*a.birdNRate.value*a.birdLeachRate.value*1)/100;
var agN=agnA*1+agnB*1+agnC*1+agnP*1+animN*1;

//Golf courses (acre-feet/year to M gpd)
var gf=a.golfArea.value*a.golfIrrig.value*a.rechIrrig.value*325851/365/1000000;

//Golf course N load (acres/year in 1000 sq ft)
var gN=a.golfArea.value*a.golfNRate.value*a.golfLeachRate.value/100*43.56; //Round to closest pound.

//Precip Flow
//roof (acres), paved/roads (acres), flow from acre-inch to M gpd
var roofa=(a.houses.value*a.roofHouse.value*1+a.units.value*a.roofUnit.value*1)/43560+a.totalIndComm.value*a.roofIndComm.value*1/100;// in acres
var pava=(a.houses.value*a.pavedHouse.value*1+a.units.value*a.pavedUnit.value*1)/43560+a.totalIndComm.value*a.roadIndComm.value*1/100;//in acres
var impa=a.whpArea.value*a.roadArea.value/100;//in acres
var ppf=((roofa+pava+impa)*a.ppInfilt.value*1+(a.whpArea.value*1-roofa-pava-impa)*a.rechRate.value*1)/12*325851/365/1000000;//acre-inches per year /12 into acre-feet, which are then changed into M gallons per day

//precip N
var impN=(pava+impa)*a.ppInfilt.value*1/12*325851*3.785*1.5*2.205/1000000;// acre-inches/12 into acre-feet then to gallons and liters * 1.5mg/L(x2.205/1000000 in lbs/L) 
var natN=((a.whpArea.value-roofa-pava-impa)*a.rechRate.value*1+roofa*a.ppInfilt.value*1)/12*325851*3.785*a.precipNCon.value*2.205/1000000;//same but for a concentration of precipNCon
var ppN=impN+natN;

//Surface Water
var surff=a.wellRate.value*a.infiltRate.value/100;//already in M gpd, times a percentage (hence the /100)

var surfN=surff*a.surfWaterNCon.value*3.785*2.205*365;//Mgpd * mg/L, and 3.785 liters to the gallon, 2.205 pounds to the kg, and 365 days per year

//Lawn N
var lawnN=(a.houses.value*a.lawnHouse.value*1+a.units.value*a.lawnUnit.value*1)*a.fertRateHousing.value/1000*a.fertleachHousing.value/100;// fertilizer applied for 1000 sq. ft, and leach rate in %

//Landfill N
var fillN=a.areaLandfill.value*a.leachingLandfill.value;

//Total Flow
var totf=sf+wwtpf+slf+agf+gf+ppf+surff;

//Total N
var totN=sN+wwtpN+slN+agN+gN+ppN+surfN+lawnN+fillN;

//water withdrawals
//well flow
var wf=a.wellRate.value*1;//if not *1, and the user does not enter a value, there is no value to this variable, and the model is not happy.

//Agriculture irrigation (M gpd)
var aof=agfA+agfB+agfC+agfP;

//Golf irrigation (acre feet/year to M gpd)
var gof=a.golfArea.value*a.golfIrrig.value*325851/1000000/365;

//Total withdrawals
var totw=wf+aof+gof;

//PercentageCalcs:
//Water sources
if (totf==0) // Otherwise, it will try to divide by zero
{
var perc=0;
a.septPerc.value=perc;
a.wwtpPerc.value=perc;
a.leakPerc.value=perc;
a.agPerc.value=perc;
a.golfPerc.value=perc;
a.precipPerc.value=perc;
a.surfPerc.value=perc
}
else{
var perc=Math.round(sf/totf*1000)/10;
a.septPerc.value=perc;
perc=Math.round(wwtpf/totf*1000)/10;
a.wwtpPerc.value=perc;
perc=Math.round(slf/totf*1000)/10;
a.leakPerc.value=perc;
perc=Math.round(agf/totf*1000)/10;
a.agPerc.value=perc;
perc=Math.round(gf/totf*1000)/10;
a.golfPerc.value=perc;
perc=Math.round(ppf/totf*1000)/10;
a.precipPerc.value=perc;
perc=Math.round(surff/totf*1000)/10;
a.surfPerc.value=perc;
}

//water withdrawals
if (totw==0)
{
var percw=0;
a.wellPerc.value=percw;
a.agOutPerc.value=percw;
a.golfOutPerc.value=percw;
}
else
{
var percw
percw=Math.round(wf/totw*1000)/10;
a.wellPerc.value=percw;
percw=Math.round(aof/totw*1000)/10;
a.agOutPerc.value=percw;
percw=Math.round(gof/totw*1000)/10;
a.golfOutPerc.value=percw;
}

//Nitrogen sources
if(totN==0)
{
var percN=0;
a.septNPerc.value=percN;
a.wwtpNPerc.value=percN;
a.leakNPerc.value=percN;
a.lawnNPerc.value=percN;
a.agNPerc.value=percN;
a.golfNPerc.value=percN;
a.fillNPerc.value=percN;
a.precipNPerc.value=percN;
a.surfNPerc.value=percN;
}
else
{
percN=Math.round(sN/totN*1000)/10;
a.septNPerc.value=percN;
percN=Math.round(wwtpN/totN*1000)/10;
a.wwtpNPerc.value=percN;
percN=Math.round(slN/totN*1000)/10;
a.leakNPerc.value=percN;
percN=Math.round(lawnN/totN*1000)/10;
a.lawnNPerc.value=percN;
percN=Math.round(agN/totN*1000)/10;
a.agNPerc.value=percN;
percN=Math.round(gN/totN*1000)/10;
a.golfNPerc.value=percN;
percN=Math.round(fillN/totN*1000)/10;
a.fillNPerc.value=percN;
percN=Math.round(ppN/totN*1000)/10;
a.precipNPerc.value=percN;
percN=Math.round(surfN/totN*1000)/10;
a.surfNPerc.value=percN;
}

//Summary calculations
if (totf==0) // Otherwise, it will try to divide by zero
{
a.totalNConc.value=totf; //If there is no flow, N calcs are impossible.
}
else
{
var nC=totN/(totf*2.205*365*3.785);//change lbs to kg, measure all in /day, and gallons to liters.
nC=Math.round(nC*100)/100;
a.totalNConc.value = nC;
}

var watB = totf-totw;
watB=Math.round(watB*100)/100;
a.waterBalance.value=watB;

//additional buildout flow
var aBFsept = ((a.buildoutHouses.value*1-a.buildoutSewHouses.value*1 + a.buildoutUnits.value*1-a.buildoutSewUnits.value*1) * a.peopleInUnit.value*a.resFlow.value+a.buildoutWastewater.value*1-a.buildoutSewWastewater.value*1)/1000000;//from septic
var aBFsew = (a.buildoutSewHouses.value*1 + a.buildoutSewUnits.value*1)* a.peopleInUnit.value*a.resFlow.value*0.1/1000000+a.buildoutSewWastewater.value*0.1/1000000;//from sewer leakage
var aBFpproof = (a.buildoutHouses.value * a.roofHouse.value*1 + a.buildoutUnits.value * a.roofUnit.value*1)/43560*(a.ppInfilt.value*1 - a.rechRate.value*1)/12*325851/365/1000000;//from roof precip
var aBFpppav = (a.buildoutHouses.value * a.pavedHouse.value*1 + a.buildoutUnits.value * a.pavedUnit.value*1)/43560*(a.ppInfilt.value*1 - a.rechRate.value*1)/12*325851/365/1000000;//from pavement precip
var aBF = aBFsept + aBFsew + aBFpproof +aBFpppav;
//additional buildout N load
var aBNsept = aBFsept * a.septicHouseNConc.value*3.785*2.205*365;//from septic
var aBNsew = aBFsew * 40*3.785*2.205*365;//from sewer leakage
var aBNpproof = aBFpproof*a.precipNCon.value*3.785*2.205*365;//from roof precip
var aBNpppav = aBFpppav * 1.5*3.785*2.205*365;//from pavement precip
var aBNlawn = (a.buildoutHouses.value * a.lawnHouse.value*1 + a.buildoutUnits.value*a.lawnUnit.value*1)*a.fertRateHousing.value/1000*a.fertleachHousing.value/100;//from lawns
var aBN = aBNsept+ aBNsew+ aBNpproof+ aBNpppav+ aBNlawn;
// new N Point source flow
var nPSF = a.buildoutNFlow.value/1000000; // in M gpd
// new N Point source N Load
var nPSN = nPSF*a.buildoutNConc.value*3.785*2.205*365;// in lbs /year

var totbf=totf+aBF+nPSF;
if (totbf==0)
{

a.buildoutTotalNConc.value = totbf;


}


else


{


var totBNConc=(totN+aBN+nPSN)/totbf/2.205/365/3.785;


totBNConc=Math.round(totBNConc*100)/100;


a.buildoutTotalNConc.value = totBNConc;


}



var bb=watB+aBF+nPSF-a.buildoutFlow.value/1000000;


bb=Math.round(bb*100)/100;


a.buildoutBalance.value=bb;




//Rounding and printing the output data




sf=Math.round(sf*1000)/1000;


a.septFlow.value=sf; 


sN=Math.round(sN);


a.septNLoad.value=sN;


wwtpf=Math.round(wwtpf*1000)/1000;


a.wwtpFlow.value=wwtpf;


wwtpN=Math.round(wwtpN);


a.wwtpNLoad.value=wwtpN;


slf=Math.round(slf*1000)/1000;


a.leakFlow.value=slf;


slN=Math.round(slN);


a.leakNLoad.value= slN;


agf=Math.round(agf*1000)/1000;


a.agFlow.value=agf;


agN=Math.round(agN)


a.agNLoad.value=agN;


gf=Math.round(gf*1000)/1000;


a.golfFlow.value=gf;


gN=Math.round(gN);


a.golfNLoad.value=gN;


ppf=Math.round(ppf*1000)/1000;


a.precipFlow.value=ppf;


ppN=Math.round(ppN);


a.precipNLoad.value=ppN;


surff=Math.round(surff*1000)/1000;


a.surfFlow.value=surff;


surfN=Math.round(surfN);


a.surfNLoad.value=surfN;


lawnN=Math.round(lawnN);


a.lawnNLoad.value=lawnN;


fillN=Math.round(fillN);


a.fillNLoad.value=fillN;


totf=Math.round(totf*1000)/1000;


a.totalInFlow.value=totf;


totN=Math.round(totN);


a.totalNLoad.value=totN;


wf=Math.round(wf*1000)/1000;


a.wellFlow.value=wf;


aof=Math.round(aof*1000)/1000;


a.agOutFlow.value=aof;


gof=Math.round(gof*1000)/1000;


a.golfOutFlow.value=gof;


totw=Math.round(totw*1000)/1000;


a.totalOutFlow.value=totw;




showLayer('page9')


}






function showLayer(lyr){



	hideLayer(currentLayer);



	document.getElementById(lyr).style.visibility = 'visible';



	currentLayer = lyr;


	scrollTo( 0, 0 );





}






function hideLayer(lyr){



	document.getElementById(lyr).style.visibility = 'hidden';



}




function helpWindow(url){
window.open('Help.html#'+url,'mywindow','width=450,height=500,scrollbars=yes,resizable=yes,screenX=400,screenY=300');

return;



}
