var oCalc = null;

/* initialize calculator */
function initCalculator() {
	oCalc = new Calculator( document.getElementById('lcd') );
}

/**
 * Constructor calculator
 */
function Calculator( oTextField ) {
	this.oTextField = ( oTextField != null ) ? oTextField : document.getElementById('calcTextField');
	this.iStoredMemory = 0;
	this.iMemory = 0;
	this.iAction = 0;
	this.iPrevAction = 0;
	
	// actions
	this.IDLE = 0;
	this.ADD = 1;
	this.SUBSTRACT = 2;
	this.MULTIPLY = 3;
	this.SQRT = 4; 
	this.PERCENT = 5
	this.DIVIDE =6;
	
	this.readingInput = false;
	this.containsDecimal = false;
	
	this.isOn = true;
}

/**
 * set current action 
 * @param int iAction (possible values are IDLE, ADD, MULTIPLY, SQRT)
 */
Calculator.prototype.setAction = function ( iAction, iValue ){
	this.iAction = iAction;
	switch ( iAction ) {
		case this.ADD:
			this.iMemory += Number( iValue );
			break;
		case this.SUBSTRACT:
			if ( this.iMemory != 0 ){
				this.iMemory -= Number( iValue );
			}
			else{
				this.iMemory = iValue;
			}
			break;
		case this.MULTIPLY:
			if ( this.iMemory == 0 ) {
				this.iMemory = Number( iValue );
			} else {
				this.iMemory = Number(this.iMemory) * Number( iValue );
			}
			break;
		case this.SQRT:
			this.iMemory = Math.sqrt( iValue ); 
			break;
		case this.PERCENT:
			if( this.iMemory > 0 ){
				nwValue = ( this.iMemory / 100 ) * iValue ;
				this.iAction = this.iPrevAction;
			}
			break;
		case this.DIVIDE:
			if ( iValue > 0 && this.iMemory > 0) {
				this.iMemory /= iValue;
			} else {
				this.iMemory = iValue;
			}
			break;
	}

	this.readingInput = false;
	this.iPrevAction = iAction;
}


Calculator.prototype.setValue = function( iNumber ) {
	this.oTextField.value = iNumber;
}

Calculator.prototype.enterDecimal = function() {
	if(! this.containsDecimal ){
		this.oTextField.value += '.';
		this.containsDecimal = true;
	}
}

Calculator.prototype.enterNumber = function( iNumber ) {
	if ( this.oTextField.value == 0 && (! this.containsDecimal) ) {
		this.oTextField.value = '';
	}
	
	if ( this.readingInput ) {
		this.oTextField.value += iNumber;
	} else {
		this.oTextField.value = iNumber;
		this.containsDecimal = false;
	}

	this.readingInput = true;
}

Calculator.prototype.getEnteredValue = function () {
	return this.oTextField.value;
}

Calculator.prototype.on = function () {
	this.isOn = true;
	this.oTextField.disabled = !(this.isOn);
	if(!this.isOn) {
		this.oTextField.value = 'Uit';
	} else {
		this.oTextField.value = '0';
	}
}

Calculator.prototype.off = function() {
	this.isOn = false;
	this.oTextField.disabled = !(this.isOn);
	if(!this.isOn) {
		this.oTextField.value = 'Uit';
	} else {
		this.oTextField.value = '0';
	}
}

Calculator.prototype.mrc = function() {
	this.iStoredMemory = 0; 
}

Calculator.prototype.mmin = function() {
	this.iStoredMemory = this.getEnteredValue();
}

Calculator.prototype.mplus = function() {
	this.oTextField.value = this.iStoredMemory;
}

Calculator.prototype.setMemory = function ( ) {
	this.iMemory = this.oTextField.value;
}

Calculator.prototype.add = function(){
	this.setAction( this.ADD, this.getEnteredValue() );
}

Calculator.prototype.substract = function(){
	this.setAction( this.SUBSTRACT, this.getEnteredValue()  );
}

Calculator.prototype.multiply = function(){
	this.setAction( this.MULTIPLY, this.getEnteredValue()  );
}

Calculator.prototype.squareRoot = function(){
	this.setAction( this.SQRT, this.getEnteredValue()  );
}

Calculator.prototype.divide = function(){
	this.setAction( this.DIVIDE, this.getEnteredValue() );
}

Calculator.prototype.percent = function(){
	this.setAction( this.PERCENT, this.getEnteredValue()  );
}

Calculator.prototype.result = function() {
	this.setAction( this.iAction, Math.abs( this.getEnteredValue() )  );
	this.oTextField.value = this.iMemory;
	this.iMemory = 0;
}

Calculator.prototype.clear = function() {
	this.oTextField.value = 0;
	this.iMemory = 0;
	this.iStoredMemory = 0;
	this.containsDecimal = false;
	this.iAction = this.IDLE;
}

