﻿var dialogId = 0;
var msgBoxId = 0;
var empireStatus;

function ScrollToBottom(what)
{
   var ob = document.getElementById(what)
   ob.scrollTop = ob.scrollHeight;
}

function SendComm(empireId)
{
   var select = document.getElementById('_PageControl__SelectPlayer');
   var text = document.getElementById('_SubspaceText');
   
   if(text.value == null || text.value == '') return;
   
   var Command = "SendComm(*¦*" + empireId + "*¦*" + select.options[select.selectedIndex].value + "*¦*" + text.value + "*¦*)";
   var Data = new Object;
   
   Data.CommandName = "SendComm";
   Data.EmpireID = empireId;
   
   AjaxExecute(Command, Data);   
}


function RefreshComm(empireId)
{
   var Command = "RefreshComm(*¦*" + empireId + "*¦*)";
   var Data = new Object;
   
   Data.CommandName = "RefreshComm";
   Data.EmpireID = empireId;
   
   AjaxExecute(Command, Data);   
}

function DeleteComm(commId)
{
   if(confirm("Are you sure you want to delete the communication?"))
   {
      var Command = "DeleteComm(*¦*" + commId + "*¦*)";
      var Data = new Object;
      
      Data.CommandName = "DeleteComm";
      Data.CommID = commId;
      
      AjaxExecute(Command, Data);   
   }
}

function SpawnMembership()
{
   dialogId = openNewPopup('_EmpireMembership', 0, 1);

}

function InfoDialog(acid)
{
   var Command = "GetAccountInfo(*¦*" + acid + "*¦*)";
   var Data = new Object;
   
   Data.CommandName = "GetAccountInfo";
   showInfoDialog();
   
   AjaxExecute(Command, Data);
}

function LeaveEmpire(playerId, slotNumber)
{
   if(confirm("Are you sure you wish to remove this character from it's current empire?"))
   {
      var Command = "LeaveEmpire(*¦*" + playerId + "*¦*)";
      var Data = new Object;
      
      Data.CommandName = "LeaveEmpire";
      Data.PlayerID = playerId;
      Data.SlotNumber = slotNumber;
     
      AjaxExecute(Command, Data);    
   }
}

function JoinEmpire(playerId, empireId, slotNumber)
{
   if(confirm("Are you SURE you wish to join this empire?"))
   {
      var Command = "JoinEmpire(*¦*" + playerId + "*¦*" + empireId + "*¦*)";
      var Data = new Object;
      
      Data.CommandName = "JoinEmpire";
      Data.PlayerID = playerId;
      Data.SlotNumber = slotNumber;
     
      AjaxExecute(Command, Data);    
   }
}

//var statuses = new EMPIRE_STATUS();

function RequestJoinEmpire(playerId, empireId, slotNumber)
{
   if(confirm("Are you SURE you wish to request to join this empire?"))
   {
      var Command = "RequestJoinEmpire(*¦*" + playerId + "*¦*" + empireId + "*¦*)";
      var Data = new Object;
      
      Data.CommandName = "RequestJoinEmpire";
      Data.PlayerID = playerId;
      Data.SlotNumber = slotNumber;
     
      AjaxExecute(Command, Data);    
   }
}

function CancelJoinRequest(playerId, empireId, slotNumber)
{
   if(confirm("Are you SURE you wish to cancel your request to join this empire?"))
   {
      var Command = "CancelJoinRequest(*¦*" + playerId + "*¦*" + empireId + "*¦*)";
      var Data = new Object;
      
      Data.CommandName = "CancelJoinRequest";
      Data.PlayerID = playerId;
      Data.SlotNumber = slotNumber;
     
      AjaxExecute(Command, Data);    
   }
}

function ToggleButton(slotNumber, leaveJoin)
{
   //leaveJoin : 0 - show leave, 1 - show join 
   
   var buttonString;
   var offButtonString, offButtonStringA;
   var numberString;
   
   if(leaveJoin == 0)
   {
      buttonString = 'Leave';
      offButtonString = 'Join';
      offButtonStringA = 'Cancel';
   }
   
   if(leaveJoin == 1)
   {
      buttonString = 'Join'; 
      offButtonString = 'Leave';
      offButtonStringA = 'Cancel';
   }
   
   if(leaveJoin == 2)
   {
      buttonString = 'Cancel'; 
      offButtonString = 'Leave';
      offButtonStringA = 'Join';
   }   
   
   switch(slotNumber)
   {
      case 1:
         numberString = 'One';
         break;
         
      case 2:
         numberString = 'Two';
         break;
         
      case 3:
         numberString = 'Three';
         break;                  
   }   
   
   document.getElementById('_PageControl__Player' + numberString + buttonString).className = "Hand";
   document.getElementById('_PageControl__Player' + numberString + offButtonString).className = "Hide";
   document.getElementById('_PageControl__Player' + numberString + offButtonStringA).className = "Hide";
}

function EmpireCallBack(result, Data)
{  
   if (Data.CommandName == "SendComm")
   {
      setTimeout(function() { RefreshComm(Data.EmpireID); }, 100);
   }  

   if (Data.CommandName == "RefreshComm")
   {
      var DataClient = document.getElementById('_SubspaceDataClient');
      DataClient.innerHTML = result;
      ScrollToBottom('_SubspaceDataClient');
   }  

   if (Data.CommandName == "DeleteComm")
   {
      if(result == "1")
      {
         var Line = document.getElementById('_PageControl__SubspaceComm__CommList__Comm' + Data.CommID + '__CommLine');
         
         if(Line)
            Line.className = 'Hide';
         else
         {
            Line = document.getElementById('_CommList__Comm' + Data.CommID + '__CommLine');
            if(Line)
               Line.className = 'Hide';
         }
      }
   }  
   
   if (Data.CommandName == "LeaveEmpire")
   {
      ToggleButton(Data.SlotNumber, 1);
   }      
   
   if (Data.CommandName == "JoinEmpire")
   {
      ToggleButton(Data.SlotNumber, 0);
   }         
   
   if (Data.CommandName == "CancelJoinRequest")
   {
      ToggleButton(Data.SlotNumber, 1);
   }
   
   if (Data.CommandName == "RequestJoinEmpire")
   {
      ToggleButton(Data.SlotNumber, 2);
   }               
   
   if (Data.CommandName == "GetAccountInfo")
   {
      document.getElementById("InfoBox_ContentArea").innerHTML = result;
   }         
   
}

function EmpireAjaxError(message, Data)
{
  alert("Exception :\n" + message);
  
}