Well, the Quran states in 5:6 states very clearly what nullifies your ablution. If you have had sexual orgasm you shall bathe. It DOES NOT say if you THINK about sexual contact with the opposite sex. So, if thinking does not nullify your ability to do prayer, it is only logical that thoughts won't nullify your prayer, as long as you do it correctly. The key is to focus on your prayers. If you are having bad thoughts, do your best to get your focus back on GOD. If you can't, implore GOD to help you do better next time. After all, GOD does not wish to make our religion difficult.
[Quran 5:6] O you who believe, when you observe the Contact Prayers (Salat), you shall: (1) wash your faces, (2) wash your arms to the elbows, (3) wipe your heads, and (4) wash your feet to the ankles. If you were unclean due to sexual orgasm, you shall bathe. If you are ill, or traveling, or had any digestive excretion (urinary, fecal, or gas), or had (sexual) contact with the women, and you cannot find water, you shall observe the dry ablution (Tayammum) by touching clean dry soil, then rubbing your faces and hands. GOD does not wish to make the religion difficult for you; He wishes to cleanse you and to perfect His blessing upon you, that you may be appreciative.
Peace