[quote]
d1 = Math.sqrt((x1-x2)*(x1-x2)+(y2-y1)*(y2-y1))+ Math.sqrt((x4-x2)*(x4-x2)+(y2-y1)*(y2-y1));
d2 = Math.sqrt((x1-x29)*(x1-x29)+(y2-y1)*(y2-y1))+ Math.sqrt((x4-x29)*(x4-x29)+(y2-y1)*(y2-y1));
d3 = Math.sqrt((x1-x26)*(x1-x26)+(y2-y1)*(y2-y1))+ Math.sqrt((x4-x26)*(x4-x26)+(y2-y1)*(y2-y1));
d4 = Math.sqrt((x1-x24)*(x1-x24)+(y2-y1)*(y2-y1))+ Math.sqrt((x4-x24)*(x4-x24)+(y2-y1)*(y2-y1));
d5 = Math.sqrt((x1-x22)*(x1-x22)+(y2-y1)*(y2-y1))+ Math.sqrt((x4-x22)*(x4-x22)+(y2-y1)*(y2-y1));
d6 = Math.sqrt((x1-x23)*(x1-x23)+(y2-y1)*(y2-y1))+ Math.sqrt((x4-x23)*(x4-x23)+(y2-y1)*(y2-y1));
d7 = Math.sqrt((x1-x25)*(x1-x25)+(y2-y1)*(y2-y1))+ Math.sqrt((x4-x25)*(x4-x25)+(y2-y1)*(y2-y1));
d8 = Math.sqrt((x1-x28)*(x1-x28)+(y2-y1)*(y2-y1))+ Math.sqrt((x4-x28)*(x4-x28)+(y2-y1)*(y2-y1));
d9 = Math.sqrt((x1-x32)*(x1-x32)+(y2-y1)*(y2-y1))+ Math.sqrt((x4-x32)*(x4-x32)+(y2-y1)*(y2-y1));
d10 = Math.sqrt((x1-x27)*(x1-x27)+(y2-y1)*(y2-y1))+ Math.sqrt((x4-x27)*(x4-x27)+(y2-y1)*(y2-y1));
d11 = Math.sqrt((x1-x30)*(x1-x30)+(y2-y1)*(y2-y1))+ Math.sqrt((x4-x30)*(x4-x30)+(y2-y1)*(y2-y1));
d12 = Math.sqrt((x1-x31)*(x1-x31)+(y2-y1)*(y2-y1))+ Math.sqrt((x4-x31)*(x4-x31)+(y2-y1)*(y2-y1));
[/quote]
The above is a copy from part of your code. It is very difficult to read or debug your code.
This is not the way to write a program.
You should have defined an array (e.g. d[12]) and do it with a for loop.
Do the same for x,y,phase, ...

You might want to check out my ejs source from the above example  to find out how to do it with a loop.