/**
 * @fileoverview
 * The Javascript to drive the call cost calculator.
 */

/**
 * Submit handler for the call cost calculator form. This function performs
 * some basic sanity checking on the input values and then dispatches a request
 * to Toolbox for the call cost itself.
 *
 * @return {boolean} False, to stop further processing of the form.
 */
function onCallCostCalculatorSubmit() {
    var origin = $("#origin").val().replace(/[^0-9]/g, '');
    var destination = $("#destination").val().replace(/[^0-9]/g, '');
    var duration = $("#duration").val().replace(/[^0-9]/g, '');
    var url = $("#cccform").attr("action");
    var errors = [];

    // Check the input values.
    if (!origin) {
        errors.push("Please fill in the number of the phone you are calling from.");
    }
    else if (!origin.match(/^[0-9]{4,}$/)) {
        errors.push("Please enter a valid number for the phone you are calling from.");
    }

    if (!destination) {
        errors.push("Please fill in the number of the phone you are calling.");
    }
    else if (!destination.match(/^[0-9]{4,}$/)) {
        errors.push("Please enter a valid number for the phone you are calling.");
    }

    if (!duration) {
        errors.push("Please fill in the duration of the phone call.");
    }
    else if (!duration.match(/^[0-9]+$/)) {
        errors.push("Please fill in numbers only for the duration.");
    }

    if (errors.length > 0) {
        // We have errors, so we'll display them and halt.
        $("#warning-message").html(errors.join("<br />"));
        $("#ccc-drop-down").slideUp("normal", function() { $("#ccc-drop-down-warning").slideDown(); });
    }
    else {
        // Show the throbber and prepare to show the result.
        $("#ajaxThrobber").css("display", "inline");
        $("#ccc-drop-down-warning, #plan-rates, #dl-title-result, #dl-title-result-dd").css("display", "none");
        $("#plan-rates dt[id$='-name'], #plan_rates dt[id$='-cost-call']").css("display", "none").html("");

        /* As this is a cross-domain call, we have to do this as a script
         * include. */
        url += "?source_number=" + origin + "&dest_number=" + destination + "&duration=" + duration + "&js=showCost&ts=" + (new Date()).getTime();
        $.getScript(url);
    }

    return false;
}

/**
 * Handler called by the "script" returned by the call cost calculator backend.
 * About all we do in here is pop the results in the appropriate HTML elements
 * and display them.
 */
function showCost(n) {
    $("#ajaxThrobber").css("display", "none");
    var planRates = $("#plan-rates");
    $("#plan-rates .plan-row").remove();
    if ( n['retval'] == 0 ) {
        planRates.append("<dt class='plan-row' style='display: inline'>There was an error: " + n['err_msg'] + "</dt><dd class='plan-row' style='display: inline'></dd>");
    }
    else {
        $.each(n, function(i, val) {
            if (i != "retval" && i != "result") {
                planRates.append("<dt class='plan-row' style='display: inline'>" + i + "</dt><dd class='plan-row' style='display: inline'>" + val.cost + "c</dd>");
            }
        });
    }
    planRates.css("display", "block");
    $("#ccc-drop-down").slideDown();
}

/* Set up the required onsubmit handler and image sprite change for the submit
 * button. */
$(function() {
    $("#cccform").submit(onCallCostCalculatorSubmit);
    $("#submit-btn").hover(function() { $(this).css("background-position", "top right"); }, function() { $(this).css("background-position", "top left"); });
});

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