
var prev_cmd="";var memory=0;var angle_mode="deg";var display_base="dec";var value_stack=new Array(0);var operator_stack=new Array(0);var base_value={"bin":2,"oct":8,"dec":10,"hex":16};var modes=["bin","oct","dec","hex","deg","rad","gra"];var hex_keys=["A","B","C","D","E","F"];var dec_keys=["8","9"];var oct_keys=["2","3","4","5","6","7"];function gray_out(key_suffix)
{document.getElementById('key_'+key_suffix).firstChild.style.color='gray';}
function light_up(key_suffix)
{document.getElementById('key_'+key_suffix).firstChild.style.color='cyan';}
function make_button_visible(key_suffix)
{document.getElementById('key_'+key_suffix).style.display='block';}
function make_button_invisible(key_suffix)
{document.getElementById('key_'+key_suffix).style.display='none';}
function set_purple_button(key_suffix)
{document.getElementById('key_'+key_suffix).firstChild.style.background='url(button_roundpurple.png) no-repeat center center';}
function update_mode_colors()
{forEach(modes,gray_out);light_up(display_base);light_up(angle_mode);if(display_base=="hex")
{forEach(chain(oct_keys,dec_keys,hex_keys),make_button_visible);}
else if(display_base=="dec")
{forEach(chain(oct_keys,dec_keys),make_button_visible);forEach(hex_keys,make_button_invisible);}
else if(display_base=="oct")
{forEach(oct_keys,make_button_visible);forEach(chain(dec_keys,hex_keys),make_button_invisible);}
else if(display_base=="bin")
{forEach(chain(oct_keys,dec_keys,hex_keys),make_button_invisible);}}
function reset(value)
{set_led(value);value_stack.length=0;operator_stack.length=0;}
function get_led()
{return document.getElementById('newled').firstChild.nodeValue;}
function set_led(str)
{if(str.length>0&&str[0]=='.')
str="0"+str;document.getElementById('newled').firstChild.nodeValue=str;}
function str_to_value(str)
{base=base_value[display_base];if(base==10)
return eval(str);else
{int_part=parseInt(str,base);frac_part=0;words=str.split('.')
if(words.length==2)
{fracstr=words[1];factor=1.0;for(i=0;i<fracstr.length;++i)
{factor=factor/base;frac_part+=parseInt(fracstr[i],base)*factor;}}
return int_part+frac_part;}}
function value_to_str(value)
{return value.toString(base_value[display_base]);}
function angle_to_radians(value)
{if(angle_mode=="deg")
return value*Math.PI/180;else if(angle_mode=="gra")
return value*Math.PI/200;else
{return value;}}
function radians_to_angle(radians)
{if(angle_mode=="deg")
return radians*180/Math.PI;else if(angle_mode=="gra")
return radians*200/Math.PI;else
{return radians;}}
function call_operator(operator,arg1,arg2)
{if(operator=='^')
return Math.pow(arg1,arg2);else if(operator=='+')
return arg1+arg2;else if(operator=='-')
return arg1-arg2;else if(operator=='*')
return arg1*arg2;else if(operator=='/')
{if(arg2!=0)
return arg1/arg2;}
else if(operator=='xCy')
{if(arg1>=arg2&&arg2>=0)
{arg1=Math.floor(arg1)
arg2=Math.floor(arg2)
return factorial(arg1)/factorial(arg1-arg2)/factorial(arg2);}}
else if(operator=='xPy')
{if(arg1>=arg2&&arg2>=0)
{arg1=Math.floor(arg1)
arg2=Math.floor(arg2)
return factorial(arg1)/factorial(arg1-arg2);}}
return 0;}
var operator_levels={"=":0,"+":1,"-":1,"*":2,"/":2,"^":3,"xCy":4,"xPy":4};function operator_level(operator)
{level=operator_levels[operator];if(level==null)
return-1;return level;}
function factorial(x)
{value=1;for(i=x;i>1;--i)
value*=i;return value;}
function execute(cmd)
{current_value=get_led();if(cmd.length==1&&(cmd=='.'||(cmd>='0'&&cmd<='9')||(cmd>='A'&&cmd<='F')))
{if(prev_cmd.length!=1||(prev_cmd!='.'&&(prev_cmd<'0'||prev_cmd>'9')&&(prev_cmd<'A'||prev_cmd>'F')))
current_value='';if(cmd=='.')
{found_period=0;for(i=0;i<current_value.length;++i)
if(current_value[i]=='.')
{found_period=1;break;}
if(!found_period)
{if(current_value=='')
current_value='0';current_value+=cmd;}}
else
{if(current_value=='0')
current_value='';current_value+=cmd.toLowerCase();}
set_led(current_value);}
else if(operator_level(cmd)>=0)
{current_level=operator_level(cmd);if(current_level>0&&operator_level(prev_cmd)>0)
{return;}
result=str_to_value(current_value);while(operator_stack.length>0)
{prev_oper=operator_stack[operator_stack.length-1]
if(prev_oper=='(')
{if(cmd=='=')
{value_stack.pop();operator_stack.pop();continue;}
else
break;}
prev_level=operator_level(prev_oper);if(prev_level>=current_level)
{arg1=value_stack.pop();arg2=result;oper=operator_stack.pop();result=call_operator(oper,arg1,arg2);}
else
break;}
set_led(value_to_str(result));if(cmd!='=')
{value_stack.push(result);operator_stack.push(cmd);}}
else if(cmd=='(')
{value_stack.push(0);operator_stack.push('(');set_led('0');}
else if(cmd==')')
{while(operator_stack.length>0)
{if(operator_stack[operator_stack.length-1]=='(')
{value_stack.pop();operator_stack.pop();break;}
arg1=value_stack.pop();arg2=str_to_value(current_value);oper=operator_stack.pop();result=call_operator(oper,arg1,arg2);set_led(value_to_str(result));}}
else if(cmd=="dec")
{value=str_to_value(current_value)
display_base="dec";reset(value_to_str(value));update_mode_colors();}
else if(cmd=="bin")
{value=str_to_value(current_value)
display_base="bin";reset(value_to_str(value));update_mode_colors();}
else if(cmd=="oct")
{value=str_to_value(current_value)
display_base="oct";reset(value_to_str(value));update_mode_colors();}
else if(cmd=="hex")
{value=str_to_value(current_value)
display_base="hex";reset(value_to_str(value));update_mode_colors();}
else if(cmd=="deg")
{angle_mode="deg";update_mode_colors();}
else if(cmd=="rad")
{angle_mode="rad";update_mode_colors();}
else if(cmd=="gra")
{angle_mode="gra";update_mode_colors();}
else if(cmd=='pi')
{current_value=value_to_str(Math.PI);set_led(current_value);}
else if(cmd=='euler')
{current_value=value_to_str(Math.E);set_led(current_value);}
else if(cmd=='MR')
{current_value=value_to_str(memory);set_led(current_value);if(memory==0)
document.getElementById('key_MR').firstChild.style.color='gray';else
document.getElementById('key_MR').firstChild.style.color='cyan';}
else if(cmd=='MS')
{memory=str_to_value(current_value);if(memory==0)
document.getElementById('key_MR').firstChild.style.color='gray';else
document.getElementById('key_MR').firstChild.style.color='cyan';}
else if(cmd=='MC')
{memory=0;document.getElementById('key_MR').firstChild.style.color='gray';}
else if(cmd=='M+')
{memory+=str_to_value(current_value);if(memory==0)
document.getElementById('key_MR').firstChild.style.color='gray';else
document.getElementById('key_MR').firstChild.style.color='cyan';}
else if(cmd=='1/x')
{result=1/str_to_value(current_value);set_led(value_to_str(result));}
else if(cmd=='sqrt')
{result=Math.sqrt(str_to_value(current_value));set_led(value_to_str(result));}
else if(cmd=='x!')
{result=factorial(Math.floor(str_to_value(current_value)))
set_led(value_to_str(result));}
else if(cmd=='x^2')
{result=str_to_value(current_value);result=result*result;set_led(value_to_str(result));}
else if(cmd=='x^3')
{result=str_to_value(current_value);result=result*result*result;set_led(value_to_str(result));}
else if(cmd=='e^x')
{result=Math.exp(str_to_value(current_value));set_led(value_to_str(result));}
else if(cmd=='10^x')
{result=Math.pow(10,str_to_value(current_value));set_led(value_to_str(result));}
else if(cmd=='log')
{result=Math.log(str_to_value(current_value))/Math.LN10;set_led(value_to_str(result));}
else if(cmd=='ln')
{result=Math.log(str_to_value(current_value));set_led(value_to_str(result));}
else if(cmd=='sin')
{result=Math.sin(angle_to_radians(str_to_value(current_value)));set_led(value_to_str(result));}
else if(cmd=='cos')
{result=Math.cos(angle_to_radians(str_to_value(current_value)));set_led(value_to_str(result));}
else if(cmd=='tan')
{result=Math.tan(angle_to_radians(str_to_value(current_value)));set_led(value_to_str(result));}
else if(cmd=='asin')
{result=Math.asin(str_to_value(current_value));set_led(value_to_str(radians_to_angle(result)));}
else if(cmd=='acos')
{result=Math.acos(str_to_value(current_value));set_led(value_to_str(radians_to_angle(result)));}
else if(cmd=='atan')
{result=Math.atan(str_to_value(current_value));set_led(value_to_str(radians_to_angle(result)));}
else if(cmd=='sinh')
{result=Math.exp(str_to_value(current_value));result=(result-1/result)/2;set_led(value_to_str(result));}
else if(cmd=='cosh')
{result=Math.exp(str_to_value(current_value));result=(result+1/result)/2;set_led(value_to_str(result));}
else if(cmd=='tanh')
{result=Math.exp(str_to_value(current_value));result=(result-1/result)/(result+1/result);set_led(value_to_str(result));}
if(cmd=='asinh')
{result=str_to_value(current_value);result=Math.log(result+Math.sqrt(result*result+1));set_led(value_to_str(result));}
else if(cmd=='acosh')
{result=str_to_value(current_value);result=Math.log(result+Math.sqrt(result*result-1));set_led(value_to_str(result));}
else if(cmd=='atanh')
{result=str_to_value(current_value);result=Math.log((result+1)/(1-result))/2;set_led(value_to_str(result));}
else if(cmd=='+/-')
{set_led(value_to_str(-str_to_value(current_value)));}
else if(cmd=='AC')
{reset(0);}
else if(cmd=='CL')
{set_led("0");}
prev_cmd=cmd;}
function load_html()
{appendChildNodes("calculator",DIV({"id":"newled","class":"led"},"0"));tbody=TBODY()
table=TABLE({"class":"calculator"},tbody)
display_objs={"bin":"binary","oct":"octal","dec":"decimal","hex":"hexadecimal","deg":"degrees","rad":"radians","gra":"gradiants","euler":"e","e^x":SPAN(null,"e",createDOM("sup",null,"x")),"10^x":SPAN(null,"10",createDOM("sup",null,"x")),"x^2":SPAN(null,"x",createDOM("sup",null,"2")),"x^3":SPAN(null,"x",createDOM("sup",null,"3")),"CL":"C"};buttons=[["box","bin","oct","dec","hex","deg","rad","gra"],["flatbutton","sin","cos","tan","sinh","cosh","tanh"],["flatbutton","asin","acos","atan","asinh","acosh","atanh"],["flatbutton","euler","pi","ln","e^x","log","10^x"],["flatbutton","x^2","x^3","sqrt","x!","xPy","xCy"],["flatbutton","(",")","1/x"],["button","MR","7","8","9","CL","AC"],["button","MS","4","5","6","*","/"],["button","M+","1","2","3","+","-"],["button","MC","0",".","+/-","=","^"],["button","A","B","C","D","E","F"]];for(i=0;i<buttons.length;++i)
{button_row=buttons[i];row=TR(null);row_type=button_row[0];for(j=1;j<button_row.length;++j)
{button_name=button_row[j];button_id="key_"+button_name;display=display_objs[button_name];if(display==null)
display=button_name;anchor=A({"class":"mouseup","onmousedown":"this.className='mousedown'","onmouseup":"execute('"+button_name+"'); this.className='mouseup'","onmouseout":"this.className='mouseup'","onmouseover":"this.className='mouseup'"},display);button=DIV({"id":button_id,"class":row_type+" clickable"},anchor);cell=TD(null,button);appendChildNodes(row,cell);}
if(row_type=="box")
appendChildNodes("calculator",TABLE({"class":"calcbox"},TBODY(null,row)));else
appendChildNodes(tbody,row);}
appendChildNodes("calculator",table);}
function initialize()
{load_html();angle_mode="deg";display_base="dec";update_mode_colors();set_purple_button("CL");set_purple_button("AC");gray_out("MR");}
addLoadEvent(initialize);
