﻿// JScript File
//document.write("<script language=\"javascript\" src=\"/common/RadControls/Treeview/Scripts/6_2_2/RadTreeView.js\"></script>")
function clpInit(
    id, 
    divWorkspace, 
    divCms, 
    divSelectedCalendarArea, 
    hfSelectedCalendars, 
    hfCurrentCategory, 
    hfCurrentWsParentId, 
    hfCurrentCmsParentId, 
    hfCurrentLiveSessionParentId, 
    hfCurrentLmsParentId, 
    divCategoryPicker, 
    tvCms, 
    gvLms,
    gvLiveSession,
    pnlEreg,   
    serverId )
{	
    var clp = cp.GetClpByServerId(serverId);
    if(clp==null || clp==undefined)
   { 
	    clp = new CalendarListPanel(
	            id, 
	            divWorkspace, 
	            divCms, 
	            divSelectedCalendarArea, 
	            hfSelectedCalendars, 
	            hfCurrentCategory, 
	            hfCurrentWsParentId,
                hfCurrentCmsParentId, 
                hfCurrentLiveSessionParentId,
                hfCurrentLmsParentId,  
                divCategoryPicker, 
                tvCms, 
                gvLms,
                gvLiveSession,
                pnlEreg,   
	            serverId );
	    cp._clpList.push(clp);	  
	}
}

function OnChangeCategory(serverId, category)
{
    var clp = cp.GetClpByServerId(serverId)
    if(clp) 
        clp.ChangeCategory(category);
}

function OnChangeWsParentId(serverId, parentId) 
{    
    var clp = cp.GetClpByServerId(serverId)
    if(clp) 
        clp.ChangeWsParentId(parentId);
}

function OnChangeCmsParentId(serverId, parentId)
{
    var clp = cp.GetClpByServerId(serverId)
    if(clp) 
        clp.ChangeCmsParentId(parentId);
}

function OnChangeLiveSessionParentId(serverId, parentId)
{
    var clp = cp.GetClpByServerId(serverId)
    if(clp)  
        clp.ChangeLiveSessionParentId(parentId);
}

function OnChangeLmsParentId(serverId, parentId)
{
    var clp = cp.GetClpByServerId(serverId)
    if(clp) 
        clp.ChangeLmsParentId(parentId);
}

function OnSelectCalendar(serverId, calendarId, name, category, checked)
{
    var clp = cp.GetClpByServerId(serverId)
    if(clp) 
    {
        clp.SelectCalendar(calendarId, name, category, checked);        
    } 
}

function AfterCheckCalendar(node)
{
    var serverId = node.Attributes["ServerId"];
    var calendarId = node.Attributes["CalendarId"];    
   if(serverId && calendarId)
   { 
        var clp = cp.GetClpByServerId(serverId)
        if(clp && calendarId>0) 
        { 
            clp.SelectCalendar(calendarId, node.Text, "CMS", node.Checked);           
        } 
    }        
   if(!node.Expanded)
        node.Expand(); 
   if(node.Nodes.length>0)
   { 
        for(var i=0;i<node.Nodes.length;i++)
        {           
           if(node.Nodes[i].Enabled)
           {   
                if(node.Checked)            
                    node.Nodes[i].Check();                            
                else
                    node.Nodes[i].UnCheck();  
           }     
            AfterCheckCalendar(node.Nodes[i]);
        } 
   }
}



function OnShowCategoryPanel(serverId)
{
    var clp = cp.GetClpByServerId(serverId)
    if(clp) 
        clp.ShowCategoryPanel();
}

function OnCancelChooseCategory(serverId)
{
    var clp = cp.GetClpByServerId(serverId)
    if(clp) 
        clp.CancelChooseCategory();
}

function OnChooseCategry(serverId, rdName)
{
    var clp = cp.GetClpByServerId(serverId)
    if(clp) 
   { 
        clp.ChooseCategory(rdName);
   } 
}

/*******CalendarListPanel************/

