﻿var XmlReq;
var IsExplorer;
var TimerID;
var PreviousGameType;


function CreateXmlReq() {
    try {
        XmlReq = null;
        if (navigator.userAgent.indexOf("MSIE") >= 0) {
            IsExplorer = 1;
            if (navigator.appVersion.indexOf("MSIE 5.5") >= 0)
                XmlReq = new ActiveXObject("Microsoft.XMLHTTP");
            else
                XmlReq = new ActiveXObject("Msxml2.XMLHTTP");
            XmlReq.onreadystatechange = HandleResponse;
        }
        else {
            IsExplorer = 0;
            XmlReq = new XMLHttpRequest();
            XmlReq.onreadystatechange = HandleResponse;
        }
    }
    catch (e) {
        XmlReq = null;
    }
}

function FetchPageData(MakeRecursive) {
    try {
        var GameTypeToFetch = document.getElementById('ctl00_ContentPlaceHolder1_lblGameType');
        if (GameTypeToFetch == null)
            GameTypeToFetch = document.getElementById('ctl00$ContentPlaceHolder1$lblGameType');

        var requestUrl = "GameData.aspx?GameID=" + GameTypeToFetch.value + "&dummy=" + MakeDumy();

        CreateXmlReq();

        if (XmlReq) {
            XmlReq.open("GET", requestUrl, true);
            XmlReq.send(null);
        }
    }
    catch (e) {
        alert(e.message);
    }
    if (MakeRecursive == true)
        if (TimerID == undefined) {
        TimerID = setInterval("FetchPageData(" + MakeRecursive + ")", 5000);
    }
}

function HandleResponse() {
    if (XmlReq != null) {
        if (XmlReq.readyState == 4) {
            if (XmlReq.status == 200) {
                HandleRequestResponse(XmlReq.responseText);
            }
        }
    }
}

function HandleRequestResponse(result) {
    var GameType = document.getElementById('ctl00_ContentPlaceHolder1_lblGameType');
    if (GameType == null)
        GameType = document.getElementById('ctl00$ContentPlaceHolder1$lblGameType');
        
    if (result.indexOf("###") > -1) {
        var result_array = result.split("###")
        if (document.getElementById('lblGameName').innerHTML != result_array[0]) {
            document.getElementById('lblGameName').innerHTML = result_array[0];
            document.getElementById('lblGameDesc').innerHTML = result_array[1];
            var imageData = result_array[2].split("$");
            document.getElementById('imgGamePic').src = imageData[0];
            document.getElementById('imgGamePic').title = imageData[1];
            document.getElementById('imgGamePic').alt = imageData[1];
        }

        var tbl = document.getElementById('MainGamesTable');
        while (tbl.rows.length > 0) { tbl.deleteRow(tbl.rows.length - 1); }
        var games_array = result_array[3].split("&&")
        if (games_array.length == 3) {
            var i = 0;
            var IsOdd = 1;
            while (i < games_array.length) {
                CreateHomePageGame(games_array[i], i % 2, result_array[8]);
                i++;
            }
        }

        var tbl2 = document.getElementById('MainCompetitionsTable');
        while (tbl2.rows.length > 0) { tbl2.deleteRow(tbl2.rows.length - 1); }
        var competitions_array = result_array[4].split("&&")
        if (competitions_array.length == 2) {
            var i = 0;
            while (i < competitions_array.length) {
                CreateHomePageCompetition(competitions_array[i], i % 2, result_array[7]);
                i++;
            }
        }

        CreateHomePageGameLink(result_array[5], result_array[7]);

        HandleJackpotStiker(result_array[6]);
    }
}

