/* 
Thanks for looking at my source :)
Please don't use it without my permission!
wartex8.com/calc 
*/

function replace(inputVar){
/*
my 2x -> 2*x does not support xsinx stuff //if item on left of x != ["+" || "-" || "*" || "/" || "(" || ")" || ""], then add "*" //its not an operator or parentheses
*/

	//////////////////////////////////
	// Power Fix ** Need to rewrite when possible **
	//////////////////////////////////
	while(inputVar.indexOf("^") != -1){
		var equationPart = inputVar.split("^"); 
		var parenthesesCount = 0;

		var exponentBase = "";
		var exponentPower = "";
		var equationPart1 = "";
		var equationPart2 = "";

		var carretlocation = equationPart[0].length-1;
		var potentialMatch = equationPart[0].charAt(carretlocation);

		while(potentialMatch.match(acceptedNumList)){
			exponentBase = potentialMatch + exponentBase;
			potentialMatch = equationPart[0].charAt(--carretlocation);
		}

		if(potentialMatch == ')'){//worst code
			potentialMatch = equationPart[0].charAt(carretlocation);
			parenthesesCount++;
	
			//this algorithm will calculate the first ) case twice
			while(parenthesesCount>0){
				potentialMatch = equationPart[0].charAt(carretlocation);
		
				if(potentialMatch == ')') parenthesesCount++;
				else if(potentialMatch == '(') parenthesesCount--;
				else if(potentialMatch == '') parenthesesCount = 0; //in case there is a no matching parenthesis
		
				carretlocation--; 
				exponentBase = potentialMatch + exponentBase;
			}
		}

		else if(potentialMatch == "x" && exponentBase == ""){ //maybe I should use acceptedPowList instead of acceptedNumList (could get rid of these next two matches + support for multivariable graphing)
			exponentBase = "x";
			carretlocation--;
		}

		else if(potentialMatch == "y" && exponentBase == ""){
			exponentBase = "y";
			carretlocation--;
		}

		while(carretlocation>-1) equationPart1 = inputVar.charAt(carretlocation--) + equationPart1;
		/////////////////////////////////////////////////
		// Parse exponentPower variable
		/////////////////////////////////////////////////
		carretlocation = 0;
		parenthesesCount = 0;
		potentialMatch = equationPart[1].charAt(carretlocation);

		while(potentialMatch.match(acceptedNumList)){//awful code...
			exponentPower += potentialMatch;
			potentialMatch = equationPart[1].charAt(++carretlocation);
		}

		if(potentialMatch == '('){
			potentialMatch = equationPart[1].charAt(carretlocation);
			parenthesesCount++;
	
			//this algorithm will calculate the first ) case twice
			while(parenthesesCount>0){
				potentialMatch = equationPart[1].charAt(carretlocation);
		
				if(potentialMatch == ')') parenthesesCount--;
				else if(potentialMatch == '(') parenthesesCount++;
				else if(potentialMatch == '') parenthesesCount = 0; //in case there is a no matching parenthesis
		
				carretlocation++; 
				exponentPower += potentialMatch;
			}
		}

		else if(potentialMatch == "x" && exponentPower == ""){// = x or w... then exponentPower = thisvar
			exponentPower = "x";
			carretlocation++;
		}

		else if(potentialMatch == "y" && exponentPower == ""){
			exponentPower = "y";
			carretlocation++;
		}

		carretlocation += inputVar.indexOf("^") + 1; //this may be wrong
		while(carretlocation<inputVar.length) equationPart2 += inputVar.charAt(carretlocation++);

		inputVar = equationPart1 + "pow(" + exponentBase + "," + exponentPower + ")" + equationPart2;
	}

	//////////////////////////////////
	// Multiplication support
	//////////////////////////////////
	while(inputVar.search(/[0-9)](?=[a-z])/gi)+1 > 0){ //may need to support x, y and z later - should this be != -1
		// need to do )(  =  )*(
		addMultiplicationHere = inputVar.search(/[0-9)](?=[a-z])/gi)+1;
		if(addMultiplicationHere>0) inputVar = inputVar.substr(0,addMultiplicationHere) + "*" + inputVar.substr(addMultiplicationHere);
	}

	while(inputVar.search(/\)\(/gi) > 0) inputVar = inputVar.replace(")(", ")*(");// looks for ")("
	//need to support 2(x)

	return inputVar;
}
