Are Complex Substitutions Legal in Integration?

This question has been irritating me for awhile so I thought I'd ask here.

Are complex substitutions in integration okay? Can the following substitution used to evaluate the Fresnel integrals:

$$\int_{0}^{\infty} \sin x^2\, dx=\operatorname {Im}\left( \int_0^\infty\cos x^2\, dx + i\int_0^\infty\sin x^2\, dx\right)=\operatorname {Im}\left(\int_0^\infty \exp(ix^2)\, dx\right)$$

Letting $ix^2=-z^2 \implies x=\pm\sqrt{iz^2}=\pm \sqrt{i}z \implies z=\pm \sqrt{-i} x \implies dx = \pm\sqrt{i}\, dz$

Thus the integral becomes

$$\operatorname {Im}\left(\pm \sqrt{i}\int_0^{\pm\sqrt{-i}\infty} \exp(z^2)\, dz\right)$$

This step requires some justification, and I am hoping someone can help me justify this step as well: $$\pm \sqrt{i}\int_0^{\pm\sqrt{-i}\infty} \exp(z^2)\, dz=\pm\sqrt{i}\int^\infty_0\exp(z^2)\, dz=\pm\sqrt{i}\left(\frac{\sqrt{\pi}}{2}\right)$$

Thus

$$\operatorname {Im}\left(\int_0^\infty \exp(ix^2)\, dx\right)=\operatorname {Im}\left(\pm\frac{\sqrt{i\pi}}{2}\right)=\operatorname {Im}\left(\pm\frac{(1+i)\sqrt{\pi}}{2\sqrt{2}}\right)=\pm\frac{1}{2}\sqrt{\frac{\pi}{2}}$$

We find that the correct answer is the positive part (simply prove the integral is positive, perhaps by showing the integral can be written as an alternating sum of integrals).

Can someone help justify this substitution? Is this legal?

-
A complex substitution is fine. The thing to be worried about is that replacing $x$ with $\pm \sqrt{i} z$ is not a substitution at all! At best, it's an (awkward) way to try and work with two different substitutions simultaneously. At worst, it's a sure-fire recipe for confusion. –  Hurkyl Jun 27 '12 at 23:09

Let's consider the legality of doing an actual u-substitution, such as $z = \sqrt{i} x$. Not only must the integrand be rewritten, so must the limits of integration.
In the original definite integral you have $x$ going from $0$ to $\infty$. Of course this then gives a path of integration for $z$, but it's not sufficient to have just limits $0$ to $\infty$ in the complex plane to specify that path. So this would be a gray area where the limitations of your notation could let you down!
In the complex plane there are many paths from $0$ to $\infty$, even many straight such paths.
A correct way to do this might go as follows. Consider the contour integral $$\oint_\Gamma e^{iz^2}\ dz$$ where $\Gamma$ is the positively oriented triangle with vertices $0, R, R+Ri$ for large $R$. Since the integrand is analytic, Cauchy's Theorem says the result is $0$. This can be written as $J_1 + J_2 + J_3=0$, where $J_1, J_2, J_3$ are the integrals over the segments $[0, R]$, $[R, R+Ri]$, and $[R+Ri,0]$ respectively.
Show that as $R \to +\infty$, $J_2 \to 0$ and $J_3 \to -(1+i) \dfrac{\sqrt{2 \pi}}{4}$. Thus $J_1 \to (1+i) \dfrac{\sqrt{2 \pi}}{4}$, which says that $$\int_0^\infty \cos(t^2)\ dt = \int_0^\infty \sin(t^2)\ dt = \dfrac{\sqrt{2 \pi}}{4}$$