$(function() {
    $("#savingscalculator").submit(function() {
        calculateSavings();
        return false;
    }).hover(function() { $(this).css("background-position", "top right"); }, function() { $(this).css("background-position", "top left"); });
});

function calculateSavings() {
    var currentDownloadLimit = $("#currentDownloadLimit").val();
    var monthlyBroadbandCost = $("#monthlyBroadbandCost").val();
    var monthlyCallCosts = $("#monthlyCallCosts").val();
    var monthlyPhoneLineRental = $("#monthlyPhoneLineRental").val();
    var errors = [];

    if (!(monthlyBroadbandCost && monthlyBroadbandCost.match(/^\d*\.?\d*$/))) {
        errors.push("Please enter your current monthly broadband cost.");
    }

    if (!(currentDownloadLimit && currentDownloadLimit.match(/^\d*\.?\d*$/))) {
        errors.push("Please enter your current quota.");
    }

    if (!(monthlyPhoneLineRental && monthlyPhoneLineRental.match(/^\d*\.?\d*$/))) {
        errors.push("Please enter your current monthly line rental cost.");
    }

    if (!(monthlyCallCosts && monthlyCallCosts.match(/^\d*\.?\d*$/))) {
        errors.push("Please enter your current monthly call costs.");
    }

    if (errors.length > 0) {
        $("#savingsresults").html("<p class='warning-title'>Error</p><p class='warning-message'>" + errors.join("<br />") + "</p>");
    }
    else {
        $.get("savings_calculator/calculator.cgi", {
            monthlyBroadbandCost: monthlyBroadbandCost,
            currentDownloadLimit: currentDownloadLimit,
            monthlyPhoneLineRental: monthlyPhoneLineRental,
            monthlyCallCosts: monthlyCallCosts,
            numberLocalCalls: $("#numberLocalCalls").val(),
            numberNationalCalls: $("#numberNationalCalls").val(),
            numberOfMinutes: $("#numberOfMinutes").val(),
            location1: $("#location1").val(),
            location1minutes: $("#location1minutes").val(),
            location2: $("#location2").val(),
            location2minutes: $("#location2minutes").val(),
            location3: $("#location3").val(),
            location3minutes: $("#location3minutes").val()
        }, function(data, status) { $("#savingsresults").html(data); });
    }

    $("#savingsresults").slideDown("normal", function() { $.scrollTo("#savingsresults"); });
}

// vim: set et ts=4 sw=4 cin nopaste:
