﻿ 
function getMulch(senderName)
{
switch (senderName)
{
case "sqft":
var sqft= document.getElementById('sqfttextbox').value;
var thickness= document.getElementById('sqftselect').value;
var pattern = new RegExp("^[-]?([1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|\.[0-9]{1,2})$")
    var result = pattern.test(sqft);
    if (result)
    {
    var total = sqft * thickness;
    total = Math.round(total);
    var twocubicbags = Math.round(total / 2);
    var threecubicbags = Math.round(total / 3);
    var cubicyards = Math.round(total / 27);
    // This variable holds a math value, cubicyards is evaluated for presentation to the client in an alert box.
    // It can contain special characters.
    var cuydsForRunningTotal = Math.round(total /27)
    if (cubicyards < 1)
    {
    cubicyards = "< 1";
    cuydsForRunningTotal = 1
    }
    var innermessage = "A cubic yard is an average, midsize pickup truck load.";
    alert("Total Sq. Footage = " + sqft + '\n' + '\n' + "Your project requires" + '\n' + twocubicbags + " - 2 cubic foot bags" + '\n' + threecubicbags + " - 3 cubic foot bags" + '\n' + "Or - " + cubicyards + " cubic yards" + '\n' + '\n' + innermessage);
    calculateRunningTotal(sqft, cuydsForRunningTotal); 
    }
    else
    {
    alert("Please enter a number to calculate the amount of mulch you need.");
    }
break;
case "circle":
diameter = document.getElementById('circletextbox').value;
thickness = document.getElementById('circleselect').value;
var pattern = new RegExp("^[-]?([1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|\.[0-9]{1,2})$")
    var result = pattern.test(diameter);
    if (result)
    {
    diameter = diameter / 2;
    diameter = (diameter * diameter) * Math.PI;
    var answer = Math.round(diameter)
    diameter = Math.round(diameter)
    
    var total = Math.round(diameter * thickness);
    var twocubicbags = Math.round(total / 2);
    var threecubicbags = Math.round(total / 3);
    var cubicyards = Math.round(total / 27);
     // This variable holds a math value, cubicyards is evaluated for presentation to the client in an alert box.
    // It can contain special characters.
    var cuydsForRunningTotal = Math.round(total /27)
    if (cubicyards < 1)
    {
    cubicyards = "< 1";
    cuydsForRunningTotal = 1
    }
    var innermessage = "A cubic yard is an average, midsize pickup truck load.";
    alert("Total Sq. Footage = " + answer + '\n' + '\n' + "Your project requires" + '\n' + twocubicbags + " - 2 cubic foot bags" + '\n' + threecubicbags + " - 3 cubic foot bags" + '\n' + "Or - " + cubicyards + " cubic yards" + '\n' + '\n' + innermessage);
    calculateRunningTotal(answer,cuydsForRunningTotal);
    }
    else
    {
    alert("Please enter a number to calculate the amount of mulch you need.");
    }
break;
case "rectangle":
var length = document.getElementById('length').value;
var width = document.getElementById('width').value;
var thickness = document.getElementById('rectangleselect').value;
var pattern = new RegExp("^[-]?([1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|\.[0-9]{1,2})$")
    var result = pattern.test(length);
    var result2 = pattern.test(width);
    if (result && result2)
    {
    var answer = length * width;    
    total = Math.round(answer * thickness);
    
    var twocubicbags = Math.round(total / 2);
    var threecubicbags = Math.round(total / 3);
    var cubicyards = Math.round(total / 27);
     // This variable holds a math value, cubicyards is evaluated for presentation to the client in an alert box.
    // It can contain special characters.
    var cuydsForRunningTotal = Math.round(total /27)
    if (cubicyards < 1)
    {
    cubicyards = "< 1";
    cuydsForRunningTotal = 1
    }
    var innermessage = "A cubic yard is an average, midsize pickup truck load.";
    alert("Total Sq. Footage = " + answer + '\n' + '\n' + "Your project requires" + '\n' + twocubicbags + " - 2 cubic foot bags" + '\n' + threecubicbags + " - 3 cubic foot bags" + '\n' + "Or - " + cubicyards + " cubic yards" + '\n' + '\n' + innermessage);
    calculateRunningTotal(answer, cuydsForRunningTotal);
    }
    else
    {
    alert("Please enter a number to calculate the amount of mulch you need.");
    }
break;
case "triangle":
sideone = document.getElementById('side1textbox').value;
sidetwo = document.getElementById('side2textbox').value;
thickness = document.getElementById('triangleselect').value;
var pattern = new RegExp("^[-]?([1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|\.[0-9]{1,2})$")
    var result = pattern.test(sideone);
    var result2 = pattern.test(sidetwo);
    if (result && result2)
    {
    var answer = Math.round((sideone * sidetwo) * 0.5);    
    total = Math.round(answer * thickness);
    
    var twocubicbags = Math.round(total / 2);
    var threecubicbags = Math.round(total / 3);
    var cubicyards = Math.round(total / 27);
     // This variable holds a math value, cubicyards is evaluated for presentation to the client in an alert box.
    // It can contain special characters.
    var cuydsForRunningTotal = Math.round(total /27)
    if (cubicyards < 1)
    {
    cubicyards = "< 1";
    cuydsForRunningTotal = 1
    }
    var innermessage = "A cubic yard is an average, midsize pickup truck load."
    alert("Total Sq. Footage = " + answer + '\n' + '\n' + "Your project requires" + '\n' + twocubicbags + " - 2 cubic foot bags" + '\n' + threecubicbags + " - 3 cubic foot bags" + '\n' + "Or - " + cubicyards + " cubic yards" + '\n' + '\n' + innermessage);
    calculateRunningTotal(answer, cuydsForRunningTotal);
    }
    else
    {
    alert("Please enter a number to calculate the amount of mulch you need.");
    }
break;
default:
alert("Unable to complete with the data provided")
}
}

function calculateRunningTotal(sqft,cuyds)
{
var currentSqFt = document.getElementById("txttotal").value;
var currentCuYds = document.getElementById("txtcubicyards").value;
if (currentSqFt != "" || currentCuYds != "")
{
document.getElementById("txttotal").value = (parseInt(currentSqFt) + parseInt(sqft));
document.getElementById("txtcubicyards").value = (parseInt(currentCuYds) + parseInt(cuyds));
}
else
{
document.getElementById("txttotal").value = sqft;
document.getElementById("txtcubicyards").value = cuyds;
}
}

