Is there any way to get a windows z position compared to any other windows opened?

The reason I ask is that I have two windows that are supposed to be attached to each other. The problem I have is if I bring another window infront of these two, then click back to one of those two windows, I want the other window of that program to come to front with the one I clicked on. So is there a way to check to see if there z position is not equal?