

var imperial;
var ie4 = ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4 ));

var subtotala=0;
var subtotalb=0;
var subtotalc=0;
var room_height=0;




var roomm = new Array();
roomm['00'] = '79'; 
roomm['01'] = '84';
roomm['02'] = '88';
roomm['03'] = '93';
roomm['04'] = '99';
roomm['05'] = '104';
roomm['06'] = '106';
roomm['10'] = '79';
roomm['11'] = '84';	 
roomm['12'] = '88';
roomm['13'] = '93';
roomm['14'] = '99';
roomm['15'] = '104';
roomm['16'] = '108';
roomm['20'] = '52';
roomm['21'] = '57';
roomm['22'] = '63';
roomm['23'] = '68';	 
roomm['24'] = '72';	 
roomm['25'] = '77';
roomm['26'] = '81';
roomm['30'] = '52';
roomm['31'] = '57';
roomm['32'] = '63';
roomm['33'] = '68';
roomm['34'] = '72';
roomm['35'] = '77';
roomm['36'] = '81';


var roomi = new Array();
roomi['00'] = '7.2'; 
roomi['01'] = '7.6';
roomi['02'] = '8.1';
roomi['03'] = '8.7';
roomi['04'] = '9.2';
roomi['05'] = '9.7';
roomi['06'] = '10.3';
roomi['10'] = '7.2';
roomi['11'] = '7.6';	 
roomi['12'] = '8.1';
roomi['13'] = '8.7';
roomi['14'] = '9.2';
roomi['15'] = '9.7';
roomi['16'] = '10.3';
roomi['20'] = '4.8';
roomi['21'] = '5.3';
roomi['22'] = '5.7';
roomi['23'] = '6.3';	 
roomi['24'] = '6.7';	 
roomi['25'] = '7.2';
roomi['26'] = '7.7';
roomi['30'] = '4.8';
roomi['31'] = '5.3';
roomi['32'] = '5.7';
roomi['33'] = '6.3';
roomi['34'] = '6.7';
roomi['35'] = '7.3';
roomi['36'] = '7.9';


var wallm = new Array();
wallm['00'] = '124';
wallm['01'] = '140';
wallm['02'] = '155';
wallm['03'] = '171';
wallm['04'] = '186';
wallm['05'] = '201';
wallm['06'] = '216';	 
wallm['10'] = '89';
wallm['11'] = '100';
wallm['12'] = '111';
wallm['13'] = '122';
wallm['14'] = '133';
wallm['15'] = '144';
wallm['16'] = '155';
wallm['20'] = '30';
wallm['21'] = '33';
wallm['22'] = '37';
wallm['23'] = '41';
wallm['24'] = '44';
wallm['25'] = '47';
wallm['26'] = '50';


var walli = new Array();
walli['00'] = '37';
walli['01'] = '42';
walli['02'] = '47';
walli['03'] = '51';
walli['04'] = '55';
walli['05'] = '61';
walli['06'] = '65';	 
walli['10'] = '27';
walli['11'] = '30';
walli['12'] = '33';
walli['13'] = '38';
walli['14'] = '41';
walli['15'] = '44';
walli['16'] = '48';
walli['20'] = '9.0';
walli['21'] = '10';
walli['22'] = '11';
walli['23'] = '12';
walli['24'] = '13';
walli['25'] = '14';
walli['26'] = '15';	 


var windowm = new Array();
windowm['00'] = '72';
windowm['01'] = '86';
windowm['02'] = '111';
windowm['10'] = '10';
windowm['11'] = '25';
windowm['12'] = '48';


var windowi = new Array();
windowi['00'] = '6.4';
windowi['01'] = '7.7';
windowi['02'] = '10.0';
windowi['10'] = '0.9';
windowi['11'] = '2.2';
windowi['12'] = '4.4';


var allwindowi = new Array();
allwindowi['0'] = '12.5';
allwindowi['1'] = '6.5';


var allwindowm = new Array();
allwindowm['0'] = '138';
allwindowm['1'] = '74';


var pwalli = new Array();
pwalli['0'] = '21';
pwalli['1'] = '24';
pwalli['2'] = '26';
pwalli['3'] = '29';
pwalli['4'] = '31';
pwalli['5'] = '33';
pwalli['6'] = '35';


var pwallm = new Array();
pwallm['0'] = '68';
pwallm['1'] = '78';
pwallm['2'] = '86';
pwallm['3'] = '96';
pwallm['4'] = '108';
pwallm['5'] = '117';
pwallm['6'] = '126';


var skylighti = new Array();
skylighti['0'] = '2.5';
skylighti['1'] = '8.0';


var skylightm = new Array();
skylightm['0'] = '158';
skylightm['1'] = '58';




	  
function init()
{	
	if (document.heatcalc.elements["resultboxi"])	
		imperial = true;	
	else
		imperial = false;
	for(count=1; count<11; count++)
		{
			skytypid="sky." + count + ".win_type";
			skyareaid="skylight." + count + ".win_area";
			document.getElementById(skytypid).disabled=true;
			document.getElementById(skyareaid).disabled=true;
		}		
	subtotala=0;
	subtotalb=0;
	subtotalc=0;
	room_height=0;
		
}


