Directing Next Page with conditions

Directing Next Page with conditions

I want to see if I can ascribe directions with conditions to a Next Page button.
There's a main page ( Level 1) which has 4 groupings of clickable objects on.
From one of these groupings the user selects one of 3-4 items which will each take them to a Level 2 page.
I want the Next Page to take user to another of the Level 2 pages in this grouping , and back to the Level 1 page once all have been visted.
The condition is that the next page can be any of the 3-4 Level 2 pages not yet vistited since being on the Level 1 page .
Is this something I could do with Javascript? I have a great book JavaScript and Jquery: Interactive Front-End Web Development by Duckett, Jon .
Could someone give me a hint what area of Javascript instructions may be helpful?
Thank you