/*
 * Calc.js
 *
 * Copyright (c) 2008 Poturi, All Rights Reserved.
 * http://www.poturi.net/
 *
 * $B%=!<%9%3!<%I$N2~JQ!&:FHRI[$O<+M3$G$9$,!"(B
 * $B$=$N:]$O$3$N(BCopyright$B$rL@5-$7$F$/$@$5$$!#(B
 * $B>&MQMxMQ$OIT2D$G$9!#(B
 */

function Calc() {
    this.mode = "num";
    this.ope = "plus";
    this.num = 0;
    this.result = 0;
}

Calc.prototype.set_output = function(str) {
    var text = document.createTextNode(str);
    var output = document.getElementById("output");
    if (output.hasChildNodes()) {
        output.replaceChild(text, output.firstChild);
    }
    else {
        output.appendChild(text);
    }
}

Calc.prototype.set_num = function(n) {
    switch (this.mode) {
        case "ope":
            this.mode = "num";
            if (n) { this.num = "" + n; }
            else { this.num = 0; }
            break;
        case "num":
            if (!this.num) {
                if (n) { this.num = "" + n; }
            }
            else {
                this.num += n;
            }
            break;
        default:
            break;
    }
    this.set_output(this.num);
}

Calc.prototype.set_dot = function() {
    switch (this.mode) {
        case "ope":
            this.mode = "num";
            this.num = "0.";
            break;
        case "num":
            if (!this.num) { this.num = "0."; }
            else {
                var check = this.num.match(/\./);
                if (!check) { this.num += "."; }
            }
            break;
        default:
            break;
    }
    this.set_output(this.num);
}

Calc.prototype.set_ope = function(arg) {
    switch (this.mode) {
        case "num":
            if (arg == "sqrt") {
                this.ope = arg;
                this.result = this.compute();
                this.set_output(this.result);
                this.num = this.result;
                this.mode = "ope";
            }
            else if (arg == "percent") {
                if (this.ope == "multiply") {
                    this.result = (this.compute() / 100);
                    this.set_output(this.result);
                }
            }
            else {
                this.mode = "ope";
                this.result = this.compute();
                this.set_output(this.result);
                if (arg != "equal") { this.ope = arg; }
            }
            break;
        case "ope":
            if (arg == "equal") {
                this.result = this.compute();
                this.set_output(this.result);
                if (this.ope == "sqrt") { this.num = this.result; }
            }
            else {
                this.ope = arg;
                this.num = this.result;
                if (arg == "sqrt") {
                    this.result = this.compute();
                    this.set_output(this.result);
                    this.num = this.result;
                }
            }
            break;
        default:
            break;
    }
}

Calc.prototype.compute = function() {
    this.num = this.num -0;
    switch (this.ope) {
        case "plus":
            return this.result += this.num;
            break;
        case "minus":
            return this.result -= this.num;
            break;
        case "multiply":
            return this.result *= this.num;
            break;
        case "divide":
            return this.result /= this.num;
            break;
        case "sqrt":
            return Math.sqrt(this.num);
            break;
        case "equal":
            break;
        default:
            break;
    }
}

Calc.prototype.clear = function(arg) {
    switch (arg) {
        case "c":
            this.mode = "num";
            this.num = 0;
            this.ope = "plus";
            this.result = 0;
            break;
        case "ce":
            this.num = 0;
            break;
        default:
            break;
    }
    this.set_output(this.num);
}

Calc.prototype.prepare_calc = function() {
    if (!document.getElementById) return false;
    this.set_output(this.num);
    this.set_onclick();
}

Calc.prototype.set_onclick = function(arg) {
    var self = this;
    var zero = document.getElementById("zero");
    var one = document.getElementById("one");
    var two = document.getElementById("two");
    var three = document.getElementById("three");
    var four = document.getElementById("four");
    var five = document.getElementById("five");
    var six = document.getElementById("six");
    var seven = document.getElementById("seven");
    var eight = document.getElementById("eight");
    var nine = document.getElementById("nine");
    var dot = document.getElementById("dot");
    var equal = document.getElementById("equal");
    var plus = document.getElementById("plus");
    var minus = document.getElementById("minus");
    var multiply = document.getElementById("multiply");
    var divide = document.getElementById("divide");
    var percent = document.getElementById("percent");
    var sqrt = document.getElementById("sqrt");
    var c = document.getElementById("c");
    var ce = document.getElementById("ce");
    zero.onclick = function() { self.set_num(0); };
    one.onclick = function() { self.set_num(1); };
    two.onclick = function() { self.set_num(2); };
    three.onclick = function() { self.set_num(3); };
    four.onclick = function() { self.set_num(4); };
    five.onclick = function() { self.set_num(5); };
    six.onclick = function() { self.set_num(6); };
    seven.onclick = function() { self.set_num(7); };
    eight.onclick = function() { self.set_num(8); };
    nine.onclick = function() { self.set_num(9); };
    dot.onclick = function() { self.set_dot(); };
    equal.onclick = function() { self.set_ope("equal"); };
    plus.onclick = function() { self.set_ope("plus"); };
    minus.onclick = function() { self.set_ope("minus"); };
    multiply.onclick = function() { self.set_ope("multiply"); };
    divide.onclick = function() { self.set_ope("divide"); };
    percent.onclick = function() { self.set_ope("percent"); };
    sqrt.onclick = function() { self.set_ope("sqrt"); };
    c.onclick = function() { self.clear("c"); };
    ce.onclick = function() { self.clear("ce"); };
}
