﻿function ajaxSetup()
{
    var xmlHttp;
    try
    {  
        // Firefox, Opera 8.0+, Safari
	    xmlHttp = new XMLHttpRequest();  
    }
    catch (e)
    {  
        // Internet Explorer  
	    try{    
		    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");    
	    }
	    catch (e){    
		    try{      
  			    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  		    }
		    catch (e){
  			    alert("Your browser does not support AJAX!");
  			    return false;
  		    }
	    }
    }
    return xmlHttp;
}
function fixtureDetails(matchID)
{
    var xmlHttp = ajaxSetup();
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState == 4)   // 4 = Completed
        {
            document.getElementById("FixtureDetails").innerHTML = xmlHttp.responseText;
        }
    }
    xmlHttp.open("GET","../ajax/fixtures.php?id=" + matchID, true);
    xmlHttp.send(null);

}
function opponentList(regionID)
{
    var xmlHttp = ajaxSetup();
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState == 4)   // 4 = Completed
        {
            document.getElementById("RecordCnt").value = xmlHttp.responseText.substr(0, xmlHttp.responseText.indexOf("_"));
            document.getElementById("OpponentsAll").innerHTML = xmlHttp.responseText.substr(xmlHttp.responseText.indexOf("_")+1);

            var recordCnt = parseInt(document.getElementById("RecordCnt").value);
            VerticalCarousel('8', '40', '8', recordCnt, '', '1');
        }
    }
    xmlHttp.open("GET","../ajax/opponents.php?regionid=" + regionID, true);
    xmlHttp.send(null);

}

function ViewVideo() {
    $("#SummaryHolder").hide();
    $("#VideoHolder").show();
}
function ViewSummary() {
    $("#VideoHolder").hide();
    $("#SummaryHolder").show();
    
    var videoID = document.getElementById("ctl00_MainContent_VideoHD").value;
    var matchID = document.getElementById("ctl00_MainContent_MatchHD").value;

    var xmlHttp = ajaxSetup();
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState == 4)   // 4 = Completed
        {
            document.getElementById("SummaryHolder").innerHTML = xmlHttp.responseText;
        }
    }
    xmlHttp.open("GET","Ajax/VideoAjax.aspx?ID=" + videoID + "&MatchID=" + matchID + "&Type=summary", true);
    xmlHttp.send(null);
    
}
function ViewOthers() {
    $("#VideoHolder").hide();
    $("#SummaryHolder").show();
    
    var videoID = document.getElementById("ctl00_MainContent_VideoHD").value;
    var matchID = document.getElementById("ctl00_MainContent_MatchHD").value;

    var xmlHttp = ajaxSetup();
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState == 4)   // 4 = Completed
        {
            document.getElementById("SummaryHolder").innerHTML = xmlHttp.responseText;
        }
    }
    xmlHttp.open("GET","Ajax/VideoAjax.aspx?ID=" + videoID + "&MatchID=" + matchID + "&Type=others", true);
    xmlHttp.send(null);
    
}


function ShowStrip(stripID)
{
    var xmlHttp = ajaxSetup();
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState == 4)   // 4 = Completed
        {
            document.getElementById("StripData").innerHTML = xmlHttp.responseText;
        }
    }
    xmlHttp.open("GET","Ajax/StripAjax.aspx?ID=" + stripID, true);
    xmlHttp.send(null);

}


function ajaxHistoryFunction(sector)
{
    var xmlHttp = ajaxSetup();
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState == 4)   // 4 = Completed
        {
            document.getElementById("ctl00_MainContent_HistorySections").innerHTML = xmlHttp.responseText;
        }
    }
    xmlHttp.open("GET","Ajax/HistoryAjax.aspx?type=sector&sector=" + sector, true);
    xmlHttp.send(null);
    
    ajaxLinksFunction(sector);
    ajaxOtherLinksFunction(sector);
}
function ajaxLinksFunction(sector)
{
    var xmlHttp = ajaxSetup();
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState == 4)   // 4 = Completed
        {
            document.getElementById("ctl00_MainContent_HistoryGroups").innerHTML = xmlHttp.responseText;
        }
    }
    xmlHttp.open("GET","Ajax/HistoryAjax.aspx?type=links&sector=" + sector, true);
    xmlHttp.send(null);

}
function ajaxOtherLinksFunction(sector)
{
    var xmlHttp = ajaxSetup();
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState == 4)   // 4 = Completed
        {
            document.getElementById("ctl00_MainContent_OtherSections").innerHTML = xmlHttp.responseText;
        }
    }
    xmlHttp.open("GET","Ajax/HistoryAjax.aspx?type=otherlinks&sector=" + sector, true);
    xmlHttp.send(null);

}


// videos
function videoCompetitions()
{
    var CompDDL = document.getElementById('competition');
    var Competition = CompDDL.options[CompDDL.selectedIndex].value;

    var xmlHttp = ajaxSetup();
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState == 4)   // 4 = Completed
        {
            document.getElementById("CompYearDDLHolder").innerHTML = xmlHttp.responseText;
        }
    }
    xmlHttp.open("GET","../ajax/videosearch.php?Comp=" + Competition, true);
    xmlHttp.send(null);
    
    if((Competition == "8") || (Competition == ""))
    {
        document.getElementById("CompYearTitle").innerHTML = "Period:";
    }
    else
    {
        document.getElementById("CompYearTitle").innerHTML = "Year:";
    }
}

