Problem in my For Loop?
This has been infuriating me for a few hours, and it seems like the kind of problem fresh eyes can help with. The issue is explaining it all... I've mentioned my project numerous times, and the same 3 or 4 community members continue to rally to my cause, so I'll get right to it.
The following script is intended to run from a button which is next to a photo. It should move said photo and all photos past it down a slot, leaving the slot it was in "blank", ready for a different photo.
function insTop(){
//If the last photo is already filled in, this spawns a new page first
if(getFieldOn("botPh", this.numPages - 1).buttonGetIcon() != this.getField("blnk").buttonGetIcon()) spawnNewPage();
event.target.setFocus();
//these variables are arrays for the affected photos/captions
//they are filled in the order they appear in the document
var topPhs = [];
var topCaps = [];
var botPhs = [];
var botCaps = [];
for(var p = event.target.page; p < this.numPages; p++){//loops through each page starting where the button is clicked
topPhs.push(getFieldOn("topPh", p));
topCaps.push(getFieldOn("topCap", p));
botPhs.push(getFieldOn("botPh", p));
botCaps.push(getFieldOn("botCap", p));
}//end of pages for loop; arrays are now filled in page order
for(var p = event.target.page; p < this.numPages; p++){//loops back through each page replacing fields
var tP = getFieldOn("topPh", p);
var tC = getFieldOn("topCap", p);
var bP = getFieldOn("botPh", p);
var bC = getFieldOn("botCap", p);
bP.buttonSetIcon(topPhs[p - event.target.page].buttonGetIcon());
bC.value = topCaps[p - event.target.page].value;
if(p == event.target.page){
tP.buttonSetIcon(this.getField("blnk").buttonGetIcon());
tC.value = "";
}else{
tP.buttonSetIcon(botPhs[p - event.target.page - 1].buttonGetIcon());
tC.value = botCaps[p - event.target.page - 1].value;
}
}//end of pages for loop
}The issue is definitely somehwere near the end, possibly in the if statement. It is supposed to bump each picture either from the top of its page to the bottom of its page, or from the bottom of its page to the top of the next page.
This shows the before state. We are on pg 5 of the PDF, and the photos are labeled to easily see the order.
After running the above script, I now have blank, 1; 1, 3; 3, 5 (expected: blank, 1; 2, 3; 4, 5). The bottom photo is working fine. But the top... The most baffling part is the photos its assigning to the top should not be stored in botPhs[] at all! I've pulled all the small parts of this out, and I cannot for the life of me understand what I've done. Please help!