function HandleJackpotStiker(jackpotData) {
    try {
        var operatorFolder = document.getElementById('ctl00_hdOperatorResources').value;
        var jackpotObj = document.getElementById('divJackpot');
        if (jackpotObj != null) {
            var jackpotData = jackpotData.split("^")
            if (jackpotData.length == 2 && jackpotData[0] != '0') {
                jackpotObj.style.display = 'block';
                jackpotObj.innerHTML = "<a href='javascript:openCompetitionDetailsPopup(" + jackpotData[0] + ");'><img src='" + operatorFolder + "images/buttons/sideJackpotText.png' alt='' />" + jackpotData[1] + "<b>€</b></a>";
            }
            else {
                jackpotObj.style.display = 'none';
            }
        }
    }
    catch (e) {
        alert(e.message);
    }
}
function CreateHomePageGameLink(linksData, IsMultiplayer) {
    var PlayDemo = document.getElementById('cmdPlayDemo');
    var PlayDemoIcon = document.getElementById('cmdPlayDemoIcon');
    var imgGratuit = document.getElementById('imgGratuit');
    var linksObj = document.getElementById('hdDemo');
    var orObj = document.getElementById('or');
    
    if (linksObj != null && linksData != '') {
        linksObj.value = linksData;
        if (IsMultiplayer == '1') {
            PlayDemo.style.visibility = 'hidden';
            PlayDemoIcon.style.visibility = 'hidden';
            imgGratuit.style.visibility = 'hidden';
            orObj.style.visibility = 'hidden';
        }
        else {
            PlayDemo.style.visibility = 'visible';
            PlayDemoIcon.style.visibility = 'visible';
            imgGratuit.style.visibility = 'visible';
            orObj.style.visibility = 'visible';
        }
    }
    else {
        PlayDemo.style.visibility = 'hidden';
        PlayDemoIcon.style.visibility = 'hidden';
        imgGratuit.style.visibility = 'hidden';
        orObj.style.visibility = 'hidden';
    }
}

function CreateHomePageCompetition(competitions_array, isOdd, IsMultiplayer) {
    var competition_data = competitions_array.split("^")
    var operatorFolder = document.getElementById('ctl00_hdOperatorResources').value;
    var tbl = document.getElementById('MainCompetitionsTable');

    if (IsMultiplayer == 1) {
        tbl.style.display = 'none';
        return;
    }
    else
        tbl.style.display = 'block';
        
        
    var row = tbl.insertRow(tbl.rows.length);
    if (isOdd == 1)
        row.className = "odd";
    else
        row.className = "even";

    var cell = row.insertCell(0);
    cell.className = "arrow";
    cell.innerHTML = "<img src='" + operatorFolder + "images/icons/homeRightArrow.png' />";

    var cell = row.insertCell(1);
    cell.className = "compImg"; 
    if (competition_data[0] != '')
        cell.innerHTML = "<a href='Competitions.aspx?gameID=" + competition_data[0] + "'><img src='" + competition_data[1] + "' class='gamePic' alt='' /></a>"; 
    else
        cell.innerHTML = "&nbsp;";

    var cell = row.insertCell(2);
    cell.className = "startDate s14";
    cell.innerHTML = competition_data[2];

    var cell = row.insertCell(3);
    cell.className = "freePlaces s16";
    if (competition_data[0] != '')
        cell.innerHTML = competition_data[3];
    else
        cell.innerHTML = "&nbsp;";

    var cell = row.insertCell(4);
    cell.className = "entry s16";
    if (competition_data[4] != '')
        cell.innerHTML = competition_data[4];
    else
        cell.innerHTML = "&nbsp;";

    var cell = row.insertCell(5);
    cell.className = "prize s16";
    if (competition_data[5] != '')
        cell.innerHTML = competition_data[5];
    else
        cell.innerHTML = "&nbsp;";

    var cell = row.insertCell(6);
    cell.className = "currency";
    if (competition_data[6] != "")
        cell.innerHTML = '<img src="' + operatorFolder + 'images/icons/Euro.png" />';
    else
        cell.innerHTML = '&nbsp;';

    var IsLogin = "0";
    if (document.getElementById("ctl00_ContentPlaceHolder1_hdIsLogin") != null)
        IsLogin = document.getElementById("ctl00_ContentPlaceHolder1_hdIsLogin").value;

    var cell = row.insertCell(7);
    cell.className = "button";
    
    if (competition_data[6] != '') {
        if (IsLogin == "1")
            cell.innerHTML = "<a class='but34' href='javascript:OpenGame(" + competition_data[6] + "," + competition_data[7] + "," + competition_data[8] + "," + competition_data[9] + ",0);'><b><img src='" + operatorFolder + "images/icons/userDataPopArrow.gif' alt='' /> " + Messages.JoinGameLine1 + "<br />&nbsp;&nbsp;&nbsp;" + Messages.JoinGameLine2 + "</b></a>";
        else
            cell.innerHTML = "<a class='but34' href='Login.aspx'><b><img src='" + operatorFolder + "images/icons/userDataPopArrow.gif' alt='' /> " + Messages.JoinGameLine1 + "<br />&nbsp;&nbsp;&nbsp;" + Messages.JoinGameLine2 + "</b></a>";
    }
    else
        cell.innerHTML = "&nbsp;";
}

