﻿/// <reference path="jquery-1.3.1.min.js" />

/* CurrencyConverterAjax.js */

//Submits a button for a given control. 

var calculatorAvailable = true;

//Disables the calculator if something is wrong
function DisableCalculator(){
    calculatorAvailable = false;
}

//Disables the calculator if something is wrong
function EnableCalculator(){
    calculatorAvailable = true;
    $("#currencyConverter").show();
}

//Disables a button
function DisableButton(button){
    button.attr("disabled", "true");
}

//Enables a button
function EnableButton(button){
    button.removeAttr("disabled"); 
}

//Clears the result label
function ClearResultLabel() {
    var context = $('#currencyConverter')[0];
    $("#currency-result", context).text(" ");
}

function DoAjaxCall(pageMethod, params, button){
    //Disable the button for the duration of the Ajax call
    DisableButton(button);
    //Clear the result label
    ClearResultLabel();
    
    $.ajax({
      url: pageMethod,
      data: params,
      success: function(msg) {
        // Replace the div's content with the page method's return.
        $resultLabel.prepend(msg.Message);
        //Reenable the button after the Ajax call
        EnableButton(button);
      }
    });
}


$(function() {
    //Only if the calculator is available should it be activated
    if (calculatorAvailable) {
        var context = $('#currencyConverter')[0];
        $resultLabel = $("#currency-result", context);
        //console.log(context);

        //"Tabify" the control
        $(context).tabs();

        //Clear the results if the tab changes
        $(context).bind('tabsselect', function(event, ui) {
            ClearResultLabel();
        });

        //Clear the results if either dropdown is selected
        $("select", context).change(function() {
            ClearResultLabel();
        });

        //Set the default buttons on the converter for each tab
        SetDefaultButton("fromNz");
        SetDefaultButton("toNz");

        // Add the page method call as an onclick handler for the button.
        $("#btnConvertFromNZD").click(function() {
            //Prepare Parameters
            var currencyToValue = $("#ctl00_PageContentPlaceHolder_currencyconverter1_cmbCurrencyTo").val();
            var amount = $("#txtFromNZDAmount").val();
            var params = "{'amountIn': '" + amount + "', " + "'currencyTo': '" + currencyToValue + "'}";
            DoAjaxCall("/Default.aspx/ConvertFromNzd", params, $("#btnConvertFromNZD"));
            //Prevent further click events
            return false;
        });

        // Add the page method call as an onclick handler for the button.
        $("#btnConvertToNZD").click(function() {
            //Prepare Parameters
            var currencyFromValue = $("#ctl00_PageContentPlaceHolder_currencyconverter1_cmbCurrencyFrom").val();
            var amount = $("#txtToNZDAmount").val();
            var params = "{'amountIn': '" + amount + "', " + "'currencyFrom': '" + currencyFromValue + "'}";
            DoAjaxCall("/Default.aspx/ConvertToNzd", params, $("#btnConvertToNZD"));
            //Prevent further click events
            return false;
        });
        //Enable the calculator
        EnableCalculator();
    }
});