function filterVideos(){
    
    var OpponentDDL = document.getElementById('opponent');
    var OpponentID = OpponentDDL.options[OpponentDDL.selectedIndex].value;
    document.getElementById('OpponentIDHD').value = OpponentID;
    var CompetitionDDL = document.getElementById('competition');
    var CompetitionID = CompetitionDDL.options[CompetitionDDL.selectedIndex].value;
    document.getElementById('CompetitionTypeIDHD').value = CompetitionID;
    var YearDDL = document.getElementById('year');
    var CompYear = YearDDL.options[YearDDL.selectedIndex].value;
    document.getElementById('CompetitionYearHD').value = CompYear;
    
    var opponentSql = "";
    if (OpponentID > 0){
        opponentSql = " AND o.ID = " + OpponentID;
    }
    var competitionSql = "";
    if (CompetitionID > 0){
        competitionSql = " AND c.CompetitionTypeID = " + CompetitionID;
    }
    var yearSql = "";
	var sqlstring = "SELECT * FROM games g, opponents o, competitionversion c, videos v WHERE g.OpponentID = o.ID AND g.CompetitionID = c.ID AND v.MatchID = g.ID AND v.TypeID = 1" + opponentSql + competitionSql + " ORDER BY Date";
    if (CompetitionID != 0 && CompetitionID != 8 && CompYear != ""){
		sqlstring = "SELECT * FROM games g, opponents o, competitionversion c, videos v WHERE g.OpponentID = o.ID AND g.CompetitionID = c.ID AND v.MatchID = g.ID AND v.TypeID = 1" + opponentSql + competitionSql + " AND c.CompetitionYear = " + CompYear + " ORDER BY Date";
    }
    else if (CompYear != ""){
		sqlstring = "SELECT * FROM games g, opponents o, competitionversion c, videos v WHERE g.OpponentID = o.ID AND g.CompetitionID = c.ID AND v.MatchID = g.ID AND v.TypeID = 1" + opponentSql + competitionSql + " AND Date >= (SELECT EarliestDate FROM matchperiods WHERE Value = " + CompYear + ") AND Date <= (SELECT LatestDate FROM matchperiods WHERE Value = " + CompYear + ") ORDER BY Date";
    }
    
    var xmlHttp = ajaxSetup();
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState == 4)   // 4 = Completed
        {
            document.getElementById('RecordCnt').value = String(xmlHttp.responseText).substr(0,String(xmlHttp.responseText).indexOf("@@@")); 
            document.getElementById("MatchDetailsAll").innerHTML = String(xmlHttp.responseText).substr(String(xmlHttp.responseText).indexOf("@@@") + 3); 
            var recordCnt = parseInt(document.getElementById('RecordCnt').value);
            VerticalCarousel('14', '17', '14', recordCnt, '', '2');
        }
    }
    xmlHttp.open("GET","../ajax/videomatchlist.php?sql=" + sqlstring, true);
    xmlHttp.send(null);
    
}

function resetVideos(){
    
    document.getElementById('opponent').selectedIndex = 0;
    document.getElementById('OpponentIDHD').value = "";
    document.getElementById('CompetitionTypeIDHD').value = "";
    document.getElementById('year').selectedIndex = 0;
    document.getElementById('CompetitionYearHD').value = "";
    
	var sqlstring = "SELECT * FROM games g, opponents o, competitionversion c, videos v WHERE g.OpponentID = o.ID AND g.CompetitionID = c.ID AND v.MatchID = g.ID AND v.TypeID = 1 ORDER BY Date";
    
    var xmlHttp = ajaxSetup();
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState == 4)   // 4 = Completed
        {
            document.getElementById('RecordCnt').value = String(xmlHttp.responseText).substr(0,String(xmlHttp.responseText).indexOf("@@@")); 
            document.getElementById("MatchDetailsAll").innerHTML = String(xmlHttp.responseText).substr(String(xmlHttp.responseText).indexOf("@@@") + 3); 
            var recordCnt = parseInt(document.getElementById('RecordCnt').value);
            VerticalCarousel('14', '17', '14', recordCnt, '', '2');
        }
    }
    xmlHttp.open("GET","../ajax/videomatchlist.php?sql=" + sqlstring, true);
    xmlHttp.send(null);
    
}


/* ======================================= FIFA Ranking ========================================= */

function updateRankChart(startid, endid, type)
{
    document.getElementById("charttype").value = type;
    document.getElementById("start").value = startid;
    document.getElementById("end").value = endid;
    
    
    if (type == "Ranking"){
        document.getElementById("othercharttype").value = "Europe";
        $("#ChartLink").html("View Zonal Chart");
    }
    else{
        document.getElementById("othercharttype").value = "Ranking";
        $("#ChartLink").html("View Global Chart");
    }
    var xmlHttp = ajaxSetup();
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState == 4)   // 4 = Completed
        {
            var output = String(xmlHttp.responseText);
            var dateStr = output.substr(0, output.indexOf("@@@"));
            var graph = output.substr(output.indexOf("@@@") + 3);
			$( "#sliderdate" ).html( dateStr );
			$("#graphholder").html(graph);
        }
    }
    xmlHttp.open("GET","../ajax/ranking.php?start=" + startid + "&end=" + endid + "&type=" + type, true);
    xmlHttp.send(null);

}
function viewYear(item){
    var year = item.value;
    if (year != ""){
        location.href = "FIFARankings/" + year;
    }

}


