﻿// JScript File
var IE = document.all?true:false
var cv = null;
/*
document.body.onclick = function(e)
{
var pnlBound = $common.getBounds($get(cv._pnlEventCategoryListId));
//alert(pnlBound.y)
    
        if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
        var x = 0;
        var y = 0; 
          if (e)
          { 
            if (e.pageX || e.pageY)
            { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
              x = e.pageX;
              y = e.pageY;              
            }
            else if (e.clientX || e.clientY)
            { // works on IE6,FF,Moz,Opera7
              x = e.clientX ;//+ document.body.scrollLeft + document.documentElement.scrollLeft;
              y = e.clientY;// + document.body.scrollTop + document.documentElement.scrollTop;
            }
        }

   
 if(!$common.containsPoint(pnlBound, x, y)) 
    CloseFilterPanel();   
}
*/
/****************************************************************************/
/*
if (Sys != undefined) Sys.Application.notifyScriptLoaded();

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_initializeRequest(InitializeRequest);
    prm.add_endRequest(EndRequest);
    var postBackElement;
    function InitializeRequest(sender, args) {
       if (prm.get_isInAsyncPostBack())
       {
         args.set_cancel(true);
       }
       postBackElement = args.get_postBackElement();
       if (postBackElement.id == 'ButtonTrigger')
       {
         $get(cv._pnlMessageId).style.display = "block";
       }
    }
    function EndRequest (sender, args) {
       if (postBackElement.id == 'ButtonTrigger')
       {
         $get(cv._pnlMessageId).style.display = "none";    
       }
    }
    function AbortPostBack() {
      if (prm.get_isInAsyncPostBack()) {
           prm.abortPostBack();
      }        
    }
*/
/****************************************************************************/
   
   function BeginRequest(sender, args)
   {        
        var tmpId = args.get_postBackElement().id;       
        // only show loading message on postback from the internal element of the calendarview control 
        if(tmpId.match(cv._id))
        {
            var tmppos = findPos($get(cv._ddlViewMode));   
            var left = tmppos[0];
            var top = tmppos[1]+22;  
            cv._timeoutId = window.setTimeout("showLoadingMessage('"+left+"','"+top+"')",1000)
        } 
       else
       {
            var tmppos = findPos(args.get_postBackElement());
           var left = tmppos[0];
            var top = tmppos[1];  
            cv._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";                
    }
 
    function EndRequest (sender, args) 
    {              
      if(cv._timeoutId >0)
        window.clearTimeout(cv._timeoutId);           
      //var pnlMessage = $get(cv._pnlMessageId);
      var pnlMessage = $get("pnlMessage");
      if(pnlMessage)
         pnlMessage.style.display = "none";         
    }

function cvInit(id, hfViewMode, hfCurrentDate, ddlViewMode, hfEventCategoryIds, hfCurrentCellId, pnlEventCategoryListId)
{   
    if(cv==null || cv==undefined)
        cv = new CalendarView(id, hfViewMode, hfCurrentDate, ddlViewMode, hfEventCategoryIds, hfCurrentCellId, pnlEventCategoryListId);
}


function OnChangeViewMode(viewMode)
{
    cv.ChangeViewMode(viewMode);
}

function OnChangeCurrentDate(cellId, cellDate, bgColor,bgCurrentDateColor, bgTodayColor)
{
    cv.ChangeCurrentDate(cellId, cellDate, bgColor,bgCurrentDateColor, bgTodayColor);
}

function OnViewModeButtonClick(viewMode)
{
    cv.SetDDLValue(viewMode);
}

function PrintCalendar(phId, printUrl)
{
    var w=window.open('', 'PrintCalendarWindow', 'width=800,height=600,scrollbars=1');
    var printDiv = document.getElementById(phId);
    var htmlString = printDiv.innerHTML;
   var form = document.createElement("form");   
   var hf = document.getElementById("hfHtml");
   if(!hf)
        hf = document.createElement("input");      
             
   hf.type = "hidden";      
   hf.value = htmlString;
   //alert(hf.value);
   hf.id= "hfHtml";
   hf.name = "hfHtml";
   form.appendChild(hf);
   document.body.appendChild(form);
   form.method= "post";
   form.action = printUrl;   
   form.target='PrintCalendarWindow';
   form.submit();   
}

function ExportEvents(filename, exportUrl)
{
    var iframe = document.getElementById("PrintFrame");
   if(!iframe)
   {
    iframe = document.createElement("IFrame");
    iframe.id = "PrintFrame"
    document.body.appendChild(iframe); 
   } 
   
   iframe.style.display="none";
    var url = exportUrl+"?filename="+filename;       
   //alert (url);
   iframe.src = url;   
   return ;   
}

function FilterEvents(hlFilterEvents)
{
    //var filterDiv = document.getElementById(pnlEventCategoryList);
   var filterDiv = $get(cv._pnlEventCategoryListId); 
    var filterLink = document.getElementById(hlFilterEvents); 
    if(filterDiv && filterLink)
    { 
        var pos = findPos(filterLink); 
        filterDiv.style.display="";  
        filterDiv.style.position = "absolute";
        filterDiv.style.left = pos[0] +"px";
        filterDiv.style.top = pos[1] + 15 +"px";  
        filterDiv.style.width = "110px";               
        filterDiv.style.background = "#cccccc";      
        filterDiv.style.border = "1px solid #000000";      
    }  
}

function CloseFilterPanel()
{
    var filterDiv =  $get(cv._pnlEventCategoryListId); 
    if(filterDiv)
        filterDiv.style.display= "none";     
         return false;      
}

function SelectAllCategory(pnlEventCategoryList, checked)
{    
     var filterDiv = document.getElementById(pnlEventCategoryList);
     var nodeList = filterDiv.getElementsByTagName("input");
     for(var i=0;i<nodeList.length;i++)
    {
        if(nodeList[i].type=="checkbox")
            nodeList[i].checked = checked;  
    } 
}

function SelectEventCategory(chbSelectAll, checked)
{
    var chbSelectAll = document.getElementById(chbSelectAll);
    if(chbSelectAll)
        if(!checked)
            chbSelectAll.checked = false; 
}


function CalendarView(id, hfViewMode, hfCurrentDate, ddlViewMode, hfEventCategoryIds, hfCurrentCellId, pnlEventCategoryListId)
{
    this._id = id;
    this._hfViewMode = hfViewMode;
    this._hfCurrentDate = hfCurrentDate; 
    this._ddlViewMode = ddlViewMode; 
    this._hfEventCategoryIds = hfEventCategoryIds;     
    this._hfCurrentCellId = hfCurrentCellId;  
    this._pnlEventCategoryListId = pnlEventCategoryListId; 
    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);    
   //$get(this._pnlMessageId).style.display="none";  
 }

