Handling Popups If Popup Name is Dynamically Changing

selenium.click("stringlocator");

selenium.waitforpagetoload();
String str[]=selenium.getallwindownames();
system.out.println(str.length);
selenium.selectwindow(str[1]);
perform action on popup
selenium.selectwindow("null"); //return to mainwindow
selenium.selectwindow(str[0]);
perform actions on mainwindow