var nrWalls = 4;
function get_new_wall_html(n) {
	var txt = 'Length = <input type="text" name="wL_'+n+'" id="wL_'+n+'" value="15" size="3"/>  &nbsp; ' +
	'Height = <input type="text" name="wH_'+n+'" id="wH_'+n+'" value="9" size="3"/>  <br>';
	return txt;
}
function add_new_wall()
{
	nrWalls++;
	$('wallDimN').innerHTML += get_new_wall_html(nrWalls);
	return false;
}
function calculate()
{
	//now the big big momma
	var total = 0;
	var St = 0, Sw = 0, Sb = 0, tt = 0;


	//add the walls surface
	//check for dims: wallDimRoom and wallDimWalls
	if($('wallDimRoom').checked) {
		// add the total room surface
		St = 2 * parseInt($('rH').value) * ( parseInt($('rW').value) + parseInt($('rL').value) );
		Sb = 2 * 0.5 * ( parseInt($('rW').value) + parseInt($('rL').value));
	}
	if($('wallDimWalls').checked) {
		for(i = 1; i <= nrWalls; i++) {
			St += $('wL_'+i).value * $('wH_'+i).value;
			Sb += 0.5 * $('wL_'+i).value;
		}
	}
	//alert("st="+St);
	Sw = St; 
	//now we have: St = total surface of room
	// Sb = surface of border area
	// Sw = surface of wallpaper area
	// now define some constants:
	var npW = 180; //un-pasted wallpaper paste - 180 square feet = 4 quarts = 1 gallon
	var ppW = 360; //pre-pasted wallpaper paste - 360 square feet = 4 quarts = 1 gallon
	var ppB = 40 * 4; //un-pasted border adhesive - 40 square feet = 1 quart
	var npB = 78 * 4; //pre-pasted border adhesive - 78 square feet = 1 quart
	var prm = 360; //primer or wall prep - 360 square feet = 4 quarts = 1 gallon
	var bs = 8; //surface of 1 roll of border (in feet)
	var ws = 1; //surface of 1 roll of wallpaper ( in feet)
	if( $('isWidthSelect').checked) {
		ws = $('ws').options[$('ws').selectedIndex].value;
	} else {
		ws = $('ws2').value;
	}
	if(ws <= 0 || ws == '0' || isNaN(ws)) {
		alert("Wallpaper width must be a number and greater than zero!");
		return false;
	}
	//border
	if($('isBorder').checked) {
		s('d_br');
		$('br').innerHTML = Math.ceil(Sb / bs);
		if($('isBPrepastedYes').checked) {
			s('d_ppB');h('d_npB');
			$('ppB').innerHTML = getGallons(Sb, ppB);
		} else {
			s('d_npB');h('d_ppB');
			$('npB').innerHTML = getGallons(Sb, npB);
		}
		Sw = St - Sb; 
	} else { h('d_br'); h('d_npB'); h('ppB'); }
	//wallpaper paste
	if($('isPrepastedYes').checked) {
		s('d_ppW');h('d_npW');
		$('ppW').innerHTML = getGallons(Sw, ppW);
	} else {
		s('d_npW');h('d_ppW');
		$('npW').innerHTML = getGallons(Sw, npW);
	}
	//primer
	$('prm').innerHTML = getGallons(St, prm);
	//wallpaper rolls
	$('wr').innerHTML = Math.ceil(Sw / ws);

}

function getGallons(sqft, glns)
{
	if(!glns) glns = 360;
	var g = Math.floor(sqft / glns);
	var q = Math.ceil((sqft - g * glns) / 90.0 );
	if(q<0)	q=0;
	if(q==4) { q=0; g++; }
	var sg = '', sq = '';
	if(g > 0) sg += '<b>'+g+"</b> gallons";
	if(q > 0) sq = "<b>"+q+"</b> quarts";
	if(g > 0 && q > 0) return sg + " and " + sq;
	if(g > 0) return sg;
	if(q > 0) return sq;
	return '-'
}

function s(id) { $(id).style.display = 'block'; }
function h(id) { $(id).style.display = 'none'; }

function $(id)
{
	return document.getElementById(id);
}

function v(id)
{
	return document.getElementById(id).value;
}
