var idxEarly = 4;
var idxMiddle = 5;
var idxLate = 6;
var idxTotal = 7;
var idxRecent = 8;
var idxPace = 9;
var idxLKML = 18;
var idxScratch = 19;

var OddsArray_Uncalibrated = new Array();
var ProbsArray_Uncalibrated = new Array();

function GetRunners()
{
    var RunningHorses = new Array();
    
    for (r in gRatings.rowsAr)
    {
        if (parseInt(gRatings.cells(r,idxScratch).getValue()) == 0)
        {
            RunningHorses.push(gRatings.cells(r,3).getValue());
        }
    }
    return RunningHorses.join("|");
}

function GetTrueOddsOfRunningHores()
{
    var TrueOdds = new Array();
    
    for (r in gRatings.rowsAr)
    {
        if (parseInt(gRatings.cells(r,idxScratch).getValue()) == 0)
        {
            TrueOdds.push(gRatings.cells(r,idxLKML).getContent());
        }
    }
    return TrueOdds.join("|");
}

function OpenGraphsWindow()
{
    window.open("http://www.latekick.com/Protected/RaceGraphs.aspx?racecode=" + document.getElementById("lbRaces").value + "&Runners=" + GetRunners() + "&TrueOdds=" + GetTrueOddsOfRunningHores());
    //window.open("/latekick/Protected/RaceGraphs.aspx?racecode=" + document.getElementById("lbRaces").value + "&Runners=" + GetRunners() + "&TrueOdds=" + GetTrueOddsOfRunningHores());
}

function ClearGrids()
{
    try
    {
        gRatings.clearAll(); 
        gPP.clearAll(); 
    }
    catch(e)    {}
}

function Downloadfreeratings()
{
    document.getElementById("lbRaces").disabled = true;
    try
    {
        gRatings.clearAll();
        gRatings.updateFromXML("http://www.latekick.com/ws/Latekick_ws.asmx/LoadRatings?racecode=" + document.getElementById("lbRaces").value + "&bSubscribed=false", true, true, OnReceivedRating);
    }
    catch(e)    
    {
        EnableListBoxes();
    }
}

function Downloadratings()
{
    document.getElementById("lbRaces").disabled = true;
    try
    {
        gRatings.clearAll();
        gRatings.updateFromXML("http://www.latekick.com/ws/Latekick_ws.asmx/LoadRatings?racecode=" + document.getElementById("lbRaces").value + "&bSubscribed=true", true, true, OnReceivedRating);
    }
    catch(e)    
    {
        EnableListBoxes();
    }
}

function SetFirstRace()
{
    document.getElementById("lbRaces").selectedIndex = 0;
}

function OnReceivedRating()
{
    gRatings.setSizes();
    
    OddsArray_Uncalibrated = new Array();
    ProbsArray_Uncalibrated = new Array();
    for (r in gRatings.rowsAr)
    {
        OddsArray_Uncalibrated.push(parseFloat(gRatings.cells(r,18).getValue()));
        ProbsArray_Uncalibrated.push(1/(1+parseFloat(gRatings.cells(r,18).getValue())));
    }    

    HighlightMaxima();
    gRatings.selectRow(0, true, false, true);
}

function GetWorkouts(id)
{
	for (var i=0; i<4; i++)
	{
	    document.getElementById("tcWorkout" + (i+1)).innerHTML = "&nbsp;<br/>&nbsp;<br/>&nbsp;<br/>";
	}		
    dhtmlxAjax.get("http://www.latekick.com/ws/Latekick_ws.asmx/LoadWorkouts?horsecode=" + gRatings.cells(id,3).getValue(), parseWorkouts);
}

function parseWorkouts(loader)
{
	if(loader.xmlDoc.responseXML!=null)
	{
		//alert("We Got Response\n\n"+loader.doSerialization());
		var xmldata = loader.xmlDoc.responseXML;
		var workouts = xmldata.getElementsByTagName("row");
		for (var i=0; i<workouts.length; i++)
		{
		    var workout = workouts[i].getElementsByTagName("cell");
	    

		    var gatestring = "";
		    if (workout[7].firstChild.nodeValue == "1")
		        gatestring = " (gate)";
		    document.getElementById("tcWorkout" + (i+1)).innerHTML = workout[0].firstChild.nodeValue + " days ago " +  "<br>" + workout[3].firstChild.nodeValue + "F on " + workout[2].firstChild.nodeValue + gatestring + "<br><strong>Rating: " + workout[8].firstChild.nodeValue + "</strong> (" + workout[5].firstChild.nodeValue + " secs)";
		}
	}
	//else
	//	alert("Response contains no XML");
}

