var vTeams = new Array();
var dGames = new Array();
var hNames = new Array();
var vNames = new Array();
var month_name = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

function initTimesOLD(form) {
  resetFields(results,true);
  ix = 1;
  if (form.schedmonth.value == "") {
	  form.home_team.options.length = 0;
    form.home_team.options[0] = new Option("-- Select Home Team --","");
    return;  
  }
  if (form.schedday.value == "") {
	  form.home_team.options.length = 0;
    form.home_team.options[0] = new Option("-- Select Home Team --","");
    return;
  }
  getDateIX(form);
}

function initTimes(form) {
  resetFields(results,true);
  ix = 1;
  if (form.schedmonth.value == "") {
	  form.home_team.options.length = 0;
    form.home_team.options[0] = new Option("-- Select Home Team --","");
    return;  
  }
  if (form.schedday.value == "") {
	  form.home_team.options.length = 0;
    form.home_team.options[0] = new Option("-- Select Home Team --","");
    return;
  }
  getDateIX(form);
}

function getMonth(form, mth, day, sched) {
  var m = mth.value
  var beg = 1;
  var end;
  if (m == "")
    return false;
  if (sched) {
	  form.home_team.options.length = 0;
    form.home_team.options[0] = new Option("-- Select Home Team --","");
	  form.visit_team.value = "";
  }
  var ix = 0;
  day.length = 0;
  day.options[ix] = new Option("--","");
  ix++;
  y = form.schedyear.value;
  var curday = "";
  for (i = 0; i < games.length; i++) {
      // Get the days to add to the day select box from the
      // Games array dates. Games dates are the first item
      // in each game string.
    var fld = games[i].split(";");
    var dt = fld[0].split("-");
    if (dt[0] > m)
      break;
    if (dt[0] == m && dt[1] != curday) {
      day.options[ix] = new Option(dt[1],dt[1]);
      ix++;
      curday = dt[1];
    }
  }
}

function getDateIX(form) {
//form.debug.value = "getDateIX\n";
  var d = form.schedday.value;
  if (d == "")
    return false;
  fillHomeTeam(form);
  return true;
}

function fillHomeTeam(form) {
  var classID = form.class_id.value;
//form.debug.value += "fillHomeTeams(from) ClassID= " + classID + "\n";
  var compDate = form.schedmonth.value + "-" + form.schedday.value;
  var g = 0;
  var ix = 0;
  var isFirst = true;
  vTeams = new Array();
  dGames = new Array();
  while (g < games.length) {
    fld = games[g].split(";");
    if (fld[0] > compDate) {
      break;
	  } else if (fld[0] == compDate) {
	    if (isFirst) {
	      form.home_team.length = 0;
	      form.home_team.options[ix] = new Option("-- Select Home Team --","");
		    vTeams[ix] = "";
		    dGames[ix] = "";
	      ix++;
		    isFirst = false;
	    }
	    var tm = team_names[fld[2]].split(";");
	    if (tm[0] == classID) {
//form.debug.value += "Name " + tm[2] + " :: " + tm[1] + "\n";
	      form.home_team.options[ix] = new Option(fld[1] + " :: " + tm[2],tm[1]);
	      vTeams[ix] = team_names[fld[3]];
	      dGames[ix] = games[g];
	      ix++
	    }
 	  }
    g++;
  }
  if (isFirst) {
    form.home_team.options.length = 0;
    form.home_team.options[0] = new Option("-- No Games Scheduled --","");
  }
}

function getVisitingTeam(form) {
  var ix = form.home_team.selectedIndex;
  if (ix == 0) return;
  form.visit_team.value = vTeams[ix].split(";")[2];
  var fld = dGames[ix].split(";");
  form.date_ix.value = fld[0];
  form.game_id.value = fld[4];
//  form.class_id.value = fld[2];
  form.home_id.value = team_names[fld[2]].split(";")[1];
  form.away_id.value = team_names[fld[3]].split(";")[1];
//form.debug.value += "home_id " + form.home_id.value + " away_id " + form.away_id.value + "\n";
  
  hNames = new Array();
  vNames = new Array();
  form.pitchH_0.length = 0;
  form.pitchH_0.options[0] = new Option("-- Select Pitcher --","0");
  form.pitchV_0.length = 0;
  form.pitchV_0.options[0] = new Option("-- Select Pitcher --","0");
  nX = 1;
  var tm = team_names[fld[2]].split(";");
  for (i = 3; i < tm.length; i++) {
    hNames[nX] = tm[i];
    nFld = hNames[nX].split("-");
    form.pitchH_0.options[nX] = new Option(nFld[1],nFld[0]);
    nX++;
  }
  nX = 1;
  var tm = team_names[fld[3]].split(";");
  for (i = 3; i < tm.length; i++) {
    vNames[nX] = tm[i];
    nFld = vNames[nX].split("-");
    form.pitchV_0.options[nX] = new Option(nFld[1],nFld[0]);
    nX++;
  }
  form.inningH_0.length = 0;
  form.inningV_0.length = 0;
}

