// script (c) copyright http://www.aleto.christian krebs aka aleto
// This work is licensed under the Creative Commons Attribution License. To  view a
// copy of this license, visit http://creativecommons.org/licenses/by/1.0/  or send
// a letter to Creative Commons, 559 Nathan Abbott Way, Stanford,  California 94305,
// USA.

var current='',current2='',temp='',memoc='';
var trezei=' '
var fac2=Math.PI/180,fac1=fac2,fac=")*"+fac1+")";
var facr2=180/Math.PI,facr1=facr2,facr=")*"+facr1;
var za=1,zacon=new Array('');
var exp='';
function run(){
	var a=document.getElementById('how').value,b='',c='',e=new Array(),f=0;
	current=a;
	za++;
	zacon.push(current);
	if(zacon.length>10)zacon.shift();
	if(za>10)za=10;
	with(Math){try{b=eval(a)}catch(e){return}};
	if(a=='')b='';
	if(b<0.0000000009&&b>-0.0000000009){b=0;exp='';}
	b=b.toString();
	if(b.indexOf('e')!=-1){
		exp=b.replace(/^..*(e)/,' $1');
		b=b.replace(/e.*.$/,'');
	}

	if(b.indexOf('.')!=-1){
		c=b.slice((b.indexOf('.')+1));
		b=b.slice(0,b.indexOf('.'))
	}
	f=b.length;
	while(f>0){
		f-=3;
		if(f<0)f=0;
		e.push(b.slice(f));
		b=b.slice(0,f)
	}
	e.reverse();
	if(c.length>0)e.push('.');
	while(c.length>0){
		c=c.slice(0,9);
		e.push(c.slice(0,3));
		c=c.slice(3);
	}
	document.getElementById('result').innerHTML=e.join(trezei)+exp;				
}
function run2(aa,ab,ac){
	current2=temp;
	current=aa+current+ab+current2;
	temp='';						
	current2='';				
	document.getElementById('how').value=current;
	temp=ac;	
	run();
}
var toggle=false;
var temp2='';
function run3(aa,ab,ac){
	if(!toggle){
		current=aa+current+ab;
		temp2=ac;
		document.getElementById('how').value=current;
		run();
		toggle=true;
	}
	else{
		current+=temp2;
		temp2='';
		document.getElementById('how').value=current;
		run();
		toggle=false;
	}
}
function back(){
	if(za>1&&zacon.length>2){
		za--;
		current=zacon[za-1];
		zacon.pop();
		document.getElementById('how').value=current;
	}
}
function clear(){
	current='';
	document.getElementById('result').innerHTML='&nbsp;';
	document.getElementById('how').value='';
	zacon=zacon.slice(0,1);
	za=1;
	exp='';
}
function memo(a1,a2,a3){
	if(a3==1){
		with(Math){try{memoc=eval(a1+a2+current)}
			catch(e){return}
			}
		}
	else{memoc=''}
	var temp=memoc;
	temp=temp.toString();
	if(temp.indexOf('.')!=-1&&temp.slice(temp.indexOf('.')).length>10){
			temp=temp.slice(0,temp.indexOf('.'))+
				"."+temp.slice(temp.indexOf('.'),temp.indexOf('.')+10);
	}
	document.getElementById('memor').innerHTML="M: "+temp;
}
function set(){
	if(fac1==fac2){
		fac1=1;
		facr1=1;
		fac=")*"+fac1+")";
		facr=")*"+facr1;
		document.getElementById('setraddeg').innerHTML="deg";
		return;
	}
	if(fac1==1){
		fac1=fac2;
		facr1=facr2;
		fac=")*"+fac1+")";
		facr=")*"+facr1;
		document.getElementById('setraddeg').innerHTML="rad";
		return;
	}
}
function init(){
	document.getElementById('how').focus();
}
