﻿// JScript File
var cp = null;
function cpInit(id, showParentServer, showSubServer, pageIndex)
{
    if(cp==null || cp==undefined)     
        cp = new CalendarPicker(id, showParentServer, showSubServer, pageIndex);
}

function CalendarPicker(id, phParentServer, phSubServer, pageIndex)
{
    this._id = id;
    this._phParentServer = phParentServer;
    this._phSubServer = phSubServer;
    this._pageIndex = pageIndex; 
    this._clpList = new Array(); 
    this._timeoutId = 0; 
    this._prm = Sys.WebForms.PageRequestManager.getInstance();
    this._prm.remove_beginRequest(BeginRequest);     
    this._prm.remove_endRequest(EndRequest);
    this._prm.add_beginRequest(BeginRequest);
    this._prm.add_endRequest(EndRequest); 
}

function ChangeShowParentServer(checked)
{
    var phParentServer = $get(cp._phParentServer);
    if( phParentServer){
        if(checked)
               phParentServer.style.display='';
        else
               phParentServer.style.display='none';
     }    
}

function ChangeShowSubServer(checked)
{
    var phSubServer = $get(cp._phSubServer);
    if(phSubServer) {
        if(checked)
            phSubServer.style.display='';
        else
            phSubServer.style.display='none'; 
     } 
}
/*
function CalendarPicker(id, showParentServer, showSubServer, pageIndex)
{
    this._id = id;
    this._showParentServerId = showParentServer;
    this._showSubServerId = showSubServer;  
    this._pageIndex = pageIndex; 
    this._clpList = new Array(); 
    this._timeoutId = 0; 
    this._prm = Sys.WebForms.PageRequestManager.getInstance();
    this._prm.remove_beginRequest(BeginRequest);     
    this._prm.remove_endRequest(EndRequest);
    this._prm.add_beginRequest(BeginRequest);
    this._prm.add_endRequest(EndRequest); 
}

function ChangeShowParentServer(checked)
{
    var hfShowParentServer = $get(cp._showParentServerId);
    if(hfShowParentServer)
    {
        if(checked)
            hfShowParentServer.value = "true";
        else
            hfShowParentServer.value = "false";
        __doPostBack(cp._showParentServerId,'');     
    }       
}

function ChangeShowSubServer(checked)
{    
    //alert(Sys.WebForms.PageRequestManager.getInstance().get_beginRequest());
    var hfShowSubServer = $get(cp._showSubServerId);
    if(hfShowSubServer)
   {
        if(checked)
            hfShowSubServer.value = "true";
        else
            hfShowSubServer.value = "false";
        __doPostBack(cp._showSubServerId, '');  
   }  

}
*/
function ChangePageIndex(pageIndex)
{
    var hfPageIndex = $get(cp._pageIndex);
    if(hfPageIndex)
    {
      
        var clpDic = new Object();        
        for(var i = 0; i<cp._clpList.length;i++)
       {       
            var clp = cp._clpList[i];
            var serverIdString = clp._serverId.toString(); 
            var selectedCalendarList = new Array(); 
            for(var j=0; j<clp._selectedCalendarList.length;j++)
            {
                var ca = new Intrafinity.Web.Presentation.Controls.CalendarAdapter();
                var caItem = clp._selectedCalendarList[j]; 
                ca.CalendarId = caItem._calendarId;
                ca.Label = caItem._label;
                ca.Category = caItem._category;
                ca.ServerId = caItem._serverId;     
                selectedCalendarList.push(ca); 
                //var calendar = clp._selectedCalendarList[j];
                //alert(calendar._serverId + " " + calendar._calendarId);  
            } 
           clpDic[serverIdString] = selectedCalendarList;
       } 
       Intrafinity.Web.Presentation.Controls.CalendarPickerWS.UpdateSelectedCalendarListSession(clpDic);    
      
        hfPageIndex.value = pageIndex;        
        __doPostBack(cp._pageIndex, '');

    }  
}


   function BeginRequest(sender, args)
   {        
        var tmpId = args.get_postBackElement().id;       
        var tmppos = findPos(args.get_postBackElement());
        var left = tmppos[0];
        var top = tmppos[1];          
        cp._timeoutId = window.setTimeout("showLoadingMessage('"+left+"','"+top+"')",1000)     
   } 

    function showLoadingMessage(left, top)
    {
       var pnlMessage = $get("pnlMessage");
       if(!pnlMessage)
       {
            pnlMessage = document.createElement('div');
            pnlMessage.id = "pnlMessage"
            pnlMessage.innerHTML = "<img src = '/common/resources/shared/images/Loading.gif' />"; 
            document.body.appendChild(pnlMessage); 
       }      
          pnlMessage.style.display = "";
          pnlMessage.style.position = "absolute";
          pnlMessage.style.left = left +"px";
          pnlMessage.style.top = top+"px";               
          pnlMessage.style.zIndex = 2000000; 
    }
 
function EndRequest (sender, args) 
    {              
      //  alert(cp._timeoutId); 
      if(cp._timeoutId >0)
        window.clearTimeout(cp._timeoutId);           
      var pnlMessage = $get("pnlMessage");
      if(pnlMessage)
         pnlMessage.style.display = "none";         
    }

CalendarPicker.prototype.GetClpByServerId = function(serverId)
{
    var retVal = null;    
    for(var i=0;i<this._clpList.length;i++)
    {
        if(this._clpList[i]._serverId == serverId)
        {
            retVal = this._clpList[i]; 
            break; 
        }         
    }
   return retVal; 
}
function CP_HandleExpandAndCollpase(contentDivId, selectMoreDivId, iconId, CollapseIconUrl, ExpandIconUrl)
{
    var divContent = document.getElementById(contentDivId);
   var img = document.getElementById(iconId);
   var divSelectMore = document.getElementById(selectMoreDivId);
   if(divContent && divSelectMore)
   {
        if(divContent.style.display!="none") // if expand, then make it collapsed
       {
            divContent.style.display="none";
            divSelectMore.style.display = "none"; 
             if(img)
             {
                img.src=CollapseIconUrl;
                img.alt="Expand"; 
             }
       } 
       else // if collpase, then make it expanded
       {
            divContent.style.display="";
            divSelectMore.style.display=""; 
           if(img)
            {
                img.src= ExpandIconUrl;
               img.alt="Collapse"; 
            }   
       }
   } 
}


