/*

File: calculate.js
Author: Jamie Norrish (jamien@web.co.nz)
Date: November 2001

Description: Javascript validation routines for the foreign exchange
calculators.

*/

function checknumber(field) {

    /*
    Check that the value of field is a number greater than zero.

    Arguments:
    -- field, object representing input field to check
    */

    var value = field.value;
    // Remove any commas and use the result as the value.
    var numbervalue = field.value.split(",").join("");

    if (isNaN(numbervalue) || !(numbervalue > 0)) {
        alert("Please ensure you have only entered numbers in the entry field. Letters, symbols and punctuation will not be recognised. The amount entered must be greater than zero.");
        //        field.focus()  // This doesn't seem to work (properly)
        return false;
    }
    return true;
}

function checkvalues(form) {

    /*
    Check that there are appropriate values in the fields in form
    form.
    
    Arguments:
    -- form, object representing form to check
    */

    var errors = "";
    var numbervalue;

    // Currently only check the amount input.
    for (var i = 0; i < form.length; i++) {
        var control = form.elements[i];
        if (control.type == "text") {
            // Remove any commas and use the result as the value.
            numbervalue = control.value.split(",").join("");
            if (isNaN(numbervalue) || !(numbervalue > 0)) {
                errors += "Please ensure you have only entered numbers in the entry field. Letters, symbols and punctuation will not be recognised. The amount entered must be greater than zero.\n";
            }
        }
    }

    if (errors != "") {
        alert(errors);
        return false;
    }
    else {
        return true;
    }

}
function ButtonClicked(buttonId, txtBoxId, omnitureType) {
    if (checknumber(($("#" + txtBoxId))[0]) === true) {
        return OmnitureTrackingCode.CaptureButtonEventForToolsUsage(document.getElementById(buttonId), "calculator:" + omnitureType);
    } else {
        return false;
    }
}
//Enables a button
function EnableButton(buttonId) {
    $("#" + buttonId).removeAttr("disabled");
}

//Assigns the ButtonClicked() function to the 'click' event for the specified button
function AssignButtonClickEvent(buttonId, txtBoxId, omnitureType) {
    var button = $("#" + buttonId);
    button.click(function() {
        return ButtonClicked(buttonId, txtBoxId, omnitureType);
    });
}
