﻿//automatically appens a <br> tag to create a new line
$.fn.appendLine = function(line) {
    this.append(line);
    this.append('<br />');
}

//standard trim functions
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}

//formats 10 digit string into (xxx)xxx-xxxx format
String.prototype.toPhone = function() {
    val = this.replace(/\D/g, '');

    if (val === null || val === undefined) {
        throw { Message: 'String is null or undefined' };
    }

    if (val.length !== 10) {
        return;
    }

    var phone = '';
    phone = '(';
    val = val.split('');

    for (var i = 0; i < val.length; i++) {
        phone += val[i];

        if (i == 2) {
            phone += ')';
        } else if (i == 5) {
            phone += '-';
        }
    }
    return phone;
}

//formats a number into money format
Number.prototype.toMoney = function() {
    return '$' + this.toFixed(2);
}

//wraps contents in <table> tag
function table(html, cssClass, id) {
    var CSS = cssClass != undefined ? "class='" + cssClass + "'" : "";
    var ID = id != undefined ? "id='" + id + "'" : "";
    var HTML = html != undefined ? html : "";

    this.begin = function() {
        return '<table ' + CSS + ' ' + ID + '>';
    }

    this.end = function() {
        return '</table>';
    }

    return this.begin() + HTML + this.end();
}

//wraps contents in <tr> tag
function row(html, cssClass, id) {
    var CSS = cssClass != undefined ? "class='" + cssClass + "'" : "";
    var ID = id != undefined ? "id='" + id + "'" : "";
    var HTML = html != undefined ? html : "";

    this.begin = function() {
        return '<tr ' + CSS + ' ' + ID + '>';
    }

    this.end = function() {
        return '</tr>';
    }

    return this.begin() + HTML + this.end();
}

//wraps contents in <td> tag
function cell(html, colspan, cssClass, id, rowspan) {
    var CSS = cssClass != undefined ? "class='" + cssClass + "'" : "";
    var ID = id != undefined ? "id='" + id + "'" : "";
    var HTML = html != undefined ? html : "";
    var COLSPAN = colspan !== undefined && colspan !== null
        ? "colspan='" + colspan + "'" : "";
    var ROWPSAN = rowspan !== undefined && rowspan !== null
        ? "rowspan='" + rowspan + "'" : "";

    this.begin = function() {
        return '<td ' + CSS + ' ' + ID + ' ' + COLSPAN + ' ' + ROWPSAN + '>';
    }

    this.contents = function() {
        return HTML;
    }

    this.end = function() {
        return '</td>';
    }

    this.toString = function() {
        return this.begin() + HTML + this.end();
    }

    return this.begin() + HTML + this.end();
}

//formats parameters into a <span> tag
function span(html, cssClass, id) {
    var CSS = cssClass != undefined ? "class='" + cssClass + "'" : "";
    var ID = id != undefined ? "id='" + id + "'" : "";
    var HTML = html != undefined ? html : "";
    
    this.begin = function() {
        return '<span ' + CSS + ' ' + ID + '>';
    }
    this.end = function() {
        return '</span>';
    }
    this.toString = function() {
        return this.begin() + HTML + this.end();
    }
    
    return this.begin() + HTML + this.end();
}

//formats parameters into a <span> tag
function div(html, cssClass, id) {
    var CSS = cssClass != undefined ? "class='" + cssClass + "'" : "";
    var ID = id != undefined ? "id='" + id + "'" : "";
    var HTML = html != undefined ? html : "";

    this.begin = function() {
        return '<div ' + CSS + ' ' + ID + '>';
    }
    this.end = function() {
        return '</div>';
    }
    this.toString = function() {
        return this.begin() + HTML + this.end();
    }

    return this.begin() + HTML + this.end();
}

//formats parameters into an <a> tag
function hyperlink(url, contents, cssClass, id) {
    var html = '<a href="' + url + '" ';
    if (cssClass != null && cssClass != undefined && cssClass != "") {
        html += 'class="' + cssClass + '" ';
    }
    if (id != null && id != undefined && id != "") {
        html += 'id="' + id + '"';
    }
    html += '>' + contents + '</a>';

    return html;
}

//formats a date into xx/xx/xxxx format
Date.prototype.toShortDateString = function() {
    var shortDateString = '';
    shortDateString += (this.getMonth() + 1) + '/';
    shortDateString += this.getDate() + '/';
    shortDateString += this.getFullYear();
    return shortDateString;
}