
/* -------------------------------------------------------------------------- */
/*                                 HTML Tags                                  */
/* -------------------------------------------------------------------------- */

// Tables
String.prototype.TH = _TH;
function _TH() {
    return JSV_th(this);
}

String.prototype.TD = _TD;
function _TD() {
    return JSV_td(this);
}

String.prototype.TR = _TR;
function _TR() {
    return JSV_tr(this);
}

String.prototype.TABLE = _TABLE;
function _TABLE() {
    return JSV_table(this);
}

function JSV_th(cellData, attributes) {
	return "<th" +
		( !JSV_isWhitespace(JSV_th.arguments[1]) ? (' ' + attributes).toLowerCase() : '' ) + ">" +
		( !JSV_isWhitespace(JSV_th.arguments[0]) ? cellData : "&nbsp;" ) + "</th>";
}

function JSV_td(cellData, attributes) {
	return "<td" +
		( !JSV_isWhitespace(JSV_td.arguments[1]) ? (' ' + attributes).toLowerCase() : '' ) + ">" +
		( !JSV_isWhitespace(JSV_td.arguments[0]) ? cellData : "&nbsp;" ) + "</td>";
}

function JSV_tr(rowData, attributes) {
	return "<tr" +
		( !JSV_isWhitespace(JSV_tr.arguments[1]) ? (' ' + attributes).toLowerCase() : '' ) + ">" +
		( !JSV_isWhitespace(JSV_tr.arguments[0]) ? rowData : JSV_td() ) + "</tr>";
}

function JSV_table(tableData, attributes) {
	return "<table" +
		( !JSV_isWhitespace(JSV_table.arguments[1]) ? (' ' + attributes).toLowerCase() : '' ) + ">" +
		( !JSV_isWhitespace(JSV_table.arguments[0]) ? tableData : JSV_tr(JSV_td()) ) + 
		"</table>";
}

// Formatting and Layout
String.prototype.CENTER = _CENTER;
function _CENTER() {
    return JSV_center(this);
}

String.prototype.H = _H;
function _H() {
    return JSV_H(this, level, attributes);
}

String.prototype.P = _P;
function _P() {
    return JSV_paragraph(this, attributes);
}

String.prototype.DIV = _DIV;
function _DIV() {
    return JSV_div(this, attributes);
}

function JSV_center(expT) {
	return JSV_div(expT, 'align="center"');
}

function JSV_header(expT, level, attributes) {
	var headerExpression = '',
		headerLevel = ( (!JSV_isWhitespace(JSV_toString(JSV_header.arguments[1]))) ? level : '1' );
	if ( !JSV_isWhitespace(JSV_header.arguments[0]) ) {
		headerExpression =
			"<h" + headerLevel +
			( (!JSV_isWhitespace(JSV_header.arguments[2])) ? (' ' + attributes).toLowerCase() : '' ) +
			">" +
			expT +
			"</h" + headerLevel + ">";
	}
	return headerExpression;
}

function JSV_paragraph(expT, attributes) {
	var paragraphExpression = '';
	if (!JSV_isWhitespace(JSV_paragraph.arguments[0])) {
		paragraphExpression =
			"<p" +
			( (!JSV_isWhitespace(JSV_paragraph.arguments[1])) ? (' ' + attributes).toLowerCase() : '' ) +
			">" +
			expT + 
			"</p>";
	}
	return paragraphExpression;
}

function JSV_div(expT, attributes) {
	var divExpression = '';
	if ( !JSV_isWhitespace(expT) ) {
		divExpression = 
			"<div" +
			( !JSV_isWhitespace(attributes) ? (' ' + attributes).toLowerCase() : '' ) + ">" +
			expT + 
			"</div>";
	}
	return divExpression;
}

function JSV_br(expN) {
	var brExpression = "<br>";
	if (!JSV_isWhitespace(expN)) brExpression = JSV_replicate(brExpression, expN);
	return brExpression;
}

function JSV_nbsp(expN) {
	var nbspExpression = "&nbsp;";
	if (!JSV_isWhitespace(expN)) nbspExpression = JSV_replicate(nbspExpression, expN);
	return nbspExpression;
}

// Page Elements
function JSV_hr(attributes, noShade) {
	shadeRule = ( !JSV_isWhitespace(noShade) ? noShade : false );
	return "<hr" +
		( !JSV_isWhitespace(attributes) ? (' ' + attributes).toLowerCase() : '' ) + 
		( (shadeRule) ? " noshade" : '' ) +
		">";
}

// HTML Page
String.prototype.HTML = _HTML;
function _HTML() {
    return JSV_html(this);
}

String.prototype.HEAD = _HEAD;
function _HEAD() {
    return JSV_head(this);
}

String.prototype.TITLE = _TITLE;
function _TITLE() {
    return JSV_title(this);
}

String.prototype.BODY = _BODY;
function _BODY() {
    return JSV_body(this);
}

function JSV_html(expH) {
    return '<html>' + ( JSV_isWhitespace(expH) ? '' : expH ) + '</html>';
}

function JSV_head() {
    var documentHead = '<head>'
    if (JSV_head.arguments.length != 0) {
        var i = 0;
        for (i=0; i<JSV_head.arguments.length; i++) {
            documentHead += JSV_head.arguments[i];
        }
    }
    else {
        documentHead += JSV_title();
    }
    documentHead += '</head>';
    return documentHead;
}

function JSV_title(expT) {
    return '<title>' + ( JSV_isWhitespace(expT) ? 'Untitled' : expT ) + '</title>';
}

function JSV_body(expD, expB) {
    return '<body' + 
		( JSV_isWhitespace(expB) ? '' : ' ' + expB ) + '>' + 
		( JSV_isWhitespace(expD) ? '' : expD ) + 
		'</body>';
}

function JSV_createBlankDocument() {
    return JSV_html( JSV_head( JSV_title() ) + JSV_body() );
}

// APPLET
function JSV_applet(appletAttributes) {
    var appletTag = '';
    if ( !JSV_isWhitespace(appletAttributes) ) {
    	appletTag = "<applet " +
        	    	appletAttributes + 
                    ">";
		// PARAM Tags
        if (JSV_applet.arguments.length > 1) {
	        var i = 0;
            for (i=1; i<JSV_applet.arguments.length; i++) {
                appletTag += JSV_applet.arguments[i];
            }
        }
        appletTag += "</applet>";
    }
    return appletTag;
}

function JSV_appletParam(paramName, paramValue) {
    return '<param  name="' + paramName + '" value="' + paramValue + '">';
}

