function getSpinnerImageHtml() {
    return '<div id="spinner_container"><img src="/gbv/images/spinner.gif" alt="Loading..." width="24" height="24" /></div>';
}

/*= Preferences Module */
/**
 * Deletes the tag or greeting specified by messageId.
 */
function preferences_DeleteTagOrGreeting(messageId) {

    $.getJSON("/mygbv/preferences/ajax-delete-tag-or-greeting", {'messageId': messageId}, function(output){

        if (output.success) {
            $('#messageRow_' + output.messageId).html('<td colspan="4">You currently have no ' + output.messageType + ' set up</td>');
        }
    });
}




/*= FindMailbox Module */
/**
 * Updates the contents of #message_table_placeholder by an ajax call with messages
 * for the status specified by the #messageTypeSelector select box
 */
function findMailbox_ResultsTableLoader() {

    $('#findMailboxResultsPlaceholder').html(getSpinnerImageHtml());

    $('#findMailboxResultsPlaceholder').load('/mygbv/find-mailbox/search-results-ajax',
                                             {
                                              'first_name'   : $('#first_name').val(),
                                              'last_name'    : $('#last_name').val(),
                                              'mailbox_dnis' : $('#mailbox_dnis').val()
                                             });
    return false;
}

/**
 * Registration Page
 */
// All js for the registration form needs to also be called in the view
function registration_formReceive (responseText) {

    var output = eval('(' + responseText + ')');

    // if validation failed, re-show the form with validation messages
    if (!output.data.formValid) {
        alertBox('Registration', output.message);
        $('#registration_form_placeholder').html(output.data.htmlContent);
        toggleCountrySpecificFields($('#country').val());
        $('#registrationForm').submit(function() {
            $(this).ajaxSubmit({
                success:registration_formReceive
            });
            return false;
        });
    } else {
        // The form is valid, if output.data.processed is set to true then we
        // are ok to show the confirmation page
        if (output.data.processed) {
            $('#registration_content_placeholder').html(output.data.htmlContent);
        } else {
            // This is a credit card registration, so we need to create a hidden
            // form which automatically posts to the worlpay site
            /*
            @todo: use this for worldpay implmentation
            alert('credit card registrations');
            fadeMainContent();
            loaderBox('Thankyou for your registration', 'You are now being redirected '  +
              'to the worldpay site to complete your secure registration. Remember, we ' +
              'will not take any payment from you if you cancel your account within '    +
              'your trial period.');
            alert(output.data.htmlContent);
            $('#world_pay_repost_form_placeholder').html(output.data.htmlContent);
            setTimeout($('#worldpay_repost_form').submit(), '700');
            */
        }
    }
}

function toggleCountrySpecificFields (country) {

    if (country.toLowerCase() == 'great britain') {
        toggleUKFields(true);
        toggleIrishFields(false);
    } else if (country.toLowerCase() == 'ireland') {
        toggleUKFields(false);
        toggleIrishFields(true);
    } else {
        toggleUKFields(true);
        toggleIrishFields(false);
    }
}

function toggleUKFields (toggleState) {

        if (toggleState) {
            $("#mailbox_location_uk-label").slideDown("fast");
            $("#mailbox_location_uk-element").slideDown("fast");
            $("#bank_name-label").slideDown("fast");
            $("#bank_name-element").slideDown("fast");
            $("#account_name-label").slideDown("fast");
            $("#account_name-element").slideDown("fast");
            $("#bank_sort_code-label").slideDown("fast");
            $("#bank_sort_code-element").slideDown("fast");
            $("#bank_account_number-label").slideDown("fast");
            $("#bank_account_number-element").slideDown("fast");
        } else {
            $("#mailbox_location_uk-label").slideUp("fast");
            $("#mailbox_location_uk-element").slideUp("fast");
            $("#bank_name-label").slideUp("fast");
            $("#bank_name-element").slideUp("fast");
            $("#account_name-label").slideUp("fast");
            $("#account_name-element").slideUp("fast");
            $("#bank_sort_code-label").slideUp("fast");
            $("#bank_sort_code-element").slideUp("fast");
            $("#bank_account_number-label").slideUp("fast");
            $("#bank_account_number-element").slideUp("fast");
        }
}

function toggleIrishFields (toggleState) {

        if (toggleState) {
            $("#mailbox_location_ireland-label").slideDown("fast");
            $("#mailbox_location_ireland-element").slideDown("fast");
            $("#cc_card_type-label").slideDown("fast");
            $("#cc_card_type-element").slideDown("fast");
        } else {
            $("#cc_card_type-label").slideUp("fast");
            $("#cc_card_type-element").slideUp("fast");
            $("#mailbox_location_ireland-label").slideUp("fast");
            $("#mailbox_location_ireland-element").slideUp("fast");
        }
}