function calculate()
{
	floor_level = document.heatcalc.floorlevel.value;
	room_height = document.heatcalc.roomheight.value;
	floor_area = document.heatcalc.floorarea.value;

	if(!floor_level)
	{
	//	alert('not floor '+floor_level);
		floor_level=0;
	}
	if(!room_height)
	{
	//	alert('not room_height '+room_height);
		room_height=0;
	}
	if(!floor_area)
	{
	//	alert('not floor_area '+floor_area);
		floor_area=0;
	}
	
	
	subtotala = step1();
	//alert('get levels '+floor_level+' floor_level '+document.getElementById('floorlevel').value);
	
	if (document.heatcalc.singlestorey.checked)		
		{	
			//alert('singlestorey '+subtotala);
			subtotala = subtotala * 1.1;	
		}
	if (document.heatcalc.ceilingnoinsul.checked)
		subtotala = subtotala * 2;
	if (document.heatcalc.insulfloor.checked)
		subtotala = subtotala * 0.78;
		
		
	//alert('after fs '+subtotala);
	subtotalb = step2();
	//alert('subtotalb '+subtotalb);
	subtotalc = step3();
	//alert('subtotalc '+subtotalc);
	if (document.heatcalc.internalfan.checked)
		result = subtotala * 2;
	else if (document.heatcalc.winwall.checked)
		result = subtotala + subtotalc;
	else	
		result = subtotala+subtotalb+subtotalc;
	adjust = cooler(result);
	adjust = exposed(adjust);
	if (document.heatcalc.vsky.checked)
	{
		sum_sky = sumskylight();
		adjust += sum_sky;
	}
	if (imperial)
		document.heatcalc.resultboxi.value = Math.round(adjust);
	else
		document.heatcalc.resultboxm.value = Math.round(adjust);
}


function step1()
{
	//alert('into step1 floor_level   '+floor_level+'  room_height  '+room_height);
	var roomel=floor_level.toString(10)+room_height.toString(10);
	//alert('into roomel   '+floor_level+'  room_height  '+roomel);
	
	//alert('into step1 imp   '+imperial+'  roomi[floor_level + room_height]  '+roomi[roomel]);
	if (imperial)
	{
		room_tabval = roomi[floor_level.toString(10)+room_height.toString(10)];
		subtotala = room_tabval * floor_area;
	//	alert('into step1 room_tabval   '+room_tabval+'  floor_area  '+floor_area);
	//	alert('into step1 room_tabval   '+room_tabval+'  subtotala   '+subtotala);
	}
	else
	{
		room_tabval = roomm[floor_level.toString(10)+room_height.toString(10)];
		subtotala = room_tabval * floor_area;	
	}
	//alert('into step1 room_tabval end step1   '+room_tabval+'  subtotala   '+subtotala);
	
	return subtotala;	
}

function step2()
{
	subtotalb=0;
	for(count=1; count<11; count++)
	{
		walltypid="wall." + count + ".type";
		walllenid="wall." + count + ".wall_length";
		
		wtyid=document.getElementById(walltypid).value;
		
		if(!wtyid)
		{
			wtyid=0;
		}
		wtcheck=wtyid.toString(10)+room_height.toString(10);
		//alert('wtcheck  '+wtcheck+' room_height '+room_height+' wtyid '+wtyid);
	
		if (imperial)
		{
			wall_tabval = walli[wtcheck];
			temp_b = wall_tabval * document.getElementById(walllenid).value;		
		}
		else
		{
			wall_tabval = wallm[wtcheck];
			temp_b = wall_tabval * document.getElementById(walllenid).value;
		}
		subtotalb += temp_b;
	}
	//alert('subtotalb  '+subtotalb);
	
	return subtotalb;

}


function step3()
{
	subtotalc=0;
	for(count=1; count<11; count++)
	{
		walltypid="wall." + count + ".type";
		wintypid="wall." + count + ".win_type";
		winareaid="wall." + count + ".win_area";
		
		watyid=document.getElementById(walltypid).value;
		wintyid=document.getElementById(wintypid).value;
		
		if(!watyid)
		{
			watyid=0;
		}
		if(!watyid)
		{
			wintyid=0;
		}
		wincheck=wintyid.toString(10)+watyid.toString(10);
		
		//alert('wincheck  '+wincheck+' wintyid '+wintyid+' watyid '+watyid);
	
		
		if (imperial)
		{
			if (document.heatcalc.winwall.checked)
			{
				win_tabval = allwindowi[document.getElementById(wintypid).value];
				temp_c = win_tabval * document.getElementById(winareaid).value;
			}
			else
			{
				win_tabval = windowi[wincheck];
				temp_c = win_tabval * document.getElementById(winareaid).value;		
			}
		}
		else
		{
			if (document.heatcalc.winwall.checked)
			{
				win_tabval = allwindowm[document.getElementById(wintypid).value];
				temp_c = win_tabval * document.getElementById(winareaid).value;
			}
			else
			{
				win_tabval = windowm[wincheck];
				temp_c = win_tabval * document.getElementById(winareaid).value;
			}
		}
		subtotalc += temp_c;
	}
	//alert('subtotalc  '+subtotalc);
	
	return subtotalc;

}



