﻿// JScript File
var m_MessageBoxSP = null;
var CurrentlyShowingMessageBoxSP = null;
var selectedGroupIdForCancelSP = null;

function KGSubscriptionControl_CallbackError(args, context, resultDiv)
{
  // Here we are dealing with authentication timeout before callback, 
  // the message from the server is 'elogin' signify an error with 
  // authentication. The response will be trimmed to form the args 
  // 'login' here..... 
  if(args == 'login')
  {
    window.location='home.aspx';
  }
  else
  {
    var controlDiv = document.getElementById(resultDiv);
    
    if(controlDiv != null)
      controlDiv.innerHTML = args;
    else
      alert('Callback on KGSubscriptionControl has failed: ' + args);
  }
}

// CallbackDone
function KGSubscriptionControl_CallbackDone_Wrapped(args, context, resultDiv)
{
	if(globalReloadCategories == true)
	{
		globalReloadCategories = false;
		setTimeout("HorizontalCategoriesControl_Reload();", 200);
	}
  // Check for a session time out before completing the callback
  if (CheckSessionTimeout())
    return;
   
  // extract all the cdatas lines from callback result
  var actions = args.split('[@|@]');
  args = args.substring(actions[0].length+5);

  var cdatas = args.split('<![CDATA[');
  
  // extract the predefined control client IDs
  var s = args.split('=');
  var ids = s[0].split('@');
  
  // search for the control container
  var holder = document.getElementById(resultDiv);
  
  // Test if the container is found
  // Yes: replace the innerHTML with the callback result
  //  No: call CallbackError with Content div not found error message
  if(holder != null)
  {
    // holder.innerHTML = args.substring(args.indexOf('=') + 1);
    holder.innerHTML = args;
    
    // loop through the cdatas and process each of them
    // ignoring the first one coz it is just HTML
    var z = 0;
    for(z = 1; z < cdatas.length; z++)
    {
      // javascript starting position
      var posStart = cdatas[z].indexOf('window');
      
      // javascript ending position 
      var posEnd = cdatas[z].lastIndexOf('}') + 1;
      
      if(posStart > -1 && posEnd > -1)
      {
        // attemp to extract the javascript
        var jscript = cdatas[z].substring(posStart, posEnd);
       
        // Now we have the potential jscript we need to execute it to 
        // reinitiallise the grid
        if(jscript.length != 0)
        {        
          // redefine the init script on Page
          eval(jscript);
          var posNameStart = jscript.indexOf('window');
          var posNameEnd = jscript.indexOf('=');
          
          var functionName = jscript.substring(posNameStart, posNameEnd);
          // execute the script
          eval(functionName+'()');
        }// end if(jscript.....
      }// end if(posStart ......         
    }// end for
    
    if(actions[0] == 'CancelSP')
    {
      // refresh the page so the horizontal category and user object summary list can be refreshed
      window.setTimeout('KGSubscriptionControl_PageReload()', 10);
    }
  }//end if(holder ....
  else
  {
    KGSubscriptionControl_CallbackError('ControlDiv not found in KGSubscriptionControl.', '');
  }
}

function KGSubscriptionControl_PageReload() 
{
  //debugger;
  try
  {
    var args = "Action=Load";
    Pfl_KGMemberControl_CallbackTrigger(args);
  }
  catch (e) 
  {
    alert("An exception occurred in the script. Error name: " + e.name + ". Error message: " + e.message); 
    debugger;
  }
}
function KGSubscriptionControl_ViewPaymentHistoryClick(_groupid)
{
  try
  {
    var keys = 'KGCurrentGroupID|Topics_SelectedTopicGroupId';
	  var values = _groupid + '|' + _groupid;
    SetSessionProperty(keys, values, true);

    //SetSessionProperty('KGCurrentGroupID', _groupid);
    //SetSessionProperty('Topics_SelectedTopicGroupId', _groupid);
    
    //SetSessionProperty('ConfigureCommunitySpace_Mode','PAYMENTHISTORY');
    
    try
    {
      if (m_srmGetTimes!= null && m_srmGetTimes == "True")
      {
        var now = new Date();
        SetSessionProperty('Srm_T0', now.getTime());
        SetSessionProperty('Srm_Action', 'PAYMENTHISTORY');
        SetSessionProperty('Srm_Callbacks', 1);
      }
    }
    catch(e){}
  
    window.location = "ConfigureCommunitySpace.aspx?Mode=PAYMENTHISTORY";
  }
  catch (e) 
  {
    alert("An exception occurred in the KGSubscriptionControl_ViewPaymentHistoryClick_Wrapped. Error name: " + e.name + ". Error message: " + e.message); 
    debugger;
  }
}