function CreateHomePageGame(games_array, isOdd, IsJackpot) {
    var operatorFolder = document.getElementById('ctl00_hdOperatorResources').value;
    var game_data = games_array.split("^");
    var competitionID = game_data[15];
    var freePlaces = game_data[12];
    var maxPlayer = game_data[19];
    var linkType = parseInt(game_data[20]);

    var Vous = (freePlaces == '0' ? '&nbsp;' : Messages.You);
    var tbl = document.getElementById('MainGamesTable');

    if (IsJackpot == 1) {
        tbl.style.display = 'none';
        return;
    }
    else
        tbl.style.display = 'block';
    
    var row = tbl.insertRow(tbl.rows.length);
    if (isOdd == 0)
        row.className = "odd";

    var cell = row.insertCell(0);
    cell.className = "arrow";
    cell.innerHTML = "<img src='" + operatorFolder + "images/icons/homeRightArrow.png' />";

    var cell = row.insertCell(1);
    cell.className = "userTd"; // current";
    if (competitionID != "") {
        cell.innerHTML = (game_data[0] != "" ? "<a href=\"javascript:openFriendPopup('" + game_data[0] + "')\"><img src='" + game_data[1] + "' class='userPic' alt='' /><Br />" + game_data[0] + "</a>" : "<img src='" + operatorFolder + "images/icons/homeEmptyPlayer.png' class='userPic' alt='' /><br />" + Vous);
        if (game_data[0] == "") Vous = '&nbsp;';
    }
    else
        cell.innerHTML = "&nbsp;";

    var cell = row.insertCell(2);
    cell.className = "userTd";
    if (competitionID != "") {
        if (parseInt(maxPlayer) >= 2) {
            cell.innerHTML = (game_data[2] != "" ? "<a href=\"javascript:openFriendPopup('" + game_data[2] + "')\"><img src='" + game_data[3] + "' class='userPic' alt='' /><Br />" + game_data[2] + "</a>" : "<img src='" + operatorFolder + "images/icons/homeEmptyPlayer.png' class='userPic' alt='' /><br />" + Vous);
            if (game_data[2] == "") Vous = '&nbsp;';
        }
        else
            cell.innerHTML = "&nbsp;";
    }
    else
        cell.innerHTML = "&nbsp;";

    var cell = row.insertCell(3);
    cell.className = "userTd";
    if (competitionID != "") {
        if (parseInt(maxPlayer) >= 3) {
            cell.innerHTML = (game_data[4] != "" ? "<a href=\"javascript:openFriendPopup('" + game_data[4] + "')\"><img src='" + game_data[5] + "' class='userPic' alt='' /><Br />" + game_data[4] + "</a>" : "<img src='" + operatorFolder + "images/icons/homeEmptyPlayer.png' class='userPic' alt='' /><br />" + Vous);
            if (game_data[4] == "") Vous = '&nbsp;';
        }
        else
            cell.innerHTML = "&nbsp;";
    }
    else
        cell.innerHTML = "&nbsp;";

    var cell = row.insertCell(4);
    cell.className = "userTd";
    if (competitionID != "") {
        if (parseInt(maxPlayer) >= 4) {
            cell.innerHTML = (game_data[6] != "" ? "<a href=\"javascript:openFriendPopup('" + game_data[6] + "')\"><img src='" + game_data[7] + "' class='userPic' alt='' /><Br />" + game_data[6] + "</a>" : "<img src='" + operatorFolder + "images/icons/homeEmptyPlayer.png' class='userPic' alt='' /><br />" + Vous);
            if (game_data[6] == "") Vous = '&nbsp;';
        }
        else
            cell.innerHTML = "&nbsp;";
    }
    else
        cell.innerHTML = "&nbsp;";

    var cell = row.insertCell(5);
    cell.className = "userTd";
    if (competitionID != "") {
        if (parseInt(maxPlayer) >= 5) {
            cell.innerHTML = (game_data[8] != "" ? "<a href=\"javascript:openFriendPopup('" + game_data[8] + "')\"><img src='" + game_data[9] + "' class='userPic' alt='' /><Br />" + game_data[8] + "</a>" : "<img src='" + operatorFolder + "images/icons/homeEmptyPlayer.png' class='userPic' alt='' /><br />" + Vous);
            if (game_data[8] == "") Vous = '&nbsp;';
        }
        else
            cell.innerHTML = "&nbsp;";
    }
    else
        cell.innerHTML = "&nbsp;";

    var cell = row.insertCell(6);
    cell.className = "userTd";
    if (competitionID != "") {
        if (parseInt(maxPlayer) >= 6) {
            cell.innerHTML = (game_data[10] != "" ? "<a href=\"javascript:openFriendPopup('" + game_data[10] + "')\"><img src='" + game_data[11] + "' class='userPic' alt='' /><Br />" + game_data[10] + "</a>" : "<img src='" + operatorFolder + "images/icons/homeEmptyPlayer.png' class='userPic' alt='' /><br />" + Vous);
            if (game_data[10] == "") Vous = '&nbsp;';
        }
        else
            cell.innerHTML = "&nbsp;";
    }
    else
        cell.innerHTML = "&nbsp;";

    var cell = row.insertCell(7);
    cell.className = "freePlaces s16";
    cell.innerHTML = (freePlaces == "" ? "&nbsp;" : freePlaces);

    var cell = row.insertCell(8);
    cell.className = "entry s16";
    cell.innerHTML = (game_data[13] == "" ? "&nbsp;" : game_data[13]); 

    var cell = row.insertCell(9);
    cell.className = "prize s16";
    cell.innerHTML = (game_data[14] == "" ? "&nbsp;" : game_data[14]);
    
    var cell = row.insertCell(10);
    cell.className = "currency";
    if (competitionID != "")
        cell.innerHTML = '<img src="' + operatorFolder + 'images/icons/Euro.png" />';
    else
        cell.innerHTML = '&nbsp;';
    
    var cell = row.insertCell(11);
    cell.className = "button";
    if (competitionID != "") {
        switch (linkType) {
            case 0:
                cell.innerHTML = "<a class='but34' href='Login.aspx'><b><img src='" + operatorFolder + "images/icons/userDataPopArrow.gif' alt='' /> " + Messages.JoinGameLine1 + "<br />&nbsp;&nbsp;&nbsp;" + Messages.JoinGameLine2 + "</b></a>";
                break;
            case 1:
                cell.innerHTML = "<a class='but34' href='javascript:ShowGratificationMessage();'><b><img src='" + operatorFolder + "images/icons/userDataPopArrow.gif' alt='' /> " + Messages.JoinGameLine1 + "<br />&nbsp;&nbsp;&nbsp;" + Messages.JoinGameLine2 + "</b></a>";
                break;
            case 2:
                cell.innerHTML = "<a class='but34' href='javascript:OpenGame(" + competitionID + "," + game_data[16] + "," + game_data[17] + "," + game_data[18] + ",0);'><b><img src='" + operatorFolder + "images/icons/userDataPopArrow.gif' alt='' /> " + Messages.JoinGameLine1 + "<br />&nbsp;&nbsp;&nbsp;" + Messages.JoinGameLine2 + "</b></a>";
                break;
        }
    }
    else
        cell.innerHTML = "&nbsp;";
}