function CalendarListPanel
(id, 
 divWorkspace, 
 divCms, 
 divSelectedCalendarArea, 
 hfSelectedCalendars, 
 hfCurrentCategory,
 hfCurrentWsParentId,
 hfCurrentCmsParentId,
 hfCurrentLiveSessionParentId,
 hfCurrentLmsParentId,        
 divCategoryPicker, 
 tvCms, 
 gvLms,
 gvLiveSession,
 pnlEreg,    
 serverId)
{
    this._id = id;
   this._divWorkspace = divWorkspace;
   this._divCms = divCms; 
   this._divSelectedCalendarArea =divSelectedCalendarArea ;
   this._hfSelectedCalendars = hfSelectedCalendars;
    this._hfcurrentCategory = hfCurrentCategory;
    this._hfCurrentWsParentId =  hfCurrentWsParentId;
    this._hfCurrentCmsParentId = hfCurrentCmsParentId; 
    this._hfCurrentLiveSessionParentId = hfCurrentLiveSessionParentId;
    this._hfCurrentLmsParentId = hfCurrentLmsParentId;
    this._divCategoryPicker =  divCategoryPicker;
    this._tvCms = tvCms;
    this._gvLms = gvLms;
    this._gvLiveSession = gvLiveSession;
    this._pnlEreg = pnlEreg;    
    this._serverId = serverId;   
    //this._timeoutId = 0;    
    this._selectedCalendarList = new Array();
    this.LoadSelectedCalendarList();    
}

CalendarListPanel.prototype.ChangeCategory=function(category)
{
    var hf = this.GetElement(this._hfcurrentCategory);
    hf.value=category;        
    __doPostBack(this._hfcurrentCategory,'');  
}

CalendarListPanel.prototype.LoadSelectedCalendarList=function()
{
   this._selectedCalendarList = new Array();
    var hfString = this.GetElement(this._hfSelectedCalendars).value;
    //var length = hfString.length;
    //hfString = hfString.slice(0, length-5);  
    //alert(hfString);
   if(hfString!=""){
        if(hfString!="EMPTY")
        { 
            var calItemList = hfString.split("~~~");
            for(var i = 0; i<calItemList.length;i++)
            {
                var calPropertyItems = calItemList[i].split("##");
                var calendarId = calPropertyItems[0];
                var label = calPropertyItems[1];
                var category = calPropertyItems[2];   
                var calAdapterItem = new CalendarItem(calendarId, label, this._serverId, category);
                this._selectedCalendarList.push(calAdapterItem); 
            }
       }  
   } 
}

CalendarListPanel.prototype.ChangeWsParentId = function(parentId)
{
    var hf = this.GetElement(this._hfCurrentWsParentId);
    hf.value = parentId;
    __doPostBack(this._hfCurrentWsParentId,'');  
}

CalendarListPanel.prototype.ChangeCmsParentId = function(parentId)
{
    var hf = this.GetElement(this._hfCurrentCmsParentId);
    hf.value = parentId;
    __doPostBack(this._hfCurrentCmsParentId,'');  
}

CalendarListPanel.prototype.ChangeLiveSessionParentId = function(parentId)
{
    var hf = this.GetElement(this._hfCurrentLiveSessionParentId);
    hf.value= parentId;
    __doPostBack(this._hfCurrentLiveSessionParentId,'');  
}

CalendarListPanel.prototype.ChangeLmsParentId = function(parentId)
{
    var hf = this.GetElement(this._hfCurrentLmsParentId);
    hf.value = parentId;
    __doPostBack(this._hfCurrentLmsParentId, '');  
}

CalendarListPanel.prototype.GetElement = function(elemId)
{ 
  return (document.getElementById) ? document.getElementById(elemId)
                                     : document.all[elemId];
}

/********CalendarItem*************/
CalendarItem = function(calendarId, label, serverId, category)
{
    this._calendarId = calendarId,
   this._label = label;
   this._serverId = serverId;
   this._category = category; 
}