function KGSubscriptionControl_ConfigureCommunitySpace(_groupID)
{
  try
  {
    SetSessionProperty('KGCurrentGroupID', _groupID);
    SetSessionProperty('Topics_SelectedTopicGroupId', _groupID);
    //SetSessionProperty('ConfigureCommunitySpace_Mode','GENERAL');
    
    try
    {
      if (m_srmGetTimes!= null && m_srmGetTimes == "True")
      {
        var now = new Date();
        SetSessionProperty('Srm_T0', now.getTime());
        SetSessionProperty('Srm_Action', 'ConfigurePrivateSpace');
        SetSessionProperty('Srm_Callbacks', 1);
      }
    }
    catch(e){}
  
    //window.location = "PrivateSpaceAdmin.aspx"; 
    window.location = "ConfigureCommunitySpace.aspx";
  }
  catch (e) 
  {
    alert("An exception occurred in the script. Error name: " + e.name + ". Error message: " + e.message); 
    debugger;
  }
}

function KGSubscriptionControl_DislayCancelSPMessage(_groupId, _controlDivClientID)
{
  try
  {
    selectedGroupIdForCancelSP = _groupId;
    EnsureCancelSPMessage(_controlDivClientID);
    PositionMessageBoxSP(_controlDivClientID, false, e);
  }
  catch (e) 
  {
    alert("An exception occurred in the KGSubscriptionControl_DislayCancelSPMessage(). Error name: " + e.name + ". Error message: " + e.message); 
    debugger;
  }
}

///////////////////////
// Ensures that the cancel private space message box is on the page
function EnsureCancelSPMessage(_parentControlDivId)
{
  m_MessageBoxSP = document.getElementById("CancelSPMessageBox");
  
  if(m_MessageBoxSP == null)
  {
    m_MessageBoxSP = document.createElement("div");
    m_MessageBoxSP.setAttribute("id", "CancelSPMessageBox");
    m_MessageBoxSP.style.display = "none";
    m_MessageBoxSP.style.width = "400px"
    m_MessageBoxSP.style.height = "120px";
    m_MessageBoxSP.style.position = "absolute";
    m_MessageBoxSP.style.zIndex = TopZIndex();
    
    m_MessageBoxSP.innerHTML = "<div class='MessageBox'><table border='0' cellpadding='5' cellspacing='0'><tr><td colspan='2' class='MessageBoxHeader'>UnSubscribe Private Space</td></tr><tr><td rowspan='2' valign='top'><img src='images/recycle.png' height='50' width='50'/></td><td style='font-size:x-small;'>Are you sure to remove subscription of this private space? <br />Click 'Yes' button to continue or 'Cancel' button to ignore it.</td></tr><tr><td align='right'><button type='button' onclick='KGSubscriptionControl_CancelSP()'>Yes</button><button id = defaultButton type='button' onclick='CloseMessageBoxSP()'>Cancel</button></td></tr></table></div>";
    
    var parentControlDiv = document.getElementById(_parentControlDivId);
    InsertAdjacentElement(parentControlDiv,"afterBegin", m_MessageBoxSP);
  }
  else
  {
    m_MessageBoxSP.style.zIndex = TopZIndex(); //Show on top of other items
  }
}
var globalReloadCategories = false;
function KGSubscriptionControl_CancelSP()
{
  m_MessageBoxSP.innerHTML= '<div style=\"position:absolute;width:400px;height:120px;border:gray solid 1pt;background:white\"><table width=\"100%\"><tr><td align=\"center\" style=\"font-size: xx-small\"><br/><img src=\"Images/loadingbar.gif\" />Cancelling Subscription....</td></tr></table></div>';
  globalReloadCategories = true;
  var args = 'action=CancelSP|groupId='+ selectedGroupIdForCancelSP;
  KGSubscriptionControl_CallbackTrigger(args);
}


