//**************************************************************************************
//
// Champ Analysis / Champ Calc
//
// Copyright Tony Barson 2004 - 2007
//
// PLEASE do not use or alter this code without the permission of the author. 
// 
// The system can be purchased or optimised for any client. Please contact the author
// for further details.
//
// E-Mail : Tony_Barson@yahoo.co.uk
//
// 17/01/2007 Version 2.3
//**************************************************************************************

var nPassed_Position;
var sGainLose;
var sSavedPosition_Driver;
var anChamp_ExtraPoints = new Array();

var asSaveDriver = new Array();
var abDisplayDriver = new Array();
var asUniqueCombos = new Array();

function Championship_Init(){

var bCycle_Finished ;
var nSaved_Score ;
var nScores = new Array();
var m;
var n;

var nMaxRoundScore;

var anChamp_Position = new Array();
asUniqueCombos = new Array();

if (bChampFirstTimeIn == true) {

   for (m=1; m <= nRounds;m++) {

       anChamp_Score[m] = new Array();
       abChamp_Include_Score[m] = new Array();

       for (n=1; n <= nDriver_Records; n++) {
           anChamp_Score[m][n] = 0;
           abChamp_Include_Score[m][n] = false;
       }
   }

   bChampFirstTimeIn = false;

}

if(nRounds_Left == 0){
   alert("Cannot Perform This Function As Championship Already Won !");
   return;
}

asChamp_Combos = new Array();

//Populate list of Championship Contenders && set-up array

nNo_Contenders = 0;

//CHECK for X in Grid Column. Need to Handle This with an ARRAY abChamp_Contender(X) = true/false

if(sChamp_Type.substring(0,6) == "SPRINT"){
   nMaxRoundScore = Number(sPoints_Array[1].substring(0,3)) + 100;
}

if(sChamp_Type.substring(0,9) == "ENDURANCE"){
   nMaxRoundScore = Number(sPoints_Array[1].substring(0,3));
}

if(sChamp_Type.substring(0,3) == "CAR"){
   nMaxRoundScore = Number(sPoints_Array[1].substring(0,3));
}

for (n = 1;n <= nDriver_Records;n++) {

    if(anChamp_Contender[n] == "Y"){
	nNo_Contenders = nNo_Contenders + 1;
    }
}


if(nNo_Contenders == 1){
   alert("Cannot Perform This Function As Championship Already Won !");
   return;
}

nNo_Contenders = 0;


for (n = 1;n <= nDriver_Records;n++) {

    if(anChamp_Contender[n] == "Y" && (anTopNTotal[1] - anTopNTotal[n]) <= Number(nMaxRoundScore)){

          nNo_Contenders = nNo_Contenders + 1;

          if(nNo_Contenders > 5){
             nNo_Contenders = 5;
             n = nDriver_Records +1;
          } else {
             asChamp_Driver[nNo_Contenders] = asDriver[n];
             anChamp_TopNTotal[nNo_Contenders] = anTopNTotal[n];
             anChamp_FullTotal[nNo_Contenders] = anFullTotal[n];
             anChamp_ExtraPoints[nNo_Contenders] = anExtraPoints[n]; 

             for (m = 1;m <= nRounds;m++) {
                 anChamp_Score[m][nNo_Contenders] = anScore[m][n];
             } 

             abChamp_Contender[nNo_Contenders] = true;
             anChamp_Diff[nNo_Contenders] = 0;
             anChamp_Max[nNo_Contenders] = 0;
             anChamp_Saved_Score[nNo_Contenders] = 0;
             anChamp_MinPoints[nNo_Contenders] = 0;
             anChamp_Position[nNo_Contenders] = n;

             asSaveDriver[nNo_Contenders] = asDriver[n];
         }

     }
} 

nSavedContenders = nNo_Contenders;

if(nNo_Contenders == 0){
   alert("Championship can not be decided by anyone at the next round !");
   return;
}

//if(nNo_Contenders > 5){
//   alert("Can only perform this function for the first 5 Championship Contenders !");
//}

//if(nNo_Contenders > 5){
//   alert("Cannot Perform This Function As There Are Too Many Championship Contenders At Present !");
//   return;
//}

//Check to see if(Simulation turned on && if(so, what the latest Simulated round is

bSimRound = false;
nCurrentSimRound = 0;
nChampRound = 0;
sChampRound = "";

if(bSimulationOn == true){
   for (Y = 1;Y <= nRounds;Y++) {
       if(Y > nCurrent_Round){
          for (X = 1;X <= nDriver_Records;X++) {
              if(anSimulatedScore[Y][X] != 0){
                 bSimRound = true;
                 nCurrentSimRound = Y;
                 X = nDriver_Records+1;
              }
          }
       }
   }
}

if(bSimulationOn == true && nCurrentSimRound > 0){
   sChampRound = String(nCurrentSimRound+1) + " (Simulated) ";
   nChampRound = String(nCurrentSimRound+1);
}else{
   sChampRound = String(nCurrent_Round+1);
   nChampRound = String(nCurrent_Round+1);
}

//Work out drivers' minimum scoring scores && populate Grid at top of screen


nSwitch = 0;

sHTML = "";
sHTML += "<TABLE class='HeadTable1' BORDER=1 ALIGN=CENTER WIDTH=600>";

if(sChamp_Type.substring(0,6) == "SPRINT"){
   sHTML += "<TR><TD class='HeadBox1' BGCOLOR='#A8D3FF' ALIGN=CENTER>Championship Winning Analysis for Round " + sChampRound + " - " + asTrackName[nChampRound] + "</TD></TR>";
}else if(sChamp_Type.substring(0,9) == "ENDURANCE"){
   sHTML += "<TR><TD class='HeadBox1' BGCOLOR='#A8D3FF' ALIGN=CENTER>Championship Winning Analysis for Round " + sChampRound + " - " + asTrackName[nChampRound] + "</TD></TR>";
}else if(sChamp_Type.substring(0,3) == "CAR"){
   sHTML += "<TR><TD class='HeadBox1' BGCOLOR='#A8D3FF' ALIGN=CENTER>Championship Winning Analysis for Round " + sChampRound + " - " + asTrackName[nChampRound] + "</TD></TR>";
}

sHTML += "</TABLE><BR>";

sHTML += "<TABLE BORDER=0 ALIGN=CENTER WIDTH=200>";
sHTML += "<TR><TD ALIGN=CENTER><A HREF='" + sInstructionsLinkPrefix + "#ChampionshipAnalysis' TARGET='_blank'><img src='../images/information.png'></A></TD></TR>";
sHTML += "</TABLE><BR>";


sHTML += "<TABLE class='HeadTable2' BORDER=1 ALIGN=CENTER WIDTH=400>";
sHTML += "<TR><TD class='HeadBox2' BGCOLOR='#A8D3FF' ALIGN=CENTER>Select Championship Contender for Analysis</TD></TR>";
sHTML += "</TABLE><BR>";

sHTML += "<TABLE class='DetTable' BORDER=1 ALIGN=CENTER WIDTH=700>";
sHTML += "<TR>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=50 ALIGN=LEFT>Position</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=263 ALIGN=LEFT>Entrant</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=153 ALIGN=CENTER>Minimum Scoring Points</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=153 ALIGN=CENTER>Minimum Scoring Position</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=80 ALIGN=CENTER>Current Difference</TD>";
sHTML += "</TR>";

for (n = 1;n <= nNo_Contenders;n++) {
   for (m = 1;m <= nRounds;m++) {
       nScores[m] = anChamp_Score[m][n];
   }

   bCycle_Finished = false;
   //Sort scores into order low - High;
   while (bCycle_Finished == false) {
      bCycle_Finished = true;
      for (m = 1;m <= nRounds - 1;m++) {
          if(nScores[m] < nScores[m + 1]){
             nSaved_Score = nScores[m];
             nScores[m] = nScores[m + 1];
             nScores[m + 1] = nSaved_Score;
             bCycle_Finished = false;
          }
      }
   }

   sMinScoreString = Minimum_Scoring_Score(sChamp_Type, nScores[nScores_To_Include]);

   if(nSwitch == 0){
      sColour = sColour1;
      nSwitch = 1;
   }else{
      sColour = sColour2;
      nSwitch = 0;
   }

   sHTML += "<TR>";
   sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=50 ALIGN=LEFT>" + anChamp_Position[n] + "</TD>";
   sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=263 ALIGN=LEFT><A onclick='Champ_Calc(" + n + "," + "\"" + asChamp_Driver[n] + "\"" + ")'><U>" + asChamp_Driver[n] + "</U></A></TD>";

   if ((anChamp_TopNTotal[1] - anChamp_TopNTotal[n]) > Number(sMinScoreString.substring(0,3))) {
      anChamp_MinPoints[n] = (anChamp_TopNTotal[1] - anChamp_TopNTotal[n]);
      sTemp_Score = "000" + String(anChamp_MinPoints[n]);
      anChamp_MinPoints[n] = sTemp_Score.substring(sTemp_Score.length-3,sTemp_Score.length);
      sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=153 ALIGN=CENTER>" + anChamp_MinPoints[n] + "</TD>";
      sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=153 ALIGN=CENTER>" + Get_Position(sChamp_Type, anChamp_MinPoints[n]) + "</TD>";
      sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=80 ALIGN=CENTER>" + ((n==1) ? 0:(anChamp_TopNTotal[1] - anChamp_TopNTotal[n])) + "</TD>";
   } else {
      anChamp_MinPoints[n] = Number(sMinScoreString.substring(0,3));
      sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=153 ALIGN=CENTER>" + sMinScoreString.substring(0,3) + "</TD>";
      sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=153 ALIGN=CENTER>" + sMinScoreString.substring(3,sMinScoreString.length) + "</TD>";
      sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=80 ALIGN=CENTER>" + ((n==1) ? 0:(anChamp_TopNTotal[1] - anChamp_TopNTotal[n])) + "</TD>";
   }
   sHTML += "</TR>";

} 

sHTML += "</TABLE>";

var bTooEarly = true;

//for (n = 1; n <= nNo_Contenders;n++) {
//    if (anChamp_MinPoints[n] != 1) {
//        bTooEarly = false;
//        n = nNo_Contenders + 1;
//    }
//}
//
//if (bTooEarly == true && nCurrent_Round < nScores_To_Include) {


//if (nCurrent_Round < nScores_To_Include && (nScores_To_Include < nRounds)) {
//   alert("Cannot perform this function as it is too early in the Championship !");
//   return;
//}

//document.getElementById("DivText_Top_Banner").innerHTML = "<P></P>";
//document.getElementById("DivText_Bottom_Banner").innerHTML = "<P></P>";
document.getElementById("DivText2").innerHTML = "<P></P>";
document.getElementById("DivText3").innerHTML = "<P></P>";
document.getElementById("DivText4").innerHTML = "<P></P>";

//document.getElementById("DivText_Top_Banner").innerHTML = "";
//document.getElementById("DivText_Bottom_Banner").innerHTML = "";
document.getElementById("DivText2").innerHTML = sHTML;

}

//*************************************************************************

function Minimum_Scoring_Score(sChamp_Type, nScore){
var n;
var nTemp_Score;
var STemp_Score;
var sTemp_Pos;
var sMinimum_Scoring_Score = ""
var bMatchFound = false;

if(sChamp_Type.substring(0,6) == "SPRINT"){
   if(nScore >= 100){
      nTemp_Score = nScore - 100;
   }else if(nScore >= 70){
      nTemp_Score = nScore - 70;
   }else if(nScore >= 40){
      nTemp_Score = nScore - 40;
   }else{
      nTemp_Score = nScore;
   }
}else{
   nTemp_Score = nScore;
}

if(sChamp_Type.substring(0,6) == "SPRINT"){
   for (n = 1;n <= sPoints_Array.length-1;n++) {

       if(nTemp_Score >= Number(sPoints_Array[n].substring(0,3))){
          bMatchFound = true;
          if(n > 1){
             sTemp_Pos = sPoints_Array[n - 1].substring(3,8);
             nTemp_Score = Number(sPoints_Array[n - 1].substring(0,3));
          }else{
             sMinimum_Scoring_Score = "000";
          }
          break; 
       }
   }
}

if(sChamp_Type.substring(0,9) == "ENDURANCE"){
   for (n = 1;n <= sPoints_Array.length-1;n++) {
       if(nTemp_Score >= Number(sPoints_Array[n].substring(0,3))){
          bMatchFound = true;
          if(n > 1){
             sTemp_Pos = sPoints_Array[n - 1].substring(3,8);
             nTemp_Score = Number(sPoints_Array[n - 1].substring(0,3));
          }else{
             sMinimum_Scoring_Score = "000";
          }
          break;
       }
   }
}

if(sChamp_Type.substring(0,3) == "CAR"){
   for (n = 1;n <= sPoints_Array.length-1;n++) {
       if(nTemp_Score >= Number(sPoints_Array[n].substring(0,3))){
          bMatchFound = true;
          if(n > 1){
             sTemp_Pos = sPoints_Array[n - 1].substring(3,8);
             nTemp_Score = Number(sPoints_Array[n - 1].substring(0,3));
          }else{
             sMinimum_Scoring_Score = "000Last";
          }
          break;
       }
   }
}

if(bMatchFound == false) {
   if(Number(sPoints_Array[sPoints_Array.length-1].substring(0,3)) == 0) {
      sTemp_Pos = sPoints_Array[sPoints_Array.length-2].substring(3,8);
      nTemp_Score = Number(sPoints_Array[sPoints_Array.length-2].substring(0,3));
   } else {
      sTemp_Pos = sPoints_Array[sPoints_Array.length-1].substring(3,8);
      nTemp_Score = Number(sPoints_Array[sPoints_Array.length-1].substring(0,3));
   }
}

if(sChamp_Type.substring(0,6) == "SPRINT"){

   if(nScore >= 100){
      sTemp_Score = "000" + String(nTemp_Score + 100);
      sTemp_Score = sTemp_Score.substring(sTemp_Score.length-3,sTemp_Score.length);
      sMinimum_Scoring_Score = sTemp_Score + "A-Final " + sTemp_Pos;
   }else if(nScore >= 70){
      sTemp_Score = "000" + String(nTemp_Score + 70);
      sTemp_Score = sTemp_Score.substring(sTemp_Score.length-3,sTemp_Score.length);
      sMinimum_Scoring_Score = sTemp_Score + "B-Final " + sTemp_Pos;
   }else{
      if(nScore >= 0){
         sTemp_Score = "000" + String(nTemp_Score + 40);
         sTemp_Score = sTemp_Score.substring(sTemp_Score.length-3,sTemp_Score.length);
         sMinimum_Scoring_Score = sTemp_Score + "C-Final " + sTemp_Pos;
      }else{
         sMinimum_Scoring_Score = "000Last";
      }
   }
}else{
   if(nTemp_Score != 0){
      sTemp_Score = "000" + String(nTemp_Score);
      sTemp_Score = sTemp_Score.substring(sTemp_Score.length-3,sTemp_Score.length);
      sMinimum_Scoring_Score = sTemp_Score + sTemp_Pos;
   }else{
      sMinimum_Scoring_Score = "000Last";
   }
}

return sMinimum_Scoring_Score;

}