function clearName(form, names, ix) {
  for (i = 1; i < names.length; i++) {
    if (ix != "" && names[i].indexOf(ix) > -1) {
      names[i] = "";
    }
  }
  return names;
}

function setInnings(inning) {
  inning.length = 0;
  inning.options[0] = new Option("---","");
  var inn_val = new Array("0.1","0.2","1.0","1.1","1.2","2.0","2.1","2.2","3.0","3.1","3.2","4.0","4.1","4.2","5.0");
  for (i = 0; i < inn_val.length; i++)
    inning.options[i+1] = new Option(inn_val[i],inn_val[i]);
}

function setHomePitchers(form, next, inning) {
  setInnings(inning);
  var nm = new Array();
  for (i = 0; i < hNames.length; i++)
    nm[i] = hNames[i];
  var ix = form.pitchH_0.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchH_1.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchH_2.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchH_3.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchH_4.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchH_5.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchH_6.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchH_7.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchH_8.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchH_9.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchH_10.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchH_11.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchH_12.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchH_13.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  name.length = 0;
  next.options[0] = new Option("-- Select Pitcher --","0");
  nX = 1;
  for (i = 0; i < nm.length; i++) {
    if (nm[i] > "") {
      nFld = nm[i].split("-");
      next.options[nX] = new Option(nFld[1],nFld[0]);
      nX++;
    }
  }
}

function setAwayPitchers(form, next, inning) {
  setInnings(inning);
  var nm = new Array();
  for (i = 0; i < vNames.length; i++)
    nm[i] = vNames[i];
  var ix = form.pitchV_0.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchV_1.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchV_2.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchV_3.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchV_4.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchV_5.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchV_6.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchV_7.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchV_8.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchV_9.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchV_10.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchV_11.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchV_12.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  ix = form.pitchV_13.value;
  if (ix != "") nm = clearName(form, nm, ix);  
  next.length = 0;
  next.options[0] = new Option("-- Select Pitcher --","0");
  nX = 1;
  for (i = 0; i < nm.length; i++) {
    if (nm[i] > "") {
      nFld = nm[i].split("-");
      next.options[nX] = new Option(nFld[1],nFld[0]);
      nX++;
    }
  }
}

function pitchCountAlert(type, ix, id) {
  var msg =  type + " team pitcher " + ix + " count not entered\n";
  id.className = 'tfvHighlight';
  return msg
}

