
	function doMouseOut(thisButton){
		if (thisButton.style.background=='black'||thisButton.style.background=='BLACK'){
			thisButton.style.background='#cdcdb4';
			thisButton.style.color='black';
		}
	}


	function doMouseOver(thisButton){
		if (thisButton.style.background=='#cdcdb4'||thisButton.style.background=='#CDCDB4'){
			thisButton.style.background='black';
			thisButton.style.color='white';
		}
	}


	var usingElevation=false;
	var packingTypeDisplay;


	function hideInputDiv(){
		//set parameter div invisible
		document.getElementById('bp_input').style.visibility='hidden';
		document.getElementById('barLabel').style.visibility='hidden';
		document.getElementById('se_input').style.visibility='hidden';
		document.getElementById('seLabel').style.visibility='hidden';
		var inputDiv=document.getElementById('inputDiv');
		inputDiv.style.visibility='hidden';
	}


	function hideResultsDiv(document){
		var inputDiv=document.getElementById('inputDiv');
		inputDiv.style.visibility='visible';
		var resultsDiv=document.getElementById('resultsDiv');
		resultsDiv.style.visibility='hidden';
		if(usingElevation){
			document.getElementById('se_input').style.visibility='visible';
			document.getElementById('seLabel').style.visibility='visible';
		}else{
			document.getElementById('bp_input').style.visibility='visible';
			document.getElementById('barLabel').style.visibility='visible';
		}
	}




	function readParameters(document){
 		Patm = round(document.getElementById('bp_input').value,3);
		Taidb=round(document.getElementById('taidb_input').value,2);
		Taiwb=round(document.getElementById('taiwb_input').value,2);
		mw0=round(document.getElementById('mwo_input').value,2);
		Twi=round(document.getElementById('twi_input').value,2);
		Two=round(document.getElementById('two_input').value,2);
		va=round(document.getElementById('va_input').value,2);
		nCells=round(document.getElementById('ncells_input').value,0);
		packingtype=document.getElementById('packingtype_input').value;
		if(packingtype=='film'){
			packingTypeDisplay='Film';			
	          	V=1.2;
         		C=1.519;
         		n=-0.769;
          		a1=0.0;
          		b1=0.0;
          		C2=148.985;
          		a2=0.198;
          		b2=-0.436;	
		}else if(packingtype=='trickle'){
			packingTypeDisplay='Trickle';		
			V=1.35;
          		C=0.843;
          		n=0.0;
          		a1=-0.760;
          		b1=0.820;
          		C2=15.98;
          		a2=0.207;
          		b2=-0.216;	
		}else if(packingtype=='splash'){
			packingTypeDisplay='Splash';			
          		V=4.4;
          		C=0.333;
          		n=-0.537;
          		a1=0.0;
          		b1=0.0;
          		C2=13.552*1.1;
          		a2=0.443;
          		b2=-0.523;
		}
		if(document.getElementById('siteRadio').checked){
			//site elevation checked
			usingElevation=true; 
		}else if (document.getElementById('barRadio').checked){
			//barometric pressure checked
			usingElevation=false;
		} 		
	}







	function populateResults(document){

		//design requirements
		document.getElementById('patm_result').innerHTML=round(Patm,5);
		document.getElementById('taidb_result').innerHTML=round(Taidb,5);
		document.getElementById('taiwb_result').innerHTML=round(Taiwb,5);
		document.getElementById('mwo_result').innerHTML=round(mw0,5);
		document.getElementById('twi_result').innerHTML=round(Twi,5);
		document.getElementById('two_result').innerHTML=round(Two,5);
		document.getElementById('va_result').innerHTML=round(va,5);
		document.getElementById('ncells_result').innerHTML=round(nCells,5);
		document.getElementById('packingtype_result').innerHTML=packingTypeDisplay;

		//results
		document.getElementById('ttl_result').innerHTML=round(Q,1);
		document.getElementById('ctipa_result').innerHTML=round(Afr,1);		
		document.getElementById('cipa_result').innerHTML=round(AfrCell,1);
		document.getElementById('ciw_result').innerHTML=round(Wcell,2);
		document.getElementById('cid_result').innerHTML=round(Dcell,2);
		document.getElementById('ctw_result').innerHTML=round(CTW,1);
		document.getElementById('ctd_result').innerHTML=round(CTD,1);
		document.getElementById('ctpa_result').innerHTML=round(PlotArea,1);
		document.getElementById('fd_result').innerHTML=round(dfanft2,0);
		document.getElementById('fsppc_result').innerHTML=round(dPcell,0);
		document.getElementById('tfsp_result').innerHTML=round(dPtotal,0);
		document.getElementById('pi_result').innerHTML=round(Afr/1000,3);
		var rd=mw0/Afr;
		if(rd<5.01){
			rd=round(rd,1);
			document.getElementById('rd_result').style.color='red';
			document.getElementById('rd_result').innerHTML=rd+'*';
			document.getElementById('rd_warning').innerHTML='<br>Rain Density below 5 m3/m2/hr - Contact GEA.<br><br>';
		}else if(rd>24.99){
			rd=round(rd,1);
			document.getElementById('rd_result').style.color='red';
			document.getElementById('rd_result').innerHTML=rd+'*';
			document.getElementById('rd_warning').innerHTML='<br>Rain Density above 25 m3/m2/hr  - Contact GEA.<br><br>';
		}else{
                  document.getElementById('rd_result').style.color='black';
			document.getElementById('rd_result').innerHTML=round(rd,1);
                  document.getElementById('rd_warning').innerHTML='<br>  <br><br>';

		}
	}



	function round(theValue,numDecimalPlaces){
		if(numDecimalPlaces==0){
			return Math.round(theValue);
		}else if(numDecimalPlaces==1){
			return (Math.round(theValue*10))/10;
		}else if(numDecimalPlaces==2){
			return (Math.round(theValue*100))/100;
		}else if(numDecimalPlaces==3){
			return (Math.round(theValue*1000))/1000;
		}else if(numDecimalPlaces==4){
			return (Math.round(theValue*10000))/10000;
		} else if(numDecimalPlaces==5){
			return (Math.round(theValue*100000))/100000;
		} else return theValue; 	
	}




	function validateInput(document){
		var validationErrorsExist=false;
		var message='Invalid Input\n-----------------\n';


		//bp or se
		usingElevation=document.getElementById('siteRadio').checked;
		if(!usingElevation){
			//barometric pressure selected
			//bp_input  60-110
			var bp=document.getElementById('bp_input').value;
			if(isNaN(bp)){
				validationErrorsExist=true;
				message+='Barometric pressure must be in the range 60 to 110 kPa.\n';
			}else if(bp<60||bp>110){
				validationErrorsExist=true;
				message+='Barometric pressure must be in the range 60 to 110 kPa.\n';
			}
		}else{
			//site elevation selected
			//se_input  -500 4000
			var se=document.getElementById('se_input').value;
			if(isNaN(se)){
				validationErrorsExist=true;
				message+='Site elevation must be in the range -500 to 4000 m.\n';
			}else if(se<-500||se>4000){
				validationErrorsExist=true;
				message+='Site elevation must be in the range -500 to 4000 m.\n';
			}
		}
		
		//taidb_input  taiwb-50	
		var taidb=document.getElementById('taidb_input').value;
		var taiwb=document.getElementById('taiwb_input').value;
		if(isNaN(taidb)){
			validationErrorsExist=true;
			message+='Ambient Air Dry Bulb Temperature must be in the range '+taiwb+' to ' + 50 + ' deg C.\n';
		}else if(taidb<taiwb){
			validationErrorsExist=true;
			message+='Ambient Air Dry Bulb Temperature must be in the range '+taiwb+' to ' + 50 + ' deg C.\n';
		}
 		
		//taiwb_input 0-taidb
		if(isNaN(taiwb)){
			validationErrorsExist=true;
			message+='Ambient Air Wet Bulb Temperature must be in the range 0 to ' + taidb + ' deg C.\n';
		}else if(taiwb<0||taiwb>taidb){
			validationErrorsExist=true;
			message+='Ambient Air Wet Bulb Temperature must be in the range 0 to ' + taidb + ' deg C.\n';
		}
 		
  		//twi_input= two-90
		two=document.getElementById('two_input').value;;
		twi=document.getElementById('twi_input').value;
	
		if(isNaN(twi)){
			validationErrorsExist=true;
			message+='Water Inlet Temperature must be in the range '+two+' to 90 deg C.\n';
		}else if(twi>90||twi<two){
			validationErrorsExist=true;
			message+='Water Inlet Temperature must be in the range '+two+' to 90 deg C.\n';
		}
	
		//two taiwb+1-twi
		var twoMinValue=taiwb-0+1;
		if(isNaN(two)){
			validationErrorsExist=true;
			message+='Water Outlet Temperature must be in the range '+(twoMinValue)+' to '+twi+' deg C.\n';
		}else if(two<twoMinValue||two>twi){
			validationErrorsExist=true;
			message+='Water Outlet Temperature must be in the range '+(twoMinValue)+' to '+twi+' deg C.\n';
		}		
		//va_input, between 1.8 and 2.75
  		var va=document.getElementById('va_input').value;
		if(isNaN(va)){
			validationErrorsExist=true;
			message+='Air Velocity through packing must be in the range 1.5 to 2.75 m/s.\n';
		}else if(va<1.8||va>2.75){
			validationErrorsExist=true;
			message+='Air Velocity through packing must be in the range 1.5 to 2.75 m/s.\n';
		}
		//nCells_input, 1 to 50   		
		var nCells=document.getElementById('ncells_input').value;
		if(isNaN(nCells)){
			validationErrorsExist=true;
			message+='Number of Cooling Tower Cells must be in the range 1 to 50.\n';
		}else if(nCells<1||nCells>50){
			validationErrorsExist=true;
			message+='Number of Cooling Tower Cells must be in the range 1 to 50.\n';
		}
		if(validationErrorsExist){
			alert(message);
			return false;
		}else{
			return true;
		}
	}