//********************************************************************

function Champ_Calc(nCurrDriver, sDriver_Name){

var nStartDriver;
var nEndDriver;
var nDriver;
var nCurrDriverPos;
var nLoop;
var nLoop_Driver;
var bChampion_Found;
var bCanWin;
var nPoints;
var bFirstTime;
var sCombination;
var asCombos = new Array();
var nCombos;
var bCombo_Exists;
var sCombo_Driver;
var nCombo_Driver;
var sTemp_String = "";
var nSaved_EndDriver = 0;

bFirstTime = true;

for (n = 1;n <= nNo_Contenders;n++) {
    nPoints = anChamp_MinPoints[n];
    nEndDriver = Calculate_End_Driver(sChamp_Type, nPoints);
    if (nEndDriver >= nSaved_EndDriver) {
       nSaved_EndDriver = nEndDriver;
    }
}

nEndDriver = nSaved_EndDriver;

if (nEndDriver < nNo_Contenders) {
   nEndDriver = nNo_Contenders;
}

nChamp_Combos = 0;

for (n = 1;n <= asChamp_Combos.length-1;n++) {
    asChamp_Combos[n] = "";
}

for (n = 1;n <= nNo_Contenders;n++) {
    nPoints = anChamp_MinPoints[n];
    anMin_Scoring_Pos[n] = Calculate_End_Driver(sChamp_Type, nPoints);
} 

bChampion_Found = false;
bCanWin = false;

nRounds_LeftX = nRounds - nChampRound;

nCombos = 0;

//********* Calculate forward 1 - n

alert("About To Calculate Results Combinations - This may take a while");

nStartDriver = 1;

if(nCurrDriver == nStartDriver){
   nStartDriver = nStartDriver + 1;
}

while (nStartDriver <= nEndDriver) {

   nCurrDriverPos = nEndDriver;

   while (nCurrDriverPos >= 1) {

      nLoop = 1;
      nLoop_Driver = nStartDriver;

      sCombination = "";

      //document.getElementById("DivText3").innerHTML = "<P>Building Results Combinations (forward) : Please Wait</P>";

      while (nLoop <= nEndDriver) {

         if(nLoop == nCurrDriverPos) {

            sTemp_String = "00" + String(nCurrDriver);
            sCombination = sCombination + sTemp_String.substring(sTemp_String.length-2, sTemp_String.length);

            if(nLoop_Driver == nCurrDriver){
               nLoop_Driver = nLoop_Driver + 1;
               if(nLoop_Driver == nCurrDriver){
                  nLoop_Driver = nLoop_Driver + 1;
               }
               if(nLoop_Driver > nEndDriver){
                  nLoop_Driver = 1;
               }
            }
            nLoop = nLoop + 1;
         }else{
            if(nLoop_Driver != nCurrDriver){
               if(nLoop_Driver <= nNo_Contenders){
                  sTemp_String = "00" + String(nLoop_Driver);
                  sCombination = sCombination + sTemp_String.substring(sTemp_String.length-2, sTemp_String.length);
               }else{
                  sCombination = sCombination + "  ";
               }
               nLoop_Driver = nLoop_Driver + 1;
               if(nLoop_Driver == nCurrDriver){
                  nLoop_Driver = nLoop_Driver + 1;
               }
               if(nLoop_Driver > nEndDriver){
                  nLoop_Driver = 1;
               }
               nLoop = nLoop + 1;
            }else{
               nLoop_Driver = nLoop_Driver + 1;
               if(nLoop_Driver == nCurrDriver){
                  nLoop_Driver = nLoop_Driver + 1;
               }
               if(nLoop_Driver > nEndDriver){
                  nLoop_Driver = 1;
               }
            }
         }
      }

      //*************************************************************************************************;
      //Check that this combination is NEW ?? if(it is, add it into Combo Array;
      //Ready for processing later.;

      bCombo_Exists = false;
      for (n = 1;n <= asCombos.length-1;n++) {
          if(sCombination == asCombos[n]){
             bCombo_Exists = true;
             n = asCombos.length+1;
          }
      }

      if(bCombo_Exists == false){
         nCombos = nCombos + 1;
         asCombos[nCombos] = sCombination;
      }
      nCurrDriverPos = nCurrDriverPos - 1;
   }

   nStartDriver = nStartDriver + 1;
   if(nStartDriver == nCurrDriver){
      nStartDriver = nStartDriver + 1;
   }
}

//********* END Calculate forward 1 - n

//********* Calculate Backward n - 1

nStartDriver = nEndDriver;

if(nCurrDriver == nStartDriver){
   nStartDriver = nStartDriver - 1;
}

alert("Stage 2 - Building Results Combinations - Please Wait");

while (nStartDriver >= 1) {

   nCurrDriverPos = nEndDriver;

   while (nCurrDriverPos >= 1) {

      nLoop = nEndDriver;
      nLoop_Driver = nStartDriver;
      sCombination = "";

      //document.getElementById("DivText3").innerHTML = "<P>Building Results Combinations (Backward) : Please Wait</P>";

      while (nLoop >= 1) {

         if(nLoop == nCurrDriverPos){
            sTemp_String = "00" + String(nCurrDriver);
            sCombination = sCombination + sTemp_String.substring(sTemp_String.length-2, sTemp_String.length);
            if(nLoop_Driver == nCurrDriver){
               nLoop_Driver = nLoop_Driver - 1;
               if(nLoop_Driver == nCurrDriver){
                  nLoop_Driver = nLoop_Driver - 1;
               }
               if(nLoop_Driver < 1){
                  nLoop_Driver = nEndDriver;
               }
            }
            nLoop = nLoop - 1;
         }else{
            if(nLoop_Driver != nCurrDriver){
               if(nLoop_Driver <= nNo_Contenders){
                  sTemp_String = "00" + String(nLoop_Driver);
                  sCombination = sCombination + sTemp_String.substring(sTemp_String.length-2, sTemp_String.length);
               }else{
                  sCombination = sCombination + "  ";
               }
               nLoop_Driver = nLoop_Driver - 1;
               if(nLoop_Driver == nCurrDriver){
                  nLoop_Driver = nLoop_Driver - 1;
               }
               if(nLoop_Driver < 1){
                  nLoop_Driver = nEndDriver;
               }
               nLoop = nLoop - 1;
            }else{
               nLoop_Driver = nLoop_Driver - 1;
               if(nLoop_Driver == nCurrDriver){
                  nLoop_Driver = nLoop_Driver - 1;
               }
               if(nLoop_Driver < 1){
                  nLoop_Driver = nEndDriver;
               }
            }
         }
      }
      //*************************************************************************************************;
      //Check that this combination is NEW ?? if(it is, add it into Combo Array;
      //Ready for processing later.;

      bCombo_Exists = false;
      for (n = 1;n <= asCombos.length-1;n++) {
          if(sCombination == asCombos[n]){
             bCombo_Exists = true;
             n = asCombos.length+1;
          }
      }

      if(bCombo_Exists == false){
         nCombos = nCombos + 1;
         asCombos[nCombos] = sCombination;
      }
      nCurrDriverPos = nCurrDriverPos - 1;
   }
   nStartDriver = nStartDriver - 1;
   if(nStartDriver == nCurrDriver){
      nStartDriver = nStartDriver - 1;
   }
}

//********* END Calculate Backward n - 1

//****** Process Combinations

//*****************************

sHTML = "";
sHTML2 = "";
sHTML3 = "";

sHTML2 =  "<BR><BR>";
sHTML2 += "<TABLE class='HeadTable3' BORDER=1 ALIGN=CENTER WIDTH=600>";
sHTML2 += "<TR><TD class='HeadBox3' BGCOLOR='#A8D3FF' ALIGN=CENTER>Championship Winning Results Combinations - " + sDriver_Name + "</TD></TR>";
sHTML2 += "</TABLE><BR>";
sHTML2 += "<TABLE class='DetTable' BORDER=1 ALIGN=CENTER WIDTH=700>";

//******************************

var nSub = 0;
var nCounter = 0;
var bInput_Box = true;

var sTempHTML = "";

abDisplayDriver = new Array();

for (n=0;n <= nNo_Contenders;n++) {
   abDisplayDriver[n] = false;
}
  

for (nSub = 1;nSub <= asCombos.length-1;nSub++) {

    nCounter++;

    if (nCounter >= 50 || nCounter == 0) {
       bInput_Box = confirm("Stage 3 - Processing Results Combinations : " + nSub + " of: " + (asCombos.length-1) + "\n\n" + "Matching Combinations Found : " + nChamp_Combos + "\n\n" + "Please Wait or click Cancel");

       if (bInput_Box != true) {
          break;
       }
       nCounter = 1;
    }

    //document.getElementById("DivText3").innerHTML = "<P>Building Results Table - Processing Results Combination : " + nSub + " of: " + (asCombos.length-1) + "</P>"

    for (nCombo_Driver = 1;nCombo_Driver <= nEndDriver;nCombo_Driver++) {

        sCombo_Driver = Number(asCombos[nSub].substring(((nCombo_Driver-1)*2),((nCombo_Driver-1)*2)+2));

        if(sCombo_Driver == nCurrDriver){

           anChamp_Score[nChampRound][nCurrDriver] = Get_Position_Points(sChamp_Type, nCombo_Driver);
        }

        if(sCombo_Driver != nCurrDriver){
           if(sCombo_Driver <= nNo_Contenders && sCombo_Driver != 0){

              anChamp_Score[nChampRound][sCombo_Driver] = Get_Position_Points(sChamp_Type, nCombo_Driver);
           }
        }
    } 

       bChampion_Found = Champ_ReCalculate_Table(nCurrDriver,sDriver_Name);

       if(bChampion_Found == true){
          bCanWin = true;
          nChamp_Combos = nChamp_Combos + 1;
          asChamp_Combos[nChamp_Combos] = asCombos[nSub];
       }

//******************************************************

      if(bChampion_Found == true){
          bCanWin = true;

          sTempHTML = "";
          sTempHTML += "<TR>";
          sTempHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=80  ALIGN=LEFT><B>Position</B></TD>";
          sTempHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=250 ALIGN=LEFT><B>Entrant</B></TD>";
          sTempHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=150 ALIGN=LEFT><B>Round Result</B></TD>";
          sTempHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=70  ALIGN=RIGHT><B>Top " + String(nScores_To_Include) + "</B></TD>";
          sTempHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=100 ALIGN=RIGHT><B>Difference</B></TD>";
          sTempHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=100 ALIGN=RIGHT><B>Maximum</B></TD>";
          sTempHTML += "</TR>";

              nSwitch = 0;

             //Re-populate grid;
             for (z = 1;z <= nNo_Contenders;z++) {
                 bFirstTime = false;

                 if(nSwitch == 0){
                    sColour = sColour1;
                    nSwitch = 1;
                 }else{
                    sColour = sColour2;
                    nSwitch = 0;
                 }

                 sTempHTML += "<TR>";
                 sTempHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=80  ALIGN=LEFT>" + z + "</TD>";
                 sTempHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=250 ALIGN=LEFT>" + asChamp_Driver[z] + "</TD>";

                 sTempHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=150 ALIGN=LEFT>" + Get_Position(sChamp_Type, anChamp_Score[nChampRound][z]) + " (" + anChamp_Score[nChampRound][z] + ")</TD>";
                 sTempHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=70  ALIGN=RIGHT>" + anChamp_TopNTotal[z] + "</TD>";
                 sTempHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=100 ALIGN=RIGHT>" + anChamp_Diff[z] + "</TD>";
                 sTempHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=100 ALIGN=RIGHT>" + anChamp_Max[z] + "</TD>";
                 sTempHTML += "</TR>";

                 for (n = 1;n <= nNo_Contenders;n++) {
                     if (asChamp_Driver[z] == asSaveDriver[n]) {
                        abDisplayDriver[n] = true;
                     }
                 }

             }

             var sTempCombo = "";
             var nSub2; 

             for (x = 0;x <= asChamp_Combos[nChamp_Combos].length-2;x = x+2) {
   
                 nSub2 = Number(asChamp_Combos[nChamp_Combos].substring(x,x+2))

                 if(abDisplayDriver[nSub2] == false){
                    sTempCombo = sTempCombo + "  ";
                 } else {
                    sTempCombo = sTempCombo + asChamp_Combos[nChamp_Combos].substring(x,x+2);
                 }
             }

             var bFound = false

             for (x = 1;x <= asUniqueCombos.length-1;x++) {
                  if (sTempCombo == asUniqueCombos[x]) {
                      bFound = true;
                      break;
                  }
             }
 
             if (bFound == false) {
                 nSub2 = asUniqueCombos.length;
                 asUniqueCombos[nSub2] = sTempCombo;
                 sHTML2 += sTempHTML;
             }

       }
//******************************************************

     Reset_Arrays();

} 

sHTML2 += "</TABLE><BR>";

if(bCanWin == true){
   Worded_Summary(nCurrDriver, sDriver_Name);
}

//MATRIX To Produce
//1111X 2222X 3333X 4444X
//222X1 333X2 444X3 111X4
//33X22 44X33 11X44 22X11
//4X333 1X444 2X111 3X222
//X4444 X1111 X2222 X3333

//4444X 3333X 2222X 1111X
//333X4 222X3 111X2 444X4
//22X33 11X22 44X11 33X43
//1X222 4X111 3X444 2X332
//X1111 X4444 X3333 X2221
//
//
//1  2   3   4){    4   3   2   1
//2  3   4   1           3   2   1   4
//3  4   1   2           2   1   4   3
//4  1   2   3           1   4   3   2
//

document.getElementById("DivText3").innerHTML = "";
document.getElementById("DivText4").innerHTML = sHTML;

sTemp_String = "";

if(bInput_Box == true) {
   sTemp_String = "Championship Calculations (Complete)";
} else {
  sTemp_String = "Championship Calculations (Aborted)";
}

if(bCanWin == true){
   alert(sTemp_String + "\n\n" + sDriver_Name + " Can Win Championship At Round " + sChampRound + " - See Table for Details");
}else{
   document.getElementById("DivText4").innerHTML = "";
   alert(sTemp_String + "\n\n" + sDriver_Name + " Cannot Win Championship At Round " + sChampRound);
}


}

