Well, ために means "for the sake of" or in some contexts "for the benefit of." It carries an implication of what's being done is particularly for someone or something's good/benifit/etc. While に(は) is more of a generic to/for. The nuance between the two is quite different, so I wouldn't say they're interchangable. Also, the emphasis in ために setences is more on what's attached to ために than the action done for it. Let's take the sentences in your first paragraph as an example. I would translate these:
アドバイスを聞きに友達に電話した。
日本語を勉強するために、クラスに出る。
While in the first sentence the advice you're asking for is for your benefit, the benefit isn't really directly from calling your friend but from asking their advice. At least that's how the emphasis shifts in this version. Let's consider:
アドバイスを聞くために、友達に電話した。
Here's a kinda visual way of looking at the differnce in emphasis in these 2 versions:
1)
I called my friend to ask his advice.
2) I called my friend
to ask his advice.
Grammatically, both work fine. In this case, the difference is in what part you want to empahsize. Personally I prefer the に version since your benefit is really coming more from asking advice rather than the calling. But that's just me.
Now, for your other sentence, "I attend...", if you translated it:
日本語を勉強しにクラスに出る。
This comes across as a very bland, straight forward statement of fact. And the emphasis shifts to your going to class over "to study Japanese", which I don't think is the intent of the original English.
That all being said, is it more clear now why the Japanese setences you gave are how they are?
Like so many other things, how you say "do X for Y" depends a lot on what kind of nuance and such you want to express. While there may be more than one construction to do basically what you want, the subtle differences between them make certain ones more appropriate for the job. Knowing and learning to use these subtleties will seperate you from just being "good" at Japanese to being "great."