//**************************************************************************************
//
// Champ Calc / Champ Calc - Race Analysis
//
// Copyright Tony Barson 2004 - 2006 / Tickled Pink IT
//
// 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
//
//**************************************************************************************

var anDriverFastestLaps = new Array();
var anDriverPoles = new Array();
var anDriverWinTimes = new Array();
var asDriver_BestFinish = new Array();

//******************************************************************

function Get_Position(sChamp_Type, nScore){

var n;
var nTemp_Score;
var sTemp_Pos

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 if(nScore > 0){
      sTemp_Pos = sPoints_Array[sPoints_Array.length-1].substring(3,8);
      return "C-Final " + sTemp_Pos;
   }
}else{
   nTemp_Score = nScore;
}

nTemp_Score = nTemp_Score;

if(sChamp_Type.substring(0,6) == "SPRINT"){
   for (n = 1;n <= sPoints_Array.length-1;n++) {

       if(nTemp_Score == sPoints_Array[n].substring(0,3)){
          sTemp_Pos = sPoints_Array[n].substring(3,8);
          break;
       }
   }
}

if(sChamp_Type.substring(0,9) == "ENDURANCE"){
   for (n = 1;n <= sPoints_Array.length-1;n++) {
       if(nTemp_Score == sPoints_Array[n].substring(0,3)){
          sTemp_Pos = sPoints_Array[n].substring(3,8);
          break;
       }
   }
}

if(sChamp_Type.substring(0,3) == "CAR"){
   for (n = 1;n <= sPoints_Array.length-1;n++) {
       if(nTemp_Score >= sPoints_Array[n].substring(0,3)){
          sTemp_Pos = sPoints_Array[n].substring(3,8);
          break;
       }
   }
}

if(sChamp_Type.substring(0,6) == "SPRINT"){
   if(nScore >= 100){
      return "A-Final " + sTemp_Pos;
   }else if(nScore >= 70){
      return "B-Final " + sTemp_Pos;
   }else{
      if(nScore != 0){
         return "C-Final " + sTemp_Pos;
      }else{
         return "-----";
      }
   }
}else{
   if(nScore != 0){
      return sTemp_Pos;
   }else{
      return "-----";
   }
}

}

//********************************************************************************************

