// Copyright Bex Hopins 2010
// Contact bex@colourfuldesigns.co.uk if you would like to use this script elsewhere, do not steal it!

function calculateShawlTriangular(thisform)
{
	with (thisform)
	{
		if( fixedStitches.value==null || fixedStitches.value=="" || rows.value==null || rows.value=="" || totalyarn.value==null || totalyarn.value=="" || usedyarn.value==null || usedyarn.value=="")
		{
			totalRows.value = null;
			return false;
		}
		else
		{
			//fixedStitches = 5; // border sts + centre st
			rowStep = 1; // 4 inc over 2 rows (4 /2 /2 = 1)
			yarnFraction = usedyarn.value / totalyarn.value;
			if (yarnFraction < 0.2)
			{
				alert("Please use 20% or more of your yarn before calculating.");
				totalRows.value = null;
				return false;
			}
			currentArea = rowStep * rows.value * ( parseInt(rows.value) + 1 + parseInt(fixedStitches.value));
			maxArea = currentArea /  yarnFraction;
			
			maxRows = rows.value;
			while( (rowStep *  maxRows * (parseInt(maxRows) + 1 + parseInt(fixedStitches.value) )) < maxArea)
			{
				maxRows++;
			}
			maxRows = maxRows - 1;
			totalRows.value = maxRows;
			return true;
		}
	}
}
