var error = "";
var player1_sets_won = 0;
var player2_sets_won = 0;
var winner = "";
 
function validate_form()
//  *********************************************************************************************************
//  INITIALIZE VARIABLES
//  VALIDATE DRAW SELECTED
//  VALIDATE PLAYER NAMES ENTERED
//  CHECK FOR A RETIRE AND A DEFAULT
//  CHECK FOR A DEFAULT
//      VALIDATE SET 1
//      VALIDATE SET 2
//      IS MATCH OVER?
//      ARE FIRST TWO SETS VALID?
//          VALIDATE SET 3
//  DISPLAY ERRORS
//      VERIFY WINNER
//  *********************************************************************************************************
{
error = "";
player1_sets_won = 0;
player2_sets_won = 0;
winner = "";

validate_selects();
if  (error != "")
    {
     alert(error);
     return (false);
    };

validate_players();

if ( (document.reportform.defaulted[0].checked && document.reportform.defaulted[1].checked) || (document.reportform.retired[0].checked && document.reportform.retired[1].checked) )
      error += "Both players cannot Default/Retire.\n";
else
if ( (document.reportform.defaulted[0].checked || document.reportform.defaulted[1].checked) && (document.reportform.retired[0].checked || document.reportform.retired[1].checked) )
      error += "Cannot have a Default and a Retire.\n";
else
if (document.reportform.defaulted[0].checked || document.reportform.defaulted[1].checked)
   {
    if  (document.reportform.Player1Set1.selectedIndex != 0 ||
         document.reportform.Player1Set2.selectedIndex != 0 ||
         document.reportform.Player1Set3.selectedIndex != 0 ||
         document.reportform.Player2Set1.selectedIndex != 0 ||
         document.reportform.Player2Set2.selectedIndex != 0 ||
         document.reportform.Player2Set3.selectedIndex != 0 )
         error += "Set scores should not be entered for a default.\n"
    else
    if  (document.reportform.defaulttext.value == "")
         error += "Please enter a reason for the default.\n"
    else
    if  (document.reportform.defaulted[0].checked)
         player2_sets_won = 1
    else
         player1_sets_won = 1
    }
else
if  (document.reportform.retired[0].checked || document.reportform.retired[1].checked)
    {
     if  (document.reportform.Player1Set3.selectedIndex != 0 || document.reportform.Player2Set3.selectedIndex != 0)
         {
          validate_set_1();
          validate_set_2();
         }
     else
     if  (document.reportform.Player1Set2.selectedIndex != 0 || document.reportform.Player2Set2.selectedIndex != 0)
         {
          if ( (document.reportform.Player1Set2.selectedIndex == 7 && document.reportform.Player2Set2.selectedIndex == 6) ||
               (document.reportform.Player1Set2.selectedIndex == 6 && document.reportform.Player2Set2.selectedIndex < 5) ||
               (document.reportform.Player2Set2.selectedIndex == 7 && document.reportform.Player1Set2.selectedIndex == 6) ||
               (document.reportform.Player2Set2.selectedIndex == 6 && document.reportform.Player1Set2.selectedIndex < 5) )
               {
                validate_set_2();
//                alert("Set 2: p1: " + player1_sets_won + " p2: " + player2_sets_won);
               };
          validate_set_1();
//          alert("Set 1: p1: " + player1_sets_won + " p2: " + player2_sets_won);
         };

     if  (document.reportform.retired[0].checked)
         {
          player2_sets_won++;
//          alert("p2++");
         }
     else
         {
          player1_sets_won++;
//          alert("p1++");
         };
    }
else
    {
    validate_set_1();
    validate_set_2();

    if  ( (player1_sets_won == 2 || player2_sets_won == 2) && (document.reportform.Player1Set3.selectedIndex != 0 || document.reportform.Player2Set3.selectedIndex != 0) )
           error += "Set 3: Please do not enter a 3rd set score.\n";
    else

    if  (player1_sets_won == 1 && player2_sets_won == 1)
         validate_set_3();
    }

if  (error != "")
    {
     alert(error);
     return (false);
    };

if  (document.reportform.retired[0].checked)
    {
     winner = document.reportform.player2.value;
     document.reportform.hWinner.value = 2;
    }
else
if  (document.reportform.retired[1].checked)
    {
     winner = document.reportform.player1.value;
     document.reportform.hWinner.value = 1;
    }
else
if  (player1_sets_won > player2_sets_won)
    {
     winner = document.reportform.player1.value;
     document.reportform.hWinner.value = 1;
    }
else
    {
     winner = document.reportform.player2.value;
     document.reportform.hWinner.value = 2;
    }

if  (!confirm("Please verify that the match was won by:          \n\n                  " + winner))
     return (false)
else
    {
     document.reportform.submit_btn.disabled = true; 
     document.reportform.player1.disabled = false; 
     document.reportform.player2.disabled = false; 
     return (true);
	} 
}

