The issue is stemming from the fact you are using incremental id
attributes, and are trying to calculate the next in the sequence on the fly. This is always problematic, and is rarely the best solution.
Use class
attributes to group similar elements - it's what they were designed for. Your code becomes much simpler too.
<div id="insert-placeholder"></div>
<fieldset class="template-image-upload-wrapper">
<legend>Image upload</legend>
<div class="form-group">
<div class="col-md-12">
<input class='template_image_upload' class='form-control input-md' title='Upload an image.' name='editPage' type='file' value='' />
<span class="help-block"></span>
</div>
</div>
</fieldset>
$(".template-image-upload-wrapper").clone(false).insertBefore("#insert-placeholder");