// File             : utils.js
// Programmer       : John Wong
// Copyright (c) Q-Surf Computing Solutions, 2003. All rights reserved.
// http://www.q-surf.com

function LTrim(str)
{
    var n1 = new String(str)
    var i = 0
    
    for (i = 0 ; i < n1.length; i ++)
    {
        if (n1.charAt(i) != " " && 
        n1.charAt(i) != "\r" &&
        n1.charAt(i) != "\n" && 
        n1.charAt(i) != "\t")
        {
            break
        }
    }
    return n1.substr(i, n1.length - i);    
}

function RTrim(str)
{
    var n1 = new String(str)
    var i = 0
    
    for (i = n1.length - 1; i > 0; i --)
    {
        if (n1.charAt(i) != " " && 
        n1.charAt(i) != "\r" &&
        n1.charAt(i) != "\n" && 
        n1.charAt(i) != "\t")
        {
            break
        }
    }
    return n1.substr(0, i + 1);    
}

function Trim(str)
{
    return LTrim(RTrim(str))
}

function IsValidInteger(str)
{
    var n1 = new String(str)
    for (i = 0; i < n1.length; i ++)
    {
        if (n1.charAt(i) < "0" || n1.charAt(i) > "9")
        {
            return false
        }
    }    
    return true
}

function HtmlSpecialChars(str)
{
    var mystr = new String(str)
    var re
    re = /&/g;
    mystr = mystr.replace(re, "&amp;");
    re = /\"/g;
    mystr = mystr.replace(re, "&quot;");
    re = /</g;
    mystr = mystr.replace(re, "&lt;");
    re = />/g;
    mystr = mystr.replace(re, "&gt;");
    return mystr
}

function IsHtmlText(str)
{
    var mystr = new String(str)
    var re
    
    re = /<(p|h1|h2|h3|h4|h5|h6|table|td|tr|ul|ol|li|b|i|u|strong|em|strike|super|sup|big|small|body|html|br|hr|font|blockquote|pre|tt|script)>/i;
    if (re.test(mystr)) return true

    re = /(&[a-zA-Z]{2,5};|&#[0-9]{1,5};)/i;
    if (re.test(mystr)) return true

    return false
}

function PlainTextToHtml(str)
{
    var mystr = new String(str)
    var re
    re = /\r\n/g;
    mystr = mystr.replace(re, "<br />");
    re = /\n/g;
    mystr = mystr.replace(re, "<br />");
    re = /\r/g;
    mystr = mystr.replace(re, "<br />");
    re = /  /g;
    mystr = mystr.replace(re, " &nbsp;");
    re = /\t/g;
    mystr = mystr.replace(re, " &nbsp; &nbsp;");
    return mystr;
}

function GetInnerTextById(id)
{
    if (!document.getElementById) return ""
    var node = document.getElementById(id)
    if (node)
    {
        if (is_ie) return node.innerText
        else
        {
            var html = document.createRange()
            html.selectNodeContents(node);
            return html.toString();
        }
    }
    return ""
}


