// When document is ready
$(document).ready(function()
{
	/**
     * Attach an event to the addBranch id
     */
    $('.addBranch').click(function()
    {
        var advCnt = parseInt($('.branchRow').attr('id')) + 1;
        $('#branchDiv div:first').attr('id', advCnt);
        //$('.branchRow').attr('id', advCnt);
        var advRow = $('#branchDiv').html();
        advRow = advRow.replace('id="'+(advCnt)+'"', 'id="advisor_'+advCnt+'"');
        $('.lastDiv').before(advRow);
        $('#advisor_' + advCnt + ' a:eq(0)').attr('id', 'delBranch_' + advCnt);
        $('#delBranch_' + advCnt).click(function()
        {
            $('#advisor_' + advCnt).remove();
            return false;
        });
        return false;
    });

    // For all delAdvisor link on page load
    $(".delBranchLink").click(function()
    {
       var uid = (this.id).replace('delBranchLink_', '');
       $('#advisor_' + uid).remove();
       return false;
    });

    // When country is changed then get its states drop-down using AJAX request
    $("#ManufacturerOrderCatalogCountryId").change(function()
    {
        // Hide the title part for Japan.
        if (this.options[this.selectedIndex].value == japanCountryId) {
            $('.userTitle').hide();
        } else {
            $('.userTitle').show();
        }

        $.get(baseUrl + "manufacturers/get_states_for_country/" + this.options[this.selectedIndex].value, function(response)
        {
            $("#countryStates").html(response);
        });
    });

   if($("#ManufacturerOrderCatalogCountryId").val() == japanCountryId) {
      $('.userTitle').hide();
   }
});