function doCalculate(document){

	if(!validateInput(document)) return;
	//hide input div
	hideInputDiv();
	//read parameters
	readParameters(document);

  	mw=mw0/3.6;    //convert to kg/s
	Q=mw*4.19*(Twi-Two)/1000.0;//MW
  
	//Start calcs
  	Pvx=Pvwbi(Taiwb);
  	wai0=wai(Patm,Taiwb,Taidb,Pvx);
  	rhosa=rhoai(Patm,Taidb,wai0);
  	AfrL=(mw*3.6)/25.0;
  	AfrR=(mw*3.6)/5.0;
  	counter=0;
  	

  do{
    Afr=(AfrL+AfrR)/2.0;
    counter=counter+1;
    mda=va*rhosa*Afr/(1.0+wai0);
    G=mda*3600.0/Afr;
    L=mw*3600.0/Afr;
    KaVLcorr=(0.25+0.1+C*P(L/G,n)*P(L,a1)*P(G,b1)*V);
    KaVLcalc=doKAVL(Patm,Taidb,Taiwb,Twi,Two,mda,mw);

    if (KaVLcorr<KaVLcalc){
	AfrL=Afr; 
    }else{
	AfrR=Afr;
    }
 
    if (counter>50){
      if (Math.abs(Afr-((mw*3.6)/5.0))>0.1){
        Afr=((mw*3.6)/25.0);
      }else{
        Afr=((mw*3.6)/5.0);
      }
      KaVLcalc=KaVLcorr;
    }//if
  }while(Math.abs(KaVLcorr-KaVLcalc)>0.00001);
  //Calculate the cells size 
  AfrCell=Afr/nCells;
  Wcell=Math.sqrt(AfrCell);
  Dcell=Wcell;
  CTW=0.3+nCells*(Wcell+0.3);
  CTD=Dcell+2*0.3;
  PlotArea=CTW*CTD;

  //Calculate the fan diameter 
  AfrFan=0.3*AfrCell;
  dfan=Math.sqrt(AfrFan*4.0/Math.PI);
  dfanft=dfan/0.3048;
  dfanft2=Math.floor(dfanft)+1;
  if (isOdd(dfanft2)){
	dfanft2=dfanft2+1;
  }

		

  //Calculate the pressure drop across the CT
  iai=Enthalpy(Taidb,Taiwb,Patm);
  iao=iai+mw*4.190*(Twi-Two)/mda;
  Tao=Find_Sat_Temp(Patm,iao*1000.0);
  rhoi=rhoai(Patm,Taidb,wai(Patm,Taiwb,Taidb,Pvwbi(Taiwb)));
  rhoo=rhoai(Patm,Tao,wai(Patm,Tao,Tao,Pvwbi(Tao)));
  rhom=(rhoi+rhoo)/2.0;
  G=mda*3600.0/Afr;
  L=mw*3600.0/Afr;
  Np=C2*P(L,a2)*P(G,b2);
  dpa=(14.8+Np*V)*0.5*rhom*va*va;
  
  //Calculate the fan shaft power (per cell and total)
  dPcell=va*AfrCell*dpa/0.65/1000.0; //kW
  dPtotal=dPcell*nCells;

  //rd=mw0/Afr;

  
  //populate results
  populateResults(document);
  //set results div visible
  var resultsDiv=document.getElementById('resultsDiv');
  resultsDiv.style.visibility='visible';
}