//*************************************************************************

function Calculate_End_Driver(sChamp_Type, nScore){

var n;
var nTemp_Score ;
var sTemp_Pos ;

bDefaultedEndDriver = false;

if(sChamp_Type.substring(0,6) == "SPRINT"){
   if(nScore < 100){
      bDefaultedEndDriver = true;
      return sPoints_Array.length-1;
   }
}

if(sChamp_Type.substring(0,6) == "SPRINT"){
   if(nScore >= 100){
      nTemp_Score = nScore - 100;
   }else if(nScore >= 70){
      nTemp_Score = nScore - 70;
   }else{
      nTemp_Score = nScore - 40;
   }

   nTemp_Score = nTemp_Score;
}else{
   nTemp_Score = nScore;
}

if(sChamp_Type.substring(0,6) == "SPRINT"){
   for (n = 1;n <= sPoints_Array.length-1;n++) {
       if(nTemp_Score >= Number(sPoints_Array[n].substring(0,3))){
          if(n < sPoints_Array.length-1){
             return n + 1;
          }else{
             return sPoints_Array-1;
          }
       }
   }
}

if(sChamp_Type.substring(0,9) == "ENDURANCE"){
   for (n = 1;n <= sPoints_Array.length-1;n++) {
       if(nTemp_Score >= Number(sPoints_Array[n].substring(0,3))){
          if(n < sPoints_Array.length-1){
             return n + 1;
          }else{
             return sPoints_Array.length-1;
          }
       }
   }
}

if(sChamp_Type.substring(0,3) == "CAR"){
   for (n = 1;n <= sPoints_Array.length-1;n++) {
       if(nTemp_Score >= Number(sPoints_Array[n].substring(0,3))){
          if(n < sPoints_Array.length-1){
             return n + 1;
          }else{
             return sPoints_Array.length-1;
          }
       }
   }
}

}

//*************************************************************************

function Get_Position_Points(sChamp_Type, nPos){

var n;
var nTemp_Score;
var sTemp_Pos;

if(sChamp_Type.substring(0,6) == "SPRINT"){
   return Number(sPoints_Array[nPos].substring(0,3)) + 100;
}

if(sChamp_Type.substring(0,9) == "ENDURANCE"){
   return Number(sPoints_Array[nPos].substring(0,3));
}

if(sChamp_Type.substring(0,3) == "CAR"){
   return Number(sPoints_Array[nPos].substring(0,3));
}

}

//********************************************************************************************

function Reset_Arrays(){

//Populate list of Championship Contenders && set-up array

nNo_Contenders = 0;

for (n = 1;n <= nDriver_Records;n++) {

    if(anChamp_Contender[n] == "Y"){
       nNo_Contenders = nNo_Contenders + 1;
       if(nNo_Contenders > 5){
          n = nDriver_Records+1;
       }else {

          asChamp_Driver[nNo_Contenders] = asDriver[n];
          anChamp_TopNTotal[nNo_Contenders] = anTopNTotal[n];
          anChamp_FullTotal[nNo_Contenders] = anFullTotal[n];
          anChamp_ExtraPoints[nNo_Contenders] = anExtraPoints[n];
          for (m = 1;m <= nRounds;m++) {
              anChamp_Score[m][nNo_Contenders] = anScore[m][n];
          }
          abChamp_Contender[nNo_Contenders] = true;
          anChamp_Diff[nNo_Contenders] = 0;
          anChamp_Max[nNo_Contenders] = 0;
          anChamp_Saved_Score[nNo_Contenders] = 0;
          if(nNo_Contenders >= 5){
             n = nDriver_Records+1;
          }
          asSaveDriver[nNo_Contenders] = asDriver[n];
       }
    }
} 

}

//*************************************************************************

function Champ_ReCalculate_Table(nDriver_No, sPassed_DriverName){

var X;
var nPoints_diff;
var bSimScoresFound;
var nWinner;

var anTop_Scores = new Array();
var nMax_Score;
var nCount;

var nRounds_LeftX;

nRounds_LeftX = nRounds - (nChampRound);

anTop_Scores[0] = 0;

//****************
//Now weed out best X results && re-calculate total

//Revar anChamp_Saved_Score(nRounds) ;
//Revar abChamp_Saved_Include_Score(nRounds) 

//Recalculate Championship Contenders array


for (X = 1;X <= nNo_Contenders;X++) {
//   Revar nScores(nRounds) ;
   var bCycle_Finished ;
   var nSaved_Score ;
   var ntopx ;

   ntopx = 0;
   anChamp_FullTotal[X] = 0;

   for (n = 1;n <= nRounds;n++) {
       nScores[n] = anChamp_Score[n][X];
       anChamp_FullTotal[X] = anChamp_FullTotal[X] + anChamp_Score[n][X];
       abChamp_Include_Score[n][X] = true;
   } 

   anChamp_FullTotal[X] = anChamp_FullTotal[X] + anChamp_ExtraPoints[X];

   bCycle_Finished = false;
//Sort scores into order

   while (bCycle_Finished == false) {
      bCycle_Finished = true;
      for (n = 1;n <= (nRounds - 1);n++) {
          if(nScores[n] > nScores[n + 1]){
             nSaved_Score = nScores[n];
             nScores[n] = nScores[n + 1];
             nScores[n + 1] = nSaved_Score;
             bCycle_Finished = false;
          }
      }
   }

//Work out Top n Total
   for (n = nRounds;n >= (nRounds - nScores_To_Include) + 1;n--) {
       ntopx = ntopx + nScores[n];
   } 

//now mark in the array whether each score is included in the total || not, false = ! included
   for (n = (nRounds - nScores_To_Include);n >= 1;n--) {
       for (m = 1;m <= nRounds;m++) {
           if(nScores[n] != 0){
              if(anChamp_Score[m][X] == nScores[n]){
                 abChamp_Include_Score[m][X] = false;
                 m = nRounds+1;
              }
           }
       }
   } 

   anChamp_TopNTotal[X] = ntopx;
   anChamp_TopNTotal[X] = anChamp_TopNTotal[X] + anChamp_ExtraPoints[X];

   //*****;
   //**************** START OF MAX SCORE CALC

   for (n = 1;n <= anTop_Scores.length-1;n++) {
       anTop_Scores[n] = 0;
   }

   nMax_Score = 0;

   for (n = 1;n <= nRounds_LeftX;n++) {
       if(sChamp_Type.substring(0,6) == "SPRINT"){
          anTop_Scores[n] = Number(sPoints_Array[1].substring(0,3)) + 100;
       }else if(sChamp_Type.substring(0,9) == "ENDURANCE"){
              anTop_Scores[n] = Number(sPoints_Array[1].substring(0,3));
       }else if(sChamp_Type.substring(0,3) == "CAR"){
              anTop_Scores[n] = Number(sPoints_Array[1].substring(0,3));
       }
   } 

//Create array of Maximum possible scores for rounds left
   nCount = 0;
   for (n = nRounds;n >= (nRounds_LeftX + 1);n--) {
       nCount = nCount + 1;
       anTop_Scores[nRounds_LeftX + nCount] = nScores[n];
   } 

//Create Maximum Top n Total score
   for (n = 1;n <= nScores_To_Include;n++) {
       nMax_Score = nMax_Score + anTop_Scores[n];
   } 

   anChamp_Max[X] = nMax_Score;
   anChamp_Max[X] = anChamp_Max[X] + anChamp_ExtraPoints[X];
//**************** END OF MAX SCORE CALC

} 


//BUBBLE SORT Into Top X order
bCycle_Finished = false;

while (bCycle_Finished == false) {
   bCycle_Finished = true;
   for (n = 1; n <= nNo_Contenders - 1;n++) {
       nWinner = 0;
       if(anChamp_TopNTotal[n] == anChamp_TopNTotal[n + 1]){
          nWinner = Champ_Tie_Breaker(n, n + 1, sChamp_Type);
       }
       if(anChamp_TopNTotal[n] < anChamp_TopNTotal[n + 1] || nWinner == 2){
          //*** SAVE ARRAY VALUES HERE;
          sSaved_Driver = asChamp_Driver[n];
          nSaved_TopNTotal = anChamp_TopNTotal[n];
          nSaved_FullTotal = anChamp_FullTotal[n];
          nSaved_MaxScore = anChamp_Max[n];
          nSaved_ExtraPoints = anChamp_ExtraPoints[n];
          for (X = 1;X <= nRounds;X++) {
              anChamp_Saved_Score[X] = anChamp_Score[X][n];
              abChamp_Saved_Include_Score[X] = abChamp_Include_Score[X][n];
          }
          //*****;

          //*****;
          asChamp_Driver[n] = asChamp_Driver[n + 1];
          anChamp_TopNTotal[n] = anChamp_TopNTotal[n + 1];
          anChamp_FullTotal[n] = anChamp_FullTotal[n + 1];
          anChamp_Max[n] = anChamp_Max[n + 1];
          anChamp_ExtraPoints[n] = anChamp_ExtraPoints[n + 1];

          for (X = 1;X <= nRounds;X++) {
              anChamp_Score[X][n] = anChamp_Score[X][n + 1];
              abChamp_Include_Score[X][n] = abChamp_Include_Score[X][n + 1];
          }
          //*****;

          //*****;
          asChamp_Driver[n + 1] = sSaved_Driver;
          anChamp_TopNTotal[n + 1] = nSaved_TopNTotal;
          anChamp_FullTotal[n + 1] = nSaved_FullTotal;
          anChamp_Max[n + 1] = nSaved_MaxScore;
          anChamp_ExtraPoints[n + 1] = nSaved_ExtraPoints;

          for (X = 1;X <= nRounds;X++) {
              anChamp_Score[X][n + 1] = anChamp_Saved_Score[X];
              abChamp_Include_Score[X][n + 1] = abChamp_Saved_Include_Score[X];
          }
          //*****;
          bCycle_Finished = false;
       }
   }
}

//Find top Maximum Score
var nTop_MaxScore ;
var nRound_MaxScore ;
nTop_MaxScore = 0 ;

for (z = 1;z <= nNo_Contenders;z++) {
    if(anChamp_Max[z] > nTop_MaxScore){
       nTop_MaxScore = anChamp_Max[z];
    }
} 

for (z = 1;z <= nNo_Contenders;z++) {
    if(z == 1){
       nPoints_diff = 0;
       anChamp_Diff[z] = nPoints_diff;
    }else{
       nPoints_diff = anChamp_TopNTotal[1] - anChamp_TopNTotal[z];
       anChamp_Diff[z] = nPoints_diff;
    }

    if(anChamp_Max[z] < anChamp_TopNTotal[1]){
       abChamp_Contender[z] = false;
    }else if(anChamp_Max[z] == anChamp_TopNTotal[1]){
           if(nRounds_LeftX == 0){
              if(z == 1){
                 abChamp_Contender[z] = true;
              }else{
                 abChamp_Contender[z] = false;
              }
           }else{
              abChamp_Contender[z] = true;
           }
    }
} 

var bOther_Contenders ;
var bCurrDriver_Is_Contender ;

bOther_Contenders = false;
bCurrDriver_Is_Contender = false;

for (z = 1;z <= nNo_Contenders;z++) {
    if(asChamp_Driver[z] == sPassed_DriverName){
       if(abChamp_Contender[z] == true){
          bCurrDriver_Is_Contender = true;
       }
    }else{
       if(abChamp_Contender[z] == true){
          bOther_Contenders = true;
       }
    }
} 

if(bCurrDriver_Is_Contender && bOther_Contenders == false){
   return true;
}else{
   return false;
}

}

//********************************************************************************************

