function simplifyRadical(radical)
{
	var radicand = radical.inner;
	var root = radical.root;
	if(radicand == 1)
	{
		return new Radical(1, 1, root);
	}
	else
	{
		var primeFactors = getPrimeFactors(radicand);

		//useful check to speed up bigger numbers:
		//	check if all exp are less than root, if so, sqrt(x) = sqrt(x), no simplification possbile.
		// may not now be needed much
				
		//var inner = 1;
		var innerRemoved = 1;
		var outer = 1;

		for(i = 0; i < primeFactors.length; i++)
		{
			//window.status = i;
			var currentFactor = primeFactors[i];
			v = eval(currentFactor.base);
			countV = eval(currentFactor.exponent);
			var tuples = Math.floor(countV / root);
			var outerComponent = Math.pow(v,tuples)
			outer *= outerComponent;
			innerRemoved *= Math.pow(outerComponent,root);
		}	
		
		var inner = radicand / innerRemoved;
		return new Radical(inner, outer, root);
	}
}

function Radical(inner, outer, root) //inner is the radicand, outer is the multiplier after simplification.
{
	this.inner = inner;
	this.outer = outer;
	this.root = root;
}

function toRadicalString(r)
{
	var rootstring;
	var str;
	
	if(r.root == 2)
	{
		
		rootstring = " sqrt";
	}
	else if(r.root == 3)
	{
		rootstring = " cubeRt";
	}
	else
	{	
		rootstring = "<sup>" + r.root + "<sup>th</sup></sup>root";
	}
	
	if(r.inner == 1)
	{
		str = "" + r.outer;
	}
	else if(r.outer == 1)
	{
		str = rootstring + "(" + r.inner + ")";
	}
	else
	{
		str = "" + r.outer + " " + rootstring + "(" + r.inner + ")";
	}
	return str;
}
