
var useWebkitAnimations;
var scrollToResults;

$(document).ready(function() {

    $('#tabs a').click(function() {

        var lastSection = $('#tabs li a.current').removeClass('current').attr('href');

        var section = $(this).addClass('current').attr('href');


        if (useWebkitAnimations) {
            $(lastSection).bind('webkitTransitionEnd', function(event) {
                $(lastSection).hide();
                $(section).show().addClass('on');
                $(lastSection).unbind('webkitTransitionEnd');

            }).removeClass('on');
        }

        else {

            $(lastSection).hide().removeClass('on');
            $(section).show().addClass('on');
        }

        return false;
    });

    $('#slabs form').submit(function() {

        if (calculateSlabPour()) {
            trackEvent('slabs');
            showResults(this);
        }

        return false;
    });

    $('#footings form').submit(function() {

        if (calculateFootingPour()) {
            trackEvent('footings');
            showResults(this);

        }

        return false;
    });

    $('#columns form').submit(function() {

        if (calculateColumnPour()) {
            trackEvent('columns');
            showResults(this);
        }

        return false;
    });

});


function showResults(el) {

    $('.calculate', el).focus();

    $('.results', el).show();

    if (scrollToResults) {

        var scrollTo = $('.results', el).offset().top;
        setTimeout(function() {
            $(window).scrollTop(scrollTo);
        }, 0);

    }

}


function calculateSlabPour() {


    var form = document.SlabCalculator;

    if (!validateField(form.Thickness, "Please enter a number value in inches for the depth."))
        return false;

    if (!validateField(form.Width, "Please enter a number value in feet for the width."))
        return false;

    if (!validateField(form.Length, "Please enter a number value in feet for the length."))
        return false;


    //starting values
    var ithickness = form.Thickness.value
    var iwidth = form.Width.value;
    var ilength = form.Length.value;

    //massage floating point values                                                               
    ithickness = ithickness / 12;

    var icubicyards = floatFix(ithickness * iwidth * ilength / 27, 2);
    var iBags40 = floatFix((icubicyards * 90), 1);
    var iBags60 = floatFix((icubicyards * 60), 1);
    var iBags80 = floatFix((icubicyards * 45), 1);

    //assign
    form.CubicYards.value = icubicyards;
    form.Bags40.value = Math.ceil(iBags40);
    form.Bags60.value = Math.ceil(iBags60);
    form.Bags80.value = Math.ceil(iBags80);

    return true;

}

function calculateFootingPour() {

    var form = document.FootingCalculator;

    if (!validateField(form.Depth, "Please enter a number value in inches for the depth."))
        return false;

    if (!validateField(form.Width, "Please enter a number value in inches for the width."))
        return false;

    if (!validateField(form.Length, "Please enter a number value in feet for the length."))
        return false;


    //starting values
    var idepth = form.Depth.value;
    var iwidth = form.Width.value;
    var ilength = form.Length.value;

    //massage floating point values                                                               
    idepth = floatFix(idepth / 12, 2);
    iwidth = floatFix(iwidth / 12, 2);

    var icubicyards = floatFix(((iwidth * idepth) * ilength) / 27, 2);
    var iBags40 = floatFix((icubicyards * 90), 1);
    var iBags60 = floatFix((icubicyards * 60), 1);
    var iBags80 = floatFix((icubicyards * 45), 1);

    //assign
    form.CubicYards.value = icubicyards;
    form.Bags40.value = Math.ceil(iBags40);
    form.Bags60.value = Math.ceil(iBags60);
    form.Bags80.value = Math.ceil(iBags80);

    return true;
}

function calculateColumnPour() {

    var form = document.ColumnCalculator;

    if (!validateField(form.Diameter, "Please enter a number value in inches for the diameter."))
        return false;

    if (!validateField(form.Height, "Please enter a number value in inches for the height."))
        return false;


    //starting values
    var idiam = form.Diameter.value;
    var iheight = form.Height.value;

    var icubicyards = floatFix(((idiam / 2) * (idiam / 2) * Math.PI * iheight * .0000214334705), 2);
    var iBags40 = floatFix((icubicyards * 90), 1);
    var iBags60 = floatFix((icubicyards * 60), 1);
    var iBags80 = floatFix((icubicyards * 45), 1);

    //assign
    form.CubicYards.value = icubicyards;
    form.Bags40.value = Math.ceil(iBags40);
    form.Bags60.value = Math.ceil(iBags60);
    form.Bags80.value = Math.ceil(iBags80);

    return true;
}





function validateField(field, errormsg) {

    var value = field.value;

    if ((isNaN(value) == true) || (value == '')) {
        alert(errormsg);
        field.value = '';
        field.focus();
        return false;
    }

    return true;
}


function floatFix(value, places) {

    outstring = Math.round(value * 100) / 100
    return (outstring);
}



function trackEvent(action) {

    //if (pageTracker) {
    //    pageTracker._trackEvent('Calculator', action);
    //}
      
}