function Champ_Tie_Breaker(nDriver1, nDriver2, sChamp_Type){

var n;
var bWinner = new Array(2);
var nDriver = new Array(2);
var nPointsCount = new Array(2);
var nEndLoop;
var Round = 0;
var Driver = 0;
var nStored_Highest = new Array(2);
var nOldHighest = new Array(2);
var nWhoScoredFirst = 0;

bWinner[1] = false;
bWinner[2] = false;
nDriver[1] = nDriver1;
nDriver[2] = nDriver2;
nPointsCount[1] = 0;
nPointsCount[2] = 0;

if(sChamp_Type.substring(0,6) == "SPRINT"){
   nEndLoop = sPoints_Array.length-1;
}

if(sChamp_Type.substring(0,9) == "ENDURANCE"){
   nEndLoop = sPoints_Array.length-1;
}

if(sChamp_Type.substring(0,3) == "CAR"){
   nEndLoop = sPoints_Array.length-1;
}

//Store highest points scored by both drivers
nStored_Highest[0] = 0;

for (nPoints = 1;nPoints <= nEndLoop;nPoints++) {

       nPointsCount[1] = 0;
       nPointsCount[2] = 0;

       for (Driver = 1;Driver <= 2;Driver++) {

           for (Round = 1;Round <= nRounds;Round++) {

              if(abChamp_Include_Score[Round][nDriver[Driver]] == true){
                 if(sChamp_Type.substring(0,6) == "SPRINT"){
                    if(anChamp_Score[Round][nDriver[Driver]] == Number(sPoints_Array[nPoints].substring(0,3)) + 100){
                       nPointsCount[Driver] = nPointsCount[Driver] + 1;
                    }
                 }

                 if(sChamp_Type.substring(0,9) == "ENDURANCE"){
                    if(anChamp_Score[Round][nDriver[Driver]] == Number(sPoints_Array[nPoints].substring(0,3))){
                       nPointsCount[Driver] = nPointsCount[Driver] + 1;
                    }
                 }

                 if(sChamp_Type.substring(0,3) == "CAR"){
                    if(anChamp_Score[Round][nDriver[Driver]] == Number(sPoints_Array[nPoints].substring(0,3))){
                       nPointsCount[Driver] = nPointsCount[Driver] + 1;
                    }
                 }

		 if(anChamp_Score[Round][nDriver[Driver]] > Number(nStored_Highest[0])){
                       nStored_Highest[0] = anChamp_Score[Round][nDriver[Driver]];
                 }
             }
           }
       } 

       if(nPointsCount[1] > nPointsCount[2]){
          bWinner[1] = true;
          return 1;
       }

       if(nPointsCount[2] > nPointsCount[1]){
          bWinner[2] = true;
          return 2;
       }
} 

//As Number of counted scores are the same, look for highest non-counted score to decide between the entrants

if(bWinner[1] == false && bWinner[2] == false){
    nStored_Highest[1] = 0;
    nStored_Highest[2] = 0;
    nOldHighest[1] = 999;
    nOldHighest[2] = 999;
    while (bWinner[1] == false && bWinner[2] == false) {
       for (Driver = 1;Driver <= 2;Driver++) {
           for (Round = 1;Round <= nRounds;Round++) {
               if(sChamp_Type.substring(0,6) == "SPRINT"){
                  if(abChamp_Include_Score[Round][nDriver[Driver]] == false || abChamp_Include_Score[Round][nDriver[Driver]] < 100){
                     if(anChamp_Score[Round][nDriver[Driver]] > nStored_Highest[Driver] && anChamp_Score[Round][nDriver[Driver]] < nOldHighest[Driver]){
                        nStored_Highest[Driver] = anChamp_Score[Round][nDriver[Driver]];
                     }
                  }
               }

               if(abChamp_Include_Score[Round][nDriver[Driver]] == false){
                  if(sChamp_Type.substring(0,9) == "ENDURANCE"){
                     if(anChamp_Score[Round][nDriver[Driver]] > nStored_Highest[Driver] && anChamp_Score[Round][nDriver[Driver]] < nOldHighest[Driver]){
                        nStored_Highest[Driver] = anChamp_Score[Round][nDriver[Driver]];
                     }
                  }

                  if(sChamp_Type.substring(0,3) == "CAR"){
                     if(anChamp_Score[Round][nDriver[Driver]] > nStored_Highest[Driver] && anChamp_Score[Round][nDriver[Driver]] < nOldHighest[Driver]){
                        nStored_Highest[Driver] = anChamp_Score[Round][nDriver[Driver]];
                     }
                  }
               }
           }
       } 

       if(nStored_Highest[1] > nStored_Highest[2]){
          bWinner[1] = true;
          return 1;
       }

       if(nStored_Highest[2] > nStored_Highest[1]){
          bWinner[2] = true;
          return 2;
       }

       if(nStored_Highest[1] == 0 && nStored_Highest[2] == 0){
         if (nStored_Highest[0] != 0) {
	           for (Round = 1; Round <= nRounds;Round++) {
			if (nWhoScoredFirst == 0) {
				for (Driver = 1; Driver <= 2;Driver++) {
				  	if(Number(anChamp_Score[Round][nDriver[Driver]]) == Number(nStored_Highest[0])){
				       		nWhoScoredFirst = Driver;
			       	       		break;
          		  	  	}
                		}
			} else {
				break;
			}
            	   }
		return nWhoScoredFirst;
	} else {
	        return 0;
	}
       }

       nOldHighest[1] = nStored_Highest[1];
       nOldHighest[2] = nStored_Highest[2];
       nStored_Highest[1] = 0;
       nStored_Highest[2] = 0;
    }
}

}

//********************************************************

function Worded_Summary(nCurrDriver, sDriver_Name){

var m;
var n;
var x;
var nSub;
var nSubs = 0;

var XString1;
var XString2;
var sSaved_Score;

var asXCombos = new Array();
var nHigh_Score = new Array();
var sSummary_String = new Array();

asChamp_Combos = new Array();

for (n = 1;n <= asUniqueCombos.length-1;n++) {
    asChamp_Combos[n] = asUniqueCombos[n];
}

//From the Championship winning Combos, sort into Driver finishing Order && Store finishing scores
//Store as 011300212703124 etc etc

for (n = 1;n <= asChamp_Combos.length-1;n++) {

    XString1 = "";
    XString2 = "";

    nPos = 0;

    for (x = 0;x <= asChamp_Combos[n].length-2;x = x+2) {
        nPos = nPos + 1;

        if(Number(asChamp_Combos[n].substring(x,x+2)) == nCurrDriver){
           sTemp_Score = "000" + String(Get_Position_Points(sChamp_Type, nPos));
           XString1 = asChamp_Combos[n].substring(x,x+2) + sTemp_Score.substring(sTemp_Score.length-3, sTemp_Score.length);
        }

        if(Number(asChamp_Combos[n].substring(x,x+2)) <= nNo_Contenders && Number(asChamp_Combos[n].substring(x,x+2)) != nCurrDriver && Number(asChamp_Combos[n].substring(x,x+2)) != 0){
           sTemp_Score = "000" + String(Get_Position_Points(sChamp_Type, nPos));
           XString2 = XString2 + asChamp_Combos[n].substring(x,x+2) + sTemp_Score.substring(sTemp_Score.length-3, sTemp_Score.length);
        }
    }
    
    if(XString1 != "" || XString2 != "") {
       nSubs = nSubs + 1;

       asXCombos[nSubs] = XString1 + XString2;
    }
}

//---- Now Sort asXCombos() into Driver(01) Score order

bCycle_Finished = 0;

while (bCycle_Finished == 0) {

   bCycle_Finished = 1;

   for (n = 1;n <= asXCombos.length-2;n++) {

       if(Number(asXCombos[n].substring(2,5)) < Number(asXCombos[n+1].substring(2,5))){
          nSaved_Combo = asXCombos[n];
          asXCombos[n] = asXCombos[n+1];
          asXCombos[n+1] = nSaved_Combo;
          bCycle_Finished = 0;
          n = 1;
       }
   }
}

//--- Now store Highest finishing Positions for each Driver from the asXCombos()

sHTML += "<BR>";
sHTML += "<TABLE class='HeadTable3' BORDER=1 ALIGN=CENTER WIDTH=600>";
sHTML += "<TR><TD class='HeadBox3' BGCOLOR='#A8D3FF' ALIGN=CENTER>Championship Winning Summary - " + sDriver_Name + "</TD></TR>";
sHTML += "</TABLE>";

nSub = 1;

while (nSub <= asXCombos.length-1) {

   sSaved_Score = asXCombos[nSub].substring(2,5);

   for (n = 1;n <= nNo_Contenders;n++) {
       nHigh_Score[n] = 0;
   }

   nHigh_Score[nCurrDriver] = sSaved_Score;

   sHTML += "<BR>";
   sHTML += "<TABLE class='DetTable' BORDER=1 ALIGN=CENTER WIDTH=700>";

   while (sSaved_Score != 999 && sSaved_Score == asXCombos[nSub].substring(2,5) && nSub <= asXCombos.length-1) {

      for (m = 1;m <= nNo_Contenders;m++) {
 
          if(m != nCurrDriver){

             for (x = 5;x <= asXCombos[nSub].length;x = x + 5) {

                if(Number(asXCombos[nSub].substring(x,x+2)) == m){

                   if(asXCombos[nSub].substring(x+2,x+5) > nHigh_Score[m]){
                      nHigh_Score[m] = asXCombos[nSub].substring(x+2,x+5);
                   } 
                }
             }
          }
      }
      nSub = nSub + 1

      if(nSub > asXCombos.length-1){
        sSaved_Score = 999;
      }
   }

   //--- Now having obtained all High Scores create Worded Summary

   for (n = 1;n <= nSavedContenders;n++) {
       sSummary_String[n] = "";
   }

   var nCount;
   nCount = 1;

   sSummary_String[nCount] = "if " + asSaveDriver[nCurrDriver] + " Finishes " + String(Get_Position(sChamp_Type, nHigh_Score[nCurrDriver]));

   for (n = 1;n <= nSavedContenders;n++) {
       if(n != nCurrDriver){
          if (abDisplayDriver[n] == true) { 

             if(nHigh_Score[n] != Get_Position_Points(sChamp_Type, 1)){
                nCount = nCount + 1;
                sSummary_String[nCount] = asSaveDriver[n] + " must finish " + String(Get_Position(sChamp_Type, nHigh_Score[n])) + " or lower";
             }
          }
       }
   } 

   for (n = 1;n <= nSavedContenders;n++) {
       sHTML += "<TR>";
       sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour1 + "' WIDTH=700 ALIGN=LEFT>" + sSummary_String[n] + "</TD>";
       sHTML += "</TR>";
   }

   sHTML += "</TABLE>";
}

if(bDefaultedEndDriver == true){
   sHTML += "<BR><B><CENTER>These Results could only be calculated with finishing positions as low as Last in the A-Final - Please check manually for other possible outcomes !</CENTER></B><BR>";
}

sHTML3 = sHTML;

sHTML += "<BR>";
sHTML += "<TABLE class='HeadTable3' BORDER=0 ALIGN=CENTER WIDTH=600>";
sHTML += "<TR><TD class='HeadBox3' ALIGN=CENTER><A onclick='Show_Results()'><U>Click to see Winning Results Combinations</U></A></TD></TR>";
sHTML += "</TABLE>";

sHTML += "<BR><BR>";

}

//*********************************************************************

function Show_Results(){

document.getElementById("DivText4").innerHTML = sHTML3 + sHTML2;

}

//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************