function validate_selects()
//  *********************************************************************************************************
//  DRAW MUST BE SELECTED
//  *********************************************************************************************************
{
if (document.reportform.sDraw.selectedIndex == 0)
     error += "Please select Draw\n";
//else
//if (document.reportform.sDraw.selectedIndex != 1 &&
//    document.reportform.sDraw.selectedIndex != 8)
//    if  (document.reportform.player1.value == "")
//         error += "Please select Round and Player\n";

//if  (document.reportform.sDraw.selectedIndex == 0)
//     error += "Please select Draw\n"
//else
//if  (parent.ifRound.document.roundform.sRound.selectedIndex == 0)
//     error += "Please select Round\n"
//else
//if  (parent.ifPlayers.document.playerform.sPlayer.selectedIndex == 0)
//     error += "Please select Player\n";
}

function validate_players()
//  *********************************************************************************************************
//  PLAYER 1 NAME MUST BE ENTERED
//  PLAYER 2 NAME MUST BE ENTERED
//  *********************************************************************************************************
{
if  (document.reportform.player1.value == "")
     error += "Please enter Player 1 name.\n";

if  (document.reportform.player2.value == "")
     error += "Please enter Player 2 name.\n";
}

function validate_set_1()
//  *********************************************************************************************************
//  SET GAME SCORES CANNOT BE THE SAME
//  ONE PLAYER MUST HAVE 6 OR 7 GAMES
//  IF ONE PLAYER HAS 7 GAMES, THE OTHER MUST HAVE 5 OR 6 GAMES
//  IF ONE PLAYER HAS 6 GAMES, MUST WIN BY 2
//  UPDATE SETS WON
//  *********************************************************************************************************
{
if  (document.reportform.Player1Set1.selectedIndex == document.reportform.Player2Set1.selectedIndex)
     error += "Set 1: Game scores cannot be the same\n";
else
if  (document.reportform.Player1Set1.selectedIndex < 6 && document.reportform.Player2Set1.selectedIndex < 6)
     error += "Set 1: One player must have 6 or 7 games\n";
else
if  ( (document.reportform.Player1Set1.selectedIndex == 7 && (document.reportform.Player2Set1.selectedIndex < 5)) ||
      (document.reportform.Player2Set1.selectedIndex == 7 && (document.reportform.Player1Set1.selectedIndex < 5)) )        
       error += "Set 1: If one player has 7 games, the other must have 5 or 6\n";
else
if  ( (document.reportform.Player1Set1.selectedIndex != 7 && document.reportform.Player2Set1.selectedIndex != 7) && 
      (Math.abs(document.reportform.Player1Set1.selectedIndex - document.reportform.Player2Set1.selectedIndex) < 2) )
       error += "Set 1: Must win by two games\n";
else
if  (document.reportform.Player1Set1.selectedIndex > document.reportform.Player2Set1.selectedIndex)
     player1_sets_won = ++player1_sets_won;
else
     player2_sets_won = ++player2_sets_won;
}