//Function to evalute a to the power b
function P(aa,bb){
  var answer = Math.pow(aa,bb);
  //alert('aa,bb='+aa +' '+bb+ ' returning '+ answer);
  return answer;
}


//Function to evaluate log10
function Log10(aa){
  //LOG10=(ln(aa))/ln(10.0);
  var answer=(Math.log(aa))/(Math.log(10.0));
  return answer;
}


function wai(Patm,Taiwb,Tai,Pvwbi){
 var answer=(2501.6-2.3263*Taiwb)/(2501.6+1.8577*Tai-4.184*Taiwb)*(0.62509*Pvwbi/((Patm*1000)-1.005*Pvwbi))
      -(1.00416*(Tai-Taiwb)/(2501.6+1.8577*Tai-4.184*Taiwb));
 return answer;
}



function rhoai(Patm,Tai,wai){
//alert(Patm+' '+Tai+' '+wai);
 var answer=(Patm*1000)/(287.08*(273.15+Tai))*(1.0+wai)*(1.0-wai/(wai+0.62198));
 return answer;
}


function isEven(x) { return (x%2)?false:true; }
function isOdd(x) { return !isEven(x); } 


function Pvwbi(Taiwb){
 a=10.79586;
 b=5.02808;
 c=0.000150474;
 d=-8.29692;
 e=0.00042873;
 f=4.76955;
 g=2.786118312;
 j=273.16/(273.15+Taiwb);
 z=a*(1.0-j)+b*Log10(j)+e*(P(10.0,(f*(1.0-j)))-1.0)+g+c*(1.0-P(10.0,(d*((273.15+Taiwb)/273.16-1.0))));
 answer=P(10.0,z);
 return answer;
}


