start off by making a different spell one at each level. then make seperate books with the spells at the different levels in each book. (if you want each spell to appear at each level independant of the other spells, youll need 128 books. assuming you have three 3 level spells and one 1 level spell. three regulars and one ultimate.) then you'll have to initalise those into an array, in an ordered fashion (see below). now, just say the player picks the ultimate spell to level up, you would add on 64 (128/2) onto a variable and then use this as the index for the index of your array (again, if this doesn't make sense see below). if he chooses the first spell (a regular one) add on 1. if he chooses the second one, add on 4. if he chooses the third one, add on 16. this will then point you to the correct book to give to the player. here is what your array should look like, i have given the levels of the spells in parenthesis' that particular version of the book should have.. the last one is the ultimate.
spellbook[0]=(0,0,0, 0)
spellbook[1]=(1,0,0, 0)
spellbook[2]=(2,0,0, 0)
spellbook[3]=(3,0,0, 0)
spellbook[4]=(0,1,0, 0)
spellbook[5]=(1,1,0, 0)
spellbook[6]=(2,1,0, 0)
...
spellbook[19]=(3,0,1, 0)
spellbook[20]=(0,1,1, 0)
spellbook[21]=(1,1,1, 0)
...
spellbook[61]=(1,3,3, 0)
spellbook[62]=(2,3,3, 0)
spellbook[63]=(3,3,3, 0)
spellbook[64]=(0,0,0, 1)
spellbook[65]=(1,0,0, 1)
spellbook[66]=(2,0,0, 1)
...
spellbook[83]=(3,0,1, 1)
spellbook[84]=(0,1,1, 1)
spellbook[85]=(1,1,1, 1)
...
spellbook[127]=(3,3,3, 1)
notice that each book has an ultimate true and ultimate false version, each is exactly 64 apart in the array. likewise, other single-difference versions differ by the amounts given above.
this is only a partial list, some key vaules so you can double check and make sure you are on track. i am aware of the overwhleming gargantuity of this task i have posed for you. unless someone can think of a better way that still uses the spell books... i have worked with items before, ones that became more powerful when something happened, just like yours. lets just say, after months of sparatic levels of sanity, either me or the problem cracked, can't remmeber which...

. oh one other thing, i can't remember if arrays start at 0 or 1, if they start at 1 just add 1 to all the values inside the []'s.
this is not meant to discourage you, i have merely put forward a possible solution. another one would be to give/take the spells to/from the hero, but that would by-pass the spellbook.