﻿
var Calculator = (function () {
    // private stuff goes here

    return {
        add: function (num1, num2) {
            if (isNaN(parseFloat(num1)) || isNaN(parseFloat(num2))) {
                throw new Error("Not a number");
            };
            if ((Math.abs(num1) > 10000000000 || Math.abs(num2) > 10000000000) ) {
                throw new Error("Number exceeds maximum length");
            }
            var answer = num1 + num2;
            return answer;
         
                
        },

        subtract: function (num1, num2) {
            if (isNaN(parseFloat(num1)) || isNaN(parseFloat(num2))){
                throw new Error("Not a number");
            };
            if ((Math.abs(num1) > 10000000000 || Math.abs(num2) > 10000000000)) {
                throw new Error("Number exceeds maximum length");
            }
            var answer = num1 - num2;
            return answer;
        },

        multiply: function (num1, num2) {
            if (isNaN(parseFloat(num1)) || isNaN(parseFloat(num2))) {
                console.log("here");
                throw new Error("Not a number");
            };
            if ((Math.abs(num1) > 10000000000 || Math.abs(num2) > 10000000000)) {
                throw new Error("Number exceeds maximum length");
            }
            var answer = num1 * num2;
            return answer;
        },

        divide: function (num1, num2) {
            if (isNaN(parseFloat(num1)) || isNaN(parseFloat(num2))) {
                throw new Error("Not a number");
            };
            if ((parseFloat(num2)) == 0) {
                throw new Error("Division By Zero");
            }
            if ((Math.abs(num1) > 10000000000 || Math.abs(num2) > 10000000000)) {
                throw new Error("Number exceeds maximum length");
            }
            var answer = num1 / num2; 
            return answer;
        }
    }
})();

var input1 = document.getElementById("number1");
var input2 = document.getElementById("number2");
var answer = document.getElementById("answer");

//add event listeners to the imput boxes, so when user clicks in them, the
//default 0.0 is cleared.
input1.addEventListener('click', function () {
    input1.value = ' ';
    //only clear other input box if starting a new calculate
    //we know this because the answer box will already have an answer from previous calculate
    if (answer.value != '0.0') {
        input2.value = '0.0';
    };
    answer.value = '0.0';
})
input2.addEventListener('click', function () {
    input2.value = ' ';
    //only clear other input box if starting a new calculate
    //we know this because the answer box will already have an answer from previous calculate
    if (answer.value != '0.0') {
        input1.value = '0.0';
    };
    answer.value = '0.0';
})

var addbutton = document.getElementById("add");
var subtractbutton = document.getElementById("subtract");
var multiplybutton = document.getElementById("multiply");
var dividebutton = document.getElementById("divide");

addbutton.addEventListener('click', function (e) {
    e.preventDefault();
    var num1 = parseFloat(document.getElementById("number1").value);
    var num2 = parseFloat(document.getElementById("number2").value);
    try {
        var result = Calculator.add(num1, num2);
        document.getElementById("answer").value = result;
    }
    catch (e) {
        alert(e.message);
        clear();
    };
})

subtractbutton.addEventListener('click', function (e) {
    e.preventDefault();
    var num1 = parseFloat(document.getElementById("number1").value);
    var num2 = parseFloat(document.getElementById("number2").value);

    try {
        var result = Calculator.subtract(num1, num2);
        document.getElementById("answer").value = result;
    }
    catch (e) {
        alert(e.message);
        clear();
    };
})

multiplybutton.addEventListener('click', function (e) {
    e.preventDefault();
    var num1 = parseFloat(document.getElementById("number1").value);
    var num2 = parseFloat(document.getElementById("number2").value);

    try {
        var result = Calculator.multiply(num1, num2);
        document.getElementById("answer").value = result;
    }
    catch (e) {
        alert(e.message);
        clear();
    };
})

dividebutton.addEventListener('click', function (e) {
    e.preventDefault();
    var num1 = parseFloat(document.getElementById("number1").value);
    var num2 = parseFloat(document.getElementById("number2").value);

    try {
        var result = Calculator.divide(num1, num2);
        document.getElementById("answer").value = result;
    }
    catch (e) {
        alert(e.message);
        clear();
    };
})

function clear() {
    input1.value = '0.0';
    input2.value = '0.0';
}








