//global vars defined for element values
var step, parent, count;
var aVal, bVal, cVal, xvar="x", yvar="y";

function init()
{
	aVal = new Array();
	bVal = new Array();
	cVal = new Array();
	dVal = new Array();
	eVal = new Array();
}

function init1()
{
	count = 0;
	//initializing second equation
	aVal[0] = parseFloat(document.getElementById('a').value);
	bVal[0] = parseFloat(document.getElementById('b').value);
	cVal[0] = parseFloat(document.getElementById('c').value);
	dVal[0] = 0.0;
	eVal[0] = 0.0;

	step = document.getElementById('step2');
	parent = step.lastChild.parentNode;

}

function eqAddX(num, which)
{       if (num == '+') num = 1;
	if (num == '-') num = -1;
	num *= 1;	//cast into number

	if (isNaN(num)) {
		//not a number
		alert("You must enter a number such as:\n2, -2, -4.4, etc");
		return(1);
	}

	if (num == 0.0 || num == -0.0) {
		//adding by zero
		return(1);
	}

	count++;

	aVal[count] = aVal[(count-1)] + num;
	bVal[count] = bVal[(count-1)];
	cVal[count] = cVal[(count-1)];
	dVal[count] = dVal[(count-1)] + num;
	eVal[count] = eVal[(count-1)];

	//rounding..
	aVal[count] = hundredthRound(aVal[count]);
	dVal[count] = hundredthRound(dVal[count]);
	updateSteps(count, which);
}

function eqAdd(num, which)
{       num *= 1;	//cast into number

	if (isNaN(num)) {
		//not a number
		alert("You must enter a number such as:\n2, -2, -4.4, etc");
		return(1);
	}

	if (num == 0.0 || num == -0.0) {
		//subtracting by zero
		return(1);
	}

	//ax + by + c = dx + e
	count++;

	aVal[count] = aVal[(count-1)];
	bVal[count] = bVal[(count-1)];
	cVal[count] = cVal[(count-1)] + num;
	dVal[count] = dVal[(count-1)];
	eVal[count] = eVal[(count-1)] + num;

	cVal[count] = hundredthRound(cVal[count]);
	eVal[count] = hundredthRound(eVal[count]);
	updateSteps(count, which);
}

function eqMinus(num, which)
{       num *= 1;	//cast into number

	if (isNaN(num)) {
		//not a number
		alert("You must enter a number such as:\n2, -2, -4.4, etc");
		return(1);
	}

	if (num == 0.0 || num == -0.0) {
		//subtracting by zero
		return(1);
	}

	//ax + by + c = dx + e
	count++;

	aVal[count] = aVal[(count-1)];
	bVal[count] = bVal[(count-1)];
	cVal[count] = cVal[(count-1)] - num;
	dVal[count] = dVal[(count-1)];
	eVal[count] = eVal[(count-1)] - num;

	cVal[count] = hundredthRound(cVal[count]);
	eVal[count] = hundredthRound(eVal[count]);
	updateSteps(count, which);
}

function eqMinusX(num, which)
{       if (num == '+') num = 1;
	if (num == '-') num = -1;
	num *= 1;	//cast into number

	if (isNaN(num)) {
		//not a number
		alert("You must enter a number such as:\n2, -2, -4.4, etc");
		return(1);
	}

	if (num == 0.0 || num == -0.0) {
		//subtracting by zero
		return(1);
	}

	count++;

	aVal[count] = aVal[(count-1)] - num;
	bVal[count] = bVal[(count-1)];
	cVal[count] = cVal[(count-1)];
	dVal[count] = dVal[(count-1)] - num;
	eVal[count] = eVal[(count-1)];

	aVal[count] = hundredthRound(aVal[count]);
	dVal[count] = hundredthRound(dVal[count]);
	updateSteps(count, which);
}

function eqTimes(num, which)
{ 
	num *= 1;	//cast into number

	if (isNaN(num)) {
		//not a number
		alert("You must enter a number such as:\n2, -2, -4.4, etc");
		return(1);
	}

	count++;

	aVal[count] = aVal[(count-1)]*num;
	bVal[count] = bVal[(count-1)]*num;
	cVal[count] = cVal[(count-1)]*num;
	dVal[count] = dVal[(count-1)]*num;
	eVal[count] = eVal[(count-1)]*num;

	aVal[count] = hundredthRound(aVal[count]);
	bVal[count] = hundredthRound(bVal[count]);
	cVal[count] = hundredthRound(cVal[count]);
	dVal[count] = hundredthRound(dVal[count]);
	eVal[count] = hundredthRound(eVal[count]);

	updateSteps(count, which);
}

