var dataloaded;
var metric = 0;
var prefix = "c1";

function showInstruct(insturl)		//shows a popup window w/Instructions
{
	var newwin = window.open("","help","scrollbars,width=540,height=420");
	newwin.location.href = insturl;
}

function loadMenus()				//reads from arrays in data file and puts items into pulldown menus
{
	var selectedprimer = 1;
	var selectedpaint = 1;
}

function changeUnits(myselectedindex)		//Set metric or English units, switch all units pulldowns
{
	metric = eval(myselectedindex);
	
	document.form1.w1widthunit.options[metric].selected = true; 
	document.form1.w2widthunit.options[metric].selected = true; 
	document.form1.rheightunit.options[metric].selected = true; 
	document.form1.trimheightunit.options[metric].selected = true; 
	document.form1.boardheightunit.options[metric].selected = true; 
	document.form1.primerUnits.options[metric].selected = true; 
	document.form1.paintUnits.options[metric].selected = true;
	document.form1.primerAreaUnits.options[metric].selected = true;
	document.form1.paintAreaUnits.options[metric].selected = true;
	calculate();
}

function checkValid()						//If any numeric fields are empty, set them to 0
{
	var w1width = document.form1.w1width.value;
	var w2width = document.form1.w2width.value;
	var rheight = document.form1.rheight.value;
	var trimheight = document.form1.trimheight.value;
	var boardheight = document.form1.boardheight.value;
	
	if ( (w1width == null) || (w1width == "") || isNaN(parseFloat(w1width)) )
	{
		document.form1.w1width.value = 0;
	 }
	
	if ( (w2width == null) || (w2width == "") || isNaN(parseFloat(w2width)) )
	{
		document.form1.w2width.value = 0;
	}
	
	if ( (rheight == null) || (rheight == "") || isNaN(parseFloat(rheight)) )
	{
		document.form1.rheight.value = 0;
	}
	
	if ( (trimheight == null) || (trimheight == "") || isNaN(parseFloat(trimheight)) )
	{
		document.form1.trimheight.value = 0;
	}
	
	if ( (boardheight == null) || (boardheight == "") || isNaN(parseFloat(boardheight)) )
	{
		document.form1.boardheight.value = 0;
	}
}

function calculate()						//Validate fields, blank out the answers field, pause before calculating result
{
	checkValid();
	
	document.form1.primerGallons.value = "";
	document.form1.paintGallons.value = "";
	document.form1.primerArea.value = "";
	document.form1.paintArea.value = "";
	
	window.setTimeout("setGallons()", 200);
}

