var timestamp=0;
var classId = 0;

var ie = false;

function getMClass(myEl) {
    if (ie)
        return myEl.getAttribute('className') ;
    else
        return myEl.getAttribute('class') ;
}

function setMClass(myEl,cl) {
    if (ie)
        myEl.setAttribute('className',cl) ;
    else
        myEl.setAttribute('class',cl) ;
}

//Browser Support Code
function ajaxFunctionLive(contextPath) {
    var ajaxRequest;    
    
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
        
        if (navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
            ie = true ;
        }
    } 
    catch (e) {
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            ie = true;
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                ie = true ;
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function() {
        if(ajaxRequest.readyState == 4) {
            var jsonString = ajaxRequest.responseText;
            
            if (jsonString == "" ) {
                return;
            }
            
            var jsonObject = eval('(' + jsonString + ')');
            
            if (jsonObject.v != 0) {
                timestamp = jsonObject.v;
            }
            
            var liveSoccerGames = jsonObject.sLive;
            var liveBasketGames = jsonObject.bLive;
            var liveResultsTd = document.getElementById("liveResults");
            
            if (liveResultsTd != null) {
                var liveResultsHtml = "";
                if (liveSoccerGames > 0) {
                    liveResultsHtml += "<a href=\"DaoServlet?sport=soccer&liveonly&runningCoupon\">";
                    if (liveSoccerGames == 1) {
                        liveResultsHtml += liveSoccerGames + " αγώνας ποδοσφαίρου σε εξέλιξη<BR>";
                    } else if (liveSoccerGames > 1) {
                        liveResultsHtml += liveSoccerGames + " αγώνες ποδοσφαίρου σε εξέλιξη<BR>";
                    }
                    liveResultsHtml += "</a>";
                }
                if (liveBasketGames> 0) {
                    liveResultsHtml += "<a href=\"DaoServlet?sport=basket&liveonly&runningCoupon\">";                
                    if (liveBasketGames == 1) {
                        liveResultsHtml += liveBasketGames + " αγώνας μπάσκετ σε εξέλιξη<BR>";
                    } else if (liveBasketGames > 1)  {
                        liveResultsHtml += liveBasketGames + " αγώνες μπάσκετ σε εξέλιξη<BR>";
                    }
                }
                liveResultsTd.innerHTML = liveResultsHtml;
                liveResultsHtml += "</a>";
            }
            
            var sGamesLength = jsonObject.sGames.length;
            var bGamesLength = jsonObject.bGames.length;
            
            var innerHtmlText = "";
            
            if (sGamesLength > 0) {
                for (var i = 0; i < sGamesLength; i++) {
                    innerHtmlText += "<tr>";
                    innerHtmlText += "<td class='l" + classId +"'>" + jsonObject.sGames[i].id + "</td>";
                    innerHtmlText += "<td class='c" + classId +"'>" + jsonObject.sGames[i].teams + "</td>";
                    
                    var statusId = jsonObject.sGames[i].status;
                    if (statusId == 0) {
                        innerHtmlText += "<td class='r" + classId +"'>" + "ΔΞ" + "</td>";
                        innerHtmlText += "<td class='rr" + classId +"'>" + jsonObject.sGames[i].time + "</td>";
                    } else if (statusId == 1) {
                        innerHtmlText += "<td class='r" + classId +"'>" + "ΕΝ" + "</td>";
                        innerHtmlText += "<td class='rr" + classId +"'>" + jsonObject.sGames[i].score + "</td>";                        
                    } else if (statusId == 2) {
                        innerHtmlText += "<td class='r" + classId +"'>" + jsonObject.sGames[i].minute + "'" + "</td>";
                        innerHtmlText += "<td class='rr" + classId +"'>" + jsonObject.sGames[i].score + "</td>";
                    } else if (statusId == 3) {
                        innerHtmlText += "<td class='r" + classId +"'>" + "ΗΜ" + "</td>";
                        innerHtmlText += "<td class='rr" + classId +"'>" + jsonObject.sGames[i].score + "</td>";
                    } else if (statusId == 4) {
                        innerHtmlText += "<td class='r" + classId +"'>" + "ΤΛ" + "</td>";
                        innerHtmlText += "<td class='rr" + classId +"'>" + jsonObject.sGames[i].score + "</td>";
                    }
                    
                    innerHtmlText += "</tr>";
                    
                    if (classId == 1) {
                        classId = 0;
                    } else {
                        classId = 1;
                    }
                }
            }
            
            if (bGamesLength > 0) {
                for (var i = 0; i < bGamesLength; i++) {
                    innerHtmlText += "<tr>";
                    innerHtmlText += "<td class='l" + classId +"'>" + jsonObject.bGames[i].id + "</td>";
                    innerHtmlText += "<td class='c" + classId +"'>" + jsonObject.bGames[i].teams + "</td>";
                    var statusId = jsonObject.bGames[i].status;
                    if (statusId == 0) {
                        innerHtmlText += "<td class='r" + classId +"'>" + "ΔΞ" + "</td>";
                        innerHtmlText += "<td class='rr" + classId +"'>" + jsonObject.bGames[i].time + "</td>";
                    } else if (statusId == 1) {
                        innerHtmlText += "<td class='r" + classId +"'>" + "ΕΝ" + "</td>";
                        innerHtmlText += "<td class='rr" + classId +"'>" + jsonObject.bGames[i].score + "</td>";                        
                    } else if (statusId == 2) {
                        innerHtmlText += "<td class='r" + classId +"'></td>";
                        innerHtmlText += "<td class='rr" + classId +"'>" + jsonObject.bGames[i].score + "</td>";
                    } else if (statusId == 3) {
                        innerHtmlText += "<td class='r" + classId +"'>" + "ΗΜ" + "</td>";
                        innerHtmlText += "<td class='rr" + classId +"'>" + jsonObject.bGames[i].score + "</td>";
                    } else if (statusId == 4) {
                        innerHtmlText += "<td class='r" + classId +"'>" + "ΤΛ" + "</td>";
                        innerHtmlText += "<td class='rr" + classId +"'>" + jsonObject.bGames[i].score + "</td>";
                    }
                    innerHtmlText += "</tr>";
                    
                    if (classId == 1) {
                        classId = 0;
                    } else {
                        classId = 1;
                    }
                }
            }
            
            if (sGamesLength > 0 || bGamesLength > 0) {
                var gameTable = document.getElementById("gmdata");
                if (gameTable != null) {
                    var oldInnerHtmlText = gameTable.tBodies[0].innerHTML;
                    innerHtmlText += oldInnerHtmlText;
                }
                
                innerHtmlText = filterRows(innerHtmlText.toUpperCase(), innerHtmlText, 12);
                
                innerHtmlText = "<table id='gmdata' class='inlive'><tbody>" + innerHtmlText;
                innerHtmlText += "</tbody></table>";
                
                var liveGamesDiv = document.getElementById("liveGames");
                liveGamesDiv.innerHTML = innerHtmlText;
            }
        }
    }
    
    ajaxRequest.open("GET", contextPath + "/EasybetServlet?timestamp=" + timestamp + "&ts="+new Date().getTime(), true);
    ajaxRequest.send(null); 
}

