// 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;
		}
	}
}


function calculateShawlOddShape(thisform)
{
	with (thisform)
	{
		if( fixedStitches.value==null || fixedStitches.value=="" || rows.value==null || rows.value=="" || totalyarn.value==null || totalyarn.value=="" || usedyarn.value==null || usedyarn.value=="" || rowStep.value==null | rowStep.value=="" )
		{
			totalRows.value = null;
			totalRows.value = 'Er.';
			return false;
		}
		else
		{
			totalRows.value = null;
			//fixedSts = 33 for ogee shawl
			// rowStep = 2.4 ((18sts * 4) / 30rows))
			yarnFraction = parseInt(usedyarn.value) / parseInt(totalyarn.value);
			if (yarnFraction < 0.2)
			{
				totalRows.value = null;
				totalRows.value = 'Er2';
				alert("Please use 20% or more of your yarn before calculating.");
				return false;
			}
			incRate = parseFloat(rowStep.value);
			currentRow = 0;
			stsPerRow = parseFloat(fixedStitches.value);
			currentArea = parseFloat(fixedStitches.value);
			while ( parseInt(rows.value) > currentRow )
			{
				stsPerRow = stsPerRow + incRate;
				currentArea = currentArea + stsPerRow;
				currentRow++
			}
				
			maxRows = currentRow;
			totalRows.value = maxRows;
			totalStsPerRow.value = stsPerRow;
				
			maxArea = currentArea /  yarnFraction;
			while( currentArea < maxArea)
			{
				stsPerRow = stsPerRow + incRate;
				currentArea = currentArea + stsPerRow;
				currentRow++
			}
			maxRows = currentRow;
			totalRows.value = maxRows;
			totalStsPerRow.value = stsPerRow;
			return true;
		}
	}
}