function Positions_Init(){

var bCycle_Finished ;
var nSaved_Score ;
var nScores = new Array();
var m;
var n;

var nMaxRoundScore;

var anChamp_Position = new Array();
asUniqueCombos = new Array();

if (bChampFirstTimeIn == true) {

   for (m=1; m <= nRounds;m++) {

       anChamp_Score[m] = new Array();
       abChamp_Include_Score[m] = new Array();

       for (n=1; n <= nDriver_Records; n++) {
           anChamp_Score[m][n] = 0;
           abChamp_Include_Score[m][n] = false;
       }
   }

   bChampFirstTimeIn = false;

}

asChamp_Combos = new Array();

//Populate list of Championship Contenders && set-up array

nNo_Contenders = 0;

//CHECK for X in Grid Column. Need to Handle This with an ARRAY abChamp_Contender(X) = true/false

if(sChamp_Type.substring(0,6) == "SPRINT"){
   nMaxRoundScore = Number(sPoints_Array[1].substring(0,3)) + 100;
}

if(sChamp_Type.substring(0,9) == "ENDURANCE"){
   nMaxRoundScore = Number(sPoints_Array[1].substring(0,3));
}

if(sChamp_Type.substring(0,3) == "CAR"){
   nMaxRoundScore = Number(sPoints_Array[1].substring(0,3));
}


for (n = 1;n <= nDriver_Records;n++) {


    if(anTopNTotal[n] <= anTopNTotal[nPassed_Position] && (anTopNTotal[n] - anTopNTotal[nPassed_Position]) <= Number(nMaxRoundScore) && ((anTopNTotal[n] - anTopNTotal[nPassed_Position])*-1) <= Number(nMaxRoundScore)){

          nNo_Contenders = nNo_Contenders + 1;

          asChamp_Driver[nNo_Contenders] = asDriver[n];
          anChamp_TopNTotal[nNo_Contenders] = anTopNTotal[n];
          anChamp_FullTotal[nNo_Contenders] = anFullTotal[n];
          anChamp_ExtraPoints[nNo_Contenders] = anExtraPoints[n];

          for (m = 1;m <= nRounds;m++) {
              anChamp_Score[m][nNo_Contenders] = anScore[m][n];
          } 

          abChamp_Contender[nNo_Contenders] = true;
          anChamp_Diff[nNo_Contenders] = 0;
          anChamp_Max[nNo_Contenders] = 0;
          anChamp_Saved_Score[nNo_Contenders] = 0;
          anChamp_MinPoints[nNo_Contenders] = 0;
          anChamp_Position[nNo_Contenders] = n;

          asSaveDriver[nNo_Contenders] = asDriver[n];

    }
}

//          var anSave_TopNTotal = new Array();
//          var anSave_FullTotal = new Array();
//          var anSave_ExtraPoints = new Array();

//          var anSave_Score = new Array();

//          var abSave_Contender = new Array();
//          var anSave_Diff = new Array();
//          var anSave_Max = new Array();
//          var anSave_Saved_Score = new Array();
//          var anSave_MinPoints = new Array();
//          va anSave_Position = new Array();

//          asSaveDriver = asDriver;
//          anSave_TopNTotal = anChamp_TopNTotal;
//          anSave_FullTotal = anChamp_FullTotal;
//          anSave_ExtraPoints = anChamp_ExtraPoints;

//          anSave_Score = anChamp_Score;

//          abSave_Contender = abChamp_Contender;
//          anSave_Diff = anChamp_Diff;
//          anSave_Max = anChamp_Max;
//          anSave_Saved_Score = anChamp_Saved_Score;
//          anSave_MinPoints = anChamp_MinPoints;
//          anSave_Position = anChamp_Position;
 

nSavedContenders = nNo_Contenders;

//Check to see if(Simulation turned on && if(so, what the latest Simulated round is

bSimRound = false;
nCurrentSimRound = 0;
nChampRound = 0;
sChampRound = "";

if(bSimulationOn == true){
   for (Y = 1;Y <= nRounds;Y++) {
       if(Y > nCurrent_Round){
          for (X = 1;X <= nDriver_Records;X++) {
              if(anSimulatedScore[Y][X] != 0){
                 bSimRound = true;
                 nCurrentSimRound = Y;
                 X = nDriver_Records+1;
              }
          }
       }
   }
}

if(bSimulationOn == true && nCurrentSimRound > 0){
   sChampRound = String(nCurrentSimRound+1) + " (Simulated) ";
   nChampRound = String(nCurrentSimRound+1);
}else{
   sChampRound = String(nCurrent_Round+1);
   nChampRound = String(nCurrent_Round+1);
}

//Work out drivers' minimum scoring scores && populate Grid at top of screen

nSwitch = 0;

sHTML = "";
sHTML += "<TABLE class='HeadTable1' BORDER=1 ALIGN=CENTER WIDTH=600>";

if(sChamp_Type.substring(0,6) == "SPRINT"){
   sHTML += "<TR><TD class='HeadBox1' BGCOLOR='#A8D3FF' ALIGN=CENTER>Position Analysis : " + sGainLose + " Position " + nPassed_Position + " - at Round " + sChampRound + " - " + asTrackName[nChampRound] + "</TD></TR>";
}else if(sChamp_Type.substring(0,9) == "ENDURANCE"){
   sHTML += "<TR><TD class='HeadBox1' BGCOLOR='#A8D3FF' ALIGN=CENTER>Position Analysis : " + sGainLose + " Position " + nPassed_Position + " - at Round " + sChampRound + " - " + asTrackName[nChampRound] + "</TD></TR>";
}else if(sChamp_Type.substring(0,3) == "CAR"){
   sHTML += "<TR><TD class='HeadBox1' BGCOLOR='#A8D3FF' ALIGN=CENTER>Position Analysis : " + sGainLose + " Position " + nPassed_Position + " - at Round " + sChampRound + " - " + asTrackName[nChampRound] + "</TD></TR>";
}

sHTML += "</TABLE><BR>";

sHTML += "<TABLE class='HeadTable2' BORDER=1 ALIGN=CENTER WIDTH=400>";
sHTML += "<TR><TD class='HeadBox2' BGCOLOR='#A8D3FF' ALIGN=CENTER>Select Contender for Analysis</TD></TR>";
sHTML += "</TABLE><BR>";

sHTML += "<TABLE class='DetTable' BORDER=1 ALIGN=CENTER WIDTH=700>";
sHTML += "<TR>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=50 ALIGN=LEFT>Position</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=263 ALIGN=LEFT>Entrant</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=153 ALIGN=CENTER>Minimum Scoring Points</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=153 ALIGN=CENTER>Minimum Scoring Position</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=80 ALIGN=CENTER>Current Difference</TD>";
sHTML += "</TR>";

var nSelCount = 0;

for (n = 1;n <= nNo_Contenders;n++) {
   for (m = 1;m <= nRounds;m++) {
       nScores[m] = anChamp_Score[m][n];
   }

   bCycle_Finished = false;
   //Sort scores into order low - High;
   while (bCycle_Finished == false) {
      bCycle_Finished = true;
      for (m = 1;m <= nRounds - 1;m++) {
          if(nScores[m] < nScores[m + 1]){
             nSaved_Score = nScores[m];
             nScores[m] = nScores[m + 1];
             nScores[m + 1] = nSaved_Score;
             bCycle_Finished = false;
          }
      }
   }

   sMinScoreString = Minimum_Scoring_Score(sChamp_Type, nScores[nScores_To_Include]);

   if(nSwitch == 0){
      sColour = sColour1;
      nSwitch = 1;
   }else{
      sColour = sColour2;
      nSwitch = 0;
   }

   sHTML += "<TR>";
   sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=50 ALIGN=LEFT>" + anChamp_Position[n] + "</TD>";

   if(sGainLose == "GAIN" && (anChamp_TopNTotal[1] - anChamp_TopNTotal[n]) <= Number(nMaxRoundScore) && (anChamp_TopNTotal[1] - anChamp_TopNTotal[n]) > 0) {
       sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=263 ALIGN=LEFT><A onclick='Positions_Calc(" + n + "," + "\"" + asChamp_Driver[n] + "\"" + ")'><U>" + asChamp_Driver[n] + "</U></A></TD>";
       nSelCount++;
   } else {
   	if(sGainLose == "LOSE" && anChamp_Position[n] == nPassed_Position) {
	    sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=263 ALIGN=LEFT><A onclick='Positions_Calc(" + n + "," + "\"" + asChamp_Driver[n] + "\"" + ")'><U>" + asChamp_Driver[n] + "</U></A></TD>";
	    nSelCount++;
        } else {
            sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=263 ALIGN=LEFT>" + asChamp_Driver[n] + "</TD>";
        }
   }

   if ((anChamp_TopNTotal[1] - anChamp_TopNTotal[n]) > Number(sMinScoreString.substring(0,3))) {
      anChamp_MinPoints[n] = (anChamp_TopNTotal[1] - anChamp_TopNTotal[n]);
      sTemp_Score = "000" + String(anChamp_MinPoints[n]);
      anChamp_MinPoints[n] = sTemp_Score.substring(sTemp_Score.length-3,sTemp_Score.length);
      sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=153 ALIGN=CENTER>" + anChamp_MinPoints[n] + "</TD>";
      sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=153 ALIGN=CENTER>" + Get_Position(sChamp_Type, anChamp_MinPoints[n]) + "</TD>";
      sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=80 ALIGN=CENTER>" + ((n==1) ? 0:(anChamp_TopNTotal[1] - anChamp_TopNTotal[n])) + "</TD>";
   } else {
      anChamp_MinPoints[n] = Number(sMinScoreString.substring(0,3));
      sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=153 ALIGN=CENTER>" + sMinScoreString.substring(0,3) + "</TD>";
      sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=153 ALIGN=CENTER>" + sMinScoreString.substring(3,sMinScoreString.length) + "</TD>";
      sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=80 ALIGN=CENTER>" + ((n==1) ? 0:(anChamp_TopNTotal[1] - anChamp_TopNTotal[n])) + "</TD>";
   }
   sHTML += "</TR>";

} 

sHTML += "</TABLE>";

//document.getElementById("DivText_Top_Banner").innerHTML = "<P></P>";
//document.getElementById("DivText_Bottom_Banner").innerHTML = "<P></P>";
document.getElementById("DivText2").innerHTML = "<P></P>";
document.getElementById("DivText3").innerHTML = "<P></P>";
document.getElementById("DivText4").innerHTML = "<P></P>";

//document.getElementById("DivText_Top_Banner").innerHTML = "";
//document.getElementById("DivText_Bottom_Banner").innerHTML = "";
document.getElementById("DivText2").innerHTML = sHTML;

if (nSelCount <= 1 && nNo_Contenders <= 1) {
    alert("No Driver/Team can " + sGainLose + " Position " + nPassed_Position + " At Round " + sChampRound);
    return;
}

sSavedPosition_Driver = asDriver[nPassed_Position];

}

//********************************************************************

function Positions_Calc(nCurrDriver, sDriver_Name){

var nStartDriver;
var nEndDriver;
var nDriver;
var nCurrDriverPos;
var nLoop;
var nLoop_Driver;
var bChampion_Found;
var bCanWin;
var nPoints;
var bFirstTime;
var sCombination;
var asCombos = new Array();
var nCombos;
var bCombo_Exists;
var sCombo_Driver;
var nCombo_Driver;
var sTemp_String = "";
var nSaved_EndDriver = 0;

bFirstTime = true;

if(nNo_Contenders <= 1) {
   document.getElementById("DivText4").innerHTML = "";
   alert("Position Calculations (Complete)" + "\n\n" + sDriver_Name + " Cannot " + sGainLose + " Position " + nPassed_Position + " At Round " + sChampRound);
   return;
}

for (n = 1;n <= nNo_Contenders;n++) {
    nPoints = anChamp_MinPoints[n];
    nEndDriver = Calculate_End_Driver(sChamp_Type, nPoints);
    if (nEndDriver >= nSaved_EndDriver) {
       nSaved_EndDriver = nEndDriver;
    }
}

nEndDriver = nSaved_EndDriver;

if (nEndDriver < nNo_Contenders) {
   nEndDriver = nNo_Contenders;
}

nChamp_Combos = 0;

for (n = 1;n <= asChamp_Combos.length-1;n++) {
    asChamp_Combos[n] = "";
}

for (n = 1;n <= nNo_Contenders;n++) {
    nPoints = anChamp_MinPoints[n];
    anMin_Scoring_Pos[n] = Calculate_End_Driver(sChamp_Type, nPoints);
} 

bChampion_Found = false;
bCanWin = false;

nRounds_LeftX = nRounds - nChampRound;

nCombos = 0;

//********* Calculate forward 1 - n

alert("About To Calculate Results Combinations - This may take a while");

nStartDriver = 1;

if(nCurrDriver == nStartDriver){
   nStartDriver = nStartDriver + 1;
}

while (nStartDriver <= nEndDriver) {

   nCurrDriverPos = nEndDriver;

   while (nCurrDriverPos >= 1) {

      nLoop = 1;
      nLoop_Driver = nStartDriver;

      sCombination = "";

      //document.getElementById("DivText3").innerHTML = "<P>Building Results Combinations (forward) : Please Wait</P>";

      while (nLoop <= nEndDriver) {

         if(nLoop == nCurrDriverPos) {

            sTemp_String = "00" + String(nCurrDriver);
            sCombination = sCombination + sTemp_String.substring(sTemp_String.length-2, sTemp_String.length);

            if(nLoop_Driver == nCurrDriver){
               nLoop_Driver = nLoop_Driver + 1;
               if(nLoop_Driver == nCurrDriver){
                  nLoop_Driver = nLoop_Driver + 1;
               }
               if(nLoop_Driver > nEndDriver){
                  nLoop_Driver = 1;
               }
            }
            nLoop = nLoop + 1;
         }else{
            if(nLoop_Driver != nCurrDriver){
               if(nLoop_Driver <= nNo_Contenders){
                  sTemp_String = "00" + String(nLoop_Driver);
                  sCombination = sCombination + sTemp_String.substring(sTemp_String.length-2, sTemp_String.length);
               }else{
                  sCombination = sCombination + "  ";
               }
               nLoop_Driver = nLoop_Driver + 1;
               if(nLoop_Driver == nCurrDriver){
                  nLoop_Driver = nLoop_Driver + 1;
               }
               if(nLoop_Driver > nEndDriver){
                  nLoop_Driver = 1;
               }
               nLoop = nLoop + 1;
            }else{
               nLoop_Driver = nLoop_Driver + 1;
               if(nLoop_Driver == nCurrDriver){
                  nLoop_Driver = nLoop_Driver + 1;
               }
               if(nLoop_Driver > nEndDriver){
                  nLoop_Driver = 1;
               }
            }
         }
      }

      //*************************************************************************************************;
      //Check that this combination is NEW ?? if(it is, add it into Combo Array;
      //Ready for processing later.;

      bCombo_Exists = false;
      for (n = 1;n <= asCombos.length-1;n++) {
          if(sCombination == asCombos[n]){
             bCombo_Exists = true;
             n = asCombos.length+1;
          }
      }

      if(bCombo_Exists == false){
         nCombos = nCombos + 1;
         asCombos[nCombos] = sCombination;
      }
      nCurrDriverPos = nCurrDriverPos - 1;
   }

   nStartDriver = nStartDriver + 1;
   if(nStartDriver == nCurrDriver){
      nStartDriver = nStartDriver + 1;
   }
}

//********* END Calculate forward 1 - n

//********* Calculate Backward n - 1

nStartDriver = nEndDriver;

if(nCurrDriver == nStartDriver){
   nStartDriver = nStartDriver - 1;
}

alert("Stage 2 - Building Results Combinations - Please Wait");

while (nStartDriver >= 1) {

   nCurrDriverPos = nEndDriver;

   while (nCurrDriverPos >= 1) {

      nLoop = nEndDriver;
      nLoop_Driver = nStartDriver;
      sCombination = "";

      //document.getElementById("DivText3").innerHTML = "<P>Building Results Combinations (Backward) : Please Wait</P>";

      while (nLoop >= 1) {

         if(nLoop == nCurrDriverPos){
            sTemp_String = "00" + String(nCurrDriver);
            sCombination = sCombination + sTemp_String.substring(sTemp_String.length-2, sTemp_String.length);
            if(nLoop_Driver == nCurrDriver){
               nLoop_Driver = nLoop_Driver - 1;
               if(nLoop_Driver == nCurrDriver){
                  nLoop_Driver = nLoop_Driver - 1;
               }
               if(nLoop_Driver < 1){
                  nLoop_Driver = nEndDriver;
               }
            }
            nLoop = nLoop - 1;
         }else{
            if(nLoop_Driver != nCurrDriver){
               if(nLoop_Driver <= nNo_Contenders){
                  sTemp_String = "00" + String(nLoop_Driver);
                  sCombination = sCombination + sTemp_String.substring(sTemp_String.length-2, sTemp_String.length);
               }else{
                  sCombination = sCombination + "  ";
               }
               nLoop_Driver = nLoop_Driver - 1;
               if(nLoop_Driver == nCurrDriver){
                  nLoop_Driver = nLoop_Driver - 1;
               }
               if(nLoop_Driver < 1){
                  nLoop_Driver = nEndDriver;
               }
               nLoop = nLoop - 1;
            }else{
               nLoop_Driver = nLoop_Driver - 1;
               if(nLoop_Driver == nCurrDriver){
                  nLoop_Driver = nLoop_Driver - 1;
               }
               if(nLoop_Driver < 1){
                  nLoop_Driver = nEndDriver;
               }
            }
         }
      }
      //*************************************************************************************************;
      //Check that this combination is NEW ?? if(it is, add it into Combo Array;
      //Ready for processing later.;

      bCombo_Exists = false;
      for (n = 1;n <= asCombos.length-1;n++) {
          if(sCombination == asCombos[n]){
             bCombo_Exists = true;
             n = asCombos.length+1;
          }
      }

      if(bCombo_Exists == false){
         nCombos = nCombos + 1;
         asCombos[nCombos] = sCombination;
      }
      nCurrDriverPos = nCurrDriverPos - 1;
   }
   nStartDriver = nStartDriver - 1;
   if(nStartDriver == nCurrDriver){
      nStartDriver = nStartDriver - 1;
   }
}

//********* END Calculate Backward n - 1

//****** Process Combinations

//*****************************

sHTML = "";
sHTML2 = "";
sHTML3 = "";

sHTML2 =  "<BR><BR>";
sHTML2 += "<TABLE class='HeadTable3' BORDER=1 ALIGN=CENTER WIDTH=600>";
sHTML2 += "<TR><TD class='HeadBox3' BGCOLOR='#A8D3FF' ALIGN=CENTER>Position " + nPassed_Position + " " + sGainLose + " - Results Combinations - " + sDriver_Name + "</TD></TR>";
sHTML2 += "</TABLE><BR>";
sHTML2 += "<TABLE class='DetTable' BORDER=1 ALIGN=CENTER WIDTH=700>";

//******************************

var nSub = 0;
var nCounter = 0;
var bInput_Box = true;
var bReportDriver1 = false;
var bReportDriver2 = false;

var sTempHTML = "";

abDisplayDriver = new Array();
for (n=0;n <= nNo_Contenders;n++) {
    abDisplayDriver[n] = false;
}
  
for (nSub = 1;nSub <= asCombos.length-1;nSub++) {

    nCounter++;

    if (nCounter >= 50 || nCounter == 0) {
       bInput_Box = confirm("Stage 3 - Processing Results Combinations : " + nSub + " of: " + (asCombos.length-1) + "\n\n" + "Matching Combinations Found : " + nChamp_Combos + "\n\n" + "Please Wait or click Cancel");

       if (bInput_Box != true) {
          break;
       }
       nCounter = 1;
    }

    for (nCombo_Driver = 1;nCombo_Driver <= nEndDriver;nCombo_Driver++) {

        sCombo_Driver = Number(asCombos[nSub].substring(((nCombo_Driver-1)*2),((nCombo_Driver-1)*2)+2));

        if(sCombo_Driver == nCurrDriver){
           anChamp_Score[nChampRound][nCurrDriver] = Get_Position_Points(sChamp_Type, nCombo_Driver);
        }

        if(sCombo_Driver != nCurrDriver){
           if(sCombo_Driver <= nNo_Contenders && sCombo_Driver != 0){

              anChamp_Score[nChampRound][sCombo_Driver] = Get_Position_Points(sChamp_Type, nCombo_Driver);
           }
        }
    } 

       bChampion_Found = Positions_ReCalculate_Table(nCurrDriver,sDriver_Name, false, true, nPassed_Position, sGainLose);

       if(bChampion_Found == true){
          bCanWin = true;
          nChamp_Combos = nChamp_Combos + 1;
          asChamp_Combos[nChamp_Combos] = asCombos[nSub];
       }

//******************************************************

      if(bChampion_Found == true){
          bCanWin = true;

          sTempHTML = "";
          sTempHTML += "<TR>";
          sTempHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=80  ALIGN=LEFT><B>Position</B></TD>";
          sTempHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=250 ALIGN=LEFT><B>Entrant</B></TD>";
          sTempHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=150 ALIGN=LEFT><B>Round Result</B></TD>";
          sTempHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=70  ALIGN=RIGHT><B>Top " + String(nScores_To_Include) + "</B></TD>";
          sTempHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=100 ALIGN=RIGHT><B>Difference</B></TD>";
          sTempHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=100 ALIGN=RIGHT><B>Maximum</B></TD>";
          sTempHTML += "</TR>";

          nSwitch = 0;
          bReportDriver1 = false;
          bReportDriver2 = false;

          //Re-populate grid;
          for (z = 1;z <= nNo_Contenders;z++) {
              bFirstTime = false;

              if(nSwitch == 0){
                 sColour = sColour1;
                 nSwitch = 1;
              }else{
                 sColour = sColour2;
                 nSwitch = 0;
              }

	      sTempHTML += "<TR>";
              sTempHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=80  ALIGN=LEFT>" + ((nPassed_Position-1)+z) + "</TD>";
              sTempHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=250 ALIGN=LEFT>" + asChamp_Driver[z] + "</TD>";

              sTempHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=150 ALIGN=LEFT>" + Get_Position(sChamp_Type, anChamp_Score[nChampRound][z]) + " (" + anChamp_Score[nChampRound][z] + ")</TD>";
              sTempHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=70  ALIGN=RIGHT>" + anChamp_TopNTotal[z] + "</TD>";
              sTempHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=100 ALIGN=RIGHT>" + Number(anChamp_TopNTotal[1]-anChamp_TopNTotal[z]) + "</TD>";
              sTempHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=100 ALIGN=RIGHT>" + anChamp_Max[z] + "</TD>";
              sTempHTML += "</TR>";

              for (n = 1;n <= nNo_Contenders;n++) {
                 if (asChamp_Driver[z] == asSaveDriver[n]) {
                    abDisplayDriver[n] = true;
                 }
              }


              if(sGainLose == "LOSE" && asChamp_Driver[z] == sDriver_Name) {
                  break;
              }

              if (sGainLose == "GAIN" && bReportDriver1 == true && bReportDriver2 == true) {
                  break;
              }

          }

          var sTempCombo = "";
          var nSub2; 

          for (x = 0;x <= asChamp_Combos[nChamp_Combos].length-2;x = x+2) {

              nSub2 = Number(asChamp_Combos[nChamp_Combos].substring(x,x+2))

              if(abDisplayDriver[nSub2] == false){
                 sTempCombo = sTempCombo + "  ";
              } else {
                 sTempCombo = sTempCombo + asChamp_Combos[nChamp_Combos].substring(x,x+2);
              }
          }

          var bFound = false

          for (x = 1;x <= asUniqueCombos.length-1;x++) {
               if (sTempCombo == asUniqueCombos[x]) {
                   bFound = true;
                   break;
               }
          }
 
          if (bFound == false) {
              nSub2 = asUniqueCombos.length;
              asUniqueCombos[nSub2] = sTempCombo;
              sHTML2 += sTempHTML;
          }
          
       }

       Reset_Positions_Arrays();
//******************************************************

} 

sHTML2 += "</TABLE><BR>";

if(bCanWin == true){
   Positions_Worded_Summary(nCurrDriver, sDriver_Name);
}

//MATRIX To Produce
//1111X 2222X 3333X 4444X
//222X1 333X2 444X3 111X4
//33X22 44X33 11X44 22X11
//4X333 1X444 2X111 3X222
//X4444 X1111 X2222 X3333

//4444X 3333X 2222X 1111X
//333X4 222X3 111X2 444X4
//22X33 11X22 44X11 33X43
//1X222 4X111 3X444 2X332
//X1111 X4444 X3333 X2221
//
//
//1  2   3   4           4   3   2   1
//2  3   4   1           3   2   1   4
//3  4   1   2           2   1   4   3
//4  1   2   3           1   4   3   2
//

document.getElementById("DivText3").innerHTML = "";
document.getElementById("DivText4").innerHTML = sHTML;

sTemp_String = "";

if(bInput_Box == true) {
   sTemp_String = "Position Calculations (Complete)";
} else {
  sTemp_String = "Position Calculations (Aborted)";
}

if(bCanWin == true){
   alert(sTemp_String + "\n\n" + sDriver_Name + " Can " + sGainLose + " Position " + nPassed_Position + " At Round " + sChampRound + " - See Table for Details");
}else{
   document.getElementById("DivText4").innerHTML = "";
   alert(sTemp_String + "\n\n" + sDriver_Name + " Cannot " + sGainLose + " Position " + nPassed_Position + " At Round " + sChampRound);
}


}