function setGallons()						//Calculate the answer
{	
	checkValid();
	
	var w1width = document.form1.w1width.value;
	var w2width = document.form1.w2width.value;
	
	var rheight = document.form1.rheight.value;
	
	var paintArrayRaw = 1;
	if (paintArrayRaw == '*')
	{
		window.alert("You must choose a paint before calculating!");
		return;
	}
	var paintSpreadRate = paintArray[1];
	
	var primerArrayRaw =  1;
	if (primerArrayRaw == '*')
	{
		window.alert("You must choose a primer before calculating!");
		return;
	}

	var primerSpreadRate = primerArray[1];
	
	var totalWall = 2 * ((w1width * rheight) + (w2width * rheight)); 		//Basic room area
				
	var totalExtra = 0;
	
	if (document.form1.ceiling.checked == true)						//Add in ceiling
	{
		totalExtra = (w1width * w2width);
	}
	
	if (document.form1.floor.checked == true)							//Add in floor
	{
		totalExtra = totalExtra + (w1width * w2width);
	}
	
	//Add in molding and baseboards
	
	if (metric)	//If the metric units are selected, centimeters to meters (total converted to feet later)
	{
		trimheight = document.form1.trimheight.value / 100; 
	}
	else
	{
		trimheight = document.form1.trimheight.value / 12; 
	}

	if (document.form1.trim1.checked)
	{
		totalExtra = totalExtra - (2 * ((w1width * trimheight) + (w2width * trimheight))); 
	}
	
	if (document.form1.trim2.checked)
	{
		totalExtra = totalExtra + (2 * ((w1width * trimheight) + (w2width * trimheight))); 
	}
	
	if (metric)	//If the metric units are selected, centimeters to meters (total converted to feet later)
	{
		boardheight = document.form1.boardheight.value / 100; 
	}
	else
	{
		boardheight = document.form1.boardheight.value / 12; 
	}

	if (document.form1.board1.checked)
	{
		totalExtra = totalExtra - (2 * ((w1width * boardheight) + (w2width * boardheight))); 
	}
	
	if (document.form1.board2.checked)
	{
		totalExtra = totalExtra + (2 * ((w1width * boardheight) + (w2width * boardheight))); 
	}
	
	var totalSurface = totalExtra;
	if (document.form1.walls.checked)
	{
		totalSurface = totalWall + totalSurface;
	}
	
	//Get the units for the result
	
	var primerUnits = document.form1.primerUnits.options[document.form1.primerUnits.selectedIndex].value;
	var paintUnits = document.form1.paintUnits.options[document.form1.paintUnits.selectedIndex].value;
	var primerAreaUnits = document.form1.primerAreaUnits.options[document.form1.primerAreaUnits.selectedIndex].value;
	var paintAreaUnits = document.form1.paintAreaUnits.options[document.form1.paintAreaUnits.selectedIndex].value;
	
	if (metric)	//If the metric units are selected, convert square meters to square feet
	{
		totalSurface = totalSurface * 10.7639;
	}
	
	var primerCoats = document.form1.primerCoats.options[document.form1.primerCoats.selectedIndex].value;
	var paintCoats = document.form1.paintCoats.options[document.form1.paintCoats.selectedIndex].value;
	
	var paintGallons = (totalSurface / 550) * paintCoats;			//Calculate gallons required
	var primerGallons = (totalSurface / 500) * primerCoats;
	
	if (primerUnits == "gallons" )
	{
		primerraw = primerGallons;
	}
	else if (primerUnits == "liters" )
	{
		primerraw = primerGallons * 3.78531;
	}

	if (primerAreaUnits == "feet2")										//Give answer in selected units
	{
		primerrawarea = totalSurface;
	}
	else if (primerAreaUnits == "meters2" )
	{
		primerrawarea = totalSurface / 10.7639;	
	}
	
	if (paintUnits == "gallons" )
	{
		paintraw = paintGallons;
	}
	else if (paintUnits == "liters" )
	{
		paintraw = paintGallons * 3.78531;
	}

	if (paintAreaUnits == "feet2")
	{
		paintrawarea = totalSurface;
	}
	else if (paintAreaUnits == "meters2" )
	{
		paintrawarea = totalSurface / 10.7639;
	}	
	
	var primerval = roundme(primerraw);								//Round up to nearest 0.25 units
	var paintval = roundme(paintraw);
	var primerareaval = roundme(primerrawarea);
	var paintareaval = roundme(paintrawarea);
	
	document.form1.primerGallons.value = primerval;
	document.form1.paintGallons.value = paintval;
	document.form1.primerArea.value = primerareaval;
	document.form1.paintArea.value = paintareaval;
}


function roundme(decnumber)					//Round up to nearest 0.25 units
{
	var floorval = Math.floor(decnumber);
	var decimalpart = decnumber - floorval;
	
	if (decimalpart == 0)
	{
		roundedval = 0;
	}
	else if (decimalpart <= 0.25)
	{
		roundedval = 0.25;
	}
	else if (decimalpart <= 0.5)
	{
		roundedval = 0.5;
	}
	else if (decimalpart <= 0.75)
	{
		roundedval = 0.75;
	}
	else
	{
		roundedval = 1;
	}

	roundedval = roundedval + floorval;
	return roundedval;
}