CalendarView.prototype.ChangeViewMode = function(viewMode)
{
    var hf = this.GetElement(this._hfViewMode);
    hf.value = viewMode;
    __doPostBack(this._hfViewMode,'');  
} 

CalendarView.prototype.ChangeCurrentDateWithRefresh = function(theDate)
{
    var hf = this.GetElement(this._hfCurrentDate);
   if(hf){ 
		if(hf.value)
			hf.value = '';
		else
			hf.value = theDate;
        __doPostBack(this._hfCurrentDate, '');  
   } 
}

CalendarView.prototype.ChangeCurrentDate = function(cellId, cellDate, bgColor, bgCurrentDateColor, bgTodayColor)
{   
    var viewMode = $get(this._hfViewMode).value; 
    var hfCurrentCellId = $get(this._hfCurrentCellId);   
    var hfCurrentDate = $get(this._hfCurrentDate);       
    var previousDate = new Date(hfCurrentDate.value);
    var currentDate = new Date(cellDate); 
    var today = new Date(); 
    var cell = $get(cellId);       
   
   if(hfCurrentDate.value=="")
        previousDate = new Date();
   
    if(hfCurrentCellId && hfCurrentDate && cell)
    {
       cell.style.backgroundColor=bgCurrentDateColor;              
       var previousCell = $get(hfCurrentCellId.value);
       if(previousDate.getDate()==today.getDate() && previousDate.getMonth() == today.getMonth())
       {
            previousCell.style.backgroundColor = bgTodayColor;
       }
       else  
            previousCell.style.backgroundColor = bgColor;                 
       hfCurrentCellId.value = cellId; 
       if(viewMode=="MONTHLY_GRID" && previousDate.getMonth()>currentDate.getMonth())
       {
            var mon = previousDate.getMonth()+1;
           hfCurrentDate.value = previousDate.getFullYear() + "/" + mon + "/1";           
       }
       else if(viewMode == "MONTHLY_GRID" && previousDate.getMonth()<currentDate.getMonth())
       {
            var mon = previousDate.getMonth()+1;        
              hfCurrentDate.value = previousDate.getFullYear() + "/" + mon + "/15";           
       }
       else
       { 
            hfCurrentDate.value = cellDate;
       }            
       //alert (hfCurrentDate.value);
    }        
   
}