function Driver_Analysis(nDriver){

var n;
var bNoFileDetails;
var bNoRaceAnalysisDetails;
var sSimRounds;
var sHTML;
var sPhoto_File_Name;
var bCycle_Finished ;
var nSaved_Score ;

var nScores = new Array();

sHTML = "";
sSimRounds = "";
sSaved_Driver = asDriver[nDriver];

document.getElementById("DivText2").innerHTML = "<P></P>";
document.getElementById("DivText3").innerHTML = "<P></P>";
document.getElementById("DivText4").innerHTML = "<P></P>";

sHTML += "<TABLE class='HeadTable1' BORDER=1 ALIGN=CENTER WIDTH=400>";
sHTML += "<TR><TD class='HeadBox1' BGCOLOR='#A8D3FF' ALIGN=CENTER>Driver/Team Details</TD></TR>";
sHTML += "</TABLE><BR>";

sHTML += "<TABLE class='StatsTable' BORDER=1 ALIGN=CENTER WIDTH=700>";

sHTML += "<TR><TD class='StatsCatBox' BGCOLOR='#A8D3FF'>Entrant</TD><TD class='StatsDetBox1' BGCOLOR='#D2E8FF'>" + asDriver[nDriver] + "</TD></TR>";
sHTML += "<TR><TD class='StatsCatBox' BGCOLOR='#A8D3FF'>Championship</TD><TD class='StatsDetBox2' BGCOLOR='#E8F3FF'>" + sChamp_Name + " - " + sChamp_Year + "</TD></TR>";
sHTML += "<TR><TD class='StatsCatBox' BGCOLOR='#A8D3FF'>Position</TD><TD class='StatsDetBox2' BGCOLOR='#E8F3FF'>" + nDriver + " of: " + nDriver_Records + "</TD></TR>";
sHTML += "<TR><TD class='StatsCatBox' BGCOLOR='#A8D3FF'>Points</TD><TD class='StatsDetBox1' BGCOLOR='#D2E8FF'>" + anTopNTotal[nDriver] + " (Best " + nScores_To_Include + " Finishes)</TD></TR>";

nNoNonZeros = 0;

for (m = 1;m <= nRounds;m++) {
    nScores[m] = anScore[m][nDriver];
    if(anScore[m][nDriver] > 0){
       nNoNonZeros = nNoNonZeros + 1;
    }
}

if(nNoNonZeros != 0){
   if(nNoNonZeros < nScores_To_Include){
      sScore_String = String(anTopNTotal[nDriver]/nNoNonZeros);
   }else{
      sScore_String = String(anTopNTotal[nDriver]/nScores_To_Include);
   }
}else{
   sScore_String = "0";
}

nPos = sScore_String.indexOf(".");
if(nPos != -1){
   sScore_String = sScore_String.substring(0,nPos);
}

Xstring = Get_Avg_Score(sChamp_Type, Number(sScore_String));

sHTML += "<TR><TD class='StatsCatBox' BGCOLOR='#A8D3FF'>Average Score/Position</TD><TD class='StatsDetBox1' BGCOLOR='#D2E8FF'>" + sScore_String + " (" + Get_Position(sChamp_Type, Number(Xstring)) + ")</TD></TR>"

// Get Next minimum Scoring Position

if(nNoNonZeros >= nScores_To_Include && nRounds_Left > 0){

   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;
          }
      }
   }

   if((sChamp_Type.substring(0,6) == "SPRINT" && nScores[nScores_To_Include] >= 40) || sChamp_Type.substring(0,6) != "SPRINT" ){
      sMinScoreString = Minimum_Scoring_Score(sChamp_Type, nScores[nScores_To_Include]);
      nMinPoints = sMinScoreString.substring(0,3);
      sMinResult = sMinScoreString.substring(3,sMinScoreString.length);

      if((nMinPoints - nScores[nScores_To_Include]) == 1){
         sText = Number(nMinPoints) + ((Number(nMinPoints) == 1) ? " Point":" Points") + " - " + sMinResult + " (+" + (nMinPoints - nScores[nScores_To_Include]) + " Point)";
      }else{
         sText = Number(nMinPoints) + ((Number(nMinPoints) == 1) ? " Point":" Points") + " - " + sMinResult + " (+" + (nMinPoints - nScores[nScores_To_Include]) + " Points)";
      }

      sHTML += "<TR><TD class='StatsCatBox' BGCOLOR='#A8D3FF'>Next Minimum Scoring Result</TD><TD class='StatsDetBox1' BGCOLOR='#D2E8FF'>" + sText + "</TD></TR>";
   }
}

if(anChamp_Contender[nDriver] == "Y"){
   sHTML += "<TR><TD class='StatsCatBox' BGCOLOR='#A8D3FF'>Championship Contender?</TD><TD class='StatsDetBox2' BGCOLOR='#E8F3FF'>Yes</TD></TR>";
}else{
   sHTML += "<TR><TD class='StatsCatBox' BGCOLOR='#A8D3FF'>Championship Contender?</TD><TD class='StatsDetBox2' BGCOLOR='#E8F3FF'>No</TD></TR>";
}

//**************************************************

var sWebPage_Link = "";
var sTemp_WebPage_Link = "";

for (n = 1;n <= asDriver_WebPage.length-1;n++) {

    nPos = asDriver_WebPage[n].indexOf("||");

    if (nPos != -1) {
        sTemp_WebPage_Link = asDriver_WebPage[n].substring(0, nPos);
    } else {
	sTemp_WebPage_Link = "?????";
    }

    if(asDriver[nDriver].substring(0, sTemp_WebPage_Link.length).toUpperCase() == sTemp_WebPage_Link.toUpperCase()){
       nPos = asDriver_WebPage[n].indexOf("||");

       if (nPos != -1) {
          nPos = nPos + 2
          sWebPage_Link = asDriver_WebPage[n].substring(nPos,asDriver_WebPage[n].length);
       } else {
             sWebPage_Link = "";
       }


       if(sWebPage_Link != ""){
          sHTML += "<TR><TD class='StatsCatBox' BGCOLOR='#A8D3FF'>Web Site</TD><TD class='StatsDetBox2' BGCOLOR='#E8F3FF'><A HREF='" + sWebPage_Link + "' TARGET='_blank'><U>Click Here<U></A></TD></TR>";
       }
       n = asDriver_WebPage.length+1;
    }
}


//**************************************************

var nNoFirst = 0;
var nNoSecond = 0;
var nNoThird = 0;