function validateFields(form) {
  var valid = tfv.exec();
  if (!valid) return false;
  var alertMessage = "";
  if (form.pitchH_1.selectedIndex > 0 && form.countH_1.value == "")
    alertMessage += pitchCountAlert("Home", 2, t_pitchH_1);
  if (form.pitchH_2.selectedIndex > 0 && form.countH_2.value == "")
    alertMessage += pitchCountAlert("Home", 3, t_pitchH_2);
  if (form.pitchH_3.selectedIndex > 0 && form.countH_3.value == "")
    alertMessage += pitchCountAlert("Home", 4, t_pitchH_3);
  if (form.pitchH_4.selectedIndex > 0 && form.countH_4.value == "")
    alertMessage += pitchCountAlert("Home", 5, t_pitchH_4);
  if (form.pitchH_5.selectedIndex > 0 && form.countH_5.value == "")
    alertMessage += pitchCountAlert("Home", 6, t_pitchH_5);
  if (form.pitchH_6.selectedIndex > 0 && form.countH_6.value == "")
    alertMessage += pitchCountAlert("Home", 7, t_pitchH_6);
  if (form.pitchH_7.selectedIndex > 0 && form.countH_7.value == "")
    alertMessage += pitchCountAlert("Home", 8, t_pitchH_7);
  if (form.pitchH_8.selectedIndex > 0 && form.countH_8.value == "")
    alertMessage += pitchCountAlert("Home", 9, t_pitchH_8);
  if (form.pitchH_9.selectedIndex > 0 && form.countH_9.value == "")
    alertMessage += pitchCountAlert("Home", 10, t_pitchH_9);
  if (form.pitchH_10.selectedIndex > 0 && form.countH_10.value == "")
    alertMessage += pitchCountAlert("Home", 11, t_pitchH_10);
  if (form.pitchH_11.selectedIndex > 0 && form.countH_11.value == "")
    alertMessage += pitchCountAlert("Home", 12, t_pitchH_11);
  if (form.pitchH_12.selectedIndex > 0 && form.countH_12.value == "")
    alertMessage += pitchCountAlert("Home", 13, t_pitchH_12);
  if (form.pitchH_13.selectedIndex > 0 && form.countH_13.value == "")
    alertMessage += pitchCountAlert("Home", 14, t_pitchH_13);
  if (form.pitchV_1.selectedIndex > 0 && form.countV_1.value == "")
    alertMessage += pitchCountAlert("Visit", 2, t_pitchV_1);
  if (form.pitchV_2.selectedIndex > 0 && form.countV_2.value == "")
    alertMessage += pitchCountAlert("Visit", 3, t_pitchV_2);
  if (form.pitchV_3.selectedIndex > 0 && form.countV_3.value == "")
    alertMessage += pitchCountAlert("Visit", 4, t_pitchV_3);
  if (form.pitchV_4.selectedIndex > 0 && form.countV_4.value == "")
    alertMessage += pitchCountAlert("Visit", 5, t_pitchV_4);
  if (form.pitchV_5.selectedIndex > 0 && form.countV_5.value == "")
    alertMessage += pitchCountAlert("Visit", 6, t_pitchV_5);
  if (form.pitchV_6.selectedIndex > 0 && form.countV_6.value == "")
    alertMessage += pitchCountAlert("Visit", 7, t_pitchV_6);
  if (form.pitchV_7.selectedIndex > 0 && form.countV_7.value == "")
    alertMessage += pitchCountAlert("Visit", 8, t_pitchV_7);
  if (form.pitchV_8.selectedIndex > 0 && form.countV_8.value == "")
    alertMessage += pitchCountAlert("Visit", 9, t_pitchV_8);
  if (form.pitchV_9.selectedIndex > 0 && form.countV_9.value == "")
    alertMessage += pitchCountAlert("Visit", 10, t_pitchV_9);
  if (form.pitchV_10.selectedIndex > 0 && form.countV_10.value == "")
    alertMessage += pitchCountAlert("Visit", 11, t_pitchV_10);
  if (form.pitchV_11.selectedIndex > 0 && form.countV_11.value == "")
    alertMessage += pitchCountAlert("Visit", 12, t_pitchV_11);
  if (form.pitchV_12.selectedIndex > 0 && form.countV_12.value == "")
    alertMessage += pitchCountAlert("Visit", 13, t_pitchV_12);
  if (form.pitchV_13.selectedIndex > 0 && form.countV_13.value == "")
    alertMessage += pitchCountAlert("Visit", 14, t_pitchV_13);
  if (alertMessage != "") {
    alert(alertMessage);
    form.Submit.disabled = false;
    valid = false;
  }
  if (valid) { 
  	 var hScore = form.home_score.value;
  	 var vScore = form.visit_score.value;
  	 if (hScore == 100 && vScore == 100) {
  	   if (!valid) form.Submit.disabled = false;
     } else {
       if (valid && hScore == vScore)
         alert("Tie scores are accepted and indicated\nas a tie in the standings\n");
     }
  }
  return valid;
}

function validateRoster(form) {
  var valid = tfv.exec();
  if (!valid) return false;
  var alertMessage = "";
//  if (form.manager_phone.length > 0 && form.manager.phone.length < 10)
//    alertMessage += pitchCountAlert("Home", 2, t_pitchH_1);

//  if (alertMessage != "") {
//    alert(alertMessage);
//    form.Submit.disabled = false;
//    valid = false;
//  }
  return valid;
}

