var officials1 = [
 "Abernathy, Ty", "Adams, Debbie", "Aldrich, Joanne", "Ahumada, Lorena", "Aliberti, Janice", "Almarode, John",
 "Bacon, Kyle", "Baker, Eric", "Baldinelli, Tom", "Baldwin, Bart", "Banuelos, Lucy", "Barise, Joe",
 "Barlow, Melissa", "Becker, Cami", "Behrens, Mark", "Bills, Andrew", "Blank, Becky",
 "Blauch, Susan", "Bomeli, Ray", "Bone, Shannon", "Bonner, Amy", "Bradley, Erica", "Brewton, Eric",
 "Briggans, Donavan", "Brockett, Reid", "Brooks, Cynthia", "Brooks, Kevin", "Brooks, Missy",
 "Brooks-Clauser, Denise", "Brown, Reta", "Browne, Heather", "Brunette, Bryan", "Bryant, Timothy",
 "Callahan, Brian", "Campbell, Randy", "Carney, Cliff", "Carroll, Charles", "Caudle, Jeff", 
 "Chelette, Newton", "Chue, Susan", "Cissoko, Fatou", "Collins, J.D.", "Comanita, Carol",
 "Contreras, Peter", "Copeland, Kelly", "Cornell, Catherine", "Courteau, June", "Creech, Rodney",
 "Cross, Gina", "Cross, Jeff", "Cunningham, Joe", "Dahlem, Teresa", "Dailey, Wendy", "Daley, Tim",
 "Danaher, Tom", "Davis, Kym", "Davis, Melanie",  "Davis, Penny", "Day, Mary", "Dean, Wesley",
 "Del  Duco, Michelle", "DeMayo, Dennis", "DeMinico, Bob", "Dennis, Kelly", "DePaul, Diana",
 "Dickerson, Jesse", "Dillard, Kevin", "Discher, Butch", "Doll, Darci", "Drayton, Ken", "Dressander, Ron"];

var officials2 = [
 "Enlund, Angela", "Ensminger, Robin", "Enterline, Bob", "Enterline, Bryan", 
 "Erdman, Craig", "Farlow, Kevin", "Faulconer, Jeff", "Feck, Shannon", "Fernandes, Patti", 
 "Fitzpatrick, John", "Fletcher, John", "Forsberg, Maj", "Fountain, Carla", "Foutz, Darla",
 "Franson, Ben", "Gallien, Jules", "Garcia, Debra Ann", "Garrett, Mazetta", "Garrow, Jon",
 "Gebhardt, Tim", "Gervasoni, Bret", "Gines, Mark", "Gonyeau, Leah", "Gonzalez, Charles",
 "Gonzalez, Luis","Goode, Shawn", "Graves, Beth", "Graves, Bethany", "Grinter, Felicia",
 "Gruca, Karen", "Gulbeyan, Roy", "Hall, Brian", "Halleand, Tom", "Hardcastle, Mark",
 "Harding, Warren", "Hermann, Scotty", "Herriman, Erika", "Higgins, John", "Hill, Brian",
 "Hobbs, Kim", "Holloway, Robert", "Humphrey, Daryl", "Hust, Charlie", "Inouye, Cameron",
 "Johnson, Kelly", "Johnson, Kent", "Jones, Jack", "Jones, Lisa", "Jones, Lori",
 "Jones, Norma", "Jones, Rachelle", "Kantner, Dee", "Karp, Marianne", "Kelly, Ken",
 "Ketterling, Cody", "Kimura, Kaili", "Knight, Doug", "Koch, Eric", "Kramer, David",
 "Krommenhoek, Julie", "Krzesnik, Darren", "Landry, Melvin", "Larance, Bill", "Larson, Eric",
 "Lasuik, Karen", "Ledington, Ronald", "Leibfried, Norma", "Lewis, Angela", "Lippa, Tony",
 "Lonergan, Kathy", "Lynch, Kathleen", "Mack, Duane", "Maddock", "Marsh, Dawn",
 "Martin, Chet", "Martinez, Jess", "Mattingly, Lisa", "Mayer, Dennis", "Maxey, Brad",
 "McClenney, Mark", "McConnell, Michael", "Miller, Doug", "Miller, Keith", "Miller, Pam",
 "Mitchell, Darrell", "Moreno, Alex", "Morningstar, John", "Morrell, Jim", "Morris, Bruce", "Morris, Laura", 
 "Morris, Rick", "Moyer, Kelly", "Muench, Chaney", "Munoz, Mark", "Murray, Micol"];