for (nSub = 1;nSub <= nRounds;nSub++) {

      if(sChamp_Type.substring(0,6) == "SPRINT"){
         if (Get_Position(sChamp_Type, anScore[nSub][nDriver]).substring(0,11) == "A-Final 1st") {
            nNoFirst = nNoFirst + 1;
         }
      } else {
         if (Get_Position(sChamp_Type, anScore[nSub][nDriver]).substring(0,3) == "1st") {
            nNoFirst = nNoFirst + 1;
         }
      }

      if(sChamp_Type.substring(0,6) == "SPRINT"){
         if (Get_Position(sChamp_Type, anScore[nSub][nDriver]).substring(0,11) == "A-Final 2nd") {
            nNoSecond = nNoSecond + 1;
         }
      } else {
         if (Get_Position(sChamp_Type, anScore[nSub][nDriver]).substring(0,3) == "2nd") {
            nNoSecond = nNoSecond + 1;
         }
      }


      if(sChamp_Type.substring(0,6) == "SPRINT"){
         if (Get_Position(sChamp_Type, anScore[nSub][nDriver]).substring(0,11) == "A-Final 3rd") {
            nNoThird = nNoThird + 1;
         }
      } else {
         if (Get_Position(sChamp_Type, anScore[nSub][nDriver]).substring(0,3) == "3rd") {
            nNoThird = nNoThird + 1;
         }
      }

}

if (nNoFirst != 0 || nNoSecond != 0 || nNoThird != 0 ) {
      sHTML += "<TR><TD class='StatsCatBox' BGCOLOR='#A8D3FF'>Podium Results</TD>";
      sHTML += "<TD class='StatsDetBox1' BGCOLOR='#D2E8FF'>";
      sHTML += "<TABLE WIDTH=390 BORDER=0 ALIGN=LEFT>";
      sHTML += "<TR>";

      if (nNoFirst == 1) {
         sHTML += "<TD class='StatsDetBox1' BGCOLOR='#D2E8FF' WIDTH=130 ALIGN=CENTER><IMG SRC='Gold_Cup.bmp'></IMG>1st -" + String(nNoFirst) + " time</TD>";
      } else {
         sHTML += "<TD class='StatsDetBox1' BGCOLOR='#D2E8FF' WIDTH=130 ALIGN=CENTER><IMG SRC='Gold_Cup.bmp'></IMG>1st -" + String(nNoFirst) + " times</TD>";
      }

      if (nNoSecond == 1) {
         sHTML += "<TD class='StatsDetBox1' BGCOLOR='#D2E8FF' WIDTH=130 ALIGN=CENTER><IMG SRC='Silver_Cup.bmp'></IMG>2nd -" + String(nNoSecond) + " time</TD>";
      } else {
         sHTML += "<TD class='StatsDetBox1' BGCOLOR='#D2E8FF' WIDTH=130 ALIGN=CENTER><IMG SRC='Silver_Cup.bmp'></IMG>2nd -" + String(nNoSecond) + " times</TD>";
      }

      if (nNoThird == 1) {
         sHTML += "<TD class='StatsDetBox1' BGCOLOR='#D2E8FF' WIDTH=130 ALIGN=CENTER><IMG SRC='Bronze_Cup.bmp'></IMG>3rd -" + String(nNoThird) + " time</TD>";
      } else {
         sHTML += "<TD class='StatsDetBox1' BGCOLOR='#D2E8FF' WIDTH=130 ALIGN=CENTER><IMG SRC='Bronze_Cup.bmp'></IMG>3rd -" + String(nNoThird) + " times</TD>";
      }

      sHTML += "</TD>";
      sHTML += "</TR>";
      sHTML += "</TABLE>";
      sHTML += "</TD>";
      sHTML += "</TR>";
}

var nTempDriverPoles = 0;
var nTempDriverFastestLaps = 0;

for (n = 1;n <= anDriverPoles.length-1;n++) {

    nPos = anDriverPoles[n].indexOf("||");

    if (nPos != -1) {
        nTempDriverPoles = anDriverPoles[n].substring(0, nPos);
    } else {
	nTempDriverPoles = "?????";
    }

    if(asDriver[nDriver].substring(0, nTempDriverPoles.length).toUpperCase() == nTempDriverPoles.toUpperCase()){
       nPos = anDriverPoles[n].indexOf("||");

       if (nPos != -1) {
          nPos = nPos + 2
          nTempDriverPoles = anDriverPoles[n].substring(nPos,anDriverPoles[n].length);
       } else {
             nTempDriverPoles = "";
       }


       if(nTempDriverPoles != 0){
          sHTML += "<TR><TD class='StatsCatBox' BGCOLOR='#A8D3FF'>Pole Positions</TD>";
          sHTML += "<TD class='StatsDetBox1' BGCOLOR='#D2E8FF'>";
          sHTML += "<TABLE WIDTH=390 BORDER=0 ALIGN=LEFT>";
          sHTML += "<TR>";

          sHTML += "<TD class='StatsDetBox1' BGCOLOR='#D2E8FF' WIDTH=130 ALIGN=LEFT>" + nTempDriverPoles + "</TD>";

          sHTML += "</TD>";
          sHTML += "</TR>";
          sHTML += "</TABLE>";
          sHTML += "</TD>";
          sHTML += "</TR>";
       }
       n = anDriverPoles.length+1;
    }
}