function eqDivide(num, which) {

	num *= 1;	//cast into number

	if (isNaN(num)) {
		//not a number
		alert("You must enter a number such as:\n2, -2, -4.4, etc");
		return(1);
	}

	if (num == 0.0 || num == -0.0) {
		//dividing by zero
		alert("You cannot divide by 0!");
		return(1);
	}

	count++;

	aVal[count] = aVal[(count-1)]/num;
	bVal[count] = bVal[(count-1)]/num;
	cVal[count] = cVal[(count-1)]/num;
	dVal[count] = dVal[(count-1)]/num;
	eVal[count] = eVal[(count-1)]/num;

	aVal[count] = hundredthRound(aVal[count]);
	bVal[count] = hundredthRound(bVal[count]);
	cVal[count] = hundredthRound(cVal[count]);
	dVal[count] = hundredthRound(dVal[count]);
	eVal[count] = hundredthRound(eVal[count]);

	updateSteps(count, which);
}

function undo(which) {

	if (count > 0) {
		count--;
		parent.removeChild(step.lastChild);
	}
}

function startOver(which) 
{       for (var i=count; i>=0; i--) undo(which);

	//initializing second equation
	aVal[0] = parseFloat(document.getElementById('a').value);
	bVal[0] = parseFloat(document.getElementById('b').value);
	cVal[0] = parseFloat(document.getElementById('c').value);
	dVal[0] = 0.0;
	eVal[0] = 0.0;

	if (isNaN(aVal[0])) aVal[0] = 0;	//a for eq
	if (isNaN(bVal[0])) bVal[0] = 0;	//b for eq

	//checking for zero coefficients
	if (( (aVal[0] == 0 && bVal[0] == 0)))
	{    alert("Please enter a NONZERO number for the coefficient of X and Y");
		return(1);
	}
	else {
		if (which == 2) step.firstChild.data = "Step " + (count+1) + ": " + formatExp2(aVal[0], bVal[0], cVal[0], dVal[0], eVal[0]);
	}

}

function formatExp2(a, b, c, d, e) 
{       var needsign = false;
        var which = 2;

	//ax + by + c = dx + e
	if (a == 0.0 && b == 0.0 && c == 0.0) return "0 = 0";
	var exp = "";

	if (a == 0 && b == 0) exp = "0 ";
	if (a != 0) 
	{   //display ax
		if (a == 1) exp += xvar;
		else if (a == -1) exp += "-" + xvar;
		else exp += a + " " + xvar;
                needsign = true;
	}
	if (b != 0) 
	{   //display by
	        exp += " ";
		if (b == 1)exp += (needsign ? "+ " : "" ) + yvar; 
		else if (a == -1) exp += "- " + yvar;
		else 
		{ if (b > 0) exp += (needsign ? "+ " : "" ) + b + " " + yvar;
		  else exp += "- " + (-b) + " " + yvar;
		}
                needsign = false;
	}

	if (c != 0) 
	{    exp += " ";
	     if (c > 0) exp += "+ " + c;
	     else exp += "- " + (-c);
	}

	exp += " = ";

	if ( d==0 && e == 0) {  exp += "0"; return exp; }

	if ( d != 0 ) 
	{   //display ax
		if (d == 1) exp += xvar;
		else if (d == -1) exp += "-" + xvar;
		else exp += d + " " + xvar;
	        needsign = true;
	}

	if ( e != 0 ) 
	{    exp += " ";
	     if (e > 0) exp += (needsign ? "+ " : "" ) + e;
	     else exp += "- " + (-e);
	}

	return exp;
}

function updateSteps(count, which) {

	var idName = "step" + count;

	newStep = document.createElement("p");
	newStep.setAttribute("id",idName);
	if (which == 1) newStep.innerHTML = "Step " + (count+1) + ": " + formatExp1(aVal[count], bVal[count], cVal[count], which, xvar);
	if (which == 2) newStep.innerHTML = "Step " + (count+1) + ": " + formatExp2(aVal[count], bVal[count], cVal[count], dVal[count], eVal[count]);

	var p = step.lastChild.parentNode;
	p.appendChild(newStep);
}

function getCoefficient(exp) {
	var coeff = "";

	//pattern for expressions can only be +/- ax or +/- xa
	var expPattern = new RegExp("^(\-{0,1}\\d*\.{0,1}\\d*x|\-{0,1}\\d*\.{0,1}x\\d*)$");
	if (!expPattern.test(exp)) {
		alert("You entered " + exp + ", which is invalid. Try ax or xa, where a is your coefficient.");
		exit(1);
	}

	//extrapolate x from amount

	if (exp == xvar) { coeff = 1; return coeff; }
	else if (exp == ("-" + xvar)) { coeff = -1; return coeff; }

	//negative coefficients
	if (exp.indexOf('-x') == 0) {
		//coefficient is negative: -xa
		coeff = "-";
	}
	else if (exp.indexOf('-') == 0) {
		//coefficient is negative: -ax
		coeff = "-";
	}

	var xPos = exp.indexOf('x');
	//now we have the position of x
	//however, it could be 2x or x2
	if (xPos < exp.length-1) {
		//the case of xa
		coeff += exp.substring(xPos+1, exp.length);
	}
	else {
		//the case of ax
		if (coeff == "-") coeff += exp.substring(1, xPos);
		else coeff += exp.substring(0, xPos);
	}
	return parseFloat(coeff);
}

function hundredthRound(num) {
	return Math.round(num*100)/100;
}