function doOnRatingSelected(id)
{
    try
    {
        GetWorkouts(id);
     }
     catch(e2)
     {}
     try
     {
        gPP.clearAll(); 
        gPP.updateFromXML("http://www.latekick.com/ws/Latekick_ws.asmx/LoadPP?horsecode=" + gRatings.cells(id,3).getValue(), true, true, EnableListBoxes);
    }
    catch(e)
    {
        EnableListBoxes();
    }
}

function EnableListBoxes()
{
        try
        {
            gPP.setSizes();
        }
        catch(e)
        {}
        document.getElementById("lbRaces").disabled = false;
}

function doOnCheck(rowId,cellInd,state)
{
    if (state)
    {
        gRatings.cells(rowId,0).cell.style.textDecoration = "line-through";
        gRatings.cells(rowId,1).cell.style.textDecoration = "line-through";
        gRatings.cells(rowId,2).cell.style.textDecoration = "line-through";
        gRatings.cells(rowId,idxEarly).cell.style.fontSize = "0px";
        gRatings.cells(rowId,idxMiddle).cell.style.fontSize = "0px";
        gRatings.cells(rowId,idxLate).cell.style.fontSize = "0px";
        gRatings.cells(rowId,idxTotal).cell.style.fontSize = "0px";
        gRatings.cells(rowId,idxRecent).cell.style.fontSize = "0px";
        gRatings.cells(rowId,idxPace).cell.style.fontSize = "0px";
        gRatings.cells(rowId,10).cell.style.textDecoration = "line-through";
        gRatings.cells(rowId,11).cell.style.textDecoration = "line-through";
        gRatings.cells(rowId,12).cell.style.textDecoration = "line-through";
        gRatings.cells(rowId,13).cell.style.textDecoration = "line-through";
        gRatings.cells(rowId,14).cell.style.textDecoration = "line-through";
        gRatings.cells(rowId,15).cell.style.textDecoration = "line-through";
        gRatings.cells(rowId,16).cell.style.textDecoration = "line-through";
        gRatings.cells(rowId,17).cell.style.textDecoration = "line-through";
        gRatings.cells(rowId,18).cell.style.textDecoration = "line-through";
    }
    else
    {
        gRatings.cells(rowId,0).cell.style.textDecoration = "";
        gRatings.cells(rowId,1).cell.style.textDecoration = "";
        gRatings.cells(rowId,2).cell.style.textDecoration = "";
        gRatings.cells(rowId,idxEarly).cell.style.fontSize = "11px";
        gRatings.cells(rowId,idxMiddle).cell.style.fontSize = "11px";
        gRatings.cells(rowId,idxLate).cell.style.fontSize = "11px";
        gRatings.cells(rowId,idxTotal).cell.style.fontSize = "11px";
        gRatings.cells(rowId,idxRecent).cell.style.fontSize = "11px";
        gRatings.cells(rowId,idxPace).cell.style.fontSize = "11px";
        gRatings.cells(rowId,10).cell.style.textDecoration = "";        
        gRatings.cells(rowId,11).cell.style.textDecoration = "";        
        gRatings.cells(rowId,12).cell.style.textDecoration = "";  
        gRatings.cells(rowId,13).cell.style.textDecoration = "";  
        gRatings.cells(rowId,14).cell.style.textDecoration = "";  
        gRatings.cells(rowId,15).cell.style.textDecoration = "";  
        gRatings.cells(rowId,16).cell.style.textDecoration = "";
        gRatings.cells(rowId,17).cell.style.textDecoration = "";  
        gRatings.cells(rowId,18).cell.style.textDecoration = "";
    }
    RecalibrateOdds();
    HighlightMaxima();
}

function RecalibrateOdds()
{

    var OddsArray_Calibrated = new Array();
    var ProbsArray_Calibrated = new Array();
    var calibration_base = 0;
    var counter = 0;

    for (r in gRatings.rowsAr)
    {
        //OddsArray_Uncalibrated.push(parseFloat(gRatings.cells(r,16).getValue()));
        //ProbsArray_Uncalibrated.push(1/(1+parseFloat(gRatings.cells(r,16).getValue())));
        //OddsArray_Calibrated.push(parseFloat(gRatings.cells(r,16).getValue()));
        //ProbsArray_Calibrated.push(1/(1+parseFloat(gRatings.cells(r,16).getValue())));

        if (parseInt(gRatings.cells(r,idxScratch).getValue()) == 0)
        {
            calibration_base = calibration_base + 1/(1 + OddsArray_Uncalibrated[counter]);
        }
        counter++;
    }

    counter = 0;
    for (r in gRatings.rowsAr)
    {
        if (parseInt(gRatings.cells(r,idxScratch).getValue()) == 0)
        {
            ProbsArray_Calibrated[counter] = ProbsArray_Uncalibrated[counter]/calibration_base;
            OddsArray_Calibrated[counter] = Math.round(10*(1/ProbsArray_Calibrated[counter] - 1))/10;
            gRatings.cells(r,18).setValue("" + OddsArray_Calibrated[counter] + "");
        }
        else
        {
            ProbsArray_Calibrated[counter] = 0;
            OddsArray_Calibrated[counter] = 0;
            gRatings.cells(r,18).setValue("");            
        }
        counter++;
    }
    
}