for (n = 1;n <= anDriverFastestLaps.length-1;n++) {

    nPos = anDriverFastestLaps[n].indexOf("||");

    if (nPos != -1) {
        nTempDriverFastestLaps = anDriverFastestLaps[n].substring(0, nPos);
    } else {
	nTempDriverFastestLaps = "?????";
    }

    if(asDriver[nDriver].substring(0, nTempDriverFastestLaps.length).toUpperCase() == nTempDriverFastestLaps.toUpperCase()){
       nPos = anDriverFastestLaps[n].indexOf("||");

       if (nPos != -1) {
          nPos = nPos + 2
          nTempDriverFastestLaps = anDriverFastestLaps[n].substring(nPos,anDriverFastestLaps[n].length);
       } else {
             nTempDriverFastestLaps = "";
       }


       if(nTempDriverFastestLaps != 0){
          sHTML += "<TR><TD class='StatsCatBox' BGCOLOR='#A8D3FF'>Fastest Laps</TD>";
          sHTML += "<TD class='StatsDetBox1' BGCOLOR='#D2E8FF'>";
          sHTML += "<TABLE WIDTH=390 BORDER=0 ALIGN=LEFT>";
          sHTML += "<TR>";

          sHTML += "<TD class='StatsDetBox1' BGCOLOR='#D2E8FF' WIDTH=130 ALIGN=LEFT>" + nTempDriverFastestLaps + "</TD>";

          sHTML += "</TD>";
          sHTML += "</TR>";
          sHTML += "</TABLE>";
          sHTML += "</TD>";
          sHTML += "</TR>";
       }
       n = anDriverFastestLaps.length+1;
    }
}


//**************************************************

sHTML += "</TABLE>";

sPhoto_File_Name = "";

for (n = 1;n <= asDriver_Photo.length-1;n++) {

    nPos = asDriver_Photo[n].indexOf("||");

    if (nPos != -1) {
        sPhoto_Driver = asDriver_Photo[n].substring(0, nPos);
    } else {
	sPhoto_Driver = "?????";
    }

    if(asDriver[nDriver].substring(0, sPhoto_Driver.length).toUpperCase() == sPhoto_Driver.toUpperCase()){
    // if(asDriver_Photo[n].substring(0,asDriver[nDriver].length) == asDriver[nDriver]){
       nPos = asDriver_Photo[n].indexOf("||");

       if (nPos != -1) {
          nPos = nPos + 2
          sPhoto_File_Name = asDriver_Photo[n].substring(nPos,asDriver_Photo[n].length);
       } else {
             sPhoto_File_Name = "";
       }

       if(sPhoto_File_Name != ""){
          sHTML += "<TABLE BORDER='0' ALIGN='Center' BGCOLOR='#FFFFFF' CELLPADDING='1' CELLSPACING='3' WIDTH=250>";
          sHTML += "<TR>";
          sHTML += "<TD WIDTH=100%><CENTER><IMG ALT='" + asDriver[nDriver] + "' BORDER=1 SRC='" + sPhoto_File_Name + "' </CENTER></TD>";
          sHTML += "</TR>";
          sHTML += "</TABLE>";
       }
       n = asDriver_Photo.length+1;
    }
}

sHTML += "<BR>";

sHTML += "<TABLE class='HeadTable2' BORDER=1 ALIGN=CENTER WIDTH=400>";
sHTML += "<TR><TD class='HeadBox2' BGCOLOR='#A8D3FF' ALIGN=CENTER>Round Analysis</TD></TR>";
sHTML += "</TABLE><BR>";

sHTML += "<TABLE class='DetTable' BORDER='1' ALIGN='Center' BGCOLOR='#FFFFFF' WIDTH=700>";
sHTML += "<TR>"

sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=50 ALIGN=CENTER>Round</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=50 ALIGN=CENTER>Date</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=150 ALIGN=LEFT>Venue</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=100 ALIGN=LEFT>Result</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=50 ALIGN=LEFT>Points</TD>"

bNoRaceAnalysisDetails = true;

for (nSub = 1;nSub <= nRounds;nSub++) {
    if(sChamp_Type.substring(0,6) == "SPRINT"){
       if(asRace_Analysis_Page[nSub] != ""){
          sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=100 ALIGN=LEFT>Race Analysis</TD>";
          bNoRaceAnalysisDetails = false;
          nSub = nRounds+1;
       }
    }

    if(sChamp_Type.substring(0,9) == "ENDURANCE"){
       if(asRace_Analysis_Page[nSub] != ""){
          sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=100 ALIGN=LEFT>Race Analysis</TD>";
          bNoRaceAnalysisDetails = false;
          nSub = nRounds+1;
       }
    }

    if(sChamp_Type.substring(0,3) == "CAR"){
       if(asRace_Analysis_Page[nSub] != ""){
          sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF' WIDTH=100 ALIGN=LEFT>Race Analysis</TD>";
          bNoRaceAnalysisDetails = false;
          nSub = nRounds+1;
       }
    }
} 

sHTML += "</TR>";

nSwitch = 0;

for (nSub = 1;nSub <= nRounds;nSub++) {

    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>" + nSub + "</TD>";

    if(sChamp_Type == "SPRINT"){
       sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=50 ALIGN=CENTER>" + asTrackDate[nSub] + "</TD>";
       sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=150 ALIGN=LEFT><A onclick='Display_Results(" + nSub + "," + "\"" + "A" + "\"" + ")'><U>" + asTrackName[nSub] + "</U></A></TD>";
    }else if(sChamp_Type == "ENDURANCE"){
           sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=50 ALIGN=CENTER>" + asTrackDate[nSub] + "</TD>";
           sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=150 ALIGN=LEFT><A onclick='Display_Results(" + nSub + "," + "\"" + "A" + "\"" + ")'><U>" + asTrackName[nSub] + "</U></A></TD>";
    }else if(sChamp_Type == "CAR"){
           sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=50 ALIGN=CENTER>" + asTrackDate[nSub] + "</TD>";
           sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=150 ALIGN=LEFT><A onclick='Display_Results(" + nSub + "," + "\"" + "A" + "\"" + ")'><U>" + asTrackName[nSub] + "</U></A></TD>";
    } 

    if(abInclude_Score[nSub][nDriver] == false){
       sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=100 ALIGN=LEFT><IMG ALT='Dropped Score' SRC='CHEQFLAG.bmp'>" + Get_Position(sChamp_Type, anScore[nSub][nDriver]) + "</TD>";
    }else{
       sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=100 ALIGN=LEFT>" + Get_Position(sChamp_Type, anScore[nSub][nDriver]) + "</TD>";
    }

    if(nSub > nCurrent_Round && bSimulationOn == true){
       bSimRound = false;
       for (nLoop = 1;nLoop <= nDriver_Records;nLoop++) {
           if(anSimulatedScore[nSub][nLoop] > 0){
              bSimRound = true;
              nLoop = nDriver_Records+1;
           }
       }

       if(bSimRound == true){
          sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=50 ALIGN=LEFT><IMG ALT='Simulated Round' SRC='SIM.bmp'>" + anScore[nSub][nDriver] + "</TD>";
          if(sSimRounds == ""){
             sSimRounds = String(nSub);
          }else{
             sSimRounds = sSimRounds + "," + String(nSub);
          }
       }else{
          sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=50 ALIGN=LEFT>" + anScore[nSub][nDriver] + "</TD>";
       }
    }else{
       sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=50 ALIGN=LEFT>" + anScore[nSub][nDriver] + "</TD>";
    }

    if(bNoRaceAnalysisDetails == false){
       if(sChamp_Type.substring(0,6) == "SPRINT"){
          if(asRace_Analysis_Page[nSub] != ""){
             sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=100 ALIGN=LEFT><A HREF='" + asRace_Analysis_Page[nSub] + "' TARGET='_blank'>Click Here</A></TD>";
          }else{
             sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=100 ALIGN=LEFT>-</TD>";
          }
       }

       if(sChamp_Type.substring(0,9) == "ENDURANCE"){
          if(asRace_Analysis_Page[nSub] != ""){
             sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=100 ALIGN=LEFT><A HREF='" + asRace_Analysis_Page[nSub] + "' TARGET='_blank'>Click Here</A></TD>";
          }else{
             sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=100 ALIGN=LEFT>-</TD>";
          }
       }

       if(sChamp_Type.substring(0,3) == "CAR"){
          if(asRace_Analysis_Page[nSub] != ""){
             sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=100 ALIGN=LEFT><A HREF='" + asRace_Analysis_Page[nSub] + "' TARGET='_blank'>Click Here</A></TD>";
          }else{
             sHTML += "<TD class='DetBox" + nSwitch + "' BGCOLOR='" + sColour + "' WIDTH=100 ALIGN=LEFT>-</TD>";
          }
       }
    }

    sHTML += "</TR>";
}