CalendarView.prototype.SetDDLValue = function(viewMode)
{
    var ddl = this.GetElement(this._ddlViewMode);
   if(ddl)
   {       
       ddl.value = viewMode; 
   } 
}

CalendarView.prototype.GetElement = function(elemId)
{ 
  return (document.getElementById) ? document.getElementById(elemId)
                                     : document.all[elemId];
}

/******************************************************/
function CV_showEventDiv(title, startdate, enddate, location, icon, allday, e)
{   
    var mousePoint = CV_getMouseXY(e);
   var id = "cv_div_event";   
   var divEvent = document.getElementById(id);
   if(divEvent==null || divEvent==undefined)
    {
        divEvent = document.createElement("div");	
       divEvent.id = id;
    }    
   else
   {
        divEvent.innerHTML="";
   }    
   divEvent.style.position="absolute";
   divEvent.style.left = mousePoint[0]+"px";
   divEvent.style.top = mousePoint[1]+"px"; 
   divEvent.style.width = 180+"px";
   divEvent.style.height = "auto";
   divEvent.style.background = "#FFFFFF";
   divEvent.style.border = "1px solid #000000";
   divEvent.zIndex=10000;
   divEvent.style.display="block";
   // add div
   
   var htmlString = "<table width=\"100%\"><tr><td style=\"font-size: 11px;font-family:Tahoma, Arial, Helvetica; \"><b>"+title+"</b></td><tr>";
   if(!allday)
   {
       htmlString+="<tr><td style=\"font-size: 10px;font-family:Tahoma, Arial, Helvetica; \">Start Time: "+startdate+"</td></tr>";
       htmlString+="<tr><td style=\"font-size: 10px;font-family:Tahoma, Arial, Helvetica; \">End Time: "+enddate+"</td></tr>";
   }
   else
   {
        htmlString += "<tr><td style=\"font-size: 10px;font-family:Tahoma, Arial, Helvetica; \">All Day<td></tr>";
   }   
   if(location!=null && location!="")
        htmlString+="<tr><td style=\"font-size: 10px;font-family:Tahoma, Arial, Helvetica; \">Location: "+location+"</td></tr>";
   htmlString+="</table>"; 
   divEvent.innerHTML= htmlString;
   document.body.appendChild (divEvent);   
}

function CV_removeEventDiv()
{
    var id = "cv_div_event";         
  	var divEvent = document.getElementById(id);
  	if(divEvent!=null && divEvent!=undefined)
  		divEvent.style.display="none";
}

function CV_getMouseXY(e)
{
   var tempX = 0;
   var tempY = 0;
   if (IE) 
   { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
    }
   else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
   } 
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  return [tempX, tempY];
}

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];
}	




