Any losses sustained by any bank in excess of its undivided profits shall be charged to its surplus fund. Its surplus fund shall thereafter be reimbursed from the earnings, and no dividends shall thereafter be declared or paid by any such bank, without the written permission of the director, until such surplus fund shall be fully restored to its former amount.