CalendarListPanel.prototype.SelectCalendar = function(calendarId, name, category, checked)
{
    var calendar = new CalendarItem(calendarId, name, this._serverId, category);
   if(typeof checked == 'boolean')
   {
        if(checked)
       { 
            this.AddSelectedCalendar(calendar);                
       } 
        else
       { 
            this.RemoveSelectedCalendar(calendar);           
        }    
   } 
   else
   {
        var chb = this.GetElement(checked);
        if(chb)
       {
             chb.checked = !chb.checked;
             this.SelectCalendar(calendarId, name, category,chb.checked)
       }  
   }
    this.DisplaySelectedCalendarList(); 
}

CalendarListPanel.prototype.AddSelectedCalendar= function(calendar)
{
    this.LoadSelectedCalendarList();  
    if(!this.HasCalendar(calendar))
   {   
        this._selectedCalendarList.push(calendar);        
   } 
   this.ConstructHiddenFieldString();    
}

CalendarListPanel.prototype.RemoveSelectedCalendar = function(calendar)
{
    this.LoadSelectedCalendarList();
    if(this.HasCalendar(calendar))
   {           
        for(var i=0;i<this._selectedCalendarList.length;i++)
       {             
          if(this._selectedCalendarList[i]._calendarId == calendar._calendarId)
                this._selectedCalendarList.splice(i,1); 
       }          
   } 
  this.ConstructHiddenFieldString();  
 
    /*uncheck checkbox if needed*/
    
   var currentCategory = $get(this._hfcurrentCategory).value; 
   var targetElm = null;
   if( calendar._category == "WORKSPACE" && currentCategory == "WORKSPACE" )
   {
        targetElm = $get(this._divWorkspace);
   }
   else if( calendar._category == "LMS" && currentCategory == "LMS")
   {
        targetElm = $get(this._gvLms);
   }
   else if( calendar._category == "LIVESESSION" && currentCategory == "LIVESESSION")
   {
        targetElm = $get(this._gvLiveSession);
   }
   else if ( calendar._category == "EREG" && currentCategory == "EREG")
   {
        targetElm = $get(this._pnlEreg);
   }
   
   if( targetElm && calendar._category!="CMS")
   {    
       var nodeList = targetElm.getElementsByTagName('input');
       if (nodeList)
       {
            var count = nodeList.length;
            if( count && count>0)
            {
                for (var i = 0; i< count; i++) 
               {
                    var node = nodeList[i]; 
                    if (node.type == "checkbox" && node.getAttribute("CalendarId"))
                    {                     
                        var calendarId = node.getAttribute("CalendarId")
                        if (calendarId == calendar._calendarId)
                        {
                            node.checked = false;
                        }
                    }
               } 
            }  
       }           
   }
  else if((calendar._category)=="CMS" && (currentCategory=="CMS" || currentCategory==""))
  {
        var tree = window[this._tvCms]; 
        //alert(tree.Nodes.length); 
       if(tree){ 
        var count = tree.AllNodes.length; 
        for(var i=0;i<count;i++)
        {
            if(tree.AllNodes[i].Attributes["CalendarId"]==calendar._calendarId)
            {
                tree.AllNodes[i].UnCheck();
                break;    
            }
        } 
      } 
   }
}


CalendarListPanel.prototype.HasCalendar = function(calendar)
{
    var retVal = false;
    for(var i=0;i<this._selectedCalendarList.length;i++)
    {
        if(calendar._calendarId==this._selectedCalendarList[i]._calendarId)
            return true; 
    }
}

CalendarListPanel.prototype.ConstructHiddenFieldString=function()
{    
   var hf = this.GetElement(this._hfSelectedCalendars);
   var hfValue = ""  
   for(var i=0;i<this._selectedCalendarList.length;i++)
   {
        //var cal = this._selectedCalendarList[i];        
        hfValue+="~~~"+this._selectedCalendarList[i]._calendarId+"##"+this._selectedCalendarList[i]._label+"##"+this._selectedCalendarList[i]._category; 
        //hfValue.concat("~~~").concat(cal._calendarId).concat("##").concat(cal._label).concat("##").concat(cal._category);      
   } 
   if(this._selectedCalendarList.length>0)   
      hf.value = hfValue.slice(3); 
   else
      hf.value="EMPTY";      
}

