function calcmana () {
	var m = document.main.mana.value;
	document.main.mana.value = intOnly(m); // Make sure our mana value is an integer
	
	setInRange();
	
	var a = 118.19;
	var b = 4564.23;
	var c = 8191 - m;
	
	// Calculate number of +223 and +53% attributes
	
	var numerator = -b + Math.sqrt( Math.pow(b, 2) - (4 * a * c) );
	var quantity  = Math.floor( numerator / (2 * a) );
	
	document.main.quantities.value = quantity;
	
	// Calculate Energy

	var product  = ( 8191 + (223 * quantity) ) * ( 1 + (0.53 * quantity) );
	var finalpro = ( m - product ) / 2;
	var energy   = Math.floor( finalpro );
	
	document.main.energy.value = energy;
	
	// Calculate Energy 95 to add
	
	var energy95 = Math.floor( energy / 95 );
	
	document.main.energy95.value = energy95;
	
	// Calculate extra energy to add
	
	var addenrgy = finalpro - (energy95 * 95);
	
	document.main.addenergy.value = Math.floor( addenrgy );
	
	// Calculate hexadecimal value of mana value
	
	document.main.hex.value = dechex(m);
}

function calcregen () {
	var m = document.main.mana.value;
	var t = document.main.telek.value;
	
	// Calculate off screen mana value
	
	var multiply = 100 / (200 - (t * 6.25)); // Calculate multiplier based on telekinesis level
	
	var offscreen = m * multiply;
	
	while ( offscreen > 8388607 )
	{
		offscreen += -16777216;
	}
	
	document.main.manaoff.value = Math.round( offscreen, 5 );
	
	 // Calculate mana regeneration cap
	 
	 var manacap = (((2097151 / m) * 120) * 100) - 100;
	 document.main.manacap.value = Math.floor(manacap);
}

function setInRange () {
	if ( document.main.mana.value < 8191 ) {
		document.main.mana.value = 8191;
	}
	
	if ( document.main.mana.value > 8388608 ) {
		document.main.mana.value = 8388608;
	}
}

function intOnly ( variable ) {
	var intvar = parseInt(variable, 10);
	return ( intvar ) ? intvar : "0";
}

function dechex (number) {
    return parseInt(number).toString(16).toUpperCase();
}