
var show_clear_history = 0;
var history_enabled    = 1;

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,"sin","Math.sin");
	s = replaceAll(s,"cos","Math.cos");
	s = replaceAll(s,"tan","Math.tan");
	
	s = replaceAll(s,"asin","Math.asin");
	s = replaceAll(s,"acos","Math.acos");
	s = replaceAll(s,"atan","Math.atan");	
	
	s = replaceAll(s,"abs","Math.abs");
	s = replaceAll(s,"exp","Math.exp");
	s = replaceAll(s,"floor","Math.floor");
	s = replaceAll(s,"log","Math.log");
	s = replaceAll(s,"max","Math.max");
	s = replaceAll(s,"min","Math.min");
	s = replaceAll(s,"pow","Math.pow");
	s = replaceAll(s,"random","Math.random");
	s = replaceAll(s,"round","Math.round");
	s = replaceAll(s,"sqrt","Math.sqrt");
	
	return s;
}

function modulo(a,n)
{
        var r;
	var q = Math.floor(a/n);
        r = a - q*n;
	return r;
}

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 v = f.value;
	var r;
	var b;
	var e;
	var base=10, bits=32, arr=10;
	
	try {
		if (v.search("mod") != -1)
		{
			var a=v.substring(0,v.indexOf("mod")-1);
			var m=str2bigInt(v.substring(v.indexOf("mod")+4,
					 v.length),base,bits,arr);		
			
			if (a.indexOf("^") != -1) // exponentiate mod m
			{
			    b=str2bigInt(a.substring(0,a.indexOf("^")),base,bits,arr);
			    e=str2bigInt(a.substring(a.indexOf("^")+1,
			    	             a.length),base,bits,arr);
			    // log_add_line("base", bigInt2str(b,base));
			    // 			    log_add_line("exponent", bigInt2str(e,base));
			    // 			    log_add_line("modulus", bigInt2str(m,base));
			    
				r = bigInt2str(powMod(b,e,m), base);
			    
			}
			else if (a.indexOf("*") != -1) // exponentiate mod m
			{
			    b=str2bigInt(a.substring(0,a.indexOf("*")),base,bits,arr);
			    e=str2bigInt(a.substring(a.indexOf("*")+1,
			    	             a.length),base,bits,arr);
			    // log_add_line("base", bigInt2str(b,base));
			    // 			    log_add_line("exponent", bigInt2str(e,base));
			    // 			    log_add_line("modulus", bigInt2str(m,base));
			    
				r = bigInt2str(multMod(b,e,m), base);
			    
			}
			else if (a.indexOf("+") != -1) // exponentiate mod m
			{
			    b=str2bigInt(a.substring(0,a.indexOf("+")),base,bits,arr);
			    e=str2bigInt(a.substring(a.indexOf("+")+1,
			    	             a.length),base,bits,arr);
			    // log_add_line("base", bigInt2str(b,base));
			    // 			    log_add_line("exponent", bigInt2str(e,base));
			    // 			    log_add_line("modulus", bigInt2str(m,base));
			    
				r = bigInt2str(mod(add(b,e),m), base);
			    
			}
			else // reduce mod m
			{
				 b = str2bigInt(a,base,bits,arr);
			     r = bigInt2str(mod(b,m), base);
				 // log_add_line("base", bigInt2str(b,base));
				 // log_add_line("modulus", bigInt2str(m,base));
			}
		}
		// else if (v.indexOf("^") != -1)
		// 		{
		// 		    var b=new Number(v.substring(0,v.indexOf("^")));
		// 		    var e=new Number(v.substring(v.indexOf("^")+1,
		// 		    	             v.length));
		// 		    
		// 		    // log_add_line("evaluating: Math.pow("+b+","+e+")", "");
		// 		    r = eval("Math.pow("+b+","+e+")");
		// 		}
		else
		{	
			r = eval(includeMaths(v));
		}
	} catch(e) {
		
		return;
	}
	
	if (r != "null") {
		write_log(f.value, r);
		f.value = r;
	}

	f.focus();
	setTimeout("doFocus()", 10);
}

function write_log(f,h)
{
	log_add_line(f, h);
	if(history_enabled == 1) {
		cookie_add_value("history","?" + f + "?" + h);
	}
}
	

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='img/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;