//*************************************************************************

function Positions_ReCalculate_Table(nDriver_No, sPassed_DriverName, bChamp_Calc, bPosition_Calc, nPassed_Position, sGainLose){

var X;
var nPoints_diff;
var bSimScoresFound;
var nWinner;

var anTop_Scores = new Array();
var nMax_Score;
var nCount;

var nRounds_LeftX;

nRounds_LeftX = nRounds - (nChampRound);

anTop_Scores[0] = 0;

//****************
//Now weed out best X results && re-calculate total

//Revar anChamp_Saved_Score(nRounds) ;
//Revar abChamp_Saved_Include_Score(nRounds) 

//Recalculate Championship Contenders array


for (X = 1;X <= nNo_Contenders;X++) {
//   Revar nScores(nRounds) ;
   var bCycle_Finished ;
   var nSaved_Score ;
   var ntopx ;

   ntopx = 0;
   anChamp_FullTotal[X] = 0;

   for (n = 1;n <= nRounds;n++) {
       nScores[n] = anChamp_Score[n][X];
       anChamp_FullTotal[X] = anChamp_FullTotal[X] + anChamp_Score[n][X];
       abChamp_Include_Score[n][X] = true;
   } 

   anChamp_FullTotal[X] = anChamp_FullTotal[X] + anChamp_ExtraPoints[X]; 

   bCycle_Finished = false;
//Sort scores into order

   while (bCycle_Finished == false) {
      bCycle_Finished = true;
      for (n = 1;n <= (nRounds - 1);n++) {
          if(nScores[n] > nScores[n + 1]){
             nSaved_Score = nScores[n];
             nScores[n] = nScores[n + 1];
             nScores[n + 1] = nSaved_Score;
             bCycle_Finished = false;
          }
      }
   }

//Work out Top n Total
   for (n = nRounds;n >= (nRounds - nScores_To_Include) + 1;n--) {
       ntopx = ntopx + nScores[n];
   } 

//now mark in the array whether each score is included in the total || not, false = ! included
   for (n = (nRounds - nScores_To_Include);n >= 1;n--) {
       for (m = 1;m <= nRounds;m++) {
           if(nScores[n] != 0){
              if(anChamp_Score[m][X] == nScores[n]){
                 abChamp_Include_Score[m][X] = false;
                 m = nRounds+1;
              }
           }
       }
   } 

   anChamp_TopNTotal[X] = ntopx;
   anChamp_TopNTotal[X] = anChamp_TopNTotal[X] + anChamp_ExtraPoints[X]; 

   //*****;
   //**************** START OF MAX SCORE CALC

   for (n = 1;n <= anTop_Scores.length-1;n++) {
       anTop_Scores[n] = 0;
   }

   nMax_Score = 0;

   for (n = 1;n <= nRounds_LeftX;n++) {
       if(sChamp_Type.substring(0,6) == "SPRINT"){
          anTop_Scores[n] = Number(sPoints_Array[1].substring(0,3)) + 100;
       }else if(sChamp_Type.substring(0,9) == "ENDURANCE"){
              anTop_Scores[n] = Number(sPoints_Array[1].substring(0,3));
       }else if(sChamp_Type.substring(0,3) == "CAR"){
              anTop_Scores[n] = Number(sPoints_Array[1].substring(0,3));
       }
   } 

//Create array of Maximum possible scores for rounds left
   nCount = 0;
   for (n = nRounds;n >= (nRounds_LeftX + 1);n--) {
       nCount = nCount + 1;
       anTop_Scores[nRounds_LeftX + nCount] = nScores[n];
   } 

//Create Maximum Top n Total score
   for (n = 1;n <= nScores_To_Include;n++) {
       nMax_Score = nMax_Score + anTop_Scores[n];
   } 

   anChamp_Max[X] = nMax_Score;
   anChamp_Max[X] = anChamp_Max[X] + anChamp_ExtraPoints[X]; 
//**************** END OF MAX SCORE CALC

} 


//BUBBLE SORT Into Top X order
bCycle_Finished = false;

while (bCycle_Finished == false) {
   bCycle_Finished = true;
   for (n = 1; n <= nNo_Contenders - 1;n++) {
       nWinner = 0;
       if(anChamp_TopNTotal[n] == anChamp_TopNTotal[n + 1]){
          nWinner = Champ_Tie_Breaker(n, n + 1, sChamp_Type);
       }
       if(anChamp_TopNTotal[n] < anChamp_TopNTotal[n + 1] || nWinner == 2){
          //*** SAVE ARRAY VALUES HERE;
          sSaved_Driver = asChamp_Driver[n];
          nSaved_TopNTotal = anChamp_TopNTotal[n];
          nSaved_FullTotal = anChamp_FullTotal[n];
          nSaved_MaxScore = anChamp_Max[n];
          nSaved_ExtraPoints = anChamp_ExtraPoints[n];
          for (X = 1;X <= nRounds;X++) {
              anChamp_Saved_Score[X] = anChamp_Score[X][n];
              abChamp_Saved_Include_Score[X] = abChamp_Include_Score[X][n];
          }
          //*****;

          //*****;
          asChamp_Driver[n] = asChamp_Driver[n + 1];
          anChamp_TopNTotal[n] = anChamp_TopNTotal[n + 1];
          anChamp_FullTotal[n] = anChamp_FullTotal[n + 1];
          anChamp_Max[n] = anChamp_Max[n + 1];
          anChamp_ExtraPoints[n] = anChamp_ExtraPoints[n + 1];

          for (X = 1;X <= nRounds;X++) {
              anChamp_Score[X][n] = anChamp_Score[X][n + 1];
              abChamp_Include_Score[X][n] = abChamp_Include_Score[X][n + 1];
          }
          //*****;

          //*****;
          asChamp_Driver[n + 1] = sSaved_Driver;
          anChamp_TopNTotal[n + 1] = nSaved_TopNTotal;
          anChamp_FullTotal[n + 1] = nSaved_FullTotal;
          anChamp_Max[n + 1] = nSaved_MaxScore;
          anChamp_ExtraPoints[n + 1] = nSaved_ExtraPoints;

          for (X = 1;X <= nRounds;X++) {
              anChamp_Score[X][n + 1] = anChamp_Saved_Score[X];
              abChamp_Include_Score[X][n + 1] = abChamp_Saved_Include_Score[X];
          }
          //*****;
          bCycle_Finished = false;
       }
   }
}

if (bPosition_Calc == true) {

    for (n = 1; n <= nNo_Contenders;n++) {
       
       if(asChamp_Driver[n] == sPassed_DriverName) {
      
          if(sGainLose == "GAIN" && n == 1) {
             return true;
          }

          if(sGainLose == "LOSE" && n > 1) {
             return true;
          }
       }
    }
}

}

//********************************************************