var officials3 = [
 "Nakasone, Shelley", "Napier, Tina", "Newton, Lawson", "Novak, Edward", "O'Connor, Colleen",
 "Olson, Steve", "Orminski, John", "Ortega, Anita", "Outlaw, Dan", "Overstreet, Meadow",
 "Palermo, John", "Pantoja, Brenda", "Pardue, Connie", "Parrish, Gator", "Paugh, Renee",
 "Pence, Jeneane", "Pence, Karen", "Perkins, Connie", "Pethtel, Kevin", "Pettus, Bonnie",
 "Pickett, Ralph", "Pierre, Kerry", "Pitock, Dave", "Plunkett, Jack", "Poles, Christopher", 
 "Porter, Laurie", "Preato, Karen", "Price, Michael", "Rahe, Laura", "Ramsey, Marlin", "Ratliffe, Rick",
 "Resch, Mark", "Rezac, Jennifer", "Ridilla, Kathy", "Riggs, Mike", "Riordan, Jack",
 "Rittman, Dave", "Roach, Rita", "Roberts, Barry", "Roberts, Beverly", "Roberts, Metta",
 "Robertson, Randy", "Ruland, Kevin", "Russi, Michelle",  "Salerno, Tommy", "Sather, Jo Ann",
 "Schimel, Gary", "Schmidt, Michael", "Schroeder, Ann", "Schumaker, Jay", "Scofield, Bob",
 "Showers, Rick", "Sidlasky, Ed", "Sisk, Dock", "Sisk, Nan", "Skrine, Jerome", "Small, Greg",
 "Smith, Barbara", "Smith, William", "Sowby, Ken", "Spence, Bonita", "Spurlock, Pualani",
 "St. Pierre, Michelle", "Stanton, Angel", "Stanton, Angela",  "Starkey, Scott", "Steratore, Frank",
 "Stevens, Clarke", "Stewart, Angela", "Stewart, David", "Stewart, Taiqua", "Stromgen, Jeffrey",
 "Strong, Robert", "Suffren, Angelica", "Sykes, Marvin", "Szeremeta, Wanda", "Tackett, Matt",
 "Taylor, John", "Thebo, Kim", "Titus, Bill", "Trammell, Bob", "Vaszily, Joseph",
 "Vera, Kristi", "Walker, Alicia", "Waller, Kim", "Waters, Richard", "Weiand, Kenneth", 
 "Welch, Vanessa", "Whitson, John", "Whittemore, Jack", "Wilcox, Ricky", "Williams, Doug",
 "Williams, Les", "Winders, Troy", "Yarbrough, Scott", "Zentz, Mark"];


var officials = [officials1, officials2, officials3];

var officialNames = [
  document.getElementById("officialOpts1"),
  document.getElementById("officialOpts2"),
  document.getElementById("officialOpts3")];

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


function getBox(e) {

  if (document.all) { cellObj=event.srcElement;
  } else { cellObj=e.target; }
  if (cellObj.className=="box")  {
     window.open("http://scores.espn.go.com/ncw/boxscore?gameId=" + cellObj.id.substring(1));
  }
}

function reset() {
   document.getElementById("startYear").selectedIndex=0;
   document.getElementById("startMonth").selectedIndex=0;
   document.getElementById("startDay").selectedIndex=0;

/*   if (document.getElementById("startHide").className=="loaded") {
      document.getElementById("startHide").style.display="none";
      document.getElementById("endDay").selectedIndex=0;
      document.getElementById("endYear").selectedIndex=0;
      document.getElementById("endMonth").selectedIndex=0;
   } else {
      document.getElementById("endDay").selectedIndex=28;
      document.getElementById("endYear").selectedIndex=6;
      document.getElementById("endMonth").selectedIndex=1;
      document.getElementById("startHide").className="loaded";
   }*/
   document.getElementById("tourney").selectedIndex=0;
   document.getElementById("round").selectedIndex=0;
   document.getElementById("team").selectedIndex=0;
   document.getElementsByName("officialsel")[0].checked = true;
   scopeOfficial(0);
}

function resetFilter() {
  //document.getElementById("startHide").className = "loaded"
   reset();
   filter();
}

function filter() {
 /*if (document.getElementById("startHide").className == "loaded") {
     document.getElementById("startHide").style.display="none";
 }*/

 document.getElementById("viewFilter").style.display = "none";
 document.getElementById("gamebody").style.display = "none";
 document.getElementById("waitbox").style.display = "";
 window.setTimeout('filter_wait()',1);
}

function filter_wait() {
 do_filter();
 document.getElementById("waitbox").style.display = "none";
 document.getElementById("viewFilter").style.display = "";
 document.getElementById("gamebody").style.display = "";
}