CalendarListPanel.prototype.DisplaySelectedCalendarList=function()
{
    var divHtml  ="";
    if(this._selectedCalendarList.length>0)
   { 
        divHtml +="<div align=\"center\" class=\"gridheader\"><b>"+this._selectedCalendarList.length+" calendar(s) selected.</b></div>"
        divHtml += "<table style=\"width:300px;border:1px solid #cccccc; border-collapse: collapse\">";
        divHtml +="<tr class=\"grid_header\"><td style=\"width:1px\"><td>Calendar Name</td><td style=\"width:1px\">Category</td><td>Action</td></tr>";    
        
       for(var i=0;i<this._selectedCalendarList.length;i++)
       {
             var calendar = this._selectedCalendarList[i];             
            divHtml+="<tr><td><img src=\"/common/resources/shared/images/iconThemes/16x16/workspace_calendar/default.gif\" /></td>";
            divHtml+="<td align=\"left\" width=\"100%\" nowrap>"+calendar._label+ "</td>";
            divHtml+="<td align=\"left\" nowrap>"+calendar._category+"</td>"
            divHtml+="<td align=\"center\"><img src=\"/common/resources/shared/images/delete.gif\" style=\"cursor:pointer;\" border=\"0\" onclick=\"javascript:OnSelectCalendar('"+calendar._serverId+"','"+calendar._calendarId+"','"+calendar._label.replace("'", "\\'")+"','"+calendar._category+"',false);\" /></td></tr>"; 
       } 
       divHtml+="</table>";    
   }
   else
   {
        divHtml+="<div align=\"center\" ><b>No calendar(s) selected.</b></div>"
    }
   
   var div = this.GetElement(this._divSelectedCalendarArea);
   div.innerHTML=divHtml;   
}

CalendarListPanel.prototype.ShowCategoryPanel = function()
{
    var divCategoryPicker = $get(this._divCategoryPicker);       
    var thisControl = this.GetElement(this._id);
    var panelZIndex = thisControl.style.zIndex;       
    
    if(divCategoryPicker && divCategoryPicker.style.display=="none")
    {
        var XY = findPos(thisControl); 
        if(Sys.Browser.agent == Sys.Browser.InternetExplorer)
       {              
           //alert(thisControl.scrollHeight);          
           divCategoryPicker.style.position = "relative";              
           divCategoryPicker.style.left = 240;
           divCategoryPicker.style.top = -thisControl.scrollHeight + 50;
           //divCategoryPicker.style.offsetTop = XY[1] +50; 
           //divCategoryPicker.style.height="100%";
           //var aa = Sys.UI.DomElement.getLocation(thisControl);
          //alert(aa.x); 
       }
       else
       {            
            divCategoryPicker.style.position="fixed";
            divCategoryPicker.style.left =(XY[0]+240)+"px";
           divCategoryPicker.style.top = (XY[1] + 50)+"px"; 
       }
        divCategoryPicker.style.zIndex = panelZIndex+1;
        divCategoryPicker.style.width="300px";                       
        divCategoryPicker.style.background="#cccccc";
        divCategoryPicker.style.border = "3px Outset gray ";
        divCategoryPicker.style.display=""; 
        //$common.setVisible(divCategoryPicker, Sys.UI.VisibilityMode.hide);
    }    
}

CalendarListPanel.prototype.CancelChooseCategory = function()
{
    var divCategoryPicker = this.GetElement(this._divCategoryPicker);
   if(divCategoryPicker)
    divCategoryPicker.style.display="none"; 
}

CalendarListPanel.prototype.ChooseCategory = function(rbtnId)
{
    var rdControl = $get(rbtnId)
   if(rdControl)
   {  
        var rdControlName = this.GetElement(rbtnId).name;   
        rdControl = document.forms[0][rdControlName];   
        var category = "";    
        for( var i=0; i< rdControl.length;i++)
        {        
            if(rdControl[i].checked)
            {
                 category = rdControl[i].value;
                 break;
            }
        }
       if(category!="") 
       {
            this.ChangeCategory(category);   
       } 
   }
   else
   {
        this.CancelChooseCategory();
   }
}

/**************************************/
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