function MakeDumy() {
    return String(new Date().getTime()).replace(/(\d\d\d)$/, '.$1');
}

function OpenGame(_CompetitionID, _CompetitionSeq, _GameSeq, _ServerID, _RematchCompetitionSeq, _IsGuest) {

    if (_RematchCompetitionSeq == undefined) _RematchCompetitionSeq = 0;
    if (_IsGuest == undefined)
        window.open('GamePage.aspx?cid=' + _CompetitionID + '&cseq=' + _CompetitionSeq + '&ser=' + _ServerID + '&gseq=' + _GameSeq + '&rm=' + _RematchCompetitionSeq + '&dummy=' + MakeDumy(), '', 'width=' + 954 + ',height=' + 726 + ',menubar=no,scrollbars=no,toolbar=no,location=no,directories=no,resizable=no,left=50,top=50');
    else
        window.open('GamePage.aspx?cid=' + _CompetitionID + '&cseq=' + _CompetitionSeq + '&ser=' + _ServerID + '&gseq=' + _GameSeq + '&rm=' + _RematchCompetitionSeq + '&at=1&dummy=' + MakeDumy(), '', 'width=' + 954 + ',height=' + 726 + ',menubar=no,scrollbars=no,toolbar=no,location=no,directories=no,resizable=no,left=50,top=50');
}


function ShowGratificationMessage() {
    var Message = Messages.GratificationMessage;
    mainScreen.ShowConfirm('', '', Message);
}

function iBoxCompetitionDetailsCallback() {
}

// Opens the popup of a friend profile
function openCompetitionDetailsPopup(competitionSeq) {
    iBox.clearEventListeners('documentLoad');
    iBox.addEventListener('documentLoad', iBoxCompetitionDetailsCallback);
    iBox.showURL('CompetitionDetails.aspx?compseq=' + competitionSeq, '', { width: '666', height: '510' });
}

