When it comes to foreigners then it starts to become interesting and the real reason behind this post/question. We have all seen foreigners get it hopelessly wrong, ie old guys 'waiing' a young waitress first or getting the position of the hands wrong, thus giving the wrong 'wai' and perhaps, in a Thai cultural sense, causing embarrassment. They, as foreigners, are forgiven because it's not their culture rather like we forgive they 15 second handshake. I've heard some Thais say that it's not automatic to give or return a 'wai' these days, especially for the younger Thais, seems to be changing, they 'wai' who they feel they should.
There are many different variations of 'wai', I've even seen a one handed version, but only from old men 60+.
I suppose one of the most basic rules is that the younger person gives the 'wai' first, then to be replied depending on a few other variables. Same like the locals have to use the "P" before someone's name if they are older, can't just use their first name.
I must also say that I don't really 'wai' anyone these days, except the wife's mother/father or her grandparents. Even at work, the only people I should be 'waiing' that most of the Thai employees are scared to death of, like heads of faculties etc, want to act in an international fashion as they gain more 'face' in front of the surrounding Thais by showing that they can interact internationally and speak English with me. I therefore allow them to feel good and practice their western skills, which as a foreign lecturer, that's what I'm there for, student and all. I can see that they gain far more face this way and if I was to get their cultural thing wrong then they could lose face by not saying something or acting in some other way.
I find myself very careful these days with this stuff after almost a decade, especially after seeing so many get it wrong and the uncomfortable looks you sometimes see.
This is more of a speculative post and would be interested to gather others thoughts on what I posted or any other points anyone feels should be raised. It's a big subject that many will have a something to add as I sure we've all ask our other-halfs about it at some point or other and there's probably a great deal of difference in what they think about it too.
