function round(val)
{
	return(Math.round(val*10000000)/10000000);
}

PI = 3.141592654;

function pie()
{
	document.keypad.display.value = PI;
}

function MakeArray(n){
	this.length=n;
   	for(var i=1; i<=n; i++) this[i]=0;
   return this
}

link = new MakeArray(5);

function enter(num)
{
	document.keypad.display.value += num;
}

function calculate(sign)
{
	var temp = document.keypad.display.value * 1;
	document.keypad.display.value = "";
	document.keypad.list.value = temp;
	link[1] = temp;
	if (sign == "/") {link[2] = "/"; document.keypad.list.value += " / ";}
	if (sign == "*") {link[2] = "*"; document.keypad.list.value += " * ";}
	if (sign == "-") {link[2] = "-"; document.keypad.list.value += " - ";}
	if (sign == "+") {link[2] = "+"; document.keypad.list.value += " + ";}
	if (sign == "p") {link[2] = "p"; document.keypad.list.value += " ^ ";}
}

function power()
{
	temp = 1;
	n = link[3];
   	for(var i=1; i<=n; i++) temp *= link[1];
	return(temp);
}

function equal()
{
	if (link[2]) {
		if (document.keypad.display.value != "") {
			var temp = document.keypad.display.value * 1;
			document.keypad.list.value += temp;
			link[3] = temp;
			if (link[2] == "/") {res = link[1] / link[3]; document.keypad.display.value = round(res)}
			if (link[2] == "*") {res = link[1] * link[3]; document.keypad.display.value = round(res)}
			if (link[2] == "-") {res = link[1] - link[3]; document.keypad.display.value = round(res)}
			if (link[2] == "+") {res = link[1] + link[3]; document.keypad.display.value = round(res)}
			if (link[2] == "p") {document.keypad.display.value = round( power() )}
			link[1]=0; link[2]=0; link[3]=0;
		}
	}
}

function calc(code)
{
	var temp = document.keypad.display.value * 1;
	if (code == 1) {temp1 = Math.sin(temp*PI/180); document.keypad.list.value = "sin "}
	if (code == 2) {temp1 = Math.cos(temp*PI/180); document.keypad.list.value = "cos "}
	if (code == 3) {temp1 = Math.tan(temp*PI/180); document.keypad.list.value = "tan "}
	if (code == 4) {temp1 = Math.sqrt(temp); document.keypad.list.value = "sqrt "}
	if (code == 5) {temp1 = Math.log(temp); document.keypad.list.value = "ln "}
	if (code == 6) {temp1 = 1/temp; document.keypad.list.value = "1/x, x="}
	if (code == 7) {temp1 = temp * temp; document.keypad.list.value = "x*x, x="}
	document.keypad.list.value += temp;
	document.keypad.display.value = round(temp1);
}

function fsolve()
{
	var a = document.keypad.c1.value*1;
	var b = document.keypad.c2.value*1;
	var c = document.keypad.c3.value*1;
	if (a==0) {document.keypad.list.value = "            Ne kvadratine";
		     document.keypad.display.value = "                 Lygtis"}
	else { x1 = (b*b-4*a*c);
		if ( x1 < 0) {document.keypad.list.value = "           Menamas SQR";
				  temp = (Math.sqrt(Math.abs(x1)))/(2*a);
				  x2 = round(-b/(2*a)) + "+/- " + round(temp) + "i";
				  document.keypad.display.value = x2}
		else {
			var x1 = (-b + Math.sqrt(b*b-4*a*c)) / (2*a);
			var x2 = (-b - Math.sqrt(b*b-4*a*c)) / (2*a);
			document.keypad.list.value = "x1 = " + round(x1);
			document.keypad.display.value = "x2 = " + round(x2);
		}
	}
}
function change()
{
	var temp = document.keypad.display.value;
	if (temp.substring(0,1) == "-") {document.keypad.list.value = "";
						   document.keypad.display.value = 0 - document.keypad.display.value * 1}
	if (temp.substring(0,1) != "-") {document.keypad.list.value = "";
						   document.keypad.display.value = "-" + temp.substring(0,temp.length)}
}

function eraser()
{
	document.keypad.list.value = "           ";
	document.keypad.display.value = "";
	document.keypad.c1.value = "";
	document.keypad.c2.value = "";
	document.keypad.c3.value = "";
	link[1]=0; link[2]=0; link[3]=0;
}

function backer()
{
	var temp = document.keypad.display.value;
	document.keypad.display.value = temp.substring(0,temp.length*1 -1);
}

var memory = 0;
function mem(val)
{
	if (val == 1 ) {document.keypad.list.value = "          --> Atmintis i";
			    memory = document.keypad.display.value * 1}
	if (val == -1) {document.keypad.display.value = memory}
	if (val == 0 ) {document.keypad.list.value = "          Išvalyti atminti";
			    document.keypad.display.value = ""; memory = 0}
}


function travel(link)
{
window.open(link,"calculator","toolbar=1,location=1,status=1,scrollbars=1,directories=1,copyhistory=1,menubar=1,resizable=1")
}

var screen=" ";
function eraser2()
{
	var ans = confirm('\nAr noryte išvalyti skaiciavimo rezultatus?\n');
	if (ans) {screen = document.notes.junk.value; document.notes.junk.value = "";}
}

function copy()
{
document.notes.junk.value = document.keypad.list.value+"\n"+document.keypad.display.value+"\n"+document.notes.junk.value;
}

function help()
{
	screen = document.notes.junk.value;
	msg1 = "Sekanti dalis iš\nkalkuliatoriaus funkcijos:\n";
	msg2 = "\n(<-> M) - Išvalyti atminti\n(--> M) - Memory In\n(<-- M) - Memory Out";
	msg3 = "\n(<--)   - Išvalyti paskutine reikšme\n(x^y)   - X to the power of Y";
	msg4 = "\n\nNorint grižti, spauskite 'Atstatyti kalkuliatoriaus rezultata'";
	document.notes.junk.value = "";
	document.notes.junk.value = msg1 + msg2 + msg3 + msg4;
}

function restore()
{
	document.notes.junk.value = "";
	document.notes.junk.value = screen;	
}