function filterRows(htmlInput, htmlInputOrig, rowLimit) {
    var index;
    var tempStr = htmlInput;
    var index = 0;
    var htmlOutput = "";
    var globalIndex = 0;
    for(var i=0; i<=rowLimit; i++) {
        globalIndex += index+4;
        index = tempStr.indexOf('</TR>');
        if (index == -1) {
            break;
        }
        tempStr = tempStr.substring(index+5, tempStr.length);
    }
    if (index == -1) {
        htmlOutput = htmlInputOrig;
    } else {
        htmlOutput = htmlInputOrig.substring(0,globalIndex+1);
    }
    
    return htmlOutput;
}

function timeMsgLive(contextPath) {
    var t=setTimeout("timeAlertLive('"+contextPath+"')",1000);
}

function timeAlertLive(contextPath) {
    var t=setTimeout("timeAlertLive('"+contextPath+"')",30000);
    ajaxFunctionLive(contextPath);
}

document.getElementsByClassName = function(cl) {
    var retnode = [];
    var myclass = new RegExp('\\b'+cl+'\\b');
    var elem = this.getElementsByTagName("td");
    for (var t = 0; t < elem.length; t++) {
        var classes = elem[t].className;
        if (myclass.test(classes)) retnode.push(elem[t]);
    }
    return retnode;
}