
	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;



	function readParameters(document){

		Ta=round(document.getElementById('tai_input').value,1)-0;
		Ti=round(document.getElementById('tpi_input').value,1)-0;
		Tr=round(document.getElementById('tpo_input').value,1)-0;
		Q=round(document.getElementById('duty_input').value,1)-0;
		
		//Alt
		Alt=round(document.getElementById('se_input').value,1);
		barometricPressure=round(document.getElementById('bp_input').value,1);
		if(document.getElementById('siteRadio').checked){
			//site elevation checked
			usingElevation=true; 
		}else if (document.getElementById('barRadio').checked){
			//barometric pressure checked
			usingElevation=false;
		}

	 	//Service
		Service=document.getElementById('desc_input').value;
		if(Service=='1'){
			descDisplay='Hydrocarbon Cooling';
			Hi = 800;
			Fi = 0.00018;			
		}else if(Service=='2'){
			descDisplay='Hydrocarbon Condensing';
			Hi = 1100;   //900
			Fi = 0;		
		}else if(Service=='3'){
			descDisplay='Water Cooling';
			Hi = 3000;
			Fi = 0.0002;			
		}else if(Service=='4'){
			descDisplay='Water Condensing';
			Hi = 7000;    //5000
			Fi = 0;		
		}else if(Service=='5'){
			descDisplay='Viscous Fluid Cooling';
			Hi = 300;
			Fi = 0.00025;			
		}  
		
	}





	function calculate(){

	//Find face velocity
	Via = 3;         //Guess inlet face velocity m/s
	PE = 2;          //pressure error, Pa
	PTol = 0.001;    //pressure tolerance, Pa
	DPaGoal = 160;   //Pa
	while (PE > PTol){
  		//Find airside detail
  		Ho = (108.46 * Via + 564.05);  //bare area W/m2K - 4 rows
  		//Pa = (-11 * Alt + 101000);   //Atmospheric pressure, Pa
            Pa=barometricPressure*1000;
 	 	RhoAir = Pa / (287.08 * (273.15 + Ta));  
  		//Improve exhaust air temperature
  		Teg = Ta + 0.1;  //Guessed exhaust temperature
  		TE = 20;        //Exhaust air temp error, deg C
  		TTol = 0.0001;  //Exhaust air temp tolerance, �C
    		while (TE > TTol){
    			U = 1 / (1 / Hi + 1 / Ho + 0.00018 + Fi);
    			LMTD = ((Ti - Teg) - (Tr - Ta)) / Math.log((Ti - Teg) / (Tr - Ta));
    			BareArea = 1000000 * Q / (LMTD * 0.8 * U);
    			TotalBundleFaceArea = BareArea * 0.206;   //four tube rows assumed
    			QAir = TotalBundleFaceArea * Via;         //m3/s
    			MAir = QAir * RhoAir;                     //kg/s
    			Tec = Ta + Q * 1000000 / (MAir * 1006);  	//Calculated exhaust temperature
    			TE = Math.abs(Tec - Teg);                      //New error
    			Teg = 0.5 * Teg + 0.5 * Tec;
  		}// while TE > TTol
  
 		 RhoAirAve = Pa / (287.08 * (273.15 + 0.5 * Ta + 0.5 * Teg));
  		VaAve = (MAir / RhoAirAve) / TotalBundleFaceArea;
  		Kbun = 15.4*1.15;         //15.4                   //On the average air properties - 4 rows
  		Dpa = 0.5 * RhoAirAve * Kbun * Math.pow(VaAve,2);  //airside pressure drop Pa
  		Via = Via + Via * (DPaGoal - Dpa) / 1000;          //air velocity, m/s
  		PE = Math.abs(Dpa - DPaGoal);
	}//while (PE>Ptol)


	//Determine geometry
//	if(TotalBundleFaceArea>(6 * 10 * 5)){
//	 Lt = 12;//Lt = tube length
//	}else if(TotalBundleFaceArea>(6 * 8 * 4)){
//	 Lt = 10;
//	}else if(TotalBundleFaceArea>(6 * 6 * 3)){
//	 Lt = 8;
//	}else if(TotalBundleFaceArea>(4 * 4 * 2)){
//	 Lt = 6;
//	}else if(TotalBundleFaceArea>(8 * 2 * 1)){
//	 Lt = 4;
//	}else if(TotalBundleFaceArea<=(8 * 2 * 2)){
//	 Lt = 2;
//	}

        Lt = 20;
        NoBundles = 0;
        while (Lt > 12){
        NoBundles = NoBundles + 2;
        Lt = Math.pow((4 * TotalBundleFaceArea / NoBundles),0.5);
        }//while (Lt>12);

	TotalBundleFaceWidth = TotalBundleFaceArea / Lt;   //the length of the plant in the cross direction
	NoBays = round((TotalBundleFaceWidth / (Lt / 2)), 0);
	BundleWidth = (TotalBundleFaceWidth / NoBays) / 2;
	NoTubes = round((BundleWidth * 4 / 0.0635), 0);      //Per bundle (4 rows in air travel direction)
	PlotArea = (Lt + 2 * 0.75) * (BundleWidth + 0.2) * NoBays * 2; //m2

	//Fan detail
	NoFans = NoBays * 2;
	FanSize = round((0.5 * 0.85 * Lt / 0.3048), 0);  //ft
	//If (((FanSize Mod 2) > 0) And (FanSize > 14)) Then FanSize = FanSize + 1
	if (((FanSize % 2) > 0) && (FanSize > 14)){
	 FanSize = FanSize + 1
	}
	PFan = ((QAir * Dpa) / 0.65) / NoFans;



}//calculate






	function doCalculate(document){
		if(!validateInput(document)) return;
		//hide input div
		hideInputDiv();
		//read parameters
		readParameters(document);
		//now perform sizing calculations
				
		calculate();
		
		//populate results
		populateResults(document);
		//set results div visible
		var resultsDiv=document.getElementById('resultsDiv');
		resultsDiv.style.visibility='visible';
	}



	function populateResults(document){
	
		//design requirements
		document.getElementById('tai_result').innerHTML=round(Ta,5);
		document.getElementById('tpi_result').innerHTML=round(Ti,5);
		document.getElementById('tpo_result').innerHTML=round(Tr,5);
		document.getElementById('duty_result').innerHTML=round(Q,5);
		document.getElementById('bp_result').innerHTML=round(barometricPressure,5);
		document.getElementById('desc_result').innerHTML=descDisplay;

		//results
		document.getElementById('exhaustAirTemperature_result').innerHTML=round(Teg,2);
		document.getElementById('inletFaceVelocity_result').innerHTML=round(Via,2);
		document.getElementById('airsidePressureDrop_result').innerHTML=round(Dpa,2);
		document.getElementById('numBays_result').innerHTML=NoBays;
		document.getElementById('numBundles_result').innerHTML=NoBays*2;
		document.getElementById('tubelength_result').innerHTML=round(Lt,2);
		document.getElementById('tubesPerBundle_result').innerHTML=NoTubes;
		document.getElementById('numFans_result').innerHTML=NoFans;
		document.getElementById('fanSize_result').innerHTML=FanSize;
		document.getElementById('powerPerFan_result').innerHTML=round((PFan/1000),1);
		document.getElementById('bundleFaceArea_result').innerHTML=round(TotalBundleFaceArea,1);
		document.getElementById('totalPlotArea_result').innerHTML=round(PlotArea,0);
		
	}



	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 doCloseResults(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 validateInput(document){

		var validationErrorsExist=false;
		var message='Invalid Input\n-----------------\n';
		
		//tai_input (0-60)	
		var tai=document.getElementById('tai_input').value;
		if(isNaN(tai)){
			validationErrorsExist=true;
			message+='Ambient Temperature must be in the range 0 to 60 deg C.\n';
		}else if(tai<0||tai>60){
			validationErrorsExist=true;
			message+='Ambient Temperature must be in the range 0 to 60 deg C.\n';
		}
		
		//tpi_input ((tai+5)-200)	
		var tpi=document.getElementById('tpi_input').value;
		var tpiMinValue=tai-0+5;
		if(isNaN(tpi)){
			validationErrorsExist=true;
			message+='Inlet Temperature must be in the range '+tpiMinValue+' to 200 deg C.\n';
		}else if(tpi<tpiMinValue||tpi>200){
			validationErrorsExist=true;
			message+='Inlet Temperature must be in the range '+tpiMinValue+' to 200 deg C.\n';
		}	
		
		//tpo_input (tai-tpi)	
		var tpo=document.getElementById('tpo_input').value-0;
		var tpoMinValue=tai;
		var tpoMaxValue=tpi;
		if(isNaN(tpo)){
			validationErrorsExist=true;
			message+='Required Outlet Temperature must be in the range '+tpoMinValue+' to '+tpoMaxValue+' deg C.\n';
		}else if(tpo<tpoMinValue||tpo>tpoMaxValue){
			validationErrorsExist=true;
			message+='Required Outlet Temperature must be in the range '+tpoMinValue+' to '+tpoMaxValue+' deg C.\n';
		}

		//duty_input   (0-200)
		var duty=document.getElementById('duty_input').value;
		if(isNaN(duty)){
			validationErrorsExist=true;
			message+='Duty must be in the range 0 to 200 MW.\n';
		}else if(duty<0||duty>200){
			validationErrorsExist=true;
			message+='Duty must be in the range 0 to 200 MW.\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';
			}
		}
	

		if(validationErrorsExist){
			alert(message);
			return false;
		}else{
			return true;
		}
	}