function sumpwall()
{
	var tot_pwall_len = 0;
	for(count=1; count<11; count++)
	{
		pwallid="wall." + count + ".internal";
		if (imperial)
		{
			pwall_tabval = pwalli[room_height];
			temp_pwall = pwall_tabval * document.getElementById(pwallid).value;		
		}
		else
		{
			pwall_tabval = pwallm[room_height];
			temp_pwall = pwall_tabval * document.getElementById(pwallid).value;
		}
		tot_pwall_len += temp_pwall;		
	}
	return tot_pwall_len;	
}



function sumskylight()
{
	var tot_skylight = 0;
	for(count=1; count<11; count++)
	{
		skytypeid="sky." + count + ".win_type";
		skylightid="skylight." + count + ".win_area";
		if (imperial)
		{
			skylight_tabval = skylighti[document.getElementById(skytypeid).value];
			temp_skyarea = skylight_tabval * document.getElementById(skylightid).value;		
		}
		else
		{
			skylight_tabval = skylightm[document.getElementById(skytypeid).value];
			temp_skyarea = skylight_tabval * document.getElementById(skylightid).value;
		}
		tot_skylight += temp_skyarea;		
	}	
	return tot_skylight;	
}


function reset()
{
	init();
	document.heatcalc.reset();
}



function cooler(d)
{
	if (document.heatcalc.cooler.checked)
		adjust = d * document.heatcalc.cooler.value;
	else
		adjust = d;
	return adjust;
}


function exposed(d)
{
	if (document.heatcalc.exposed.checked)
		adjust = d * document.heatcalc.exposed.value;
	else
		adjust = d;
	return adjust;

}



function internalroom()
{
	if (document.heatcalc.internalfan.checked)
		for(count=1; count<11; count++)
		{
			walltypid="wall." + count + ".type";
			walllenid="wall." + count + ".wall_length";
			wintypid="wall." + count + ".win_type";
			winareaid="wall." + count + ".win_area";
			document.getElementById(walltypid).disabled=true;
			document.getElementById(walllenid).disabled=true;
			document.getElementById(wintypid).disabled=true;
			document.getElementById(winareaid).disabled=true;
		}
	else
		for(count=1; count<11; count++)
		{
			walltypid="wall." + count + ".type";
			walllenid="wall." + count + ".wall_length";
			wintypid="wall." + count + ".win_type";
			winareaid="wall." + count + ".win_area";
			document.getElementById(walltypid).disabled=false;
			document.getElementById(walllenid).disabled=false;
			document.getElementById(wintypid).disabled=false;
			document.getElementById(winareaid).disabled=false;
		}
}


function allWindow()
{
	if (document.heatcalc.winwall.checked)
	{
		document.heatcalc.internalfan.checked=false;
		internalroom();
		for(count=1; count<11; count++)
		{
			walllenid="wall." + count + ".wall_length";
			walltypid="wall." + count + ".type";
			document.getElementById(walltypid).disabled=true;
			document.getElementById(walllenid).disabled=true;					
		}
	}
	else
		for(count=1; count<11; count++)
		{
			walllenid="wall." + count + ".wall_length";
			walltypid="wall." + count + ".type";
			document.getElementById(walltypid).disabled=false;
			document.getElementById(walllenid).disabled=false;					
		}
}


function pwall()
{
	if (document.heatcalc.partywall.checked)
		for(count=1; count<11; count++)
		{
			inwalllenid="wall." + count + ".internal";
			document.getElementById(inwalllenid).disabled=false;
		}
	else
		for(count=1; count<11; count++)
		{
			inwalllenid="wall." + count + ".internal";
			document.getElementById(inwalllenid).disabled=true;
		}
	
}


function vskylight()
{
	
	if (document.heatcalc.vsky.checked)
		for(count=1; count<11; count++)
		{
			skytypid="sky." + count + ".win_type";
			skyareaid="skylight." + count + ".win_area";
			document.getElementById(skytypid).disabled=false;
			document.getElementById(skyareaid).disabled=false;
		}
	else
		for(count=1; count<11; count++)
		{
			skytypid="sky." + count + ".win_type";
			skyareaid="skylight." + count + ".win_area";
			document.getElementById(skytypid).disabled=true;
			document.getElementById(skyareaid).disabled=true;						
		}
}


function NewWallType(prefix,idx)
	 {
	 	document.getElementById(prefix+'.'+idx+'.line').style.display='inline';
//alert(prefix+'.'+idx+'.line');
//alert(document.getElementById(prefix+'.'+idx+'.line').style.display);
	 	document.getElementById(prefix+'.'+idx+'.new').style.display='none';
		idx++;
		if (idx <= 10)
	 		document.getElementById(prefix+'.'+idx+'.new').style.display='inline';
//alert(document.getElementById('new_2').style.display);
	 }





