
	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 noiseLevelDisplay;


	function readParameters(document){
		exhaustSteamFlow=round(document.getElementById('esf_input').value,1);
		exhaustSteamPressure=round(document.getElementById('esp_input').value,3);
		exhaustSteamQuality=round(document.getElementById('esq_input').value,1);
		airInletTemperature=round(document.getElementById('ait_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;
		}
		noiseLevel=document.getElementById('nl_input').value;
		if(noiseLevel=='1'){
			noiseLevelDisplay='Standard';			
			vFace=2.8;			
		}else if(noiseLevel=='2'){
			noiseLevelDisplay='Low Noise';		
			vFace=2.2;		
		}else if(noiseLevel=='3'){
			noiseLevelDisplay='Ultra Low Noise';			
			vFace=1.8;			
		} 
		exhaustSteamFlow-=0;
		exhaustSteamPressure-=0;
		exhaustSteamQuality-=0;
		airInletTemperature-=0;	
		barometricPressure-=0;
		
	}


	function doConfigurationCalculation(){
		var numAframes=1;
		var numBundlesPerAframe=new Array(6);
		numBundlesPerAframe[0]=24;
		numBundlesPerAframe[1]=30;
		numBundlesPerAframe[2]=32;
		numBundlesPerAframe[3]=36;
		numBundlesPerAframe[4]=40;
		numBundlesPerAframe[5]=48;
		while(true){
			for(var a=0;a<6;a++){
				var numBundlesTried=numBundlesPerAframe[a];
					for(var bundleLength=10;bundleLength>6.5;bundleLength-=0.5){
						for(var bundleWidth=2;bundleWidth<3.1;bundleWidth+=0.1){		
							var thisAface=numAframes*numBundlesTried*bundleLength*bundleWidth;
							if(thisAface>AFace){
								//result found
								numAframesRequired=numAframes;
								numBundlesPerAFrameRequired=numBundlesTried;
								requiredBundleWidth=bundleWidth;
								requiredBundleLength=bundleLength;
								if(numBundlesTried==24) numFanModulesPerAframeRequired=4;
								if(numBundlesTried==30) numFanModulesPerAframeRequired=5;
								if(numBundlesTried==36) numFanModulesPerAframeRequired=6;
								if(numBundlesTried==32) numFanModulesPerAframeRequired=4;
								if(numBundlesTried==40) numFanModulesPerAframeRequired=5;
								if(numBundlesTried==48) numFanModulesPerAframeRequired=6;
								numBundlesPerModule=numBundlesTried/numFanModulesPerAframeRequired;
								numFanModulesRequired=numFanModulesPerAframeRequired*numAframes;								
								walkWayWidth=0.8;
								ACCwidth=(numAframes+1)*walkWayWidth+numAframes*(bundleLength+0.7);
								ACClength=(bundleWidth+0.075)*(numBundlesPerModule/2)*numFanModulesPerAframeRequired;							
								plotArea=ACCwidth*ACClength;
								fanArea=(AFace/numFanModulesRequired)*0.3;
								for(var fanDiameter=10;fanDiameter<34;fanDiameter+=2){
									var thisFanArea=Math.PI*0.25*Math.pow(fanDiameter*0.3048,2);
									if(thisFanArea>fanArea) {
										fanDiameterRequired=fanDiameter;
										break;
									}
								}
								fanDepth=Math.min(numFanModulesPerAframeRequired,numAframesRequired);
								X=-Math.log(0.985-0.975);
								if(fanDepth==1){
									inletHeight=X*fanDiameterRequired*0.3048/4.882;
								}else{
									inletHeight=6.35*fanDiameterRequired*0.3048*X/(1+(45/fanDepth));
								}
								return;
							}
						}
					}
			}
		numAframes++;
		}
	}


	function doCalculate(document){
		if(!validateInput(document)) return;
		//hide input div
		hideInputDiv();
		//read parameters
		readParameters(document);
		//now perform sizing calculations	
		//calculate area
		tSteam = (5149.6889628 / (Math.log(102047284300 / (exhaustSteamPressure*100000)))) - 273.15;
    		airDensity = (barometricPressure*1000) / (287.08 * (airInletTemperature + 273.15));
		cpAir = 1045.356 - 
				(0.3161783 * (273.15 + airInletTemperature))+
				(0.0007083814 * Math.pow((273.15 + airInletTemperature) , 2)) -
				(0.0000002703119 * Math.pow((273.15 + airInletTemperature),  3));
		UA_AFace= 2680.0 * Math.pow(vFace, 0.4);      /* per m2 frontal area */
		hfg = 3483181.4 - 
			5862.7703 * (tSteam + 273.15) +
			12.139568 * Math.pow((tSteam + 273.15),2)  -
		 	0.0140290431 * Math.pow((tSteam + 273.15) , 3);
		Q=hfg*(exhaustSteamQuality/100)*exhaustSteamFlow*1000/3600;
 		NTU = UA_AFace/ (airDensity * vFace * cpAir);                 	
		eta=1-Math.exp(-NTU);
		AFace=Q/(eta*cpAir*airDensity*vFace*(tSteam-airInletTemperature));    /* this is the answer */
		//calculate fan power
		airOutletTemperature= airInletTemperature + Q / cpAir / (AFace * vFace * airDensity);
  		Kdp = 67.0 - 12.0 * vFace + 0.52 * Math.exp(vFace);
  		dp = Kdp * 0.5 * airDensity * Math.pow(vFace,2);
  		Pfan = dp * (vFace * AFace) / etaFan;
		doConfigurationCalculation();
		budgetPrice=AFace*0.001; 
		//dumpParameters(document);
		//populate results
		populateResults(document);
		//set results div visible
		var resultsDiv=document.getElementById('resultsDiv');
		resultsDiv.style.visibility='visible';
	}



	function populateResults(document){
		//design requirements
		document.getElementById('esf_result').innerHTML=round(exhaustSteamFlow,5);
		document.getElementById('esp_result').innerHTML=round(exhaustSteamPressure,5);
		document.getElementById('esq_result').innerHTML=round(exhaustSteamQuality,5);
		document.getElementById('ait_result').innerHTML=round(airInletTemperature,5);
		document.getElementById('bp_result').innerHTML=round(barometricPressure,5);
		document.getElementById('nl_result').innerHTML=noiseLevelDisplay;
		//results
		document.getElementById('ct_result').innerHTML=round(tSteam,1);
		document.getElementById('hl_result').innerHTML=round((Q/1000000),1);		
		document.getElementById('nfm_result').innerHTML=round(numFanModulesRequired,0);
		document.getElementById('accw_result').innerHTML=round(ACCwidth,2);
		document.getElementById('accl_result').innerHTML=round(ACClength,2);
		document.getElementById('accpa_result').innerHTML=round(ACClength*ACCwidth,0);
		document.getElementById('naf_result').innerHTML=round(numAframesRequired,0);
		document.getElementById('ih_result').innerHTML=round(inletHeight,1);
		document.getElementById('fd_result').innerHTML=round(fanDiameterRequired,0);
		document.getElementById('fspt_result').innerHTML=round((Pfan/1000),0);
		document.getElementById('fsppf_result').innerHTML=round((Pfan/1000/numFanModulesRequired),1);
		document.getElementById('price_result').innerHTML=round((budgetPrice),3);

	}


	function dumpParameters(document){
		var buffer='';
		buffer+='vFace='+vFace+'\r\n';
		buffer+='tSteam='+tSteam+'\r\n'; 								
		buffer+='airDensity='+airDensity+'\r\n';
		buffer+='cpAir='+cpAir+'\r\n';
		buffer+='UA_AFace='+UA_AFace+'\r\n';
		buffer+='hfg='+hfg+'\r\n';
		buffer+='Kdp='+Kdp+'\r\n';
		buffer+='dp='+dp+'\r\n';
		buffer+='Q='+Q+'\r\n';									
		buffer+='NTU='+NTU+'\r\n';
		buffer+='eta='+eta+'\r\n';
		buffer+='AFace='+AFace+'\r\n';
		buffer+='airOutletTemperature='+airOutletTemperature+'\r\n';
		buffer+='etaFan='+etaFan+'\r\n';
		buffer+='Pfan='+	Pfan/1000+'\r\n';																	
		buffer+='\r\n'
		buffer+='numAframesRequired='+numAframesRequired+'\r\n';				
		buffer+='numBundlesPerAFrameRequired='+numBundlesPerAFrameRequired+'\r\n';
		buffer+='requiredBundleWidth='+requiredBundleWidth+'\r\n';
		buffer+='requiredBundleLength='+requiredBundleLength+'\r\n';
		buffer+='numFanModulesRequired='+numFanModulesRequired+'\r\n';		
		buffer+='numFanModulesPerAframeRequired='+numFanModulesPerAframeRequired+'\r\n';
		buffer+='numBundlesPerModule='+numBundlesPerModule+'\r\n';
		buffer+='ACCwidth='+ACCwidth+'\r\n';							
		buffer+='ACClength='+ACClength+'\r\n';							
		buffer+='walkWayWidth='+walkWayWidth+'\r\n';
		buffer+='plotArea='+plotArea+'\r\n';//display in m2 1 digits
		buffer+='fanDiameterRequired='+fanDiameterRequired+'\r\n';				
		buffer+='budgetPrice='+budgetPrice+'\r\n';					
		buffer+='inletHeight='+inletHeight+'\r\n';							
		alert(buffer);
	}
	


	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';
		//esf_input  10 - 10 000
		var esf=document.getElementById('esf_input').value;
		if(isNaN(esf)){
			validationErrorsExist=true;
			message+='Steam Flow Rate must be in the range 10 to 10 000 t/hr.\n';
		}else if(esf<10||esf>10000){
			validationErrorsExist=true;
			message+='Steam Flow Rate must be in the range 10 to 10 000 t/hr.\n';
		}
		//esq_input  85-100%
		var esq=document.getElementById('esq_input').value;
		if(isNaN(esq)){
			validationErrorsExist=true;
			message+='Steam Quality must be in the range 85 to 100 %.\n';
		}else if(esq<85||esq>100){
			validationErrorsExist=true;
			message+='Steam Quality must be in the range 85 to 100 %.\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';
			}
		}
		//esp_input   funksie -  1
		var esp=document.getElementById('esp_input').value;
		var espMinValue =  (102047284300/(Math.exp(5149.6889628/((document.getElementById('ait_input').value-0)+273.15+5.0))))/100000;
		espMinValue=round(espMinValue,3);
		if(isNaN(esp)){
			validationErrorsExist=true;
			message+='Steam Back Pressure must be in the range ' + espMinValue + ' to 1 Bar(a).\n';
		}else if(esp<espMinValue||esp>1){
			validationErrorsExist=true;
			message+='Steam Back Pressure must be in the range ' + espMinValue + ' to 1 Bar(a).\n';
		}
		//ait_input   0-funksie	
		var ait=document.getElementById('ait_input').value;
		var aitMaxValue=((5149.6889628 / (Math.log(102047284300 / ((document.getElementById('esp_input').value-0)*100000)))) - 273.15)-5;
		aitMaxValue=round(aitMaxValue,3);
		if(isNaN(ait)){
			validationErrorsExist=true;
			message+='Air Temperature must be in the range 0 to ' + aitMaxValue + ' deg C.\n';
		}else if(ait<0||ait>aitMaxValue){
			validationErrorsExist=true;
			message+='Air Temperature must be in the range 0 to ' + aitMaxValue + ' deg C.\n';
		}

		if(validationErrorsExist){
			alert(message);
			return false;
		}else{
			return true;
		}
	}



	/*
	tSteam                //0 display in 'deg C'  1 digit "Condensing Temperature"
	Q/1000000             //1 display in MW (currently in watts)  1 ditit  MW=1000000 W  - "Heat Load"
	numFanModulesRequired //2 display 0 digits
	ACCwidth              //3 display in m 2 digits
	ACClength             //4 display in m 2 digits
	numAframesRequired    //5 display 0 digits
	inletHeight           //6 display in m 2 digits
	fanDiameterRequired   //7 display in ft 0 digits
	Pfan/1000             //8 display in kW 2 digits (currently in watt) - "Fan Shaft Power (total)"		
	//new field		    //9 display in kW 2 digits (currently in watt)   "Fan Shaft Power (per Fan)"	(=Pfan/numFanModulesRequired)	
	budgetPrice           //10 display price index 3 digits "Price Index (FOB SA Port)"

	*/

