/*
 * Controls the initiation of the autocomplete function
 */
$(document).ready(function() {

    /*
     *  Only display county field on page load
     */
    if(String.toLowerCase($("#UserProfileCountry").val()) != 'united kingdom') {
        $("#UserProfileCounty").parent('div.input.text').hide();
    } else {
        $("#UserProfileCounty").parent('div.input.text').show();
    }

    /*
     * Country of Origin
     */
    if(typeof(objCountriesOfOrigin) != 'undefined') {
        
        $("#UserProfileCountryOfOrigin").autocomplete(objCountriesOfOrigin)
        .result(function() {
            $("#UserProfileCountryOfOrigin").blur();
        });

        // validate input
        $("#UserProfileCountryOfOrigin").blur(function() {
            var arrCountriesOfOriginLower = $.map(objCountriesOfOrigin, function(n, i) {
                return n.toLowerCase();
            });

            if($.inArray($(this).val().toLowerCase(), arrCountriesOfOriginLower) == -1) {
                invalidate($("#UserProfileCountryOfOrigin"), $('#invalid_country_l10n').text());
            } else {
                validate($("#UserProfileCountryOfOrigin"));
            }
        });
    }

    /*
     *  County
     */
    if(typeof(objCounties) != 'undefined') {
        
        $("#UserProfileCounty").autocomplete(objCounties)
        .result(function() {
            $("#UserProfileCounty").blur();
        });

        // validate input
        $("#UserProfileCounty").blur(function() {
            var arrCounties = $.map(objCounties, function(n, i) {
                return n.toLowerCase();
            });

            if($.inArray($(this).val().toLowerCase(), arrCounties) == -1) {
                invalidate($("#UserProfileCounty"), 'You must select from the suggested counties.');
            } else {
                validate($("#UserProfileCounty"));
            }
        });
    }

    /*
     * Country
     */
    if(typeof(objCountries) != 'undefined') {

        $("#UserProfileCountry").autocomplete(objCountries)
        .result(function() {
            $("#UserProfileCountry").blur();
        });

        // validate input
        $("#UserProfileCountry").blur(function() {
            var arrCountriesLower = $.map(objCountries, function(n, i) {
                return n.toLowerCase();
            });

            if($.inArray($(this).val().toLowerCase(), arrCountriesLower) == -1) {
                invalidate($("#UserProfileCountry"), $('#invalid_country_l10n').text());
            } else {
                validate($("#UserProfileCountry"));

                // Only show 'County' field if user selects United Kingdom
                if(String.toLowerCase($("#UserProfileCountry").val()) != 'united kingdom') {
                    $("#UserProfileCounty").parent('div.input.text').hide();
                } else {
                    $("#UserProfileCounty").parent('div.input.text').show();
                }
            }
        });
    }

    if(typeof(objAgencies) != 'undefined') {
        
        $("#UserProfileAgency").autocomplete(objAgencies)
        .result(function() {
            $("#UserProfileAgency").blur();
        });
    }
  
    $('#UserProfileMobile, #UserProfilePhone').blur(function() {
        $(this).val($(this).val().replace(/[^0-9]/g,''));
    });
    
    function showHideHearAboutOther() {
        if ($('#UserProfileHearAbout').val() == 'x'){
            $('#UserProfileHearAboutOtherDiv').show(100);
        } else {
            $('#UserProfileHearAboutOtherDiv').hide();
        }
    }
    
    function showHideMobProviderOther() {
        if ($('#UserProfileMobileProvider').val() == 'x'){
            $('#UserProfileMobileProviderOtherDiv').show(100);
        } else {
            $('#UserProfileMobileProviderOtherDiv').hide();
        }
    }
    
    $('#UserProfileMobileProvider').change(function() {
        showHideMobProviderOther();
    });
    $('#UserProfileHearAbout').change(function() {
        showHideHearAboutOther()
    });
    
    showHideHearAboutOther();
    showHideMobProviderOther();
    
    $('.scroll').click(function() {
        $.scrollTo($(this).attr('href'), 500);
        return false;
    });
  
});

function invalidate(objElement, strMsg) {
    validate($(objElement));
    $(objElement).parent().addClass('error');
    $(objElement).addClass('form-error');
    $(objElement).after('<div class="error-message">'+strMsg+'</div>');
}

function validate(objElement) {
    $(objElement).parent().removeClass('error');
    $(objElement).removeClass('form-error');
    $('~ .error-message', objElement).remove();
}

