Your for() loop is outside PHP (it's being written into your Javascript).
<?$index = 0;
for(i=0; i < 4; i++) {
$index++;
?>
document.write("<?echo $index?>")
<?
}
?>
Of course, $index always equals $i+1 when it's used here, so
<?
for(i=1; i < 5; i++) {
?>
document.write("<?echo $i?>")
<?
}
?>