function SparrowTable(sparrowsArray){
    this.sparrows = sparrowsArray;
    this.HTML = '';
    
    this.sparrowUrl = null;
    this.id = null;
    this.tableClass = null;
    this.headerRowClass = null;
    this.headerCellClass = null;
    this.rowClass = null;
    this.rowCellClass = null;
}

SparrowTable.prototype.innerHTML = function(){
    this.startTable();
    this.addHeader();
    for( i=0; i < this.sparrows.length; i++)
        this.addRow(this.sparrows[i]);
    this.endTable();
    return this.HTML;
};

SparrowTable.prototype.addClass = function(classObject){
    if( classObject == null)
        return '';
    else
        return ' class="' + classObject + '"';
};

SparrowTable.prototype.startTable = function(){
    this.HTML = '<table';

    if( this.id != null )
        this.HTML += ' id="' + this.id + '"';

    this.HTML += this.addClass(this.tableClass) + '>';
};

SparrowTable.prototype.endTable = function(){
    this.HTML += '</table>'
};

SparrowTable.prototype.addHeader = function(){
	pageURL = window.location.toString();
	queryIndex = pageURL.indexOf("?");
	ampIndex = pageURL.indexOf("&");
    this.HTML += '<tr' + this.addClass(this.headerRowClass) + '>';
	if (queryIndex > -1)
	{
		if (ampIndex > -1)
		{
			pageURL = pageURL.substring(0, queryIndex);
			this.HTML += '<th' + this.addClass(this.headerCellClass) + '><a href="' + pageURL + '?sortOrder=1">Sparrow</a></th>';
			this.HTML += '<th' + this.addClass(this.headerCellClass) + '><a href="' + pageURL + '?sortOrder=3">State</a></th>';
			this.HTML += '<th' + this.addClass(this.headerCellClass) + '><a href="' + pageURL + '?sortOrder=2">Club</a></th>';
			this.HTML += '<th' + this.addClass(this.headerCellClass) + '><a href="' + pageURL + '?sortOrder=4">Adoption Date</a></th>';
		}
		else
		{
			pageURL = pageURL.substring(0, queryIndex);
			this.HTML += '<th' + this.addClass(this.headerCellClass) + '><a href="' + pageURL + '?sortOrder=1&opposite=1">Sparrow</a></th>';
			this.HTML += '<th' + this.addClass(this.headerCellClass) + '><a href="' + pageURL + '?sortOrder=3&opposite=1">State</a></th>';
			this.HTML += '<th' + this.addClass(this.headerCellClass) + '><a href="' + pageURL + '?sortOrder=2&opposite=1">Club</a></th>';
			this.HTML += '<th' + this.addClass(this.headerCellClass) + '><a href="' + pageURL + '?sortOrder=4&opposite=1">Adoption Date</a></th>';

		}
	}
	else
	{
		this.HTML += '<th' + this.addClass(this.headerCellClass) + '><a href="' + pageURL + '?sortOrder=1">Sparrow</a></th>';
		this.HTML += '<th' + this.addClass(this.headerCellClass) + '><a href="' + pageURL + '?sortOrder=3">State</a></th>';
		this.HTML += '<th' + this.addClass(this.headerCellClass) + '><a href="' + pageURL + '?sortOrder=2">Club</a></th>';
		this.HTML += '<th' + this.addClass(this.headerCellClass) + '><a href="' + pageURL + '?sortOrder=4">Adoption Date</a></th>';
	
	}
    this.HTML += '</tr>';
};

SparrowTable.prototype.addRow = function(sparrow){
    this.HTML += '<tr' + this.addClass(this.rowClass) + '>';
    this.HTML += '<td' + this.addClass(this.rowCellClass) + '>'
    if( this.sparrowUrl != null )
        this.HTML += '<a href="' + this.sparrowUrl + '?id=' + sparrow.Id + '" title="View this Sparrow">';
    this.HTML += sparrow.FirstName + ' ' + sparrow.LastName
    if( this.sparrowUrl != null )
        this.HTML += '</a>';
    this.HTML += '</td>';
    this.HTML += '<td' + this.addClass(this.rowCellClass) + '>' + sparrow.State + '</td>';
    this.HTML += '<td' + this.addClass(this.rowCellClass) + '>' + sparrow.Club + '</td>';
    this.HTML += '<td' + this.addClass(this.rowCellClass) + '>' + formatDate(sparrow.NewDate, "M/d/yyyy") + '</td>';
    this.HTML += '</tr>';
};