function validate_set_2()
//  *********************************************************************************************************
//  SET GAME SCORES CANNOT BE THE SAME
//  ONE PLAYER MUST HAVE 6 OR 7 GAMES
//  IF ONE PLAYER HAS 7 GAMES, THE OTHER MUST HAVE 5 OR 6 GAMES
//  IF ONE PLAYER HAS 6 GAMES, MUST WIN BY 2
//  UPDATE SETS WON
//  *********************************************************************************************************
{
if  (document.reportform.Player1Set2.selectedIndex == document.reportform.Player2Set2.selectedIndex)
     error += "Set 2: Game scores cannot be the same\n";
else
if  (document.reportform.Player1Set2.selectedIndex < 6 && document.reportform.Player2Set2.selectedIndex < 6)
     error += "Set 2: One player must have 6 or 7 games\n";
else
if  ( (document.reportform.Player1Set2.selectedIndex == 7 && (document.reportform.Player2Set2.selectedIndex < 5)) ||
      (document.reportform.Player2Set2.selectedIndex == 7 && (document.reportform.Player1Set2.selectedIndex < 5)) )        
       error += "Set 2: If one player has 7 games, the other must have 5 or 6\n";
else
if  ( (document.reportform.Player1Set2.selectedIndex != 7 && document.reportform.Player2Set2.selectedIndex != 7) && 
      (Math.abs(document.reportform.Player1Set2.selectedIndex - document.reportform.Player2Set2.selectedIndex) < 2) )
       error += "Set 2: Must win by two games\n";
else
if  (document.reportform.Player1Set2.selectedIndex > document.reportform.Player2Set2.selectedIndex)
     player1_sets_won = ++player1_sets_won;
else
     player2_sets_won = ++player2_sets_won;
}

function validate_set_3()
//  *********************************************************************************************************
//  SET GAME SCORES CANNOT BE THE SAME
//  ONE PLAYER MUST HAVE 6 OR 7 GAMES
//  IF ONE PLAYER HAS 7 GAMES, THE OTHER MUST HAVE 5 OR 6 GAMES
//  IF ONE PLAYER HAS 6 GAMES, MUST WIN BY 2
//  UPDATE SETS WON
//  *********************************************************************************************************
{
if  (document.reportform.Player1Set3.selectedIndex == document.reportform.Player2Set3.selectedIndex)
     error += "Set 3: Game scores cannot be the same\n";
else
if  (document.reportform.Player1Set3.selectedIndex < 6 && document.reportform.Player2Set3.selectedIndex < 6)
     error += "Set 3: One player must have 6 or 7 games\n";
else
if  ( (document.reportform.Player1Set3.selectedIndex == 7 && (document.reportform.Player2Set3.selectedIndex < 5)) ||
      (document.reportform.Player2Set3.selectedIndex == 7 && (document.reportform.Player1Set3.selectedIndex < 5)) )        
       error += "Set 3: If one player has 7 games, the other must have 5 or 6\n";
else
if  ( (document.reportform.Player1Set3.selectedIndex != 7 && document.reportform.Player2Set3.selectedIndex != 7) && 
      (Math.abs(document.reportform.Player1Set3.selectedIndex - document.reportform.Player2Set3.selectedIndex) < 2) )
       error += "Set 3: Must win by two games\n";
else
if  (document.reportform.Player1Set3.selectedIndex > document.reportform.Player2Set3.selectedIndex)
     player1_sets_won = ++player1_sets_won;
else
    player2_sets_won = ++player2_sets_won;
}

function clear_scores()
//  *********************************************************************************************************
//  SET SCORES SHOULD NOT BE ENTERED FOR A DEFAULT
//  *********************************************************************************************************
{
if (document.reportform.defaulted[0].checked == true || document.reportform.defaulted[1].checked == true)
   {
    document.reportform.Player1Set1.selectedIndex=[0];
    document.reportform.Player1Set2.selectedIndex=[0];
    document.reportform.Player1Set3.selectedIndex=[0];
    document.reportform.Player2Set1.selectedIndex=[0];
    document.reportform.Player2Set2.selectedIndex=[0];
    document.reportform.Player2Set3.selectedIndex=[0];
    document.getElementById("DefaultReason").style.visibility = "visible";
    document.reportform.defaulttext.focus();
   }
else
   {
    document.getElementById("DefaultReason").style.visibility = "hidden";
			 document.reportform.defaulttext.value = "";
			}
}
