Ciao,
per quanto ne so (non tanto), non esiste una funzione Trim
in JS. Potresti scriverla tu !

while (testo.charCodeAt(0) == 32)
{
testo = testo.substr(1);
}