function Enthalpy(t1,t2,Patm){
  cpvx=1890.0;
  cpax=1006.0;
  Pvx=Pvwbi(t2);
  wa=wai(Patm,t2,t1,Pvx);
  ivap=wa*(2501.6+((cpvx*t1)/(1000.0)));
  ida=(cpax*t1)/1000.0;
  var theAnswer=ida+ivap;
  return theAnswer;
}

function doKAVL(Patm,Taidb,Taiwb,Twi,Two,mda,mwi){
  iai=Enthalpy(Taidb,Taiwb,Patm);
  cp=4190.0/1000.0;
  //term 1
  T=Two+0.1*(Twi-Two);
  iL1=Enthalpy(T,T,Patm);
  iG1=iai+0.1*cp*(Twi-Two)*(mwi/mda);
  term1=1.0/(iL1-iG1);
  //term 2
  T=Two+0.4*(Twi-Two);
  iL2=Enthalpy(T,T,Patm);
  iG2=iai+0.4*cp*(Twi-Two)*(mwi/mda);
  term2=1.0/(iL2-iG2);
  //term 3
  T=Two+0.6*(Twi-Two);
  iL3=Enthalpy(T,T,Patm);
  iG3=iai+0.6*cp*(Twi-Two)*(mwi/mda);
  term3=1.0/(iL3-iG3);
  //term 4
  T=Two+0.9*(Twi-Two);
  iL4=Enthalpy(T,T,Patm);
  iG4=iai+0.9*cp*(Twi-Two)*(mwi/mda);
  term4=1.0/(iL4-iG4);
  KAVL=0.25*(Twi-Two)*(term1+term2+term3+term4)*cp;
  return KAVL;
}


function Find_Sat_Temp(Patm,ia){
  TR=100.0;
  TL=0.0;
  counter=0;
  do{
    T=(TL+TR)/2.0;
    counter=counter+1;
    iacalc=Enthalpy(T,T,Patm)*1000.0;
    if (iacalc<ia){
	TL=T;
    }else{
	TR=T;
    }
    if (counter>50){
      T=0.0;
      iacalc=ia;// Exit from the loop 
    }
  }while(Math.abs(ia-iacalc)>0.00001);
  //alert('counter'+counter);
  var theAnswer=T;
  return theAnswer;
}



