function LTrim(str) {
    var s = new String(str);
    var whitespace = new String(' \t\n\r');

    // Strip all leading white-space characters
    if (whitespace.indexOf(s.charAt(0)) != -1) {

        var j=0, i = s.length;
    
        // Iterate from the left until we have no more whitespace...
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
            j++;

        // Get the substring from the first non-whitespace 
        // character to the end of the string...
        s = s.substring(j, i);
    }
    return s;
}

function RTrim(str) {
    var s = new String(str);
    var whitespace = new String(' \t\n\r');

    // Strip all trailing white-space characters
    if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {

        var i = s.length - 1;

        // Iterate from the right until we have no more whitespace...
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
            i--;

        // Get the substring from the beginning of the string to
        // where the last non-whitespace character
        s = s.substring(0, i+1);
    }
    return s;
}

function Trim(str) {
    // Strip away all leading and trailing white-space characters
    return RTrim(LTrim(str));
}

function TrimAll(form) {
   // Trim() the text and hidden inputs values
    for(var x = 0; x < form.elements.length; x++) {
		if(form.elements[x].type == 'text' || form.elements[x].type == 'hidden' || form.elements[x].type == 'textarea') {
            form.elements[x].value = Trim(form.elements[x].value);
        }
    }
}