sHTML += "</TABLE>";

if(bSimulationOn == true){
   if(sSimRounds != ""){
      if(sSimRounds.indexOf(",") == -1){
          sHTML += "<TABLE BORDER=0 ALIGN=CENTER WIDTH=700>";
          sHTML += "<TR><TD><B>(Note: Score for Round " + sSimRounds + " is Simulated)</B></TD></TR>";
          sHTML += "</TABLE>";
      }else{
          sHTML += "<TABLE BORDER=0 ALIGN=CENTER WIDTH=700>";
          sHTML += "<TR><TD><B>(Note: Scores for Rounds " + sSimRounds + " are Simulated)</B></TD></TR>";
          sHTML += "</TABLE>";
      }
   }
}

sHTML += "<BR>";

sHTML += "<TABLE class='HeadTable3' BORDER=1 ALIGN=CENTER WIDTH=400>";
sHTML += "<TR><TD class='HeadBox3' BGCOLOR='#A8D3FF' ALIGN=CENTER>Points Analysis</TD></TR>";
sHTML += "</TABLE><BR>";

sHTML += "<TABLE class='DetTable' BORDER='1' ALIGN='Center' BGCOLOR='#FFFFFF' WIDTH=700>";

sHTML += "<TR>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF'>Position</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF'>Name</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF'>Top " + nScores_To_Include + "</TD>";
sHTML += "<TD class='DetHeadBox' BGCOLOR='#A8D3FF'>Difference</TD>";
sHTML += "</TR>";

nSwitch = 0;

for (n = 1;n <= nDriver_Records;n++) {

   if(n == nDriver){
      sColour = "RED";
   }else{
      if(nSwitch == 0){
         sColour = sColour1;
         nSwitch = 1;
      }else{
         sColour = sColour2;
         nSwitch = 0;
      }
   }

   sHTML += "<TR>";
   sHTML += "<TD class='DetBox' BGCOLOR='" + sColour + "'>" + n + "</TD>";
   sHTML += "<TD class='DetBox' BGCOLOR='" + sColour + "'><U><A onclick='Driver_Analysis(" + n + ")'>" + asDriver[n] + "</A></U></TD>";
   sHTML += "<TD class='DetBox' BGCOLOR='" + sColour + "'>" + anTopNTotal[n] + "</TD>"

   if(anTopNTotal[n] - anTopNTotal[nDriver] > 0){
      sHTML += "<TD class='DetBox' BGCOLOR='" + sColour + "'>+" + (anTopNTotal[n] - anTopNTotal[nDriver]) + "</TD>";
   }else{
      sHTML += "<TD class='DetBox' BGCOLOR='" + sColour + "'>" + (anTopNTotal[n] - anTopNTotal[nDriver]) + "</TD>";
   }

   sHTML += "</TR>";

}

sHTML +="</TABLE>";

document.getElementById("DivText2").innerHTML = sHTML;

}

//********************************************************

function Get_Avg_Score(sChamp_Type, nScore){

var n;
var nTemp_Score;

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;
      return nTemp_Score;
   }
}else{
   nTemp_Score = nScore;
}

nTemp_Score = nTemp_Score;

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(nScore >= 100){
             return (Number(sPoints_Array[n].substring(0,3)) + 100);
          }else if(nScore >= 70){
                   return (Number(sPoints_Array[n].substring(0,3)) + 70);
          }else{
                return (Number(sPoints_Array[n].substring(0,3)) + 40);
          }

          n = sPoints_Array.length +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))) {
          return (Number(sPoints_Array[n].substring(0,3)));
          n = 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))){
          return (Number(sPoints_Array[n].substring(0,3)));
          n = sPoints_Array.length +1;
       }
   }

}

}




