﻿//
// Handy utility functions for use through starrate apps
//

if (StarrateUtils == null || typeof (StarrateUtils) != "object") { var StarrateUtils = new Object(); }
if (StarrateUtils.JQ == null || typeof (StarrateUtils.JQ) != "object") { StarrateUtils.JQ = new Object(); }

// Handy jquery functions
StarrateUtils.JQ = {

    // If a radio button is checked, show the contents of the provided selector
    ShowIfRadioButtonIsChecked: function(radioSelector, divSelector) {
        var userSpecifiedCountries = $(radioSelector);
        if (userSpecifiedCountries != null) {
            if (userSpecifiedCountries.attr('checked')) {
                $(divSelector).show();
            }
            else {
                $(divSelector).hide();
            }
        }
    },

    // Attach an event handler to a radio button that will set the visibility of a selector dependant on the value of the radio button
    ToggleVisibilityOnRadioValue: function(radioGroupSelector, showValue, showSelector) {
        StarrateUtils.JQ.ShowIfRadioButtonIsChecked(radioGroupSelector + "[value='" + showValue + "']", showSelector);
        $(radioGroupSelector).click(function() {
            if ($(this).attr('value') == showValue) {
                $(showSelector).show();
            }
            else {
                $(showSelector).hide();
            }
        });
    },

    // Attach an event handler to a item that has a value attribute
    ToggleVisibilityOnValEquals: function(controlSelector, showOnValue, showSelector) {

        var hide = false;
        var arr = jQuery.makeArray(showOnValue);

        if (jQuery.inArray($(controlSelector).val(), arr) != -1) {
            $(showSelector).show();
        }
        else {
            $(showSelector).hide();
        }

        $(controlSelector).change(function() {
            if (jQuery.inArray($(this).val(), arr) != -1) {
                $(showSelector).show();
            }
            else {
                $(showSelector).hide();
            }
        });
    }
}
