Search everything API and some rewriteng

Ahoy mates,

I did some rewriting (seperated HTML from PHP) and implemented an API for searching with AJAX. I had to make some core changes.

Also, the security issue with unescaped terms should also be fixed.