Would someone please review this code and tell me why the for loops are going infinite? I need a fresh set of eyes. Thank you!
var routeData = [{"id":1,"c1_id":43,"c2_id":56,"cost":20,"c1_x":658,"c1_y":68,"c2_x":568,"c2_y":149,"owned":false},{"id":2,"c1_id":27,"c2_id":56,"cost":25,"c1_x":571,"c1_y":10,"c2_x":568,"c2_y":149,"owned":false},{"id":3,"c1_id":39,"c2_id":56,"cost":20,"c1_x":510,"c1_y":53,"c2_x":568,"c2_y":149,"owned":false},{"id":4,"c1_id":56,"c2_id":8,"cost":18,"c1_x":568,"c1_y":149,"c2_x":495,"c2_y":202,"owned":false},{"id":5,"c1_id":56,"c2_id":49,"cost":14,"c1_x":568,"c1_y":149,"c2_x":605,"c2_y":217,"owned":false},{"id":6,"c1_id":49,"c2_id":4,"cost":14,"c1_x":605,"c1_y":217,"c2_x":572,"c2_y":282,"owned":false},{"id":7,"c1_id":8,"c2_id":10,"cost":11,"c1_x":495,"c1_y":202,"c2_x":448,"c2_y":250,"owned":false},{"id":8,"c1_id":4,"c2_id":48,"cost":10,"c1_x":572,"c1_y":282,"c2_x":504,"c2_y":293,"owned":false},{"id":9,"c1_id":48,"c2_id":10,"cost":12,"c1_x":504,"c1_y":293,"c2_x":448,"c2_y":250,"owned":false},{"id":10,"c1_id":10,"c2_id":31,"cost":17,"c1_x":448,"c1_y":250,"c2_x":450,"c2_y":370,"owned":false},{"id":11,"c1_id":48,"c2_id":33,"cost":16,"c1_x":504,"c1_y":293,"c2_x":530,"c2_y":373,"owned":false},{"id":12,"c1_id":10,"c2_id":35,"cost":14,"c1_x":448,"c1_y":250,"c2_x":372,"c2_y":250,"owned":false},{"id":13,"c1_id":33,"c2_id":47,"cost":17,"c1_x":530,"c1_y":373,"c2_x":479,"c2_y":431,"owned":false},{"id":14,"c1_id":33,"c2_id":31,"cost":15,"c1_x":530,"c1_y":373,"c2_x":450,"c2_y":370,"owned":false},{"id":15,"c1_id":47,"c2_id":57,"cost":9,"c1_x":479,"c1_y":431,"c2_x":479,"c2_y":466,"owned":false},{"id":16,"c1_id":57,"c2_id":60,"cost":12,"c1_x":479,"c1_y":466,"c2_x":484,"c2_y":527,"owned":false},{"id":17,"c1_id":60,"c2_id":17,"cost":12,"c1_x":484,"c1_y":527,"c2_x":521,"c2_y":511,"owned":false},{"id":18,"c1_id":17,"c2_id":58,"cost":13,"c1_x":521,"c1_y":511,"c2_x":598,"c2_y":527,"owned":false},{"id":19,"c1_id":58,"c2_id":36,"cost":10,"c1_x":598,"c1_y":527,"c2_x":650,"c2_y":584,"owned":false},{"id":20,"c1_id":57,"c2_id":7,"cost":16,"c1_x":479,"c1_y":466,"c2_x":430,"c2_y":432,"owned":false},{"id":21,"c1_id":57,"c2_id":54,"cost":14,"c1_x":479,"c1_y":466,"c2_x":430,"c2_y":462,"owned":false},{"id":22,"c1_id":31,"c2_id":59,"cost":16,"c1_x":450,"c1_y":370,"c2_x":350,"c2_y":344,"owned":false},{"id":23,"c1_id":35,"c2_id":59,"cost":14,"c1_x":372,"c1_y":250,"c2_x":350,"c2_y":344,"owned":false},{"id":24,"c1_id":59,"c2_id":29,"cost":19,"c1_x":350,"c1_y":344,"c2_x":260,"c2_y":314,"owned":false},{"id":25,"c1_id":59,"c2_id":12,"cost":12,"c1_x":350,"c1_y":344,"c2_x":299,"c2_y":395,"owned":false},{"id":26,"c1_id":12,"c2_id":2,"cost":6,"c1_x":299,"c1_y":395,"c2_x":290,"c2_y":400,"owned":false},{"id":27,"c1_id":2,"c2_id":22,"cost":10,"c1_x":290,"c1_y":400,"c2_x":255,"c2_y":412,"owned":false},{"id":28,"c1_id":22,"c2_id":29,"cost":16,"c1_x":255,"c1_y":412,"c2_x":260,"c2_y":314,"owned":false},{"id":29,"c1_id":29,"c2_id":14,"cost":14,"c1_x":260,"c1_y":314,"c2_x":232,"c2_y":257,"owned":false},{"id":30,"c1_id":29,"c2_id":9,"cost":10,"c1_x":260,"c1_y":314,"c2_x":209,"c2_y":300,"owned":false},{"id":31,"c1_id":9,"c2_id":32,"cost":8,"c1_x":209,"c1_y":300,"c2_x":192,"c2_y":308,"owned":false},{"id":32,"c1_id":32,"c2_id":16,"cost":12,"c1_x":192,"c1_y":308,"c2_x":170,"c2_y":225,"owned":false},{"id":33,"c1_id":16,"c2_id":15,"cost":10,"c1_x":170,"c1_y":225,"c2_x":195,"c2_y":166,"owned":false},{"id":34,"c1_id":32,"c2_id":37,"cost":11,"c1_x":192,"c1_y":308,"c2_x":155,"c2_y":315,"owned":true},{"id":35,"c1_id":37,"c2_id":19,"cost":9,"c1_x":155,"c1_y":315,"c2_x":101,"c2_y":285,"owned":false},{"id":36,"c1_id":19,"c2_id":1,"cost":12,"c1_x":101,"c1_y":285,"c2_x":70,"c2_y":330,"owned":false},{"id":37,"c1_id":19,"c2_id":5,"cost":12,"c1_x":101,"c1_y":285,"c2_x":131,"c2_y":243,"owned":false},{"id":38,"c1_id":22,"c2_id":41,"cost":12,"c1_x":255,"c1_y":412,"c2_x":304,"c2_y":461,"owned":true},{"id":39,"c1_id":41,"c2_id":18,"cost":12,"c1_x":304,"c1_y":461,"c2_x":350,"c2_y":499,"owned":true},{"id":40,"c1_id":18,"c2_id":45,"cost":8,"c1_x":350,"c1_y":499,"c2_x":370,"c2_y":504,"owned":false},{"id":41,"c1_id":45,"c2_id":54,"cost":13,"c1_x":370,"c1_y":504,"c2_x":430,"c2_y":462,"owned":false},{"id":42,"c1_id":54,"c2_id":34,"cost":14,"c1_x":430,"c1_y":462,"c2_x":400,"c2_y":532,"owned":false},{"id":43,"c1_id":34,"c2_id":51,"cost":19,"c1_x":400,"c1_y":532,"c2_x":408,"c2_y":623,"owned":false},{"id":44,"c1_id":41,"c2_id":26,"cost":16,"c1_x":304,"c1_y":461,"c2_x":300,"c2_y":554,"owned":true},{"id":45,"c1_id":26,"c2_id":55,"cost":11,"c1_x":300,"c1_y":554,"c2_x":270,"c2_y":573,"owned":false},{"id":46,"c1_id":26,"c2_id":40,"cost":12,"c1_x":300,"c1_y":554,"c2_x":350,"c2_y":585,"owned":false},{"id":47,"c1_id":40,"c2_id":51,"cost":13,"c1_x":350,"c1_y":585,"c2_x":408,"c2_y":623,"owned":false},{"id":48,"c1_id":60,"c2_id":53,"cost":17,"c1_x":484,"c1_y":527,"c2_x":470,"c2_y":610,"owned":false},{"id":49,"c1_id":53,"c2_id":51,"cost":11,"c1_x":470,"c1_y":610,"c2_x":408,"c2_y":623,"owned":false},{"id":50,"c1_id":53,"c2_id":42,"cost":13,"c1_x":470,"c1_y":610,"c2_x":540,"c2_y":659,"owned":false},{"id":51,"c1_id":42,"c2_id":6,"cost":10,"c1_x":540,"c1_y":659,"c2_x":510,"c2_y":705,"owned":false},{"id":52,"c1_id":6,"c2_id":53,"cost":16,"c1_x":510,"c1_y":705,"c2_x":470,"c2_y":610,"owned":true},{"id":53,"c1_id":6,"c2_id":13,"cost":13,"c1_x":510,"c1_y":705,"c2_x":433,"c2_y":673,"owned":false},{"id":54,"c1_id":53,"c2_id":13,"cost":15,"c1_x":470,"c1_y":610,"c2_x":433,"c2_y":673,"owned":false},{"id":55,"c1_id":13,"c2_id":51,"cost":15,"c1_x":433,"c1_y":673,"c2_x":408,"c2_y":623,"owned":false},{"id":56,"c1_id":51,"c2_id":25,"cost":16,"c1_x":408,"c1_y":623,"c2_x":338,"c2_y":693,"owned":false},{"id":57,"c1_id":25,"c2_id":28,"cost":11,"c1_x":338,"c1_y":693,"c2_x":298,"c2_y":675,"owned":false},{"id":58,"c1_id":28,"c2_id":50,"cost":8,"c1_x":298,"c1_y":675,"c2_x":282,"c2_y":710,"owned":false},{"id":59,"c1_id":50,"c2_id":23,"cost":12,"c1_x":282,"c1_y":710,"c2_x":210,"c2_y":699,"owned":false},{"id":60,"c1_id":40,"c2_id":52,"cost":11,"c1_x":350,"c1_y":585,"c2_x":314,"c2_y":639,"owned":false},{"id":61,"c1_id":52,"c2_id":28,"cost":10,"c1_x":314,"c1_y":639,"c2_x":298,"c2_y":675,"owned":false},{"id":62,"c1_id":41,"c2_id":11,"cost":12,"c1_x":304,"c1_y":461,"c2_x":232,"c2_y":490,"owned":true},{"id":63,"c1_id":11,"c2_id":21,"cost":9,"c1_x":232,"c1_y":490,"c2_x":230,"c2_y":513,"owned":true},{"id":64,"c1_id":55,"c2_id":21,"cost":15,"c1_x":270,"c1_y":573,"c2_x":230,"c2_y":513,"owned":true},{"id":65,"c1_id":55,"c2_id":46,"cost":9,"c1_x":270,"c1_y":573,"c2_x":240,"c2_y":596,"owned":false},{"id":66,"c1_id":46,"c2_id":24,"cost":19,"c1_x":240,"c1_y":596,"c2_x":170,"c2_y":560,"owned":false},{"id":67,"c1_id":21,"c2_id":3,"cost":11,"c1_x":230,"c1_y":513,"c2_x":150,"c2_y":528,"owned":true},{"id":68,"c1_id":3,"c2_id":24,"cost":13,"c1_x":150,"c1_y":528,"c2_x":170,"c2_y":560,"owned":false},{"id":69,"c1_id":24,"c2_id":20,"cost":12,"c1_x":170,"c1_y":560,"c2_x":112,"c2_y":567,"owned":false},{"id":70,"c1_id":20,"c2_id":38,"cost":12,"c1_x":112,"c1_y":567,"c2_x":38,"c2_y":553,"owned":true},{"id":71,"c1_id":24,"c2_id":44,"cost":22,"c1_x":170,"c1_y":560,"c2_x":61,"c2_y":661,"owned":false},{"id":72,"c1_id":38,"c2_id":44,"cost":16,"c1_x":38,"c1_y":553,"c2_x":61,"c2_y":661,"owned":true},{"id":73,"c1_id":59,"c2_id":30,"cost":16,"c1_x":350,"c1_y":344,"c2_x":340,"c2_y":425,"owned":false},{"id":74,"c1_id":30,"c2_id":41,"cost":12,"c1_x":340,"c1_y":425,"c2_x":304,"c2_y":461,"owned":false},{"id":75,"c1_id":30,"c2_id":18,"cost":12,"c1_x":340,"c1_y":425,"c2_x":350,"c2_y":499,"owned":true}];
var trainData = [{"id":2,"type":"STANDARD","game_player_id":2,"game_id":1,"route_id":18,"origin_city_id":58,"dest_city_id":17,"track_unit":3,"direction":"_","line":"RED","status":"ENROUTE","goods":[{"train_id":2,"goods_id":31,"name":"Furniture"},{"train_id":2,"goods_id":7,"name":"Chocolate"}]},{"id":3,"type":"STANDARD","game_player_id":3,"game_id":1,"route_id":30,"origin_city_id":9,"dest_city_id":29,"track_unit":2,"direction":"_","line":"RED","status":"ENROUTE","goods":[{"train_id":3,"goods_id":19,"name":"Marble"}]},{"id":4,"type":"STANDARD","game_player_id":4,"game_id":1,"route_id":5,"origin_city_id":49,"dest_city_id":56,"track_unit":2,"direction":"_","line":"RED","status":"ENROUTE","goods":[]},{"id":5,"type":"STANDARD","game_player_id":5,"game_id":1,"route_id":1,"origin_city_id":56,"dest_city_id":43,"track_unit":0,"direction":"_","line":"RED","status":"ARRIVED","goods":[]},{"id":15,"type":"STANDARD","game_player_id":1,"game_id":1,"route_id":67,"origin_city_id":21,"dest_city_id":3,"track_unit":8,"direction":"+","line":"RED","status":"ARRIVED","goods":[{"train_id":15,"goods_id":29,"name":"Timber"}]}];
var routeCount = [routeData.length];
var train = [];
var trackColor = "#888";
for(var i = 0; i < routeData.length; i++) {
var trainCount = 0;
train = [];
routeCount[i] = [];
routeCount[i]["route_id"] = routeData[i]["id"];
trackColor = routeData[i]["owned"] ? "#555" : "#888";
for(var j = 0; j < trainData.length; j++) {
if(trainData[j]["route_id"] == i) {
new Dialog().showMessage("test", j+":route match on "+i);
train[trainCount] = trainData[j];
trainCount++;
}
}
if(train[0]) { new Dialog().showMessage("train", i+":train="+train.toSource()); }
drawTrack(parseInt(routeData[i]["c1_y"]),
parseInt(routeData[i]["c1_x"]),
parseInt(routeData[i]["c2_y"]),
parseInt(routeData[i]["c2_x"]),
trackColor,
train);
}