var rosterData = new Array (
     new Array('<a href=\"team.php?playerID=28\">Bell, Carey</a>', '28', 'SO', 'D', '6-2', '190', 'Fairfax, VA'),
     new Array('<a href=\"team.php?playerID=16\">Berry, Frank</a>', '16', 'SR', 'F', '6-1', '190', 'Allentown, PA'),
     new Array('<a href=\"team.php?playerID=24\">Brooks, Craig</a>', '24', 'SR', 'D', '5-10', '180', 'Douglas, MA'),
     new Array('<a href=\"team.php?playerID=20\">Cera, Taylor</a>', '20', 'SO', 'F', '6-2', '215', 'Girard, OH'),
     new Array('<a href=\"team.php?playerID=18\">Cerutti, Chris</a>', '18', 'FR', 'F', '5-10', '170', 'Harrison City, PA'),
     new Array('<a href=\"team.php?playerID=26\">Clark, Alan</a>', '26', 'FR', 'F', '6-1', '180', 'Dunellen, NJ'),
     new Array('<a href=\"team.php?playerID=15\">Collins, Kurt</a>', '15', 'FR', 'F', '6-2', '190', 'Port Matilda, PA'),
     new Array('<a href=\"team.php?playerID=6\">Conte, John</a>', '6', 'JR', 'D', '6-1', '195', 'Mahwah, NJ'),
     new Array('<a href=\"team.php?playerID=25\">Dakan, Scott</a>', '25', 'SR', 'D', '6-1', '185', 'Murrysville, PA'),
     new Array('<a href=\"team.php?playerID=27\">Daley, Paul</a>', '27', 'FR', 'F', '6-2', '190', 'Toms River, NJ'),
     new Array('<a href=\"team.php?playerID=9\">DeLorenzo, Luke</a>', '9', 'SR', 'F', '5-11', '190', 'Leechburg, PA'),
     new Array('<a href=\"team.php?playerID=23\">Diethorn, Mike</a>', '23', 'SR', 'F', '5-11', '180', 'Bethel Park, PA'),
     new Array('<a href=\"team.php?playerID=11\">Herel, David</a>', '11', 'JR', 'F', '6-1', '195', 'Vestal, NY'),
     new Array('<a href=\"team.php?playerID=35\">Hume, Teddy</a>', '35', 'SO', 'G', '6-4', '230', 'Austin, TX'),
     new Array('<a href=\"team.php?playerID=1\">Jay, John</a>', '1', 'SO', 'G', '6-1', '195', 'Dedham, MA'),
     new Array('<a href=\"team.php?playerID=17\">Kirstein, Matt</a>', '17', 'JR', 'F', '6-1', '195', 'Dublin, OH'),
     new Array('<a href=\"team.php?playerID=2\">Magulick, Andrew</a>', '2', 'SR', 'D', '6-2', '220', 'State College, PA'),
     new Array('<a href=\"team.php?playerID=4\">Mills, Kyle</a>', '4', 'SR', 'D', '6-0', '180', 'Trumbull, CT'),
     new Array('<a href=\"team.php?playerID=10\">O\'Brien, Tim</a>', '10', 'SO', 'F', '6-0', '175', 'Bethel Park, PA'),
     new Array('<a href=\"team.php?playerID=8\">Paradis, Ryan</a>', '8', 'FR', 'F', '5-9', '175', 'Rochester, NH'),
     new Array('<a href=\"team.php?playerID=21\">Peck, Steve</a>', '21', 'SR', 'F', '6-2', '200', 'New Hartford, NY'),
     new Array('<a href=\"team.php?playerID=3\">Petrick, Dan</a>', '3', 'FR', 'D', '6-1', '180', 'State College, PA'),
     new Array('<a href=\"team.php?playerID=5\">Polidor, Marek</a>', '5', 'SO', 'F', '5-9', '175', 'Coraopolis, PA'),
     new Array('<a href=\"team.php?playerID=7\">Pronchik, Chris</a>', '7', 'SO', 'F ', '5-8', '165', 'Bethlehem, PA'),
     new Array('<a href=\"team.php?playerID=29\">Robinson, Zach</a>', '29', 'FR', 'F', '6-4', '220', 'Ballston Lake, NY'),
     new Array('<a href=\"team.php?playerID=19\">Rubeo, Brandon</a>', '19', 'SR', 'F', '5-8', '175', 'McMurray, PA'),
     new Array('<a href=\"team.php?playerID=12\">Seravalli, Nick</a>', '12', 'FR', 'F', '5-7', '175', 'Ivyland, PA'),
     new Array('<a href=\"team.php?playerID=33\">Signet, Nick</a>', '33', 'SR', 'G', '6-1', '200', 'Sewickley, PA'),
     new Array('<a href=\"team.php?playerID=13\">Thurston, Steve</a>', '13', 'SR', 'D', '6-3', '205', 'Wallingford, CT'),
     new Array('<a href=\"team.php?playerID=22\">Tranter, Brent</a>', '22', 'SR', 'D', '6-0', '190', 'Cranberry Twp, PA'),
     new Array('<a href=\"team.php?playerID=14\">Zimmel, Jaime</a>', '14', 'SR', 'F', '5-9', '190', 'Lincroft, NJ')
);

