
var show_clear_history = 0;
var history_enabled    = 0;

function body_load()
{
	var c = cookie_check_support();

	if(c == false) {
		history_enabled = 0;
	}
	
	document.getElementById('display').focus();
	document.getElementById('display').value = '';
	
	if(history_enabled == 1) {
		cookie_load_history();
	}
}

function calc(eventer) 
{	
	var key;

	if(!eventer) {
		eventer = window.event;
	}

	if(eventer.which) {
		key = eventer.which;
	} else if (eventer.keyCode){
		key = eventer.keyCode;
	}
	
	if(key == 13) calculate();
}

function replaceAll(s, pattern, replacement)
{
	var r = "";
	var pos;
	
	for(var i=0; i < s.length; ) {
		pos = s.indexOf(pattern, i);
		if(pos == -1) {
			r = r + s.substr(i,s.length - i);
			break;
		}
		r = r + s.substring(i,pos);
		i = pos + pattern.length;
		r = r + replacement;
	}
	
	return r;
}

function includeMaths(s)
{	
	/* s = replaceAll(s,",","."); */

	s = replaceAll(s,"PI", "Math.PI");
	s = replaceAll(s,"E", "Math.E");

	s = replaceAll(s,"sin","Math.sin");
	s = replaceAll(s,"cos","Math.cos");
	s = replaceAll(s,"tan","Math.tan");
	
	s = replaceAll(s,"abs","Math.abs");
	s = replaceAll(s,"exp","Math.exp");
	s = replaceAll(s,"floor","Math.floor");
	s = replaceAll(s,"lg", "custom_lg");
	s = replaceAll(s,"log", "custom_lg");
	s = replaceAll(s,"lb", "custom_lb");
	s = replaceAll(s,"ln","Math.log");
	s = replaceAll(s,"max","Math.max");
	s = replaceAll(s,"min","Math.min");
	s = replaceAll(s,"random","Math.random()");
	s = replaceAll(s,"round","Math.round");
	s = replaceAll(s,"sqrt","Math.sqrt");
	
	return s;
}

function custom_lg(x)
{
	return Math.log(x) / Math.log(10);
}

function custom_lb(x)
{
	return Math.log(x) / Math.log(2);
}

function insert(t)
{	
	var v = document.getElementById("display");
	v.value = v.value + t.getElementsByTagName("div")[2].innerHTML;
	setTimeout("doFocus()", 10);
}

function clear_display()
{
	document.getElementById("display").value = "";
	setTimeout("doFocus()", 10);
}

function calculate()
{
	var l = document.getElementById("log");
	var f = document.getElementById("display");
	
	var v = includeMaths(f.value);
	var r;
	
	try {
		r = eval(v);
	} catch(e) {
		
		return;
	}
	
	log_add_line(f.value, r);
	
	if(history_enabled == 1) {
		cookie_add_value("history","?" + f.value + "?" + r);
	}
	
	f.value = r;
	f.focus();
	setTimeout("doFocus()", 10);
}

function cookie_clear()
{
	cookie_delete("history");
}

function cookie_load_history()
{
	var c = cookie_get_value("history");

	var run = 1;
	var i=0;
	var count=0;
	
	if(c == null) return;
	
	while(run == 1) {
		var pos_input  = c.indexOf("?", i);
		if(pos_input == -1) {
			run = 0;
			break;
		}
		i = pos_input; i++;
		var pos_result = c.indexOf("?", i);
		if(pos_result == -1) {
			run = 0;
			break;
		}
		i = pos_result;	i++;
		var pos_next_input = c.indexOf("?", i);
		if(pos_next_input == -1) {
			run = 0;
			pos_next_input = c.length;
		}
		i = pos_next_input;

		count = count + 1;

		var input  = c.substring(pos_input+1,  pos_result);
		var result = c.substring(pos_result+1, pos_next_input);
	
		log_add_line(input, result);
	}
	
	if(count > 0) {
		show_clear_history = 1;
		document.getElementById("clear_history").style["display"] = "block";
	}
}

function log_add_line(input, result)
{
	if(show_clear_history == 0) {
		show_clear_history = 1;
		document.getElementById("clear_history").style["display"] = "block";
	}
	var log = document.getElementById("log");

	var a = "<div id='log_line' onMouseDown='javascript:insert(this)'>";
	a = a + "<div class='table_l'><img id='r' src='calcr_ficheiros/r.png'></div>";
	a = a + "<div class='table_m'>";
	a = a + input;
	a = a + "</div>";
	a = a + "<div class='table_r'>";
	a = a + result;
	a = a + "</div>";
	a = a + "</div>";
	
	log.innerHTML = a.toString() + log.innerHTML;
	
}

function doFocus()
{
	var f = document.getElementById("display");
	f.focus();
}

function clear_history()
{
	show_clear_history = 0;
	document.getElementById("clear_history").style["display"] = "none";
	if(history_enabled == 1) {
		cookie_clear();
	}
	var l = document.getElementById("log");
	l.innerHTML = "";          
}

document.onkeydown = calc;