function HighlightMaxima()
{
    var MaxEarly = 0;
    var MaxMiddle = 0;
    var MaxLate = 0;
    var MaxTotal = 0;
    var MaxRecent = 0;
    var MaxPace = 0;
    
    
    //Find maxima
    for (r in gRatings.rowsAr)
    {
        if (parseInt(gRatings.cells(r,idxScratch).getValue()) == 0)
        {
            if (parseInt(gRatings.cells(r,idxEarly).getValue()) > MaxEarly)
                MaxEarly = parseInt(gRatings.cells(r,idxEarly).getValue());

            if (parseInt(gRatings.cells(r,idxMiddle).getValue()) > MaxMiddle)
                MaxMiddle = parseInt(gRatings.cells(r,idxMiddle).getValue());

            if (parseInt(gRatings.cells(r,idxLate).getValue()) > MaxLate)
                MaxLate = parseInt(gRatings.cells(r,idxLate).getValue());

            if (parseInt(gRatings.cells(r,idxTotal).getValue()) > MaxTotal)
                MaxTotal = parseInt(gRatings.cells(r,idxTotal).getValue());

            if (parseInt(gRatings.cells(r,idxRecent).getValue()) > MaxRecent)
                MaxRecent = parseInt(gRatings.cells(r,idxRecent).getValue());

            if (parseInt(gRatings.cells(r,idxPace).getValue()) > MaxPace)
                MaxPace = parseInt(gRatings.cells(r,idxPace).getValue());
        }
    }


    gRatings.setSkin("modern");
    //Highlight maxima
    for (r in gRatings.rowsAr)
    {
        var bgcol = "red";
        if (parseInt(gRatings.cells(r,idxScratch).getValue()) == 0)
        {        
            if (parseInt(gRatings.cells(r,idxEarly).getValue()) == MaxEarly)
                gRatings.cells(r,idxEarly).cell.style.backgroundColor = "LightGreen";
            else
            {
                //gRatings.cells(r,idxEarly).cell.style.backgroundColor = bgcol;
            }
            if (parseInt(gRatings.cells(r,idxMiddle).getValue()) == MaxMiddle)
                gRatings.cells(r,idxMiddle).cell.style.backgroundColor = "LightGreen";
            else
            {
                //gRatings.cells(r,idxMiddle).cell.style.backgroundColor = bgcol;
            }

            if (parseInt(gRatings.cells(r,idxLate).getValue()) == MaxLate)
                gRatings.cells(r,idxLate).cell.style.backgroundColor = "LightGreen";
            else
            {
                //gRatings.cells(r,idxLate).cell.style.backgroundColor = bgcol;
            }
            if (parseInt(gRatings.cells(r,idxTotal).getValue()) == MaxTotal)
                gRatings.cells(r,idxTotal).cell.style.backgroundColor = "LightGreen";
            else
            {
                //gRatings.cells(r,idxTotal).cell.style.backgroundColor = bgcol;
            }
            if (parseInt(gRatings.cells(r,idxRecent).getValue()) == MaxRecent)
                gRatings.cells(r,idxRecent).cell.style.backgroundColor = "LightGreen";
            else
            {
                //gRatings.cells(r,idxRecent).cell.style.backgroundColor = bgcol;
            }
            if (parseInt(gRatings.cells(r,idxPace).getValue()) == MaxPace)
                gRatings.cells(r,idxPace).cell.style.backgroundColor = "LightGreen";
            else
            {
                //gRatings.cells(r,idxPace).cell.style.backgroundColor = bgcol;
            }
        }
        else
        {
            gRatings.cells(r,idxEarly).cell.style.backgroundColor = "Gainsboro";
            gRatings.cells(r,idxMiddle).cell.style.backgroundColor = "Gainsboro";
            gRatings.cells(r,idxLate).cell.style.backgroundColor = "Gainsboro";
            gRatings.cells(r,idxTotal).cell.style.backgroundColor = "Lightblue";
            gRatings.cells(r,idxRecent).cell.style.backgroundColor = "Lightblue";
            gRatings.cells(r,idxPace).cell.style.backgroundColor = "LightGoldenrodYellow";
        }
    }
}

function wopen(url, name, w, h)
{
    // Fudge factors for window decoration space.
    // In my tests these work well on all platforms & browsers.
    w += 32;
    h += 96;
    var win = window.open(url,
        name, 
        'width=' + w + ', height=' + h + ', ' +
        'location=no, menubar=no, ' +
        'status=no, toolbar=no, scrollbars=no, resizable=no');
    win.resizeTo(w, h);
    win.focus();
}
