var operand_keys = "0123456789.";
var operator_keys = "+-*/";
var operand = new Array(2);
var operator = '';
var degrees = true;



var len_limit = 8;

operand[0] = '0';
operand[1] = '';



function keyb_input()
{
	var pressed_key;
	var pressed_char;
	pressed_key = keyCode;

	switch (pressed_key)
	{
		case 190:
		case 110:
				append_it('.');
							break;
		case 8:		del_it();
							break;
		case 13:	calc_it();
							break;
		case 46:	clear_all();
							break;
		case 111:
		case 106:
		case 109:
		case 107:	pressed_char = String.fromCharCode (pressed_key-64);
				set_operator (pressed_char);
							break;
		default:	pressed_char = String.fromCharCode (pressed_key-48);

							if (operand_keys.indexOf(pressed_char) != -1)
							{
								append_it (pressed_char);
							}
							else
							{
								if (operator_keys.indexOf(pressed_char) != -1)
								{
									set_operator (pressed_char);
								}
							}
	}
}

function active_operand()
{
	if (operator)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

function draw_it()
{
	display_value = operand[active_operand()] + '';



	value = Math.abs(parseFloat(display_value));
	value_string = Math.abs(parseFloat(display_value)) + '';

	if (value_string.length > len_limit)
	{
		var max_value = 9;
		for (i = 0; i < len_limit - 1; i++)
		{
			max_value = (max_value * 10) + 9;
		}

		if (value > max_value)
		{
			display_value = parseFloat(display_value).toExponential(len_limit - 5)+'';
		}
		else
		{
			digits = len_limit - (value_string.indexOf('.') + 1);
			display_value = Math.round(display_value*Math.pow(10,digits))/Math.pow(10,digits);
		}
	}



	lcdText.caption = display_value;
        SetOption('lastResult',lcdText.Caption);  	
}

function calc_it()
{
	var multiplier = 10;

	if (operator)
	{
		if (operand[1] == '')
		{
			operand[1] = operand[0];
		}
		operand0 = operand[0] + '';
		operand1 = operand[1] + '';

		if (operator == '^')
		{
			operand[0] = Math.pow(operand[0], operand[1]);
		}
		else
		{

			multiplier =  Math.pow (multiplier, Math.max(operand0.substring(operand0.indexOf('.'),operand0.length).length, operand1.substring(operand1.indexOf('.'),operand1.length).length));
			operand[0] = operand[0] * multiplier;
			operand[1] = operand[1] * multiplier;



			operand[0] = eval(operand[0] + operator + operand[1]);					

			switch (operator)
			{
				case '/':		operand[0] = operand[0] + "";
										break;
				case '*':		operand[0] = operand[0] / multiplier / multiplier + "";
										break;
				case '-':		operand[0] = operand[0] / multiplier + "";
										break;
				case '+':		operand[0] = operand[0] / multiplier + "";
									break;
			}
		}
		
		result = operand[0];
		operand[1] = '';
		operand[0] = '';
		operator = '';

		operand[active_operand()] = result;


		draw_it();
	}
}


function del_it()
{
	operand[active_operand()] = operand[active_operand()].substring(0, operand[active_operand()].length - 1);
	if (operand[active_operand()] == "")
	{
		operand[active_operand()] = '0';
	}
	draw_it();
}

function clear_entry()
{
	operand[active_operand()] = '0';
	draw_it();
}

function clear_all()
{
	operand[0] = '0';
	operand[1] = '';
	operator = '';
	draw_it();
}

function append_it(char)
{
	if ((operand[active_operand()] == "0") && (char != '.'))
	{
		if (operand_keys.indexOf(char) != -1)
		{
			operand[active_operand()] = char;
		}
	}
	else
	{
		if ((operand_keys.indexOf(char) != -1) && ((Math.abs(operand[active_operand()]) + '').length < len_limit))
		{
			if ((char != '.') || ((char == '.') && (operand[active_operand()].indexOf('.') == -1)))
			{
				operand[active_operand()] += char;
			}
		}
	}
	draw_it();
}

function set_operator(op)
{
	if ((operator == op) && (operand[active_operand()]))
	{
		calc_it();
	}
	operator = op;
}

function mod_it()
{
	if ((operator == '%') || (operand[active_operand()]))
	{
		calc_it();
	}
	operator = '%';
}

function neg_it()
{
	if (operand[active_operand()].charAt(0) == '-')
	{
		operand[active_operand()] = operand[active_operand()].substring(1, operand[active_operand()].length) + '';
	}
	else
	{
		if (operand[active_operand()] != '0')
		{
			operand[active_operand()] = '-' + operand[active_operand()];
		}
	}
	draw_it();
}

function sqrt_it()
{
	operand[active_operand()] = Math.sqrt(operand[active_operand()]) + "";
	draw_it();
}

function inv_it()
{
	operand[active_operand()] = 1 / eval(operand[active_operand()]) + "";
	draw_it();
}

function log_it()
{
	operand[active_operand()] = Math.log(operand[active_operand()]) / Math.log(10) + "";
	draw_it();
}

function ln_it()
{
	operand[active_operand()] = Math.log(operand[active_operand()]) + "";
	draw_it();
}

function n_it()
{
	operand[active_operand()] = factorial(operand[active_operand()]) + "";
	draw_it();
}

function factorial(x)
{

	if ((x >= 180) || (x != Math.round(x)))
	{
		return 'Error';
	}

	t = 1;

	while (x > 1)
	{
		t *= x--;
	}

	return t;
}


function tan_it()
{
	if (degrees)
		operand[active_operand()] = Math.tan(operand[active_operand()] * Math.PI/180) + "";
	else
		operand[active_operand()] = Math.tan(operand[active_operand()]) + "";
	draw_it();
}

function cos_it()
{
	if (degrees)
		operand[active_operand()] = Math.cos(operand[active_operand()] * Math.PI/180) + "";
	else
		operand[active_operand()] = Math.sin(operand[active_operand()]) + "";
	draw_it();
}

function sin_it()
{
	if (degrees)
		operand[active_operand()] = Math.sin(operand[active_operand()] * Math.PI/180) + "";
	else
		operand[active_operand()] = Math.sin(operand[active_operand()]) + "";
	draw_it();
}


function x2_it()
{
	operand[active_operand()] = Math.pow(operand[active_operand()], 2) + "";
	draw_it();
}


function x3_it()
{
	operand[active_operand()] = Math.pow(operand[active_operand()], 3) + "";
	draw_it();
}


function xy_it(char)
{
	if ((operator == '^') || (operand[active_operand()]))
	{
		calc_it();
	}
	operator = '^';
}

function pi_it(char)
{
	operand[active_operand()] = Math.PI;
	draw_it();
}


function SettingsClosed()
{
/*	resultsIn = System.Gadget.Settings.read("resultsIn");

	if (resultsIn != '' && resultsIn == 'radians')
	{
		degrees = false;
	}
	else
	{
		degrees = true;		
	}
	
*/	
}

function checkState()
{
}

function widgetOnDock()
{
  LockRefresh;
  len_limit = 8;
  widgetWidth = 135;
  bg.Src = 'images\\background.png';
  UnLockRefresh;
  lcdText.width = 106;
//  ShowMsg('dock');
}

function widgetOnUnDock()
{
  LockRefresh;
  len_limit = 14;
  widgetWidth = 220;
  bg.Src = 'images\\background-big.png';
  UnLockRefresh;
  lcdText.Width = 190;
//  ShowMsg('undock');
}

function widgetOnInit()
{
  operand[0] = GetOption('lastResult');
//  operand[1] = operand[0];
  lcdText.Caption = GetOption('lastResult');
}

function bt0OnMouseDown()
{
  append_it('0');   
}

function bt1OnMouseDown()
{
    append_it('1');   

}

function bt2OnMouseDown()
{
  append_it('2');   
}

function bt3OnMouseDown()
{
  append_it('3');    
}

function bt4OnMouseDown()
{
  append_it('4');   
}



function bt5OnMouseDown()
{
  append_it('5');
}

function bt6OnMouseDown()
{
  append_it('6');
}

function bt7OnMouseDown()
{
  append_it('7');
}

function bt8OnMouseDown()
{
  append_it('8');
}

function bt9OnMouseDown()
{
  append_it('9');
}

function btDelOnMouseDown()
{
  del_it();
}

function btSqrtOnMouseDown()
{
  sqrt_it();
}

function btCEOnMouseDown()
{
  clear_entry();
}

function btCOnMouseDown()
{
  clear_all();
}

function btModOnMouseDown()
{
  mod_it();
}

function btNegOnMouseDown()
{
  neg_it();
}

function btInvOnMouseDown()
{
  inv_it();
}

function btDotOnMouseDown()
{
  append_it('.');
}

function btDivOnMouseDown()
{
  set_operator('/');
}

function btMulOnMouseDown()
{
  set_operator('*');
}

function btSubOnMouseDown()
{
  set_operator('-');
}

function btAddOnMouseDown()
{
  set_operator('+');
}

function btCalOnMouseDown()
{
  calc_it();
}

function btLogOnMouseDown()
{
  log_it();
}

function btInOnMouseDown()
{
  ln_it();
}

function btNOnMouseDown()
{
  n_it();
}

function btXYOnMouseDown()
{
  xy_it();
}

function btTanOnMouseDown()
{
  tan_it();
}

function btX2OnMouseDown()
{
  x2_it();
}

function btCosOnMouseDown()
{
  cos_it();
}

function btX3OnMouseDown()
{
  x3_it();
}

function btSinOnMouseDown()
{
  sin_it();
}

function btPiOnMouseDown()
{
  pi_it();
}

function OnAllSettingsSaved()
{              
  var res = GetOption('radians');
  if(res=='0')
    degrees = true
  else
    degrees = false;
   
}