function resetFields(form, aReset) {
  form.date_ix.value = "";
  form.game_id.value = "";
  form.home_id.value = "";
  form.away_id.value = "";
  if (aReset) {
    form.schedmonth.value = 0;
    form.schedmonth.options.length = 0;
    form.schedmonth.options[0] = new Option("---","");
    var curMth = "";
    var ix = 1;
    for (i = 0; i < games.length; i++) {
        // Get the months to add to the month select box from the
        // Games array dates. Games dates are the first item
        // in each game string.
      var fld = games[i].split(";");
      var dt = fld[0].split("-");
      if (dt[0] != curMth) {
        form.schedmonth.options[ix] = new Option(month_name[dt[0]-1],dt[0]);
        ix++;
        curMth = dt[0];
      }
    }
    
    form.schedday.value = 0;
    form.schedday.options[0] = new Option("--","");
    form.home_team.options.length = 0;
    form.home_team.options[0] = new Option("-- Select Home Team --","");
  } else {
    form.home_team.selectedIndex = 0;
  }
  form.home_score.value = "";
  form.visit_team.value = "";
  form.visit_score.value = "";
  form.pitchH_0.options.length = 0;
  form.pitchH_0.options[0] = new Option("-- Select Pitcher --","");
  form.countH_0.value = "";
  form.pitchH_1.options.length = 0;
  form.pitchH_1.options[0] = new Option("-- Select Pitcher --","");
  form.countH_1.value = "";
  form.pitchH_2.options.length = 0;
  form.pitchH_2.options[0] = new Option("-- Select Pitcher --","");
  form.countH_2.value = "";
  form.pitchH_3.options.length = 0;
  form.pitchH_3.options[0] = new Option("-- Select Pitcher --","");
  form.countH_3.value = "";
  form.pitchH_4.options.length = 0;
  form.pitchH_4.options[0] = new Option("-- Select Pitcher --","");
  form.countH_4.value = "";
  form.pitchH_5.options.length = 0;
  form.pitchH_5.options[0] = new Option("-- Select Pitcher --","");
  form.countH_5.value = "";
  form.pitchH_6.options.length = 0;
  form.pitchH_6.options[0] = new Option("-- Select Pitcher --","");
  form.countH_6.value = "";
  form.pitchH_7.options.length = 0;
  form.pitchH_7.options[0] = new Option("-- Select Pitcher --","");
  form.countH_7.value = "";
  form.pitchH_8.options.length = 0;
  form.pitchH_8.options[0] = new Option("-- Select Pitcher --","");
  form.countH_8.value = "";
  form.pitchH_9.options.length = 0;
  form.pitchH_9.options[0] = new Option("-- Select Pitcher --","");
  form.countH_9.value = "";
  form.pitchH_10.options.length = 0;
  form.pitchH_10.options[0] = new Option("-- Select Pitcher --","");
  form.countH_10.value = "";
  form.pitchH_11.options.length = 0;
  form.pitchH_11.options[0] = new Option("-- Select Pitcher --","");
  form.countH_11.value = "";
  form.pitchH_12.options.length = 0;
  form.pitchH_12.options[0] = new Option("-- Select Pitcher --","");
  form.countH_12.value = "";
  form.pitchH_13.options.length = 0;
  form.pitchH_13.options[0] = new Option("-- Select Pitcher --","");
  form.countH_13.value = "";
  form.pitchV_0.options.length = 0;
  form.pitchV_0.options[0] = new Option("-- Select Pitcher --","");
  form.countV_0.value = "";
  form.pitchV_1.options.length = 0;
  form.pitchV_1.options[0] = new Option("-- Select Pitcher --","");
  form.countV_1.value = "";
  form.pitchV_2.options.length = 0;
  form.pitchV_2.options[0] = new Option("-- Select Pitcher --","");
  form.countV_2.value = "";
  form.pitchV_3.options.length = 0;
  form.pitchV_3.options[0] = new Option("-- Select Pitcher --","");
  form.countV_3.value = "";
  form.pitchV_4.options.length = 0;
  form.pitchV_4.options[0] = new Option("-- Select Pitcher --","");
  form.countV_4.value = "";
  form.pitchV_5.options.length = 0;
  form.pitchV_5.options[0] = new Option("-- Select Pitcher --","");
  form.countV_5.value = "";
  form.pitchV_6.options.length = 0;
  form.pitchV_6.options[0] = new Option("-- Select Pitcher --","");
  form.countV_6.value = "";
  form.pitchV_7.options.length = 0;
  form.pitchV_7.options[0] = new Option("-- Select Pitcher --","");
  form.countV_7.value = "";
  form.pitchV_8.options.length = 0;
  form.pitchV_8.options[0] = new Option("-- Select Pitcher --","");
  form.countV_8.value = "";
  form.pitchV_9.options.length = 0;
  form.pitchV_9.options[0] = new Option("-- Select Pitcher --","");
  form.countV_9.value = "";
  form.pitchV_10.options.length = 0;
  form.pitchV_10.options[0] = new Option("-- Select Pitcher --","");
  form.countV_10.value = "";
  form.pitchV_11.options.length = 0;
  form.pitchV_11.options[0] = new Option("-- Select Pitcher --","");
  form.countV_11.value = "";
  form.pitchV_12.options.length = 0;
  form.pitchV_12.options[0] = new Option("-- Select Pitcher --","");
  form.countV_12.value = "";
  form.pitchV_13.options.length = 0;
  form.pitchV_13.options[0] = new Option("-- Select Pitcher --","");
  form.countV_13.value = "";
  form.Submit.disabled = false;
  if (aReset) {
    form.schedmonth.selectedIndex = 0;
    form.schedmonth.focus();
  } else
    form.home_team.focus();
  return false;
}
