// Copyright (c) 2004 Paul Hsieh
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 
//     Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
// 
//     Redistributions in binary form must reproduce the above copyright 
//     notice, this list of conditions and the following disclaimer in the 
//     documentation and/or other materials provided with the distribution.
// 
//     Neither the name of format.js nor the names of its contributors may be 
//     used to endorse or promote products derived from this software without
//     specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

fmt_Font  = 'Courier, Arial, Geneva';
fmt_Size  = 2;
fmt_Col   = 0;
fmt_Mode0 = '<b>';
fmt_Mode1 = '</b>';
fmt_Tabwidth = 4;

function fmt_Write (q) {
    var xhead = '<font face = "' + fmt_Font + '" size = "' + fmt_Size + '">' + fmt_Mode0;
    var xtail = fmt_Mode1 + '</font>';

    if (fmt_Font == '') {
        xhead = fmt_Mode0;
        xtail = fmt_Mode1;
    }

    v = xhead;
    s = unescape (q);
    for(i=0; i < s.length; i++) {
        if (s.charAt (i) == '&') {
            v = v + '&amp;';
            fmt_Col = fmt_Col + 1;
	} else if (s.charAt (i) == '<') {
            v = v + '&lt;';
            fmt_Col = fmt_Col + 1;
	} else if (s.charAt (i) == '>') {
            v = v + '&gt;';
            fmt_Col = fmt_Col + 1;
	} else if (s.charAt (i) == ' ') {
            v = v + '&nbsp;';
            fmt_Col = fmt_Col + 1;
	} else if (s.charAt (i) == '\%') {
            v = v + '&#37;';
            fmt_Col = fmt_Col + 1;
	} else if (s.charAt (i) == '\n' || s.charAt (i) == '\r') {
            v = v + xtail + '<br>' + xhead;
            fmt_Col = 0;
        } else if (s.charAt (i) == '\t') {
            do {
                v = v + '&nbsp;';
                fmt_Col = fmt_Col + 1;
            } while ((fmt_Col % fmt_Tabwidth) != 0);
	} else {
            v = v + s.charAt (i);
            fmt_Col = fmt_Col + 1;
	}
    }
    v = v + xtail;
    document.write (v);
}

function fmt_SetFont (q) {
    fmt_Font = q;
}

function fmt_SetFontSize (q) {
    fmt_Size = q;
}

function fmt_SetCol (c) {
    fmt_Col  = c;
}

function fmt_ModeNormal () {
    fmt_Mode0 = '';
    fmt_Mode1 = '';
}

function fmt_ModeBold () {
    fmt_Mode0 = '<b>';
    fmt_Mode1 = '</b>';
}

function fmt_ModeItalic () {
    fmt_Mode0 = '<em>';
    fmt_Mode1 = '</em>';
}

function fmt_ModeBoldItalic () {
    fmt_Mode0 = '<b><em>';
    fmt_Mode1 = '</em></b>';
}