function do_filter() {
var tableRef = document.getElementById("assignTable");


var anyOfficial = document.getElementsByName("officialsel")[0].checked;
var startPtr = document.getElementById("startYear").selectedIndex;
var endPtr = document.getElementById("endYear").selectedIndex;

var firstYear = document.getElementById("startYear").options[startPtr].innerHTML;
var lastYear = document.getElementById("endYear").options[endPtr].innerHTML;

startPtr = document.getElementById("startMonth").selectedIndex;
endPtr = document.getElementById("endMonth").selectedIndex;

var firstMonth = document.getElementById("startMonth").options[startPtr].innerHTML;
var lastMonth = document.getElementById("endMonth").options[endPtr].innerHTML;

if (firstYear == "any") { firstMonth == "any" };
if (lastYear == "any") { lastMonth == "any" };

startPtr = document.getElementById("startDay").selectedIndex;
endPtr = document.getElementById("endDay").selectedIndex;

var firstDay = document.getElementById("startDay").options[startPtr].innerHTML;
var lastDay = document.getElementById("endDay").options[endPtr].innerHTML;

if (firstMonth == "any") { firstDay == "any" };
if (lastMonth == "any") { lastDay == "any" };



var anyDate = (firstYear == "any" && lastYear == "any" &&
               firstMonth == "any" && lastMonth == "any" &&
               firstDay == "any" && lastDay == "any");

var tourneyPtr = document.getElementById("tourney").selectedIndex;
var tourneySel = document.getElementById("tourney").options[tourneyPtr].innerHTML;
var roundPtr = document.getElementById("round").selectedIndex;
var roundSel = document.getElementById("round").options[roundPtr].innerHTML.charAt(0);
var teamPtr = document.getElementById("team").selectedIndex;
var teamSel = document.getElementById("team").options[teamPtr].innerHTML;

var showRow = "none";
var gameDate = "";
var official = "";
var gameOfficial = "";
var nameMap = [0, 0, 1, 2, 3]
var at = 0;
var color = 1;
var yearOk = false;
var monthOk = false;
var dayOk = false;

if (!anyOfficial) {

     for (var i = 1; i < 4; i++) {
        if (document.getElementsByName("officialsel")[i].checked) {
           official = officialNames[nameMap[i]].options[officialNames[nameMap[i]].selectedIndex].innerHTML;
           at = official.indexOf(",");
           if (at > -1) {
              official = official.substring(at + 2) + " " + official.substring(0, at);
           }
           break;
        }
     }

}


var names = "";
var inc = 0;
var yr = "";
var team1 = "";
var team2 = "";

//alert(tableRef.rows[2].cells[1].innerHTML);

for (var i = 2; i < tableRef.rows.length; i++) {
 showRow = "";
 if (!anyDate) {
   gameDate = tableRef.rows[i].cells[1].innerHTML;
   yearOk = (firstYear == "any" || firstYear <= gameDate.substring(0,2)) &&
            (lastYear == "any" || lastYear >= gameDate.substring(0,2));
   monthOk = (firstMonth == "any" || firstYear < gameDate.substring(0,2) ||
              firstMonth.charAt(1) <= gameDate.charAt(3)) &&
            (lastMonth == "any" || lastYear > gameDate.substring(0,2) ||
               lastMonth.charAt(1) >= gameDate.charAt(3));
   dayOk =  (firstDay == "any" ||
              (firstYear + firstMonth.charAt(1)) < (gameDate.substring(0,2) + gameDate.charAt(3)) ||
              firstDay <= gameDate.substring(5)) &&
            (lastDay == "any" ||
              (lastYear + lastMonth.charAt(1)) > (gameDate.substring(0,2) + gameDate.charAt(3)) ||
              lastDay >= gameDate.substring(5));
    if (!(yearOk && monthOk && dayOk)) { showRow="none";}
 }

 if (showRow == "" && tourneySel != "any") {
    if (tableRef.rows[i].cells[2].innerHTML.indexOf(tourneySel) == -1) { showRow="none";}
 }


 if (!anyOfficial && tableRef.rows[i].cells.length < 8 ||
     tableRef.rows[i].cells[7] == "") { showRow = "none";}


 if (showRow == "" && !anyOfficial) {

    gameOfficial = tableRef.rows[i].cells[7].innerHTML;
    names= gameOfficial.split(",");
    for (var inc=0; inc < names.length; inc++) {
       at = names[inc].indexOf(" (");
       if (at > -1) { names[inc] = names[inc].substring(0, at); }
       if (official == names[inc].trim()) { break; }
    }

    if (inc == names.length) { showRow="none";}
 }

  if (showRow == "" && roundPtr > 0 &&
      roundSel != tableRef.rows[i].cells[3].innerHTML.charAt(0) ) {
      showRow="none";
  }

  if (showRow == "" && teamPtr > 0) {
      team1 = tableRef.rows[i].cells[4].innerHTML;
      if (team1.indexOf(")") > - 1) {
         team1 = team1.substring(team1.indexOf(")") + 2);
      }
      team2 = tableRef.rows[i].cells[6].innerHTML;
      if (team2.indexOf(")") > - 1) {
         team2 = team2.substring(team2.indexOf(")") + 2);
      }
      if (teamSel != team1 && teamSel != team2) { showRow="none"; }
 }

 tableRef.rows[i].style.display = showRow;
 if (showRow == "") {
     if (color ==1) { tableRef.rows[i].bgColor="#E1E4F2";
     } else {  tableRef.rows[i].bgColor="white";}
     color = color * (-1);
    }
}
}

