// Main javascript functions.
// Created By Neeraj R on 21 June 2007

// Path to the Date time picker Script
   var dtppath = "<script language=\"javascript\" type=\"text/javascript\" src=\"Scripts/datetimepicker.js\"></script>";
   document.write(dtppath);

//##### Function used for checkbox selection and deselections Starts here

  function allselect()
   {
    // Function for check all and check none 

     if(document.frm1.all.checked)
      {
       for(i=0;i<document.frm1.elements.length;i++)
        {
         if(document.frm1.elements[i].id.indexOf('check')>-1)
          {
           document.frm1.elements[i].checked = true;
          }
        }
      }
     else
      {
       for(i=0;i<document.frm1.elements.length;i++)
        {
         if(document.frm1.elements[i].id.indexOf('check')>-1)
          {
           document.frm1.elements[i].checked = false;
          } 
        }
      } 
    } 
 
   function rcheck(id)
    {
     // Function for Uncheck or Check the 'All select Check box ' when a check box is checked or unchecked

      flag = 1;
      if(document.frm1.all.checked)
       {
        document.frm1.all.checked = false;
       }  
      else
       {
        for(i=0;i<document.frm1.elements.length;i++)
         {
          if(document.frm1.elements[i].id.indexOf('check')>-1)
           {
            if(document.frm1.elements[i].checked == false)
             { 
              flag = 0;
             }
           } 
         }
        if(flag)
         {
          document.frm1.all.checked = true;
         }
       } 
     }  

//##### Function used for checkbox selection and deselections  Ends here

// #### AJAX Functions

// AJAX Common Functions.
   var xmlHttp;
   var xmlHttpChat;

   function GetXmlHttpObject()
    { 
	var objXMLHttp = null;
	if (window.XMLHttpRequest)
	 {
	    objXMLHttp=new XMLHttpRequest()
	 }
	else if (window.ActiveXObject)
	 {
	   //for IE
	   objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	 }
	return objXMLHttp
    }

//COMMON FUNCTION ENDS

// #### AJAX Function for GAME paging

   function showPage(id)
    { 
      xmlHttp = GetXmlHttpObject();
      if (xmlHttp == null)
       {
	 alert ("Browser does not support HTTP Request");
 	 return;
       } 
      var srhFld   = document.getElementById("srhField").value;
      var srhValue = document.getElementById("srhValue").value;
      var url      = document.getElementById("AjaxUrl").value;
      //var url      = "gameList.php";
          url      = url+"?from1="+id+"&srhfld="+srhFld+"&srh="+srhValue;
	  xmlHttp.onreadystatechange=stateChanged;
	  xmlHttp.open("GET",url,true);
	  xmlHttp.send(null);

    }
 
   function stateChanged() 
    {
       if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
        {
	   document.getElementById("resultAjax").innerHTML=xmlHttp.responseText; 
        } 
     } 

//Added on 18 July 2007
// #### AJAX Function for checking status of Real Time GAME 

   function realTimeStatusCheck()
    { 
      xmlHttp = GetXmlHttpObject();
      if (xmlHttp == null)
       {
	 alert ("Browser does not support HTTP Request");
 	 return;
       } 
      var url      = "tourneys/realTimeStatusCheck.php";
          url     += "?pid="+pid+'&gid='+gid;
	 xmlHttp.onreadystatechange=stateChanged_1;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
    }
 
   function stateChanged_1() 
    {
       if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
        {
          if(xmlHttp.responseText != "")
           {
	     document.getElementById("gmMessage").innerHTML = xmlHttp.responseText+'<br>'; 
             if(xmlHttp.responseText.indexOf("Running") != -1)
              {
                document.getElementById("gameCod").style.display="block";
              }
             if(xmlHttp.responseText.indexOf("OVER") != -1)
              {
                document.getElementById("gameCod").style.display="none";
              }
           }
           setTimeout('realTimeStatusCheck()', 2000);
        } 
     } 

//Added on 18 July 2007
// #### AJAX Function for Score based game Status

   function scoreBasedStatusCheck()
    { 
      xmlHttp = GetXmlHttpObject();
      if (xmlHttp == null)
       {
	 alert ("Browser does not support HTTP Request");
 	 return;
       } 
      var url      = "tourneys/scoreBasedStatus.php";
          url     += "?pid="+pid+'&gid='+gid;
	 xmlHttp.onreadystatechange=stateChanged_2;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
    }
 
   function stateChanged_2() 
    {
       if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
        {
          if(xmlHttp.responseText != "")
           {
	     document.getElementById("gmMessage").innerHTML = xmlHttp.responseText+'<br>'; 
             if(xmlHttp.responseText.indexOf("Running") != -1)
              {
                document.getElementById("gameCod").style.display="block";
              }
             if(xmlHttp.responseText.indexOf("OVER") != -1)
              {
                document.getElementById("gameCod").style.display="none";
              }
           }
           setTimeout('scoreBasedStatusCheck()', 2000);
        } 
     } 

