//						 PARAMETERS
//	op		operation sign:	'a'(add),		's'(subtract),
//							'm'(multiply),	'd'(divide)
//	bkgclr	background color: 'rrggbb'
//	ttlclr	title line color: 'rrggbb'
//	form	options selection form object: document.formname	

function startSumPlus(op, bkgclr, ttlclr, form)
{
	var	htmlString = "";
	
	//header and title lines
	htmlString += "<HTML>";
	htmlString += "<HEAD>";
	htmlString += "<TITLE>SumPlus Exercise Sheet</TITLE>";
//	htmlString += "<script language='javascript' src='sumplus.js'></script>";
	htmlString += "</HEAD>";
	htmlString += "<BODY bgcolor='#" + bkgclr + "'>";
	htmlString += "<center>";
	htmlString += "<font size='4' face='Arial, Helvetica' color='#" + ttlclr + "'><b>";
	htmlString += op == 's' ? "Subtracting" :
				  op == 'm' ? "Multiplying" :
				  op == 'd' ? "Dividing" :
				  			  "Adding" ;
	htmlString += "<br><hr width='400'><br>";

	//applet (sumplus.class)
	var cols =	(parseInt(form.max1.value, 10) >= 10000 || parseInt(form.max2.value, 10) >= 10000)? 2 : 
				(parseInt(form.max1.value, 10) >= 1000  || parseInt(form.max2.value, 10) >= 1000)? 3 : 4;
	var n = form.neqs.value;
	var m = n % cols;
	if(m > 0) n -= m;
	var rows = n / cols;
	if(m > 0) rows++;
	var ht = rows * (rows <= 1 ? 20 : rows <= 3 ? 35 : (rows >= 4 && rows <= 8) ? 40 : 45);
	htmlString += "<APPLET code='sumplus.class' width=500 height=" + ht + ">";
	htmlString += "<param name='oper' value='" + op + "'>";
	htmlString += "<param name='bkgcolor' value='" + bkgclr + "'>";
	htmlString += "<param name='nEquations' value='" + form.neqs.value + "'>";
	htmlString += "<param name='max1' value='" + form.max1.value + "'>";
	htmlString += "<param name='max2' value='" + form.max2.value + "'>";
	htmlString += "<param name='min1' value='" + form.min1.value + "'>";
	htmlString += "<param name='min2' value='" + form.min2.value + "'>";
	if(op == 'a')
		htmlString += "<param name='maxResult' value='" + form.maxResult.value + "'>";
	else
		htmlString += "<param name='maxResult' value='0'>";
	if(op == 's')
		htmlString += "<param name='allowNegative' value='" + (form.aNeg.checked ? 1 : 0) + "'>";
	else
		htmlString += "<param name='allowNegative' value='0'>";
	htmlString += "<param name='allowRepeatEqs' value='" + (form.aRep.checked ? 1 : 0) + "'>";
	if(op == 'd')
		htmlString += "<param name='DecimalPt' value='" + form.decdigs.value + "'>";
	else
		htmlString += "<param name='DecimalPt' value='0'>";
	htmlString += "</APPLET>";

	//utility buttons
//	htmlString += "<form name='reset'><font size='2'>";
//	htmlString += "<input type='button' name='home' value='Home' onclick='back();'>";
//	htmlString += "<input type='button' name='again' value='Again' onclick='.'>";
//	htmlString += "</font></form>";

	htmlString += "</b></font></center>";
	htmlString += "</BODY>";
	htmlString += "</HTML>";

	//write HTML document (to wframe)
	parent.wframe.document.open();
	parent.wframe.document.write(htmlString);
	parent.wframe.document.close();

//	document.open();
//	document.write(htmlString);
//	document.close();
}

function checkNumberOfEquations(form)
{
	if(parseInt(form.max1.value, 10) <= 10 && parseInt(form.max2.value, 10) <= 10)
	{
		if(!form.aRep.checked && parseInt(form.neqs.value, 10) > 12)
		{
			alert("Due to the value you just changed, the number of equations per page will be set to 12 only.");
			form.neqs.value = 12;
		}
		else
		{
			if(parseInt(form.maxResult.value, 10) <= 5 && parseInt(form.neqs.value, 10) > 5)
			{
				alert("Due to the value you just changed, the number of equations per page will be set to 5 only.");
				form.neqs.value = 5;
			}
			if(parseInt(form.maxResult.value, 10) <= 10 && parseInt(form.neqs.value, 10) > 10)
			{
				alert("Due to the value you just changed, the number of equations per page will be set to 10 only.");
				form.neqs.value = 10;
			}
			if(parseInt(form.neqs.value, 10) > 30)
			{
				alert("Due to the value you just changed, the number of equations per page will be set to 30 only.");
				form.neqs.value = 30;
			}
		}
	}

	if(parseInt(form.neqs.value, 10) > 50)
	{
		alert("Value overflow: the max number of equations per page is 50.");
		form.neqs.value = 50;
	}
}

function checkMinimumValue(form)
{
	if(parseInt(form.min1.value, 10) <= 0)
	{
		alert("Minimum value should be 1.");
		form.min1.value = 1;
	}
	if(parseInt(form.min2.value, 10) <= 0)
	{
		alert("Minimum value should be 1.");
		form.min2.value = 1;
	}

	if((parseInt(form.max1.value, 10) - parseInt(form.min1.value, 10)) < 5)
	{
		alert("The difference between A-Max and A-Min shouldn't be less than 5.");
		form.min1.value = parseInt(form.max1.value, 10) - 5;
	}

	if((parseInt(form.max2.value, 10) - parseInt(form.min2.value, 10)) < 5)
	{
		alert("The difference between B-Max and B-Min shouldn't be less than 5.");
		form.min2.value = parseInt(form.max2.value, 10) - 5;
	}
}

function checkMaximumValue(form)
{
	if(parseInt(form.max1.value, 10) < 10)
	{
		alert("Value underflow: the maximum of A-max is 10.");
		form.max1.value = 10;
	}

	if(parseInt(form.max2.value, 10) < 10)
	{
		alert("Value underflow: the maximum of B-max is 10.");
		form.max2.value = 10;
	}

	if(parseInt(form.max1.value, 10) > 10000)
	{
		alert("Value overflow: the maximum of A-max is 10000.");
		form.max1.value = 10000;
	}
	if(parseInt(form.max2.value, 10) > 10000)
	{
		alert("Value overflow: the maximum of B-max is 10000.");
		form.max2.value = 10000;
	}

	if((parseInt(form.max1.value, 10) - parseInt(form.min1.value, 10)) < 5)
	{
		alert("The difference between A-max and A-min shouldn't be less than 5.");
		form.max1.value = parseInt(form.min1.value, 10) + 5;
	}

	if((parseInt(form.max2.value, 10) - parseInt(form.min2.value, 10)) < 5)
	{
		alert("The difference between B-max and B-min shouldn't be less than 5.");
		form.max2.value = parseInt(form.min2.value, 10) + 5;
	}
}

function checkMaximumResult(form)
{
	var result = parseInt(form.max1.value, 10);
	if(result < parseInt(form.max2.value, 10)) result = parseInt(form.max2.value, 10);
	if(result > parseInt(form.maxResult.value, 10))
	{
		alert("WARNING ---\n\nDue to the current setting of A-max and B-max, we suggest you to change the R-max value to at least " + result + ".");
//		form.maxResult.value = result;
	}

	if(parseInt(form.maxResult.value, 10) < 5)
	{
		alert("Value underflow: the minimum of R-Max is 5.");
		form.maxResult.value = 5;
	}

	checkNumberOfEquations(form);
}