function loadIt() {

if (document.getElementById("officialOpts1").options.length == 0) {
   loadArray(officials1, 0, document.getElementById("officialOpts1"));
   loadArray(officials2, 0, document.getElementById("officialOpts2"));
   loadArray(officials3, 0, document.getElementById("officialOpts3"));
}

showGuide();


    reset();
    filter();
/*allOfficials = document.all.officialsel[3].checked;


if (allOfficials
&& document.getElementById("startDay").selectedIndex == 0
&& document.getElementById("endDay").selectedIndex == 0) {
  var curtable = document.getElementById("assignTable");
  var rows = curtable.getElementsByTagName("tr");
  for(i = 2; i < rows.length; i++){
     if (i%2 ==0) { rows[i].bgColor="#E1E4F2";
     } else {  rows[i].bgColor="white";}
  }
  return;
}
*/

}
function loadArray(srcArray, srcPtr, tgtList) {

 for(var i = tgtList.options.length - 1; i >= 0; i--) {
  tgtList.remove(i);
 }
 for (var i=0; i < srcArray.length;++i){
  addOption(tgtList, srcArray[i], "");
 }
 tgtList.selectedIndex = srcPtr;

}

function addOption(selectbox,text,value) {
 var optn = document.createElement("OPTION");
 optn.text = text;
 optn.value = value;
 
   selectbox.options.add(optn);
}

function scopeOfficial(ptr) {
document.getElementById("officialOpts1").disabled =  (ptr !=1);
document.getElementById("officialOpts2").disabled =  (ptr !=2);
document.getElementById("officialOpts3").disabled =  (ptr !=3);
}


function setStart() {
if (document.getElementById("startDay").selectedIndex >
document.getElementById("endDay").selectedIndex) {
document.getElementById("endDay").selectedIndex =
document.getElementById("startDay").selectedIndex;
}
}

function setEnd() {
if (document.getElementById("endDay").selectedIndex <
document.getElementById("startDay").selectedIndex) {
document.getElementById("startDay").selectedIndex =
document.getElementById("endDay").selectedIndex;
}
}


function setStartYear() {
if (document.getElementById("startYear").selectedIndex >
document.getElementById("endYear").selectedIndex) {
document.getElementById("endYear").selectedIndex =
document.getElementById("startYear").selectedIndex;
}
}

function setEndYear() {
if (document.getElementById("endYear").selectedIndex <
document.getElementById("startYear").selectedIndex) {
document.getElementById("startYear").selectedIndex =
document.getElementById("endYear").selectedIndex;
}
}


function setStartYear() {
if (document.getElementById("startYear").selectedIndex >
document.getElementById("endYear").selectedIndex) {
document.getElementById("endYear").selectedIndex =
document.getElementById("startYear").selectedIndex;
}
}

function setEndYear() {
if (document.getElementById("endYear").selectedIndex <
document.getElementById("startYear").selectedIndex) {
document.getElementById("startYear").selectedIndex =
document.getElementById("endYear").selectedIndex;
}
}


function setStartMonth() {
if (document.getElementById("startMonth").selectedIndex >
document.getElementById("endMonth").selectedIndex) {
document.getElementById("endMonth").selectedIndex =
document.getElementById("startMonth").selectedIndex;
}
}

function setEndMonth() {
if (document.getElementById("endMonth").selectedIndex <
document.getElementById("startMonth").selectedIndex) {
document.getElementById("startMonth").selectedIndex =
document.getElementById("endMonth").selectedIndex;
}
}

function setStartDay() {}
function setEndDay() {}


function showGuide() {
var opt = document.getElementById("guideChoice").selectedIndex;
document.getElementById("viewOfficial").style.display = opt == 1 ? "" : "none";
document.getElementById("viewAllOfficials").style.display = opt == 2 ? "" : "none";
document.getElementById("viewDays").style.display = opt == 3 ? "" : "none";
document.getElementById("viewTourney").style.display = opt == 4 ? "" : "none";
}