sortRoster = function(sortColumn) {
  var desc = false;
  if (sortColumn == lastSortColumn) {
    if (lastSortDirection == 'asc') {
      desc = true;
    }
  }

  lastSortColumn = sortColumn;
  var comparator = rosterDataRowComparatorFactory(sortColumn);
  rosterData.sort(comparator);

  if (desc) {
    lastSortDirection = 'desc'
    rosterData.reverse();
  } else {
    lastSortDirection = 'asc'
  }

  for (var r = 0; r < rosterData.length; r++) {
    for (var c = 0; c < rosterData[r].length; c++) {
      var item = 'r' + r + 'c' + c;
      document.getElementById(item).innerHTML = rosterData[r][c]
    }
  }
}

rosterDataRowComparatorFactory = function(sortColumn) {
  rosterDataRowComparator = function(a1, a2) {
    if (sortColumn == 0) {
      var c1 = getLastName(a1[sortColumn]);
      var c2 = getLastName(a2[sortColumn]);
    } else {
      var c1 = stripWhitespace(a1[sortColumn]);
      var c2 = stripWhitespace(a2[sortColumn]);
    }

    if (c1 == c2) {
      return 0;
    } else if (c1 == '') {
      return -1;
    } else if (c1 > c2) {
      return 1;
    } else {
      return -1;
    }
  }

  return rosterDataRowComparator;
}

getLastName = function(str) {
  if (str.substring(0,2) == '<a') {
    splitByLessThans = stripWhitespace(str).split('<');
    splitByGreaterThans = stripWhitespace(splitByLessThans[1]).split('>');
    fullNameAsDisplayed = stripWhitespace(splitByGreaterThans[1]);
  } else {
    fullNameAsDisplayed = stripWhitespace(str)
  }

  if (fullNameAsDisplayed.indexOf(',') >= 0) {
    return fullNameAsDisplayed
  } else {
    nameSplit = fullNameAsDisplayed.split(' ')
    return nameSplit[1]
  }
}

stripWhitespace = function(str) {
  var numLeadingSpaces = 0;
  for (i = 0; i < str.length; i++) {
    if (str[i] == ' ') {
      numLeadingSpaces++;
    } else {
      break;
    }
  }

  str = str.substring(numLeadingSpaces, str.length);
  var numTrailingSpaces = 0;
  for (i = str.length - 1; i >= 0; i--) {
    if (str[i] == ' ') {
      numTrailingSpaces++;
    } else {
      break;
    }
  }

  str = str.substring(0, str.length-numTrailingSpaces);

  if ((str.length==1) && (str != 'G') && (str != 'D') && (str != 'F')) {
    str = parseInt( str );
  }

  if (str=='FR') { str = 1; }
  if (str=='SO') { str = 2; }
  if (str=='JR') { str = 3; }
  if (str=='SR') { str = 4; }

  return str;
}
