
var idxEarly = 3;
var idxMiddle = 4;
var idxLate = 5;
var idxTotal = 6;
var idxRecent = 7;
var idxPace = 8;
var idxLKML = 17;
var idxScratch = 18;

var OddsArray_Uncalibrated = new Array();
var ProbsArray_Uncalibrated = new Array();

function getQueryVariable(variable) 
{ 
    var query = window.location.search.substring(1); 
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) 
    { 
        var pair = vars[i].split("=");
        if (pair[0] == variable) 
            return pair[1]; 
    } 
} 

function GetRunners()
{
    var RunningHorses = new Array();
    
    for (r in gRatings.rowsAr)
    {
        if (parseInt(gRatings.cells(r,idxScratch).getValue()) == 0)
        {
            RunningHorses.push(gRatings.cells(r,20).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).getValue());
        }
    }
    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_Special(racecode) {
    try {
        gRatings.clearAll();
        gRatings.updateFromXML("http://www.latekick.com/ws/Latekick_ws.asmx/LoadRatings?racecode=" + racecode + "&bSubscribed=true", true, true, OnReceivedRating);
    }
    catch (e) 
    {
    }
}

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, idxLKML).getValue()));
        ProbsArray_Uncalibrated.push(1 / (1 + parseFloat(gRatings.cells(r, idxLKML).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, 20).getValue(), parseWorkouts);
}

function doOnPPSelected(id) 
{
    try 
    {
        dhtmlxAjax.get("http://www.latekick.com/ws/Latekick_ws.asmx/GetRaceDetails?racecode=" + gPP.cells(id, 18).getValue(), parseRaceDetails);
    }
    catch (e) {}
}

function parseRaceDetails(loader) 
{
    document.getElementById("RaceDesc").innerText = "";
    var xmldata = loader.xmlDoc.responseXML;
    var race = xmldata.getElementsByTagName("race");
    var desc = race[0].getElementsByTagName("description");
    
//    var race = races.selectSingleNode("race");
    document.getElementById("RaceDesc").innerText = desc[0].firstChild.nodeValue;
}


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");
		var gatestring = workmates = "";
		var daysago = daysago_next = 0;
		var workid = 1;
		
		for (var i=0; i<workouts.length; i++)
		{
		    var workout = workouts[i].getElementsByTagName("cell");
		    if (workout[7].firstChild.nodeValue == "1")
		        gatestring = " (gate)";
		    daysago = workout[0].firstChild.nodeValue;

		    if (i < workouts.length - 1) 
		    {
		        var workout_next = workouts[i + 1].getElementsByTagName("cell");
		        daysago_next = workout_next[0].firstChild.nodeValue;
		    }
		    else 
		    {
		        daysago_next = 9999;
		    }

	        if (workout[10].firstChild != null) 
	        {
	            workmates += "<a href='pp_horse.aspx?horsename=" + escape(workout[10].firstChild.nodeValue) + "&horsecode=" + workout[9].firstChild.nodeValue + "' target='blank'>" + workout[10].firstChild.nodeValue + "</a> ";
	        }

	        if (daysago != daysago_next) 
	        {
	            document.getElementById("tcWorkout" + workid).innerHTML = daysago + " 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)<br><strong>Workmates:</strong> " + workmates;
	            workmates = "";
	            workid++;
	        }
		    
		}
	}
	//else
	//	alert("Response contains no XML");
}