function Positions_Worded_Summary(nCurrDriver, sDriver_Name){

var m;
var n;
var x;
var nSub;
var nSubs = 0;

var XString1;
var XString2;
var sSaved_Score;

var asXCombos = new Array();
var nHigh_Score = new Array();
var sSummary_String = new Array();

asChamp_Combos = new Array();

for (n = 1;n <= asUniqueCombos.length-1;n++) {
    asChamp_Combos[n] = asUniqueCombos[n];
}

//From the Championship winning Combos, sort into Driver finishing Order && Store finishing scores
//Store as 011300212703124 etc etc


for (n = 1;n <= asChamp_Combos.length-1;n++) {

    XString1 = "";
    XString2 = "";

    nPos = 0;

    for (x = 0;x <= asChamp_Combos[n].length-2;x = x+2) {
        nPos = nPos + 1;

        if(Number(asChamp_Combos[n].substring(x,x+2)) == nCurrDriver){
           sTemp_Score = "000" + String(Get_Position_Points(sChamp_Type, nPos));
           XString1 = asChamp_Combos[n].substring(x,x+2) + sTemp_Score.substring(sTemp_Score.length-3, sTemp_Score.length);
        }

        if(Number(asChamp_Combos[n].substring(x,x+2)) <= nNo_Contenders && Number(asChamp_Combos[n].substring(x,x+2)) != nCurrDriver && Number(asChamp_Combos[n].substring(x,x+2)) != 0){
           sTemp_Score = "000" + String(Get_Position_Points(sChamp_Type, nPos));
           XString2 = XString2 + asChamp_Combos[n].substring(x,x+2) + sTemp_Score.substring(sTemp_Score.length-3, sTemp_Score.length);
        }
    }

    if(XString1 != "" || XString2 != "") {
       nSubs = nSubs + 1;

       asXCombos[nSubs] = XString1 + XString2;
    }
}

//---- Now Sort asXCombos() into Driver(01) Score order

bCycle_Finished = 0;

while (bCycle_Finished == 0) {

   bCycle_Finished = 1;

   for (n = 1;n <= asXCombos.length-2;n++) {

       if(Number(asXCombos[n].substring(2,5)) < Number(asXCombos[n+1].substring(2,5))){
          nSaved_Combo = asXCombos[n];
          asXCombos[n] = asXCombos[n+1];
          asXCombos[n+1] = nSaved_Combo;
          bCycle_Finished = 0;
          n = 1;
       }
   }
}


//--- Now store Highest finishing Positions for each Driver from the asXCombos()

sHTML += "<BR>";
sHTML += "<TABLE class='HeadTable3' BORDER=1 ALIGN=CENTER WIDTH=600>";
sHTML += "<TR><TD class='HeadBox3' BGCOLOR='#A8D3FF' ALIGN=CENTER>Position " + nPassed_Position + " " + sGainLose + " Summary - " + sDriver_Name + "</TD></TR>";
sHTML += "</TABLE>";

nSub = 1;

while (nSub <= asXCombos.length-1) {

   if(sGainLose == "GAIN") {
      sSaved_Score = asXCombos[nSub].substring(2,5);

      for (n = 1;n <= nNo_Contenders;n++) {
          nHigh_Score[n] = 0;
      }

      nHigh_Score[nCurrDriver] = sSaved_Score;

      sHTML += "<BR>";
      sHTML += "<TABLE class='DetTable' BORDER=1 ALIGN=CENTER WIDTH=700>";
 
      while (sSaved_Score != 999 && sSaved_Score == asXCombos[nSub].substring(2,5) && nSub <= asXCombos.length-1) {

         for (m = 1;m <= nNo_Contenders;m++) {
   
             if(m != nCurrDriver){

                for (x = 5;x <= asXCombos[nSub].length;x = x + 5) {

                   if(Number(asXCombos[nSub].substring(x,x+2)) == Number(m)){

                      if(asXCombos[nSub].substring(x+2,x+5) > nHigh_Score[m]){
                         nHigh_Score[m] = asXCombos[nSub].substring(x+2,x+5);
                      } 
                   }
                }
             }
         }
         nSub = nSub + 1

         if(nSub > asXCombos.length-1){
           sSaved_Score = 999;
         }
      }
   }

   if(sGainLose == "LOSE") {
      sSaved_Score = asXCombos[nSub].substring(2,5);

      for (n = 1;n <= nNo_Contenders;n++) {
          nHigh_Score[n] = 0;
      }

      nHigh_Score[nCurrDriver] = sSaved_Score;

      sHTML += "<BR>";
      sHTML += "<TABLE class='DetTable' BORDER=1 ALIGN=CENTER WIDTH=700>";
 
      while (sSaved_Score != 999 && sSaved_Score == asXCombos[nSub].substring(2,5) && nSub <= asXCombos.length-1) {

         for (m = 1;m <= nNo_Contenders;m++) {
   
             if(m != nCurrDriver){

                for (x = 5;x <= asXCombos[nSub].length;x = x + 5) {

                   if(Number(asXCombos[nSub].substring(x,x+2)) == m){

                      if(asXCombos[nSub].substring(x+2,x+5) < nHigh_Score[m] || nHigh_Score[m] == 0 ) {
                         nHigh_Score[m] = asXCombos[nSub].substring(x+2,x+5);
                      } 
                   }
                }
             }
         }
         nSub = nSub + 1

         if(nSub > asXCombos.length-1){
           sSaved_Score = 999;
         }
      }
   }

   //--- Now having obtained all High Scores create Worded Summary

   for (n = 1;n <= nSavedContenders;n++) {
       sSummary_String[n] = "";
   }

   var nCount;
   nCount = 1;


   sSummary_String[nCount] = "if " + asSaveDriver[nCurrDriver] + " Finishes " + String(Get_Position(sChamp_Type, nHigh_Score[nCurrDriver]));

   for (n = 1;n <= nSavedContenders;n++) {
       if(n != nCurrDriver){
          nCount = nCount + 1;

          if (abDisplayDriver[n] == true) { 
             if(sGainLose == "GAIN") {
                sSummary_String[nCount] = asSaveDriver[n] + " must finish " + String(Get_Position(sChamp_Type, nHigh_Score[n])) + " or lower";
             } else {
                if (String(Get_Position(sChamp_Type, nHigh_Score[n])).substring(0,3) == "1st")  {
                   sSummary_String[nCount] = asSaveDriver[n] + " must finish " + String(Get_Position(sChamp_Type, nHigh_Score[n]));
                } else {
                   sSummary_String[nCount] = asSaveDriver[n] + " must finish " + String(Get_Position(sChamp_Type, nHigh_Score[n])) + " or higher";
                } 
             } 
           }
       }
   } 

   for (n = 1;n <= nSavedContenders;n++) {
       sHTML += "<TR>";
       sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour1 + "' WIDTH=700 ALIGN=LEFT>" + sSummary_String[n] + "</TD>";
       sHTML += "</TR>";
   }

   sHTML += "</TABLE>";
}

if(bDefaultedEndDriver == true){
   sHTML += "<BR><B><CENTER>These Results could only be calculated with finishing positions as low as Last in the A-Final - Please check manually for other possible outcomes !</CENTER></B><BR>";
}

sHTML3 = sHTML;

sHTML += "<BR>";
sHTML += "<TABLE class='HeadTable3' BORDER=0 ALIGN=CENTER WIDTH=600>";
sHTML += "<TR><TD class='HeadBox3' ALIGN=CENTER><A onclick='Show_Results()'><U>Click to see Matching Results Combinations</U></A></TD></TR>";
sHTML += "</TABLE>";

sHTML += "<BR><BR>";

}

//******************************************************************

function Position_Analysis_Header(){

document.getElementById("DivText2").innerHTML = "<P></P>";
document.getElementById("DivText3").innerHTML = "<P></P>";
document.getElementById("DivText4").innerHTML = "<P></P>";

var n;
var m;

//Check to see if(Simulation turned on && if(so, what the latest Simulated round is

bSimRound = false;
nCurrentSimRound = 0;
nChampRound = 0;
sChampRound = "";

if(bSimulationOn == true){
   for (Y = 1;Y <= nRounds;Y++) {
       if(Y > nCurrent_Round){
          for (X = 1;X <= nDriver_Records;X++) {
              if(anSimulatedScore[Y][X] != 0){
                 bSimRound = true;
                 nCurrentSimRound = Y;
                 X = nDriver_Records+1;
              }
          }
       }
   }
}

if(bSimulationOn == true && nCurrentSimRound > 0){
   sChampRound = String(nCurrentSimRound+1) + " (Simulated) ";
   nChampRound = String(nCurrentSimRound+1);
}else{
   sChampRound = String(nCurrent_Round+1);
   nChampRound = String(nCurrent_Round+1);
}

sHTML = "";
sHTML += "<TABLE class='HeadTable1' BORDER=1 ALIGN=CENTER WIDTH=600>";
sHTML += "<TR><TD class='HeadBox1' BGCOLOR='#A8D3FF' ALIGN=CENTER>Position Analysis for Round " + sChampRound + " - " + asTrackName[nChampRound] + "</TD></TR>";
sHTML += "</TABLE><BR>";

sHTML += "<TABLE BORDER=0 ALIGN=CENTER WIDTH=200>";
sHTML += "<TR><TD ALIGN=CENTER><A HREF='" + sInstructionsLinkPrefix + "#PosAnalysis' TARGET='_blank'><img src='../images/information.png'></A></TD></TR>";
sHTML += "</TABLE><BR>";

sHTML += "<TABLE class='HeadTable2' BORDER=1 ALIGN=CENTER WIDTH=400>";
sHTML += "<TR><TD class='HeadBox2' BGCOLOR='#A8D3FF' ALIGN=CENTER>Select whether to Gain or Lose Position</TD></TR>";
sHTML += "</TABLE><BR>";

sHTML += "<TABLE BORDER=0 ALIGN=CENTER WIDTH=300>";
sHTML += "<TR>";

//**************************************************************

sHTML += "<TD ALIGN=LEFT>";
sHTML += "<TABLE class='HeadTable2' BORDER=0><TR><TD class='HeadBox2' BGCOLOR='#A8D3FF' ALIGN=CENTER>Select Position</TD></TR>";

sHTML += "<TR><TD ALIGN=CENTER><SELECT NAME='ListPositions'>";

 for (n = 1;n <= nDriver_Records;n++) {
     sHTML += "<OPTION>" + n;
 }

sHTML += "</SELECT>";
sHTML += "</TD></TR></TABLE></TD>";

//**************************************************************

sHTML += "<TD ALIGN=LEFT>";
sHTML += "<TABLE class='HeadTable2' BORDER=0><TR><TD class='HeadBox2' BGCOLOR='#A8D3FF' ALIGN=CENTER>Select Gain/Lose</TD></TR>";

sHTML += "<TR><TD ALIGN=CENTER><SELECT NAME='ListGainLose'>";

sHTML += "<OPTION>GAIN";
sHTML += "<OPTION>LOSE";

sHTML += "</SELECT>";
sHTML += "</TD></TR></TABLE></TD>";

//******************************************************************

sHTML += "<TD ALIGN=LEFT>";
sHTML += "<INPUT TYPE='BUTTON' VALUE='Select' onclick='Select_Settings_Click()'>";
sHTML += "</TD>";

sHTML += "</TR></TABLE>";

document.getElementById("DivText2").innerHTML = sHTML;

}

//********************************************************************************************

function Reset_Positions_Arrays(){

//          var anSave_TopNTotal = new Array();
//          var anSave_FullTotal = new Array();
//          var anSave_ExtraPoints = new Array();

//          var anSave_Score = new Array();

//          var abSave_Contender = new Array();
//          var anSave_Diff = new Array();
//          var anSave_Max = new Array();
//          var anSave_Saved_Score = new Array();
//          var anSave_MinPoints = new Array();
//          va anSave_Position = new Array();

//          asSaveDriver = asDriver;
//          anSave_TopNTotal = anChamp_TopNTotal;
//          anSave_FullTotal = anChamp_FullTotal;
//          anSave_ExtraPoints = anChamp_ExtraPoints;

//          anSave_Score = anChamp_Score;

//          abSave_Contender = abChamp_Contender;
//          anSave_Diff = anChamp_Diff;
//          anSave_Max = anChamp_Max;
//          anSave_Saved_Score = anChamp_Saved_Score;
//          anSave_MinPoints = anChamp_MinPoints;
//          anSave_Position = anChamp_Position;


//Populate list of Position Contenders && set-up array

var nMaxRoundScore;

for (m=1; m <= nRounds;m++) {

    anChamp_Score[m] = new Array();
    abChamp_Include_Score[m] = new Array();

    for (n=1; n <= nDriver_Records; n++) {
        anChamp_Score[m][n] = 0;
        abChamp_Include_Score[m][n] = false;
    }
}

//Populate list of Championship Contenders && set-up array

nNo_Contenders = 0;

//CHECK for X in Grid Column. Need to Handle This with an ARRAY abChamp_Contender(X) = true/false

if(sChamp_Type.substring(0,6) == "SPRINT"){
   nMaxRoundScore = Number(sPoints_Array[1].substring(0,3)) + 100;
}

if(sChamp_Type.substring(0,9) == "ENDURANCE"){
   nMaxRoundScore = Number(sPoints_Array[1].substring(0,3));
}

if(sChamp_Type.substring(0,3) == "CAR"){
   nMaxRoundScore = Number(sPoints_Array[1].substring(0,3));
}


for (n = 1;n <= nDriver_Records;n++) {


    if(anTopNTotal[n] <= anTopNTotal[nPassed_Position] && (anTopNTotal[n] - anTopNTotal[nPassed_Position]) <= Number(nMaxRoundScore) && ((anTopNTotal[n] - anTopNTotal[nPassed_Position])*-1) <= Number(nMaxRoundScore)){

          nNo_Contenders = nNo_Contenders + 1;

          asChamp_Driver[nNo_Contenders] = asDriver[n];
          anChamp_TopNTotal[nNo_Contenders] = anTopNTotal[n];
          anChamp_FullTotal[nNo_Contenders] = anFullTotal[n];
          anChamp_ExtraPoints[nNo_Contenders] = anExtraPoints[n];

          for (m = 1;m <= nRounds;m++) {
              anChamp_Score[m][nNo_Contenders] = anScore[m][n];
          } 

          abChamp_Contender[nNo_Contenders] = true;
          anChamp_Diff[nNo_Contenders] = 0;
          anChamp_Max[nNo_Contenders] = 0;
          anChamp_Saved_Score[nNo_Contenders] = 0;
          anChamp_MinPoints[nNo_Contenders] = 0;
          //anChamp_Position[nNo_Contenders] = n;

          asSaveDriver[nNo_Contenders] = asDriver[n];
    }
} 

nSavedContenders = nNo_Contenders;

}

//********************************************************************************************c
function Select_Settings_Click(){

var n;
var nIndex;
var sSelected;

nIndex = document.Form1.ListPositions.selectedIndex;
sSelected = document.Form1.ListPositions.options[nIndex].text;
nSelected_Position = sSelected;

nIndex = document.Form1.ListGainLose.selectedIndex;
sSelected = document.Form1.ListGainLose.options[nIndex].text;
sSelected_GainLose = sSelected;

nPassed_Position = nSelected_Position;
sGainLose = sSelected_GainLose;

Positions_Init();

}

