﻿
//===========================================================================
//============== This is for coloring occupied square in RS-Game ============
//===========================================================================

        var secs=60,TimerID,TimerID1;
        var xmlHttp_DB = null;
        var xmlHttp_DB1 = null;
        function CallTimer()
        {
            if(secs==60)
            {
                TimerID=setInterval("CallTimer()",1000);
            }
            secs--;
            if(secs==0)
            {
                secs=60;
                clearInterval(TimerID);
            }
        }
        
        function test(value)
        {
            var containerDiv = document.getElementById("divGrid");
            
            var coords = new Array();
            var index = value.indexOf(',');
            var i = 0;
            
            while(index > 0)
            {
                //alert(value.slice(0,index));
                coords[i] = value.slice(0,index);
                value = value.slice(index + 1);
                index = value.indexOf(',');
                //alert(value);
                i++;
            }
            if(index < 0)
            {
                coords[i] = value;
            }
            //alert("Value : " + coords[0]);
            
            var imgYellow = document.createElement('IMG');
            
            //imgYellow.setAttribute('id','imgYellow');window.pageYOffset
			imgYellow.style.position = "absolute";
			imgYellow.setAttribute('src','images/Green_box.gif');
			imgYellow.style.top = (parseInt(coords[1]) + 11) + "px";
			imgYellow.style.left = (parseInt(coords[0]) + 131) + "px";
			
			imgYellow.style.width = "10px";
			imgYellow.style.height = "10px";
			//imgYellow.style.border = "solid 1px Red";
            containerDiv.appendChild(imgYellow);
            
            //alert("Value_Top : " + coords[1] + ", Value_Left : " + coords[0]);
        }
        function Minimize()
        {
            window.innerWidth = 100;
            window.innerHeight = 100;
            window.screenX = screen.width;
            window.screenY = screen.height;
            alwaysLowered = true;
        }

        function Maximize()
        {
            window.innerWidth = screen.width;
            window.innerHeight = screen.height;
            window.screenX = 0;
            window.screenY = 0;
            alwaysLowered = false;
        }
        
        var xmlHttp = null;
        function isFirstTime()
        {
            document.getElementById('unAuth').style.display='none';
            document.getElementById('fade').style.display='none';
            document.getElementById('light').style.display='none';
//            //========= This is for checking wheather user is logged in or not and is it first visit or not?? =======
//            try
//            {
//                try
//                {
//                    xmlHttp = new XMLHttpRequest();
//                }
//                catch(e)
//                {
//                    try
//                    {
//                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
//                    }
//                    catch(err)
//                    {
//                        alert("This browser is not 'XMLHttp' compatible!");
//                        return;
//                    }
//                }
//                
//                var url = "GetAjaxValue.aspx";
//                var data = "c=1";
//                xmlHttp.open("POST", url, true);
//                xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//                xmlHttp.send(data);
//                
//                xmlHttp.onreadystatechange = AfterComplete;
//            }
//            catch(Error)
//            {
//                alert("Error : " + Error);
//            }
        }
        
        function AfterComplete()
        {
            try
            {
                if(xmlHttp.readyState == 4)
                {
                    if(xmlHttp.responseText == "Exist")
                    {
                        document.getElementById('unAuth').style.display='none';
                        document.getElementById('fade').style.display='none';
                        document.getElementById('light').style.display='none';
                    }
                    else if(xmlHttp.responseText == "Not-Exist")
                    {
                        document.getElementById('unAuth').style.display='none';
                        document.getElementById('fade').style.display='block';
                        document.getElementById('light').style.display='block';
                    }
                    else if(xmlHttp.responseText == "Un-Registered")
                    {
                        document.getElementById('unAuth').style.display='block';
                        document.getElementById('fade').style.display='block';
                        document.getElementById('light').style.display='none';
                    }
                    else
                    {
                        document.getElementById('unAuth').style.display='block';
                        document.getElementById('fade').style.display='block';
                        document.getElementById('light').style.display='none';
                        //alert(xmlHttp.responseText);
                    }
                }
            }
            catch(e)
            {
                
            }
        }
        
        function isRadioChk(radioButNum)
        {
            try
            {
                if(radioButNum=="0")
                {
                    document.getElementById('radioBut_0').checked = true;
                    document.getElementById('radioBut_1').checked = false;
                }
                else if(radioButNum=="1")
                {
                    document.getElementById('radioBut_0').checked = false;
                    document.getElementById('radioBut_1').checked = true;
                }
                else
                {
                    alert(radioButNum);
                }
            }
            catch(e)
            {
                alert('Error : '+e);
            }
        }
        
        function getvalue()
        {
            try
            {
                if(checkValidity())
                {
                    var txtColor = document.getElementById('txtColor').value;
                    var txtFavGame = document.getElementById('txtFavGame').value;
                    var drpAction = document.getElementById('drpAction').value;
                    var emailAlert = 'true';
                    
                    if(document.getElementById('radioBut_1').checked)
                    {
                        emailAlert = 'false';
                    }
                    
                    //alert('Color : ' + txtColor + ', FavGame : ' + txtFavGame + ', DrpAction : ' + drpAction + ', EmailAlert : ' + emailAlert);
                    
                    //===== Send All data to database using AJAX ======//
                    
                    document.getElementById('light').style.display='none';
                    sendToDb(txtColor,txtFavGame, drpAction, emailAlert);
                    
                    
                    //document.getElementById('light').style.display='none';
                    //document.getElementById('fade').style.display='none';
                }
                else
                {
                    return false;
                }
            }
            catch(e)
            {
                alert("Error : " + e);
                document.getElementById('light').style.display='block';
                document.getElementById('fade').style.display='block';
                return false;
            }
        }
        
        var xmlHttpDB = null;
        function sendToDb(dC, fG, dA, eA)
        {
            //========= This is for sending data to DataBase =======//
            try
            {
                try
                {
                    xmlHttpDB = new XMLHttpRequest();
                }
                catch(e)
                {
                    try
                    {
                        xmlHttpDB = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch(err)
                    {
                        alert("This browser is not 'XMLHttp' compatible!");
                        return;
                    }
                }
                
                var url = "GetAjaxValue.aspx";
                var data = "dC=" + dC + "&fG=" + fG + "&dA=" + dA + "&eA=" + eA;
                xmlHttpDB.open("POST", url, true);
                xmlHttpDB.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xmlHttpDB.send(data);
                
                xmlHttpDB.onreadystatechange = onSuccessFullComplete;
            }
            catch(Error)
            {
                alert("Error : " + Error);
            }
        }
        function onSuccessFullComplete()
        {
            try
            {
                if(xmlHttpDB.readyState == 4)
                {
                    if(xmlHttpDB.responseText == "true")
                    {
                        document.getElementById('light').style.display='none';
                        document.getElementById('fade').style.display='none';
                    }
                    else if(xmlHttpDB.responseText == "false")
                    {
                        document.getElementById('light').style.display='block';
                        document.getElementById('fade').style.display='block';
                    }
                }
            }
            catch(e)
            {
                alert(e);
            }
        }
        
        function checkValidity()
        {
            if(document.getElementById('drpAction').value != "Select")
            {
                document.getElementById('spnStar').style.display='none';
                return true;
            }
            else
            {
                document.getElementById('spnStar').style.display='block';
                return false;
            }
        }
        function DynTopSet()
        {
            try
            {
                var docHeight = getDocumentSize('height');
                var whiteDivTop = (docHeight * 0.25) + Get_Scroller_Value('Top');
                document.getElementById('fade').style.top = Get_Scroller_Value('Top') + "px";
                document.getElementById('light').style.top = whiteDivTop + "px";
                document.getElementById('unAuth').style.top = whiteDivTop + "px";
            }
            catch(e)
            {
                document.getElementById('fade').style.top = Get_Scroller_Value('Top') + "px";
                alert(e);
            }
        }
        
        function squareClicked(sqrId)
        {
            //alert(sqrId);
//            if(secs==60)
//            {
                //CallTimer();
                document.getElementById('divLoadClick').style.display='block';
                document.getElementById('fade').style.display = 'block';
                //=== From here 
                
                //esolzAJAX("POST", "GetAjaxValue.aspx","data");
                xmlHttpDB = null;
                try
                {
                    try
                    {
                        xmlHttpDB = new XMLHttpRequest();
                    }
                    catch(e)
                    {
                        try
                        {
                            xmlHttpDB = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        catch(err)
                        {
                            alert("This browser is not 'XMLHttp' compatible!");
                            return;
                        }
                    }
                    
                    var url = "GetAjaxValue.aspx";
                    var data = "si=" + sqrId;
                    xmlHttpDB.open("POST", url, true);
                    xmlHttpDB.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    xmlHttpDB.send(data);
                    
                    xmlHttpDB.onreadystatechange = function(){ clickOnSquare(sqrId); };
                    document.getElementById('hidSqrId').value = sqrId;
                }
                catch(Error)
                {
                    alert("Error : " + Error);
                }
//            }
//            else
//            {
//                alert("Wait for " + secs +"(sec) for next move!!!");
//            }
        }
        function clickOnSquare(sqrId)
        {
            try
            {
                if (xmlHttpDB.readyState == 4)
                {
                    var response;
                    if(xmlHttpDB.responseText != null)
                    {
                        response = xmlHttpDB.responseText;
                        
                        if(response == "Success")
                        {
                            response = "<b>Play Game and Own the Square("+ sqrId +")</b>";
                            document.getElementById('fade').style.display = 'block';
                            document.getElementById('divAction').style.display = 'block';
                            document.getElementById('divLoadClick').style.display='none';
                            document.getElementById('divMessage').innerHTML = response;
                            window.scrollTo(20,20);
                        }
                        else if(response == "Self_Taken")
                        {
                            response = "This Square Already Taken by You! Try another.";
                            
                            document.getElementById('divLoadClick').style.display='none';
                            alert(response);
                            //==== Here need to check weather that square is accured by himself or other?? If other then this message is ok, otherwise it need to change
                            
                            document.getElementById('divResult').style.display = 'none';
                            document.getElementById('fade').style.display = 'none';
                            document.getElementById('divAction').style.display = 'none';
                        }
                        else if(response == "ERROR")  
                        {
                            alert(response);
                            document.getElementById('fade').style.display = 'none';
                            document.getElementById('divAction').style.display = 'none';
                            document.getElementById('divLoadClick').style.display='none';
                        }
                        else if(response == "Session_Expired")
                        {
                            alert("Login Session has expired! Please Login to play RS Game.");
                            
                            //==== Show the Login Provision =====//
                            document.getElementById('unAuth').style.display='block';
                            document.getElementById('fade').style.display='block';
                            document.getElementById('light').style.display='none';
                            document.getElementById('divResult').style.display = 'none';
                            document.getElementById('divAction').style.display = 'none';
                            document.getElementById('divLoadClick').style.display='none';
                        }
                        else //response == "Other_Taken" 
                        {
                            var uId = response;
                            response = "This Square Already Taken by Other User!" + response;
                            //alert(response);
                            //==== Here we need to provide provision for asking for re-taking new visitor
           			        divClose("divLoading");
                            DisplaySquareOwnerDetails(sqrId,uId);
                        }
                    }
                    else
                    {
                        alert("Sorry no response from Server.");
                        document.getElementById('divLoadClick').style.display='none';
                        document.getElementById('fade').style.display = 'none';
                    }
                }
            }
            catch(err)
            {
                alert(err);
            }
        }
        function DisplaySquareOwnerDetails(sqrId,userId)
        {
            xmlHttp_DB = null;
            try
            {
                try
                {
                    xmlHttp_DB = new XMLHttpRequest();
                }
                catch(e)
                {
                    try
                    {
                        xmlHttp_DB = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch(err)
                    {
                        alert("This browser is not 'XMLHttp' compatible!");
                        return;
                    }
                }
                
                var url = "GetAjaxValue.aspx";
                var data = "squareId=" + sqrId;
                xmlHttp_DB.open("POST", url, true);
                xmlHttp_DB.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xmlHttp_DB.send(data);
                xmlHttp_DB.onreadystatechange = function() { onSuccessDisplaySquareOwnerDetails(sqrId,userId); };
            }
            catch(Error)
            {
                alert("Error : " + Error);
            }
        }
        
        function onSuccessDisplaySquareOwnerDetails(sqrId,userId) 
        {
            try
            {
                if (xmlHttp_DB.readyState == 4)
                {
                    var xmlDoc = null;
                    if(xmlHttp_DB.responseXML != null)
                    {
                        xmlDoc = xmlHttp_DB.responseXML.documentElement;
                        var Name = xmlDoc.getElementsByTagName("Name")[0].childNodes[0].nodeValue;
                        var Age = xmlDoc.getElementsByTagName("Age")[0].childNodes[0].nodeValue;
                        var Gender = xmlDoc.getElementsByTagName("Gender")[0].childNodes[0].nodeValue;
                        var Country = xmlDoc.getElementsByTagName("Country")[0].childNodes[0].nodeValue;
                        var Points = xmlDoc.getElementsByTagName("Points")[0].childNodes[0].nodeValue;
                        var UserImg = xmlDoc.getElementsByTagName("UserImg")[0].childNodes[0].nodeValue;
                        var GameId = xmlDoc.getElementsByTagName("GameId")[0].childNodes[0].nodeValue;
                        var GameName = xmlDoc.getElementsByTagName("GameName")[0].childNodes[0].nodeValue;
                        var Score = xmlDoc.getElementsByTagName("Score")[0].childNodes[0].nodeValue;
                        var PlayDate = xmlDoc.getElementsByTagName("PlayDate")[0].childNodes[0].nodeValue;
                        
                        var other_Info = "<b> "+ Age + "/" + Gender + "/" + Country + ",<br/>" + Points + " RS Points, <br/> Game Played : " + GameName + ",<br/>Score : " + Score + ",<br/> Play Date : "+ PlayDate +"</b> <br/>";
                        getSquareOwnerInfo(Name,UserImg,other_Info);
                        document.getElementById('spanUserName').innerHTML = Name;
                        document.getElementById('spanFlashGame').innerHTML = GameName;
                        if(GameId == 'swimrace' || GameId == 'buzzer')
                        {
                            document.getElementById('spnMoreLess').innerHTML='less';
                        }
                        else
                        {
                            document.getElementById('spnMoreLess').innerHTML='more';
                        }
                        document.getElementById('spanFlashGameScore').innerHTML = Score;
                        window.scrollTo(20,20);
                    }
                    else
                    {
                       alert("Error, no data found!!!");
                    }
                }
            }
            catch(err)
            {
                alert(err);
            }
        }

        function getSquareOwnerInfo(userName, imgSrc, otherInfo)
        {
            try
            {
		        document.getElementById('imgUserPic').setAttribute('src',imgSrc);
		        //document.getElementById('spanUser').style.width = "130px";
		        document.getElementById('spanUser').style.textAlign = "left";
		        document.getElementById('spanUser').style.padding = "3px 2px 3px 3px";
//		        document.getElementById('spanUser').style.cssFloat = "right";   //=== This is for Mozilla =====//
//		        document.getElementById('spanUser').style.styleFloat = "right"; //=== This is for IE =====//
		        document.getElementById('spanUser').style.fontSize = "10px";
		        document.getElementById('spanUser').style.color = "Black";
		        document.getElementById('spanUser').style.fontWeight = "bold";
		        document.getElementById('spanUser').style.fontFamily = "Arial";
		        document.getElementById('tdUserName').innerHTML = userName;
		        document.getElementById('spanUser').innerHTML = otherInfo;
		        document.getElementById('divResult').style.display = 'block';
                document.getElementById('fade').style.display = 'block';
                document.getElementById('divAction').style.display = 'none';
                document.getElementById('divLoadClick').style.display='none';
		        //========= Loading Div Close =========//
		        divClose("divLoading");
		    }
		    catch(err)
		    {
		        alert("Error :: " + err);
		    }
        }
        function showDetail(isClose)
        {
            try
            {
                var y = document.getElementById('divResultChart');
                if(y!=null)
                {
                    if(isClose == 'yes')
                    {
                        document.getElementById('divResultChart').style.display = 'none';
                        document.getElementById('ancClose').style.display = 'none';
                        document.getElementById('ancShow').style.display = 'block';
                    }
                    else if(isClose == 'no')
                    {
                        document.getElementById('divResultChart').style.display = 'block';
                        document.getElementById('ancClose').style.display = 'block';
                        document.getElementById('ancShow').style.display = 'none';
                    }
                }
             }
             catch(err)
             {
                alert(err);
             }
        }
        function NewTurn()
        {
            document.getElementById('fade').style.display = 'none';
            document.getElementById('divAction').style.display = 'none';
            document.getElementById('divResult').style.display = 'none';
            document.getElementById('divResultChart').style.display = 'none';
       		window.scrollTo(20,20);
        }
        
        
        function FetchResult1()
        {
            xmlHttp_DB = null;
            try
            {
                try
                {
                    xmlHttp_DB = new XMLHttpRequest();
                }
                catch(e)
                {
                    try
                    {
                        xmlHttp_DB = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch(err)
                    {
                        alert("This browser is not 'XMLHttp' compatible!");
                        return;
                    }
                }
                
                var url = "GetAjaxValue.aspx";
                var data = "FetchResult=" + document.getElementById('hidSqrId').value;
                xmlHttp_DB.open("POST", url, true);
                xmlHttp_DB.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xmlHttp_DB.send(data);
                xmlHttp_DB.onreadystatechange = onSuccessFetchResult1;
            }
            catch(Error)
            {
                //alert("Error : " + Error);
            }
        }
//        function onClick(val)
//        {
//            var animStyle = 'slide'
//            $("#modalbody").text(val);
//            $('#msg1').notificationmsg({animation:animStyle});
//            $('#msg1').notificationmsg('show');
//        }
        function onSuccessFetchResult1()
        {
            try
            {
            //alert(xmlHttpDB.readyState);
                if (xmlHttp_DB.readyState == 4)
                {
                   var ResponseVal = new Array();
                   ResponseVal = xmlHttp_DB.responseText.split(':');
                   if(ResponseVal.length > 2)
                   {
                       if(ResponseVal[0] > 0)
                       {
                          var colorCode;
                          if(ResponseVal[1]=="M") colorCode = "#1569C7";
                          else if (ResponseVal[1] == "F") colorCode = "#F52887";
                          else colorCode = "#617C58";
                          document.getElementById(ResponseVal[2]).style.backgroundColor=colorCode;
//                          var strJs = "<script type=\"text/javascript\"> " +
//                                     " new HelpBalloon({ " +
//                                     " title: 'Game Ownership History'," +
//                                     " dataURL: 'GameHistory.aspx?imgId=" + ResponseVal[2] + "'," +
//                                     " cacheRemoteContent: false," +
//                                     " fixedPosition: HelpBalloon.POS_BOTTOM_LEFT," +
//                                     " useEvent: ['click']" +
//                                     " });" +
//                                     " </script>";
//                          document.getElementById('spn'+ResponseVal[2]).title='Click here to view game ownership history';
//                          document.getElementById('spn'+ResponseVal[2]).innerHTML = strJs;
                          //onClick('You have win the game');
                          if(TimerID1 != null || TimerID1 !="")
                          {
                            clearInterval(TimerID1);
                          }
                       }
                       else
                       if(ResponseVal[0]==0)
                       {
                          //onClick('You have lost the game');
                          if(TimerID1 != null || TimerID1 !="")
                          {
                              clearInterval(TimerID1);
                          }
                       }
                   }  
                }
            }
            catch(e)
            {
                alert(e);
            }
        }
        function FetchResult()
        {
            TimerID1=setInterval("FetchResult1()",1000);
        }
        function goNext()
        {
            NewTurn();
            if(TimerID1 != null || TimerID1 !="")
            {
                clearInterval(TimerID1);
            }
            FetchResult();
            showPopWin('/tmp/testGameLoad.aspx?sqId=' + document.getElementById('hidSqrId').value, 518, 642, returnRefresh);
            return false;
        }
        
        function onSuccessGoNext()
        {
            try
            {
                if (xmlHttpDB.readyState == 4)
                {
                    document.getElementById('hidAction').value = xmlHttpDB.responseText;
                    
                    document.getElementById('divNext').style.display = 'block';
                    document.getElementById('divResultChart').style.display = 'none';
                    document.getElementById('ancClose').style.display = 'none';
                    document.getElementById('ancShow').style.display = 'none';
                    document.getElementById('divLoadClick').style.display='none';
                    document.getElementById('fade').style.display = 'block';
                    document.getElementById('divAction').style.display='none';
                    document.getElementById('divMsgNext').innerHTML = "Occupying user set action of this square is : " + document.getElementById('hidAction').value;
                }
            }
            catch(e)
            {
                alert(e);
            }
        }
        
        //========================
        
        function checkValidityAction()
        {
            if(document.getElementById('selAction').value != "Select")
            {
                document.getElementById('spnStarAction').style.display = 'none';
                return true;
            }
            else
            {
                document.getElementById('spnStarAction').style.display = 'block';
                return false;
            }
        }
        
        function checkValidityActionNext()
        {
            if(document.getElementById('selNext').value != "Select")
            {
                document.getElementById('spnNext').style.display = 'none';
                return true;
            }
            else
            {
                document.getElementById('spnNext').style.display = 'block';
                return false;
            }
        }
        
function updateAction()
{
    try
    {
        if(checkValidityAction())
        {
            var gId = document.getElementById('selAction').options[document.getElementById('selAction').selectedIndex].value;
            NewTurn();
            FetchResult();
            showPopWin('/tmp/testGameLoad.aspx?gId='+gId+'&sqId=' + document.getElementById('hidSqrId').value, 518, 642, returnRefresh);
            return false;
        }
        else
        {
            return false;
        }
    }
    catch(e)
    {
        alert("Error : " + e);
        return false;
    }
}
function Acquire()
{
    try
    {
        if(checkValidityActionNext())
        {
            var occupaying_UserAction = document.getElementById('hidAction').value;
            var visiting_UserAction = document.getElementById('selNext').value;
            
            var outCome = getOutCome(occupaying_UserAction, visiting_UserAction);
            
            document.getElementById('divNext').style.display = 'none';
            
            if(outCome == 'Battle')
            {
                alert('Your OutCome is : Battle. So You have to play a Flash Game that will determine who will be winner.');
                //===== Here we need further info from client about flash game. Then move next.
            }
            else if(outCome == 'Transit')
            {
                alert('Your OutCome is : Transit.');
            }
            else if(outCome == 'Talk')
            {
                alert('Your OutCome is : Talk');
            }
            else if(outCome == 'Nothing')
            {
                alert('Error has occured due to some reason.');
            }
            
            CancelAction();
        }
    }
    catch(e)
    {
        alert(e);
    }
}
function CancelAction()
{
    document.getElementById('divNext').style.display='none';
    document.getElementById('divResultChart').style.display = 'none';
    document.getElementById('ancClose').style.display = 'none';
    document.getElementById('ancShow').style.display = 'none';
    document.getElementById('divLoadClick').style.display='none';
    document.getElementById('fade').style.display = 'none';
    document.getElementById('divAction').style.display='none';
}

function getOutCome(occuUserAction, visiUserAction)
{
    var outCome = 'Nothing';
    try
    {
        if(occuUserAction == 'Battle' || visiUserAction == 'Battle')
        {
            outCome = 'Battle';
        }
        else if(occuUserAction == 'Transit' || visiUserAction == 'Transit')
        {
            outCome = 'Transit';
        }
        else if(occuUserAction == 'Talk' || visiUserAction == 'Talk')
        {
            outCome = 'Talk';
        }
        else if(occuUserAction == 'Transit' && visiUserAction == 'Talk')
        {
            outCome = 'Transit';
        }
        else if(occuUserAction == 'Talk' && visiUserAction == 'Transit')
        {
            outCome = 'Talk';
        }
        else
        {
            outCome = 'Nothing';
        }
    }
    catch(e)
    {
        alert(e);
        outCome = 'Nothing';
    }
    return outCome;
}


        