function doOnRatingSelected(id)
{
    try
    {
        GetWorkouts(id);
     }
     catch(e2)
     {}
     try 
     {
         counter = 1;
//         for (r in gRatings.rowsAr) 
//         {
//             if (id == counter) 
//             {
//                 if (gRatings.cells(counter, idxEarly).cell.style.backgroundColor != "lightgreen")
//                     gRatings.cells(counter, idxEarly).cell.style.backgroundColor = "Lightblue";
//                 if (gRatings.cells(counter, idxMiddle).cell.style.backgroundColor != "lightgreen")
//                     gRatings.cells(counter, idxMiddle).cell.style.backgroundColor = "Lightblue";
//                 if (gRatings.cells(counter, idxLate).cell.style.backgroundColor != "lightgreen")
//                     gRatings.cells(counter, idxLate).cell.style.backgroundColor = "Lightblue";
//                 if (gRatings.cells(counter, idxTotal).cell.style.backgroundColor != "lightgreen")
//                     gRatings.cells(counter, idxTotal).cell.style.backgroundColor = "Lightblue";
//                 if (gRatings.cells(counter, idxRecent).cell.style.backgroundColor != "lightgreen")
//                     gRatings.cells(counter, idxRecent).cell.style.backgroundColor = "Lightblue";
//                 if (gRatings.cells(counter, idxPace).cell.style.backgroundColor != "lightgreen")
//                     gRatings.cells(counter, idxPace).cell.style.backgroundColor = "Lightblue";
//             }
//             else 
//             {
//                 if (gRatings.cells(counter, idxEarly).cell.style.backgroundColor != "lightgreen")
//                     gRatings.cells(counter, idxEarly).cell.style.backgroundColor = "Gainsboro";
//                 if (gRatings.cells(counter, idxMiddle).cell.style.backgroundColor != "lightgreen")
//                     gRatings.cells(counter, idxMiddle).cell.style.backgroundColor = "Gainsboro";
//                 if (gRatings.cells(counter, idxLate).cell.style.backgroundColor != "lightgreen")
//                     gRatings.cells(counter, idxLate).cell.style.backgroundColor = "Gainsboro";
//                 if (gRatings.cells(counter, idxTotal).cell.style.backgroundColor != "lightgreen")
//                     gRatings.cells(counter, idxTotal).cell.style.backgroundColor = "Lightblue";
//                 if (gRatings.cells(counter, idxRecent).cell.style.backgroundColor != "lightgreen")
//                     gRatings.cells(counter, idxRecent).cell.style.backgroundColor = "Lightblue";
//                 if (gRatings.cells(counter, idxPace).cell.style.backgroundColor != "lightgreen")
//                     gRatings.cells(counter, idxPace).cell.style.backgroundColor = "LightGoldenrodYellow";
//             }
//             counter++;
//         }
        
        gPP.clearAll();
        gPP.updateFromXML("http://www.latekick.com/ws/Latekick_ws.asmx/LoadPP?horsecode=" + gRatings.cells(id,20).getValue(), true, true, EnableListBoxes);
    }
    catch(e) {
        try 
        {
            EnableListBoxes();
        }
        catch (e2)
        { }
    }
    
}

function EnableListBoxes()
{
        try
        {
            gPP.setSizes();
        }
        catch(e)
        { }
        try 
        {
            document.getElementById("lbRaces").disabled = false;
        } 
        catch (e2)
        { }
}

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, 9).cell.style.textDecoration = "line-through";        
        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";
    }
    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, 9).cell.style.textDecoration = "";
        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 = "";  
    }
    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, idxLKML).setValue("" + OddsArray_Calibrated[counter] + "");
        }
        else
        {
            ProbsArray_Calibrated[counter] = 0;
            OddsArray_Calibrated[counter] = 0;
            gRatings.cells(r, idxLKML).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)
    {
        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 = "Gainsboro";
            }
            if (parseInt(gRatings.cells(r,idxMiddle).getValue()) == MaxMiddle)
                gRatings.cells(r,idxMiddle).cell.style.backgroundColor = "LightGreen";
            else
            {
                //gRatings.cells(r,idxMiddle).cell.style.backgroundColor = "Gainsboro";
            }

            if (parseInt(gRatings.cells(r,idxLate).getValue()) == MaxLate)
                gRatings.cells(r,idxLate).cell.style.backgroundColor = "LightGreen";
            else
            {
                //gRatings.cells(r, idxLate).cell.style.backgroundColor = "Gainsboro";
            }
            if (parseInt(gRatings.cells(r,idxTotal).getValue()) == MaxTotal)
                gRatings.cells(r,idxTotal).cell.style.backgroundColor = "LightGreen";
            else
            {
                //gRatings.cells(r,idxTotal).cell.style.backgroundColor = "Lightblue";
            }
            if (parseInt(gRatings.cells(r,idxRecent).getValue()) == MaxRecent)
                gRatings.cells(r,idxRecent).cell.style.backgroundColor = "LightGreen";
            else
            {
                //gRatings.cells(r,idxRecent).cell.style.backgroundColor = "Lightblue";
            }
            if (parseInt(gRatings.cells(r,idxPace).getValue()) == MaxPace)
                gRatings.cells(r,idxPace).cell.style.backgroundColor = "LightGreen";
            else
            {
                //gRatings.cells(r, idxPace).cell.style.backgroundColor = "LightGoldenrodYellow";
            }
        }
        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();
}