//******************************************************************

function Championship_Summary(){

document.getElementById("DivText2").innerHTML = "<P></P>";
document.getElementById("DivText3").innerHTML = "<P></P>";
document.getElementById("DivText4").innerHTML = "<P></P>";

//***********************************************************************************
// Championship Summary Table
//***********************************************************************************

var sChampSummary_String = "";
var nLoop = 0;
var bTableWritten = false;
var sHTML2 = "";

nSwitch = 0;

bTableWritten = true;

sHTML2 += "<TABLE class='HeadTable1' BORDER=1 ALIGN=CENTER WIDTH=400>";
sHTML2 += "<TR><TD class='HeadBox1' BGCOLOR='#A8D3FF' ALIGN=CENTER>" + sChamp_Name + " - " + sChamp_Year + "</TD></TR>";
sHTML2 += "</TABLE><BR>";

sHTML2 += "<TABLE class='HeadTable2' WIDTH=500 BORDER=1 ALIGN=CENTER>";
sHTML2 += "<TR><TD class='HeadBox2' BGCOLOR='#E8F3FF' ALIGN=CENTER>Championship Summary After Round " + nCurrent_Round + " - " + asTrackName[nCurrent_Round] + " " + asTrackDate[nCurrent_Round] + "</TD></TR>";
sHTML2 += "</TABLE>";
sHTML2 += "<BR>";

sHTML2 += "<TABLE class='DetTable' BORDER='1' ALIGN='Center' BGCOLOR='#FFFFFF' WIDTH=1200>";
sHTML2 += "<TR>";

sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=300 ALIGN=CENTER>Entrant Name</TD>";

if (bChamp_Calc_Summary_BCF) {
	sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>BCF</TD>";
}

sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Races Entered</TD>";
sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Main Wins</TD>";
sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Main 2nds</TD>";
sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Main 3rds</TD>";
sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Main Podiums</TD>";

if (bChamp_Calc_Summary_ExtraRaces) {
	sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Qualy Wins</TD>";
	sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Qualy 2nds</TD>";
	sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Qualy 3rds</TD>";
	sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Qualy Podiums</TD>";
}

sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Pole Positions</TD>";
sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Fastest Laps</TD>";
sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Lap Records</TD>";
sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>DNFs</TD>";
sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Total Passes</TD>";
sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Best Improver 1sts</TD>";
sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Best Improver 2nds</TD>";
sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Best Improver 3rds</TD>";
sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Best Improver Total</TD>";
sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=40 ALIGN=CENTER>Total Honours</TD>";
sHTML2 += "</TR>";


for (var nLoop1 = 1;nLoop1 <= nDriver_Records;nLoop1++) {

	for (nLoop = 1;nLoop <= asChampSummary.length-1;nLoop++) {

	    sChampSummary_String = asChampSummary[nLoop];

	    var nField = -1;
    	    var sField = new Array();
    	    var n = 0;
    	    var sChar = "";
    	    var sTempField = "";

    	    while (n < sChampSummary_String.length) {

       	    	sTempField = "";

       		sChar = sChampSummary_String.substring(n, n+1);

       		while (n < sChampSummary_String.length && sChar != "|") {
             		sTempField += sChar;
             		n++;
             		if (n < sChampSummary_String.length) {
                		sChar = sChampSummary_String.substring(n, n+1);
             		} 
       		}

       		nField++;

       		sField[nField] = sTempField;
    
       		sChar = sChampSummary_String.substring(n, n+1);

       		while (n < sChampSummary_String.length && sChar == "|") {
             		n++;
             		if (n < sChampSummary_String.length) {
                		sChar = sChampSummary_String.substring(n, n+1);
             		}
       		}
    	     }

	  if(sField[0] == asDriver[nLoop1]) {

    	  	if(nSwitch == 0){
       			sColour = sColour1;
       			nSwitch = 1;
    	  	}else{
       			sColour = sColour2;
       			nSwitch = 0;
    	  	}



    	  	sHTML2 += "<TR>";
    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=300 ALIGN=LEFT><U><A onclick='Find_Driver(" + "\"" + sField[0] + "\"" + ")'>" + sField[0] + "</A></U></TD>";

		if (bChamp_Calc_Summary_BCF) {
    	  		sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + sField[1] + "</TD>";
		}

    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + sField[2] + "</TD>";
    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[3] != 0 ? sField[3] : "----") + "</TD>";
    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[4] != 0 ? sField[4] : "----") + "</TD>";
    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[5] != 0 ? sField[5] : "----") + "</TD>";
    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[6] != 0 ? "<b>" : "") + sField[6] + (sField[6] != 0 ? "</b>" : "") + "</TD>";

		if (bChamp_Calc_Summary_ExtraRaces) {
    	  		sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[7] != 0 ? sField[7] : "----") + "</TD>";
    	  		sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[8] != 0 ? sField[8] : "----") + "</TD>";
    	  		sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[9] != 0 ? sField[9] : "----") + "</TD>";
    	  		sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[10] != 0 ? "<b>" : "") + sField[10] + (sField[10] != 0 ? "</b>" : "") + "</TD>";
		}

    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[11] != 0 ? sField[11] : "----") + "</TD>";
    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[12] != 0 ? sField[12] : "----") + "</TD>";
    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[13] != 0 ? sField[13] : "----") + "</TD>";
    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[14] != 0 ? sField[14] : "----") + "</TD>";
    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + sField[15] + "</TD>";
    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[16] != 0 ? sField[16] : "----") + "</TD>";
    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[17] != 0 ? sField[17] : "----") + "</TD>";
    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[18] != 0 ? sField[18] : "----") + "</TD>";
    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[19] != 0 ? "<b>" : "") + sField[19] + (sField[19] != 0 ? "</b>" : "") + "</TD>";
    	  	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=40 ALIGN=CENTER>" + (sField[20] != 0 ? "<b>" : "") + sField[20] + (sField[20] != 0 ? "</b>" : "") + "</TD>";
    	  	sHTML2 += "</TR>";
	}

     }
}

if (bTableWritten = true) {
	sHTML2 += "</TABLE>";
}

if(asSummary_Notes.length > 0) {
	sHTML2 += "<br><TABLE BORDER=0 ALIGN=CENTER WIDTH=1200>";
	sHTML2 += "<TR><TD><B>Please Note :</B></TD></TR>";
	

	for (var n = 1;n <= asSummary_Notes.length-1;n++) {
		sHTML2 += "<TR><TD>" + asSummary_Notes[n] + "</TD></TR>";
	}
	sHTML2 += "</TABLE>";
}

//*****************************************************
//     document.getElementById("DivText2").innerHTML = sHTML + sHTML2;


sHTML2 += "<BR><BR><TABLE class='HeadTable2' WIDTH=500 BORDER=1 ALIGN=CENTER>";
sHTML2 += "<TR><TD class='HeadBox2' BGCOLOR='#E8F3FF' ALIGN=CENTER>Race Entry Analysis</TD></TR>";
sHTML2 += "</TABLE>";
sHTML2 += "<BR>";

sHTML2 += "<TABLE class='DetTable' BORDER='1' ALIGN='Center' BGCOLOR='#FFFFFF' WIDTH=900>";
sHTML2 += "<TR>";

for (Y = 1;Y <= nRounds;Y++) {
         var bNoRaceAnalysisDetails = true;

         if(sChamp_Type.substring(0,6) == "SPRINT"){
	   if(asRace_Analysis_Page[Y] != ""){
               bNoRaceAnalysisDetails = false;
	    }
	 }
	
	 if(sChamp_Type.substring(0,9) == "ENDURANCE"){
	    if(asRace_Analysis_Page[Y] != ""){
	       bNoRaceAnalysisDetails = false;
	    }
	 }
	
	 if(sChamp_Type.substring(0,3) == "CAR"){
	    if(asRace_Analysis_Page[Y] != ""){
	       bNoRaceAnalysisDetails = false;
	    }
	 }

         if (bNoRaceAnalysisDetails == false) {
             sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=30 VALIGN=BOTTOM ALIGN=LEFT><TABLE WIDTH=30 CELLSPACING=0 BORDER=0><TR><TD><IMG ";
             if(sChamp_Type.substring(0,6) == "SPRINT"){
                sHTML2 += "SRC='" + "../images/" + asTrackTabName[Y] + "' WIDTH=22 HEIGHT=95></TD><TD VALIGN=BOTTOM><A HREF='" + asRace_Analysis_Page[Y] + "' TARGET='_blank'><IMG ALT='Click for Race Analysis' SRC='../images/RaceAnalysis.gif' WIDTH=12 HEIGHT=95></A></TD></TR></TABLE></TD>";
             }else if(sChamp_Type.substring(0,9) == "ENDURANCE"){
                sHTML2 += "SRC='" + "../images/" + asTrackTabName[Y] + "' WIDTH=22 HEIGHT=95></TD><TD VALIGN=BOTTOM><A HREF='" + asRace_Analysis_Page[Y] + "' TARGET='_blank'><IMG ALT='Click for Race Analysis' SRC='../images/RaceAnalysis.gif' WIDTH=12 HEIGHT=95></A></TD></TR></TABLE></TD>";
             }else if(sChamp_Type.substring(0,3) == "CAR"){  
                sHTML2 += "SRC='" + "../images/" + asTrackTabName[Y] + "' WIDTH=22 HEIGHT=95></TD><TD VALIGN=BOTTOM><A HREF='" + asRace_Analysis_Page[Y] + "' TARGET='_blank'><IMG ALT='Click for Race Analysis' SRC='../images/RaceAnalysis.gif' WIDTH=12 HEIGHT=95></A></TD></TR></TABLE></TD>";
             }
         } else {
              sHTML2 += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=30 VALIGN=BOTTOM ALIGN=CENTER><IMG ";
              if(sChamp_Type.substring(0,6) == "SPRINT"){
                sHTML2 += "SRC='" + "../images/" + asTrackTabName[Y] + "' WIDTH=22 HEIGHT=95></TD>";
              }else if(sChamp_Type.substring(0,9) == "ENDURANCE"){
                sHTML2 += "SRC='" + "../images/" + asTrackTabName[Y] + "' WIDTH=22 HEIGHT=95></TD>";
              }else if(sChamp_Type.substring(0,3) == "CAR"){  
                sHTML2 += "SRC='" + "../images/" + asTrackTabName[Y] + "' WIDTH=22 HEIGHT=95></TD>";
              }
         }
}

sHTML2 += "</TR>";

sHTML2 += "<TR>";

for (Y = 1;Y <= nRounds;Y++) {
	sHTML2 += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=30 ALIGN=CENTER>" + anRace_Entries[Y] + "</TD>";
}


sHTML2 += "</TR>";
sHTML2 += "</TABLE>";

document.getElementById("DivText2").innerHTML = sHTML2;

}

//******************************************************************

function Championship_Graph(){

document.getElementById("DivText2").innerHTML = "<P></P>";
document.getElementById("DivText3").innerHTML = "<P></P>";
document.getElementById("DivText4").innerHTML = "<P></P>";

//***********************************************************************************
// Display Championship Graph Applet
//***********************************************************************************

var sHTML = "";
var sHTML2 = "";
var nWidth = 625+(nRounds*25);
var nHeight = 100+(nDriver_Records*20);
nHeight = nHeight > 2000?2000:nHeight;

sHTML2 += "<TABLE class='HeadTable1' BORDER=1 ALIGN=CENTER WIDTH=400>";
sHTML2 += "<TR><TD class='HeadBox1' BGCOLOR='#A8D3FF' ALIGN=CENTER>" + sChamp_Name + " - " + sChamp_Year + "</TD></TR>";
sHTML2 += "</TABLE><BR>";

sHTML2 += "<TABLE class='HeadTable2' WIDTH=500 BORDER=1 ALIGN=CENTER>";
sHTML2 += "<TR><TD class='HeadBox2' BGCOLOR='#E8F3FF' ALIGN=CENTER>Championship Graph After Round " + nCurrent_Round + " - " + asTrackName[nCurrent_Round] + " " + asTrackDate[nCurrent_Round] + "</TD></TR>";
sHTML2 += "</TABLE>";
sHTML2 += "<BR>";

sHTML2 += "<TABLE class='DetTable' BORDER='1' ALIGN='Center' BGCOLOR='#FFFFFF' WIDTH=1200>";
sHTML2 += "<TR>";

sHTML2 += "</TR>";
sHTML2 += "</TABLE>";

document.getElementById("DivText2").innerHTML = sHTML2;

sHTML = "<TABLE class='DetTable' BORDER='0' WIDTH='" + nWidth + "' ALIGN=CENTER>";
sHTML += "<TR><TD>";
sHTML += "<DIV style='height:" + nHeight + ";width:" + nWidth + "; overflow:auto' ALIGN='CENTER'>";
sHTML += "<TABLE BORDER='0'>";
sHTML += "<TR>";
sHTML += "<TD>";

sHTML += "<APPLET CODE='ChampAnalysisGraph2.class' height=" + nHeight + " width=" + nWidth + ">";
sHTML += "<PARAM name='NoRounds' value=" + nRounds + ">";
sHTML += "<PARAM name='CurrentRound' value=" + nCurrent_Round + ">";
sHTML += "<PARAM name='NoDrivers' value=" + nDriver_Records + ">";
sHTML += "<PARAM name='NoDroppedScores' value=" + (nRounds - nScores_To_Include) + ">";
sHTML += "<PARAM name='WindowHeight' value=" + nHeight + ">";
sHTML += "<PARAM name='WindowWidth' value=" + nWidth + ">";

for (n = 1;n <= nDriver_Records;n++) {
	sHTML += "<PARAM name='DriverData_" + (n-1) + "' value='" + asDriver[n];

	for (m = 1;m <= nRounds;m++) {
		sHTML += "," + anScore[m][n];
        }  

	sHTML += "," + anExtraPoints[n] + "'>";
}

sHTML += "</APPLET>";

sHTML += "</TD>";
sHTML += "</TR>";
sHTML += "</TABLE>";
sHTML += "</DIV>";

sHTML += "</TD></TR>";
sHTML += "</TABLE>";

document.getElementById("DivText3").innerHTML = sHTML;

}