//////////////////
// Closes the message box
function CloseMessageBoxSP()
{
  if(m_MessageBoxSP != null)
  {
    m_MessageBoxSP.style.visibility = "hidden";
    m_MessageBoxSP.style.display = "none";
    CurrentlyShowingMessageBoxSP = false;
  }
}

/////////////////////
// Positions the supplied message box relative to the cursor
function PositionMessageBoxSP(_parentControlDivId, _isKeyPressEvent, e)
{
  var parentControl = document.getElementById(_parentControlDivId); 
  
  
  if(_isKeyPressEvent)
  {
    // we don't know where the cursor is (may be off the page) so display in middle of parentControlDiv
    m_MessageBoxSP.style.top = (parentControl.offsetHeight/2);
    m_MessageBoxSP.style.left = (parentControl.offsetWidth/2) - (GetPixelWidth(m_MessageBoxSP)/2);
  }
  else
  {
    //Displays messagebox relative to cursor     
    e = e || window.event;
    var x = e.x || e.pageX;
    var y = e.y || e.pageY;
    
        
    m_MessageBoxSP.style.top = Math.min(document.body.clientHeight - GetPixelHeight(m_MessageBoxSP),y + 30);   
    if(_parentControlDivId.toLowerCase().indexOf("subscrip") != -1)  
    {
			m_MessageBoxSP.style.top = y;
			//debugger;
			//m_MessageBoxSP.style.top = parseInt(y.replace("px","")) + document.body.clientHeight + document.getElementById("TCC_myProfile").scrollTop;// parseInt(m_MessageBoxSP.style.top.replace("px","")) + 800;
    }
    m_MessageBoxSP.style.left = x - GetPixelWidth(m_MessageBoxSP) / 2;
    
  }
    
  m_MessageBoxSP.style.display = "block";
  m_MessageBoxSP.style.zIndex = TopZIndexSP();
  m_MessageBoxSP.style.visibility = "visible";
  focusOn("defaultButton");
  CurrentlyShowingMessageBoxSP = true;
}


function TopZIndexSP()
{
  var maxZindex = 0;
  var elems = document.getElementsByTagName("div");
  
  for (var ti=0; ti<elems.length; ti++)
  {
    if (elems[ti].style.zindex > maxZindex)
    {
       maxZindex = parseInt(elems[ti].style.zindex);
    }
  }

  return maxZindex + 1;
}

function KGSubscriptionControl_ConfigurePrivateSpace(_groupID)
{
  try
  {
    SetSessionProperty('KGCurrentGroupID', _groupID);
    SetSessionProperty('Topics_SelectedTopicGroupId', _groupID);
    //SetSessionProperty('ConfigureCommunitySpace_Mode','GENERAL');
    
    try
    {
      if (m_srmGetTimes!= null && m_srmGetTimes == "True")
      {
        var now = new Date();
        SetSessionProperty('Srm_T0', now.getTime());
        SetSessionProperty('Srm_Action', 'ConfigurePrivateSpace');
        SetSessionProperty('Srm_Callbacks', 1);
      }
    }
    catch(e){}
  
    //window.location = "PrivateSpaceAdmin.aspx"; 
    window.location = "ConfigureCommunitySpace.aspx";
  }
  catch (e) 
  {
    alert("An exception occurred in the script. Error name: " + e.name + ". Error message: " + e.message); 
    debugger;
  }
}


function CreateAnotherPrivateSpace()
{
	try
  {
		globalReloadCategories = true;
		var args = 'action=CreateAnotherPS';
		KGSubscriptionControl_CallbackTrigger(args);
  }
  catch (e) 
  {
    alert("An exception occurred in the script. Error name: " + e.name + ". Error message: " + e.message); 
    debugger;
  }
}