//AJAX Functions Used for CHAT sections

   function AddMsg(chat, pid, atoid)
    {
       xmlHttpChat = GetXmlHttpObject();
       if (xmlHttpChat == null)
        {
          alert ("Browser does not support HTTP Request");
          return;
        }
//       var toId = document.chatform.toid.value; 
       var toId = atoid;
       var url  = "tourneys/listMessag.php?chat="+chat+"&playId="+pid+"&toId="+toId;
       xmlHttpChat.open("GET",url,true);
       xmlHttpChat.onreadystatechange=stateChanged_addChat;
       xmlHttpChat.send(null);
    }

   function stateChanged_addChat() 
    { 
      if (xmlHttpChat.readyState==4 || xmlHttpChat.readyState=="complete")
      {
         document.getElementById("chatMain").innerHTML=xmlHttpChat.responseText;
         document.getElementById("chatMain").scrollTop = document.getElementById("chatMain").scrollHeight;
      }
    }
   function dispMsg()
    {
      xmlHttpChat1 = GetXmlHttpObject();
      if (xmlHttpChat1 == null)
       {
         alert ("Browser does not support HTTP Request");
         return;
       }
//      var toId = document.chatform.toid.value;
        var toId = document.chatform.channel.options[document.chatform.channel.selectedIndex].value;

      var pid  = document.chatform.plid.value;
      var url  = "tourneys/listMessag.php";
          url += "?playId="+pid+"&toId="+toId;
          url += "&bc=" + Math.floor(Math.random()*10000);
      xmlHttpChat1.open("GET",url,true);
      xmlHttpChat1.onreadystatechange=stateChanged_chat;
      xmlHttpChat1.send(null);
    }

   function stateChanged_chat() 
    { 
      if (xmlHttpChat1.readyState==4 || xmlHttpChat1.readyState=="complete")
      {
         //alert("chat");
         document.getElementById("chatMain").innerHTML=xmlHttpChat1.responseText;
         document.getElementById("chatMain").scrollTop = document.getElementById("chatMain").scrollHeight;
         xmlHttpChat1 = null;
         setTimeout("dispMsg()",2000);
      }
    }

   function popUp(pid)
    {
      var xmlHttpPop1 = GetXmlHttpObject();
      if (xmlHttpPop1 == null)
       {
         alert ("Browser does not support HTTP Request");
         return;
       }
      var url = "tourneys/popUp.php?player="+pid;

      xmlHttpPop1.onreadystatechange=function() { 
      if (xmlHttpPop1.readyState==4 || xmlHttpPop1.readyState=="complete")
        { 
          var Str;
          Str = xmlHttpPop1.responseText;
          strArray = Str.split("#");
          for(var i=0;i<(strArray.length)-1;i++)
           {
             win = 'newwind'+strArray[i]; 
             win = window.open('userChat.php?action=new&toid='+strArray[i],'newwind'+strArray[i],'left=20,top=20,width=380,height=500,toolbar=1,resizable=0,scrollbars=yes');
             if (win && win.open && !win.closed) win.moveTo(0, 0);
           }
         } 
       }
      xmlHttpPop1.open("GET",url,true);
      xmlHttpPop1.send(null);
      setTimeout("popUp("+pid+")",3000);
    }

// function for displaying the current Server Time
   function currentServerTime()
    {
      var xmlHttpTime = GetXmlHttpObject();
      if (xmlHttpTime == null)
       {
         alert ("Browser does not support HTTP Request");
         return;
       }
      var url = "tourneys/currentServerTime.php";

      xmlHttpTime.onreadystatechange=function() { 
      if (xmlHttpTime.readyState==4 || xmlHttpTime.readyState=="complete")
        {
          document.getElementById("serverTime").innerHTML = xmlHttpTime.responseText;
        } 
       }
      xmlHttpTime.open("GET",url,true);
      xmlHttpTime.send(null);
      setTimeout("currentServerTime()",2000);
    }

//added on 31 Aug 
// Function for updating the list of Real time tournaments

   function ListRealTour()
    {
      var xmlHttpReal = GetXmlHttpObject();
      if (xmlHttpReal == null)
       {
         alert ("Browser does not support HTTP Request");
         return;
       }
      var url = "tourneys/TournamentList.php?ID=REAL";

      xmlHttpReal.onreadystatechange=function() { 
      if (xmlHttpReal.readyState==4 || xmlHttpReal.readyState=="complete")
        {
          document.getElementById("RealTour").innerHTML = xmlHttpReal.responseText;
        } 
       }
      xmlHttpReal.open("GET",url,true);
      xmlHttpReal.send(null);
      setTimeout("ListRealTour()",10000);
    }

//added on 31 Aug 
// Function for updating the list of  Score time tournaments

   function ListScoreTour()
    {
      var xmlHttpScore = GetXmlHttpObject();
      if (xmlHttpScore == null)
       {
         alert ("Browser does not support HTTP Request");
         return;
       }
      var url = "tourneys/TournamentList.php?ID=SCORE";

      xmlHttpScore.onreadystatechange=function() { 
      if (xmlHttpScore.readyState==4 || xmlHttpScore.readyState=="complete")
        {
          document.getElementById("ScoreTour").innerHTML = xmlHttpScore.responseText;
        } 
       }
      xmlHttpScore.open("GET",url,true);
      xmlHttpScore.send(null);
      setTimeout("ListScoreTour()",10000);
    }


//AJAX Functions ENDS


//Added on 27 June 2007

  function setSearch()
   {
     document.getElementById("srhField").value = document.getElementById("srhField1").value;
     document.getElementById("srhValue").value = document.getElementById("srhValue1").value;
   }

// Added on 12 Sep 2007
// Function used for popping out the game window

   function popOut(Gmid)
   {
     var fram = document.getElementById("gmFrm");
     var url  = fram.src;
         url += '&ID='+Gmid;
     var win  = 'gamwind';
     if (!(win && win.open && !win.closed))
      {
        win = window.open('/flashgames/gamePop.php?game='+url,'gamwind','left=20,top=20,width=682,height=620,toolbar=1,resizable=no,  scrollbars=yes');
      } 
     else
      {
        win.moveTo(0, 0);
      }
   }
