/*************************************************************************************
Description of File:  Common JavaScript functions used in the portal application.
*************************************************************************************/

//
// Define the browsers
//
var ces_ieBr = document.all;
var ces_ns6Br = document.getElementById && !document.all;
var ces_ns4Br = document.layers;
var ces_macbr = navigator.appVersion.indexOf("Mac") != -1;
var ces_supportedBrowser = ces_ieBr || ces_ns6Br || ces_ns4Br;
var ces_resizing=0;

var ces_tkHeights = new Array();
function ces_tkHeight(id, height) {
  this.id = id;
  this.height = height;
}

var offsetxpoint=0;
var offsetypoint=0;
	
function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}	

function ces_DisableButton(id) {

    document.forms[0].submit();
    window.setTimeout("ces_disableButton('" + id + "');", 0);
}

function ces_disableButton(id) {
	document.getElementById(id).disabled = true;
}

function ces_change_src(iframe, src)
{
	try
	{
		if(iframe && src)
			iframe.src = src;	
	} catch(exception)
	{
	}			
}

function ces_mapObj(id, type) {
  this.id=id;
  this.type=type;
}

var ces_maxRowSelect = -1;

// Variable used to determine whether to highlight a row and show the task list.
// If it is true, then the row will not be "selected" and the task list won't
// appear.  All items with the "tasklink" prefix set this var to true.
var ces_linkClick = false;
var ces_activeToolkit = ""; // an int flag that equals the active toolkit
var ces_isThumbnailView = false;
var ces_lastRowObj;
var ces_lastSrcId;
var ces_selectedThumbnail;
var ces_selectedKey;
var ces_ENTER_KEY = 13;
var ces_ESCAPE_KEY = 27;
var ces_SHIFT_KEY = 16;
var ces_CTRL_KEY = 17;
var ces_ARROW_LEFT = 37;
var ces_ARROW_UP = 38;
var ces_ARROW_RIGHT = 39;
var ces_ARROW_DOWN = 40;
var ces_DELETE_KEY = 46;
var ces_X_KEY = 88;
var ces_C_KEY = 67;
var ces_V_KEY = 86;
var ces_selectedList = new Array();
var ces_pivot; // The row object ces_pivot that is used for multi select
var ces_activeComponentId = -1;
var ces_pointedId = -1;
var ces_pointerId = -1;
var ces_pointedRow = -1;
var ces_currCompId = -1;
var ces_activeCompManageList = -1;
var ces_activeCompSurroundTable = -1;
var ces_currentPageUrl = '';
var _ces__EVENTTARGET = '';
var _ces__EVENTARGUMENT = '';
var _ces__ButtonX = '';
var _ces__ButtonY = '';
var _ces__VIEWSTATE = '';

var _syndicated__EVENTARGUMENT = '';
var _syndicated__EVENTTARGET = '';
var _syndicated__VIEWSTATE = '';

var _ces_clientId = '';
var _ces_nsbr_xcoord = 0;
var _ces_nsbr_ycoord = 0;

// TODO: desc of this array and obj below
var ces_componentArray = new Array();

var ces_docClickFtns = new Array();

function ces_syncomp(comp_id, scheme, host, path, portal)
{
	var url;
		
	if(scheme && host && path && portal)
	{
		url = scheme + '://' + host + path + '/portalcontent.aspx?galleryComponentId=' + comp_id + '&outputFormat=text/html&encoded=true&_synHost=' + host + '&_synPath=' + path + '&portalName=' + portal;
	}
	else
	{
		url = 'portalcontent.aspx?galleryComponentId=' + comp_id + '&outputFormat=text/html&encoded=true';
	}
	ces_staticLoadScript(url);
}

function ces_staticLoadScript(url)
{
	var qs = document.location.search;
	
	var ramp = /&/gi;
	qs=qs.replace(ramp, '&amp;');
	url=url.replace(ramp, '&amp;');
	
	if(qs.length > 0 && qs.substring(0,1) == '?')
		qs = qs.substring(1);
	qs += '&amp;_synRawUrl=' + escape(document.location.pathname + document.location.search);
	
	if(_ces__EVENTTARGET != '')
	{
		alert(_ces__EVENTTARGET);
		qs += '&amp;__EVENTTARGET=' + _ces__EVENTTARGET;
	}	
	if(_ces__EVENTARGUMENT != '')
	{
		alert(_ces__EVENTARGUMENT);
		qs += '&amp;__EVENTARGUMENT=' + _ces__EVENTARGUMENT;
	}
	
	if(_ces__VIEWSTATE != '')
	{
		alert(escape(_ces__VIEWSTATE).replace(/\+/g,'%2b'));	
		qs += '&amp;__VIEWSTATE=' + escape(_ces__VIEWSTATE).replace(/\+/g,'%2b');
	}

	document.write('<script type=\'text/javascript\' src="', url + '&amp;' + qs, '" type="text/JavaScript"></script>');
}

function ces_portalComponent(id, name) 
{
  this.id = id;
  this.name = name;
}

/*************************************************************************************
Function:     ces_execServerPull
Parameters:   (String)url - the url to be loaded
Description:  This function is called to cause a server url to load with no effect on
			  the current page.
*************************************************************************************/
var ces_serverPullIFrame = null;
function ces_execServerPull(e, url, callback)
{
	if(!ces_serverPullIFrame)
	{
		try
		{
			ces_serverPullIFrame = document.createElement('iframe');
			ces_serverPullIFrame.setAttribute('id','spIFrame');
			ces_serverPullIFrame.style.display = 'none';
			ces_serverPullIFrame.style.width = '0px';
			ces_serverPullIFrame.style.height = '0px';
			ces_serverPullIFrame.style.border = '0px';
			document.body.appendChild(ces_serverPullIFrame);
		
		} catch(exception)
		{
			var iframeHTML='\<iframe id="spIFrame" style="';
			iframeHTML+='display:none;';
			iframeHTML+='border:0px;';
			iframeHTML+='width:0px;';
			iframeHTML+='height:0px;';
			iframeHTML+='"><\/iframe>';
			document.body.innerHTML+=iframeHTML;
			ces_serverPullIFrame = new Object();
			ces_serverPullIFrame.document = new Object();
			ces_serverPullIFrame.document.location = new Object();
			ces_serverPullIFrame.document.location.iframe = document.getElementById('spIFrame');
			ces_serverPullIFrame.document.location.replace = function(location) {
				this.iframe.src = location; }		
		}
	}
	var IFrameDoc;
	if(ces_serverPullIFrame.contentDocument) 
	{
		// For NS6
		IFrameDoc = ces_serverPullIFrame.contentDocument; 
	} else if (ces_serverPullIFrame.contentWindow) 
	{
		// For IE5.5 and IE6
		IFrameDoc = ces_serverPullIFrame.contentWindow.document;
	} else if (ces_serverPullIFrame.document) 
	{
		// For IE5
		IFrameDoc = ces_serverPullIFrame.document;
	} else 
	{
		return true;
	}
  
    if(callback)
    {
		var funcPtr = new Function(callback);
		if(funcPtr)
			ces_serverPullIFrame.attachEvent('onload', funcPtr );
	}			

    IFrameDoc.location.replace(url);
    return false;
}

/*************************************************************************************
Function:     ces_taskSelected
Parameters:   (string)path - the path to set the current location.  This is the custom handler.
              (string)task - the CES task id (see CES task documentation)
              (bool)isPopup - should the task be opened in a new window
              (int)rowNum - the row in the component's table that the user clicked on
              (int)elementId - the component id being acted on
              (int)width - the initial width of popup
              (int)height - the initial height of popup
              (bool)async - specifies if call should use ces_execServerPull
              (string)callback - defines a callback function to exec after the server pull
Description:  This function is called when a task is selected from a dynamically
              drawn toolkit in the components.
*************************************************************************************/
function ces_taskSelected(path, task, isPopup, rowNum, 
						elementId, width, height, async, callback, isModal, onExecute) 
{

  try {
    if (onExecute == 'return false;')
      return;
  } catch (exception) {
  
  }
  
  if(ces_pointerId > -1) {
    path = path.replace(ces_pointedId, ces_pointerId);
  }

  var bContinue = true;
  if (task.indexOf("alert_delete") > -1) {
    if (task.indexOf("multi") > -1) {
      if (!confirm('Are you sure you want to PERMANENTLY delete these Alerts?')) {
        bContinue = false;
      }
    } else {
      if (!confirm('Are you sure you want to PERMANENTLY delete this Alert?')) {
        bContinue = false;
      }
    }
  } else if (task.indexOf("_delete") > -1) {
    if (task.indexOf("multi") > -1) {
      if (!confirm('Are you sure you want to PERMANENTLY delete these items?')) {
        bContinue = false;
      }
    } else {
      if (!confirm('Are you sure you want to PERMANENTLY delete this item?')) {
        bContinue = false;
      }
    }
  }  else if (task.indexOf("remove") > -1) {
    if (!confirm('Are you sure you want to remove this item?')) {
      bContinue = false;
    }
  } else if (task.indexOf("_publish") > -1) {
    if (!confirm('Are you sure you want to publish this item?')) {
      bContinue = false;
    }
  } else if (task.indexOf("_unpublish") > -1) {
    if (!confirm('Are you sure you want to un-publish this item?')) {
      bContinue = false;
    }
  } else if (task == "recycle_bin_clear") {
    if (!confirm('Are you sure you want to PERMANENTLY clear the recycle bin?')) {
      bContinue = false;
    }
  }

  if (bContinue) 
  {
    var ProcessArea = document.getElementById('ProcessAlert');

    // If no custom handler, submit back to the current page and use the ces form fields
    if (path == '') {    
		
      if (ProcessArea) 
        ces_showProcessArea();
      		
		  if(document.getElementById('ces_task'))
			  document.getElementById('ces_task').value = task;
  			
		  if(document.getElementById('ces_component'))
			  document.getElementById('ces_component').value = elementId;
  			
		  if(document.getElementById('selected_toolkit_row'))
			  document.getElementById('selected_toolkit_row').value = rowNum;
        
      if(onExecute) {
        var funcPtr = new Function(onExecute);
        if(funcPtr)
          funcPtr();
		  } else {
			  document.forms[0].submit();
		  }
    } 
    else // else a custom handler is defined
    { 
      if (async && async == 1) 
      {
		    ces_execServerPull(null, path, callback);
        ces_hideToolkit(ces_activeToolkit);
		    return;
		    
	    } 
	    else if (isPopup == 'true' || isPopup == 'True') 
	    {

		    if(isModal || isModal == 'true') 
			    ces_openModalWin(path, width, height);
		    else
			    ces_openWin(path, width, height);
			
        if (ProcessArea)
          ces_hideProcessArea();
 
        ces_hideToolkit(ces_activeToolkit);
      } 
      else 
      {   
        var ProcessArea = document.getElementById('ProcessAlert');
        if (ProcessArea && path.toLowerCase().indexOf("documentviewer.aspx") == -1 
			&& path.toLowerCase().indexOf("export") == -1 
			&& path.toLowerCase().indexOf("movecontent.aspx") == -1
			&& path.toLowerCase().indexOf("resources/") == -1
			&& path.toLowerCase().indexOf("resourcesidx/") == -1) 
        {
          ces_showProcessArea();
        }
        document.location=path;
      }
    }
  }
}

function ces_openDocument(componentId, path, rowNum) 
{
  ces_hideToolkit(componentId + "-" + rowNum);
  document.location = path;
}

function ces_openWin(url, width, height) {
  var leftPos = 100;
  var topPos = 5;
  if (width == null) {
    width = 650;
  }
  if (height == null) {
    height = 550;
  }     
  var openWin = window.open(url, '_blank', 'resizable=yes,titlebar=no,scrollbars=yes,width=' + width + ',height=' + height + ',left=' + leftPos + ',top=' + topPos);
  if(!openWin)
  {
	alert("You must disable your pop-up blocker for this site.");
  }
}

function ces_openModalWin(url, width, height) {

  var ProcessArea = document.getElementById('ProcessAlert');
  if (ProcessArea)
    ces_hideProcessArea();

  if (width == null)
    width = 500;
  if (height == null)
    height = 400;
  if (document.body.onclick == null) {
    document.body.onclick=ces_checkModal;
  }
  if (document.body.onfocus == null) {
    document.body.onfocus=ces_checkModal;
  }
  ces_openDGDialog(url, width, height, '');
  return false;
}

function ces_openDocBrowserWin(url) {
  ces_openModalWin(url, 700, 400);
}

function ces_closeOpenWin(locationStr) {
  if (locationStr != null) {
    opener.document.location = locationStr;
  }
  window.close();
}

function ces_GetOrCreateDiv(id)
{
	var div = document.getElementById(id);
	if(div)
		return div;
		
	div = document.createElement('div');
	div.style.width='1%';
	div.style.display='block';
	div.style.position='absolute';	
	div.id = id;
	document.body.appendChild(div);
	return div;
}

function ces_PlaceDiv(div, clientX, clientY, justify)
{

	div.style.visibility = 'visible';
	div.style.height='auto';
	var curX=0;
	var curY=0;

	curX = (ces_ns6Br)?clientX : clientX+ietruebody().scrollLeft;		
	curY = (ces_ns6Br)?clientY : clientY+ietruebody().scrollTop;

	//Find out how close the mouse is to the corner of the window
	var rightedge=ces_ieBr&&!window.opera? ietruebody().clientWidth-clientX-offsetxpoint : window.innerWidth-clientX-offsetxpoint-20;
	var bottomedge=ces_ieBr&&!window.opera? ietruebody().clientHeight-clientY-offsetypoint : window.innerHeight-clientY-offsetypoint-20;

	var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000;
	
	//if the horizontal distance isn't enough to accomodate the width of the context menu
	if (rightedge<div.offsetWidth && justify != 0)
	{
		//move the horizontal position of the menu to the left by its width
		div.style.left=ces_ieBr? ietruebody().scrollLeft+clientX-div.offsetWidth+"px" : window.pageXOffset+clientX-div.offsetWidth+"px";
	}
	else if (curX<leftedge)
	{
		div.style.left="5px";
	}
	else
	{
		//position the horizontal position of the menu where the mouse is positioned
		div.style.left=curX+offsetxpoint+"px";
	}


	if (curY < div.offsetHeight) 
	{
		if (bottomedge<div.offsetHeight)
		  div.style.top=curY-100+"px";
		else
	    div.style.top=curY+offsetypoint+"px";
	}
	else if (bottomedge<div.offsetHeight)
	{	
	    div.style.top = clientY+"px";
	}
	else
	{	

		div.style.top=clientY+"px";	

		if (ces_ieBr)
		{
		  try 
		  {
			  var dypos = ces_findPosY(div);
			  var ydiff = dypos - clientY;

			  var realDiv = div.parentElement;
			 
			  var realScroll = 0;
			  if (realDiv)
			  {
				  realScroll = realDiv.scrollTop;
			  }
			  div.style.top=(clientY - ydiff)+realScroll+"px";
			}
			catch (exception) 
			{
			}
	  }
	}	
	
  ces_moveDivFromBottomRight(div);
}

function ces_moveDivFromBottomRight(div)
{
		try 
		{
			var dHeight = div.offsetHeight;
			var dWidth = div.offsetWidth;

			var top = ces_stripPx(div.style.top);
			var wHeight = (ces_ieBr&&!window.opera? ietruebody().clientHeight : window.innerHeight) - 0;

			if (wHeight - top < dHeight)
			{
				if (top-dHeight > 0)
				{
					div.style.top=(top-dHeight)+"px";
				}
				else
				{
					div.style.top="10px";
				}
			}
			else 
			{
				if (ces_stripPx(div.style.top) < 0)
					div.style.top="10px";
			}

			var left = ces_stripPx(div.style.left);
			var wWidth = (ces_ieBr&&!window.opera? ietruebody().clientWidth : window.innerWidth) - 0;

			//If the div width + the position of the x coord is less than the total available space, continue
			if ((left-dWidth) > 0)
			{
				//If the div won't fit against the right side of the window, show it to the left.
				if (wWidth - left < dWidth)
					div.style.left=(left-dWidth)+"px";
			}
		} 
		catch (exception)
		{
		}
}

function ces_stripPx(val) 
{
  try
  {
		var pind = val.indexOf('px');
		val = val.substring(0, pind) - 0;
	} 
	catch (exception)
	{
	}
	return val;
}

function ces_GetToolkitDiv(rowNum, componentId) 
{
  var toolkitDiv;
  _ces_clientId = '';

  if (rowNum == "multi")
    toolkitDiv = document.getElementById(_ces_clientId + componentId + "_toolkit-MULTI");
  else
    toolkitDiv = document.getElementById(_ces_clientId + componentId + "_toolkit-" + rowNum);
    
  return toolkitDiv;
}

function ces_showToolkit(componentId, rowNum, evt, x, y) 
{
  ces_currCompId = componentId;
  
  var toolkitDiv = null;
  toolkitDiv = ces_GetToolkitDiv(rowNum, componentId);
  var toolkitAjaxDiv = document.getElementById('ces_aj_context_menu');
  if(toolkitAjaxDiv == null)
	return;
  toolkitAjaxDiv.style.visibility = 'visible';

	var curX=0;
	var curY=0;

	if (x != null)
		curX = x;
	else
		curX = (ces_ns6Br)?evt.pageX : event.clientX+ietruebody().scrollLeft;
		
	if (y != null)
		curY = y;
	else
		curY = (ces_ns6Br)?evt.pageY : event.clientY+ietruebody().scrollTop;

  ces_PlaceDiv(toolkitAjaxDiv, curX, curY);

  if (toolkitDiv != null) 
  { 

    var toolkitTable = document.getElementById(componentId + 'DynToolkitTable-' + rowNum);
    var tempTk = toolkitDiv.innerHTML;

    // Clear all pointers
    ces_pointedId = -1;
    ces_pointedRow = -1;
    ces_pointerId = -1;

    toolkitDiv.style.visibility = 'visible';
    ces_PlaceDiv(toolkitDiv, curX, curY);

    if (rowNum == "multi") 
    {

      //11/9/04
      var multiTable = document.getElementById(componentId + "DynToolkitTable-MULTI");

      for (var i=multiTable.rows.length - 1; i >= 1; i--) 
      {
        multiTable.deleteRow(i);
      }

      // This jscript function is build in DocumentUtil.cs
      // or in any file that needs to utilize multi select.
      try 
      {
        eval('buildTaskList_' + componentId + '()');
      } catch (exception) {

      }
    }
  } 
  else if (rowNum == 'multi') 
  {

  } 
  else 
  { // Toolkit Div is null

    var sourceId = '';
    var sourceObj = ces_getRightClickSourceObj(evt);
    if (sourceObj != null)
      sourceId = sourceObj.id;

    var itemType = ces_getRowItemBySource('itemtype', sourceId);

    var itemId = ces_getRowItemBySource('itemid', sourceId);

    if (itemType == 'folder' || itemType == 'CES.NET.ContentCategory')
      itemType = 'c';
    else if (itemType == 'element' || itemType == 'CES.NET.ContentElement')
      itemType = 'e';
    else if (itemType == 'package' || itemType == 'CES.NET.ContentPackage')
      itemType = 'p';
    else if (itemType.indexOf('calendarEntry') > -1)
      itemType = 'calentry';
    else if (itemType.indexOf('calendarDay') > -1)
      itemType = 'calday';
    else if (itemType.indexOf('cesxmlcomp') > -1)
      itemType = 'cesxmlcomp';
    else if (itemType.indexOf('portalbox') > -1)
      itemType = 'portalbox';
    else if (itemType.indexOf('breadcrumb') > -1) {
      var breadIndex = itemType.indexOf('breadcrumb');
      var code = itemType.substring(breadIndex + 10, breadIndex + 11);
      itemType = 'breadcrumb' + code;
    }
    else if (itemType.indexOf('rbc') > -1)
      itemType = 'rbc';   
    else if (itemType.indexOf('rbe') > -1)
      itemType = 'rbe';     

    var bComplete = false;
	  try 
	  {
		  var customPostBack = eval('__ces_postbackftn_' + componentId);

		  var tReturnUrl = '';

		  try 
			{
				tReturnUrl = eval('returnUrl_rclk');
			} catch(Exception){}		  

      if (tReturnUrl != null && tReturnUrl.split(';').length > 1)
      {
        tReturnUrl = tReturnUrl.split(';')[1];     
      }
  
		  ces_generateRightClickPostBackMenu(tReturnUrl, customPostBack, componentId, rowNum, itemId, itemType);
		  bComplete = true;
			  
	  } catch(Exception){}

    if (!bComplete) 
    {

      var rtPkgId = 0;
	    var rtDocId = 0;
	    try 
	    {
		    rtPkgId = eval('rootPkgId_' + componentId);
	    } catch(Exception){}
	    try 
	    {
		    rtDocId = eval('rootDocId_' + componentId);
	    } catch(Exception){}

      // If this is an element in a package, set the type to package element
      if (rtPkgId > 0 && itemType == 'e')
      {
        itemType = 'pe';
        itemId = rtPkgId + '-' + itemId;
      }
      if (rtPkgId > 0 && itemType == 'package_package')
      {
        itemType = 'pp';
        itemId = rtPkgId + '-' + itemId;
      }

      if (rtDocId > 0 && itemType == 'document_package')
      {
        itemType = 'ep';
        itemId = rtDocId + '-' + itemId;     
      }
      if (rtPkgId > 0 && itemType == 'package_element')
      {     
        itemId = rtPkgId + '-' + itemId;
      }

		var returnUrl = '';
	    try 
	    {
		    try 
				{
					returnUrl = eval('returnUrl_rclk');

				} catch(Exception){}

				if (returnUrl != null && returnUrl.split(';').length > 1)
				{
					returnUrl = returnUrl.split(';')[1];     
				}			

		    ces_generateItemToolkit(returnUrl, componentId, rowNum, itemId, itemType);			    

	    } catch(Exception){}
	  }
  }
}

function ces_hideToolkit(tk) 
{
  var dash = tk.indexOf("-");
  var rowNum = tk.substring(dash + 1, tk.length);
  var componentId = tk.substring(0, dash);

  var cid = '';
  
  try 
  {
    cid = ces_getRowItemBySource('componentid', ces_lastRowObj.id);
  } catch (exception) {}

  if (componentId == '')
    componentId = cid;

  var toolkitDiv;
  if (ces_pointedRow > -1)
    toolkitDiv = ces_GetToolkitDiv(ces_pointedRow, componentId);
  else
    toolkitDiv = ces_GetToolkitDiv(rowNum, componentId);

  var ajToolkitDiv = document.getElementById('ces_aj_context_menu');
  if (ajToolkitDiv != null) 
  {
    if (ajToolkitDiv.innerHTML != '')
    {
      ajToolkitDiv.innerHTML = '';
    }
  }

  if (toolkitDiv != null) 
  {     
    toolkitDiv.style.visibility = 'hidden';
  }
  ces_activeToolkit = '';
}

function ces_highlightToolkitRow(compId, num, row, size) { 

  if (num == 'MULTI') {
    var multiTable = document.getElementById(compId + "DynToolkitTable-MULTI");
    if (multiTable != null) {
      size = multiTable.rows.length - 1;
    }
  }

  var thisRow = -1;
  if (row != null) {
    thisRow = row.id;
  }

  for (var n=0; n < size; n++) {
  	
    row = document.getElementById(compId + "toolkit-" + num + "-row-" + n);

    for (var i=0; i < row.cells.length; i++) {    
      if (thisRow == row.id) {
        row.cells[i].className="selected";
      } else {
        row.cells[i].className="";
      }
    }
  }
}

function ces_highlightRow(rowObj, rowNum) {
  if(rowObj == null)
	return;
  tempLastRow = rowObj;

  // Loop through each cell in the row and change the className
  for (var i=0; i < rowObj.cells.length; i++) {    
    rowObj.cells[i].className="selected";
  }

  if (ces_lastRowObj != null) {
    if (ces_lastRowObj.id != rowObj.id) {
      for (var i=0; i < ces_lastRowObj.cells.length; i++) {    
        ces_lastRowObj.cells[i].className="";
      }
    }
  }  
  
  ces_lastRowObj = rowObj;
}

var tempLastRow = null;

function ces_highlightTempRow(rowObj, rowNum) {

  for (var i=0; i < rowObj.cells.length; i++) {
    if (ces_lastRowObj.id == rowObj.id) {
      rowObj.cells[i].className="selected";
    }
    else {
      rowObj.cells[i].className="selected-temp";
    }
    
    if (tempLastRow.id != ces_lastRowObj.id) {
      tempLastRow.cells[i].className="";
    }
  }
  tempLastRow = rowObj;
  document.body.style.cursor = 'hand';
}

function ces_mouseOutOfRow(rowObj, rowNum) {

  for (var i=0; i < rowObj.cells.length; i++) {
    if (ces_lastRowObj.id == rowObj.id) {
      rowObj.cells[i].className="selected";
    }
    else {
      rowObj.cells[i].className="";
    }
  }
  document.body.style.cursor = 'auto';  
}

function ces_getParentSourceObj(strId, sourceObj) {

  var parentSourceObj = ces_ns6Br ? sourceObj.parentNode : sourceObj.parentElement;

  while (parentSourceObj != null && parentSourceObj.id != null) {
    if (parentSourceObj.id.indexOf(strId) > -1)
      break;
    else
      parentSourceObj = ces_ns6Br ? parentSourceObj.parentNode : parentSourceObj.parentElement;
  }
  return parentSourceObj;
}

function ces_getRightClickSourceObj(evt) {

  var parentSourceObj = null;
  var sourceObj = ces_ns6Br ? evt.target : event.srcElement;
  var sourceId = null;

  if (sourceObj != null)
    sourceId = sourceObj.id;
  else
    return null;
    
  if (sourceId == null)
    return null;

  try {
    if (sourceId.indexOf("link") > -1) {
      sourceObj = ces_ns6Br ? sourceObj.parentNode : sourceObj.parentElement;//sourceObj.parentElement;      
      sourceId = sourceObj.id;
    }
  } catch (exception) {
  }

  // desc
  if (sourceId != null && (sourceId.indexOf("r_clk") != -1 || sourceId.indexOf("breadcrumb") != -1)) 
  {
    return sourceObj;
  } 
  else 
  {
    parentSourceObj = ces_getParentSourceObj('r_clk', sourceObj);
  }

  // If still no valid object, see if there is a portal box right click menu
  if (parentSourceObj == null || parentSourceObj.id == null || parentSourceObj.id.indexOf("r_clk") == -1) {
    parentSourceObj = ces_getParentSourceObj('cesportalbox', sourceObj);
  }
  
  return parentSourceObj;
}

function ces_showRightClickToolkit(evt)
{
  var componentId = null;
  var sourceId = null;

  var sourceObj = ces_getRightClickSourceObj(evt);
  if (sourceObj != null) 
  {
    sourceId = sourceObj.id;
    ces_lastSrcId = sourceId;
    componentId = ces_getComponentIdBySource(sourceObj);
  }

  if (componentId != null 
    && componentId > -1 
    && sourceId != null 
    && sourceId != '') {

    if (ces_isThumbnailView) {
      if(sourceId.indexOf("r_clk") > -1) {    
        var thumbnailPanel = document.getElementById(sourceId.replace('r_clk', 'thumbnail'));
        if (thumbnailPanel != null) {
          if (ces_selectedThumbnail != null)
            ces_selectedThumbnail.className = "ces-thumbnail-panel";
          ces_selectedThumbnail = thumbnailPanel;
          thumbnailPanel.className="ces-thumbnail-panel-selected";
        }
      }
    }

    var rowNum = ces_getRowItemBySource('rownum', sourceId);

    var rowObj = getRowObjectByNum(rowNum, componentId);
    if (ces_selectedList.length > 1) 
    {
      var doesContain = false;
      for (var i=0; i < ces_selectedList.length; i++) 
      {
        if (ces_selectedList[i] != null) 
        {
          if (getRowNum(ces_selectedList[i]) == rowNum) 
          {
            doesContain = true; 
            break;
          }
        }
      }
      if (!doesContain) 
      {
        unHighlightAllRows();
        ces_selectedList = new Array();
        ces_selectedList[0] = rowObj;     
      }
    }

    // If multi select
    if (ces_selectedList.length > 1) 
    {
      ces_hideRightClickToolkit(evt);
      ces_activeToolkit = 'MULTI';
      ces_showToolkit(componentId, 'multi', evt);
      return false;
      
    // else single select
    } 
    else 
    {

      ces_hideRightClickToolkit(evt);
      ces_showToolkit(componentId,rowNum,evt);
      ces_activeToolkit = componentId + "-" + rowNum;

      if (rowObj != null) 
      {
        ces_selectedList[0] = rowObj;
        ces_highlightRow(rowObj, rowNum);
      }
      return false;
    }
  } 
  else 
  {
    _ces_clientId = '';
    ces_showToolkit(componentId,rowNum,evt);    
  }
}

function ces_getRowItemBySource(type, sourceId) 
{
  if (sourceId == null)
    return '';
    
  var dash = sourceId.indexOf("-");
  var dash2 = sourceId.indexOf('-', dash + 1);
  var dash3 = sourceId.indexOf('-', dash2 + 1);
  var finalDelim = sourceId.indexOf('_', dash3 + 1);//doesn't work for date because 8:30 is represented as 8_30 or what item items with "_" in their name?
  var elip = sourceId.indexOf("...");
  var len = sourceId.length;

  if (type == 'rownum') {
    if (dash2 == -1)
      return sourceId.substring(dash + 1, len);
    else
      return sourceId.substring(dash + 1, dash2);
  } else if (type == 'componentid') {
    var compStr = 'component';
    var componentStrIndex = sourceId.indexOf(compStr);
    return sourceId.substring(componentStrIndex + compStr.length, elip);  
  } else if (type == 'itemtype') {
    return sourceId.substring(dash2 + 1, dash3);
  } else if (type == 'itemid') {
    if (sourceId.indexOf("tv.") > -1)
      len = finalDelim;
    return sourceId.substring(dash3 + 1, len);
  } else if (type == 'clientid') {
    var componentStrIndex = sourceId.indexOf('component');
    return sourceId.substring(0, componentStrIndex);  
  } else {
    return '';
  }
}

function ces_getComponentInfo(id) {
  for (var i=0; i < ces_componentArray.length; i++) {
    if (ces_componentArray[i].id == id)
      return ces_componentArray[i].name;
  }
  return "";
}

function ces_hideRightClickToolkit(evt) {

try
{
	if(ces_activeMenuItems)
	{
		for (var i=ces_activeMenuItems.length-1; i>-1;i--) 
		{
			document.getElementById(ces_activeMenuItems[i]).style.visibility = 'hidden';	
			c_sif(false, ces_activeMenuItems[i]);
		}
		ces_activeMenuItems = new Array();
	}
} catch(exception)
{
}

  var sourceId = ces_ns6Br ? evt.target.id : event.srcElement.id;
  if (sourceId != null)
  {
    if (sourceId.indexOf("DynToolkit") == -1) {
      if (ces_activeToolkit == 'MULTI' || ces_activeToolkit != '') {
        ces_hideToolkit(ces_activeToolkit);
      }
    }
  }
}

//111504
function ces_checkClick(evt) {
  var sourceId = ces_ns6Br ? evt.target.id : event.srcElement.id;
  if (sourceId != null) {
    if (sourceId.indexOf('dyntoolkitcell') == -1 && sourceId.indexOf('ElementTaskButton') == -1) {
      ces_hideToolkit(ces_activeHtmlToolkit);
    }
  }
}

//111504
var ces_activeHtmlToolkit = '';

function ces_showHtmlPlaceholderToolkit(compId, rowNum) 
{
  ces_activeHtmlToolkit = compId + '-' + rowNum;
  ces_showToolkit(compId, rowNum);
}

function ces_checkHtmlPlaceholderToolkit(evt) 
{
  var sourceId = ces_ns6Br ? evt.target.id : event.srcElement.id; 
  if (sourceId != null) 
  {
    if (sourceId.indexOf('ElementTaskButton') == -1)
    {
      if (ces_activeHtmlToolkit != '')   
        ces_hideToolkit(ces_activeHtmlToolkit);
    }
  }
}

function ces_docClick(evt) 
{
  ces_checkHtmlPlaceholderToolkit(evt);

  var sourceId = '';
  var sourceObj = ces_getRightClickSourceObj(evt);
  if (sourceObj != null)
    sourceId = sourceObj.id;  

  if (ces_activeToolkit == 'MULTI' || ces_activeToolkit != "") 
  {
    ces_hideToolkit(ces_activeToolkit);
  }
  
  if (sourceId == null)
    sourceId = '';
    
  if (sourceId.indexOf("r_clk") == -1) 
  {
    unHighlightAllRows();
    ces_selectedList = new Array();  
    ces_activeComponentId = -1;
    ces_lastSrcId = null;
  } else {
    ces_activeComponentId = ces_getRowItemBySource('componentid', sourceId);
    ces_lastSrcId = sourceId;
  }

  // Begin Thumbnails
  if (sourceId != '' && ces_isThumbnailView) 
  {
    if(sourceId.indexOf("r_clk") > -1) {
      var thumbnailPanel = document.getElementById(sourceId.replace('r_clk', 'thumbnail'));
      if (thumbnailPanel != null) {
        if (ces_selectedThumbnail != null)
          ces_selectedThumbnail.className = "ces-thumbnail-panel";
        ces_selectedThumbnail = thumbnailPanel;
        thumbnailPanel.className="ces-thumbnail-panel-selected";
      }
    }
  }
  // End thumbnails
  
  // hide any active toolbar items
  var editModeTB =  document.getElementById('ces_editmode_tb');
  if (editModeTB)
    ces_hideMenuDropDowns(event, editModeTB, '');
    
  try 
  {
    //020707
    var dynContent = document.getElementById('cesDynCntDiv');
    if (dynContent != null)
    {
      dynContent.style.display = 'none';
    }
  }
  catch (exception) 
  {
  }
  
  try
  {
    //022707
    for (var i=0; i < ces_docClickFtns.length; i++) 
    {
        try
        {
          eval(ces_docClickFtns[i]);
				}
				catch (exception) 
				{
			  
				}
    }
  }
  catch (exception) 
  {
  
  }  
}

function ces_disableRightClick(evt) {
  return false;
}

function ces_selectRow(rowObj) {
  if (ces_selectedKey == ces_CTRL_KEY) {

    // if selected list has 0 items, this is ces_pivot
    if (ces_selectedList.length == 0)
      ces_pivot = rowObj;
      
    // if already selected, unselect!
    if (alreadySelected(rowObj)) {

      ces_removeFromSelected(rowObj);
      
      //unselect
      for (var i=0; i < rowObj.cells.length; i++) {    
        rowObj.cells[i].className="";
      }    
    } else {

      // Add to selected list
      ces_addSelectedRow(rowObj);
      
      //select
      for (var i=0; i < rowObj.cells.length; i++) {    
        rowObj.cells[i].className="selected";
      }
      ces_lastRowObj = rowObj;
    }

  } else if (ces_selectedKey == ces_SHIFT_KEY) {

    var rowNum = getRowNum(rowObj);
 
    // if selected list has 0 items, this is ces_pivot
    if (ces_selectedList.length == 0)
      ces_pivot = rowObj;
      
    if (rowNum > getRowNum(ces_pivot)) {
      shift('down', rowObj);
    } else {
      shift('up', rowObj);
    }
    ces_lastRowObj = rowObj;
  
  } else {
    ces_selectRowObject(rowObj);  
  }
}

function ces_selectRowObject(rowObj) {
  
  unHighlightAllRows();
  ces_selectedList = new Array();
  ces_selectedList[0] = rowObj;
  
  // if selected list has 0 or 1 item(s), this is ces_pivot
  if (ces_selectedList.length == 0 || ces_selectedList.length == 1)
    ces_pivot = rowObj;

  if (rowObj != null) {
    for (var i=0; i < rowObj.cells.length; i++) { 

      rowObj.cells[i].className="selected";
      if (ces_lastRowObj != null) {
        if (ces_lastRowObj.id != rowObj.id) {
          if (ces_lastRowObj.cells[i] != null)
            ces_lastRowObj.cells[i].className="";
        }
      }
    }
  }
  ces_lastRowObj = rowObj;
}

function shift(type, rowObj) {
  var first;
  var last;

  if (type == 'down') {
    last = getRowNum(rowObj);
    first = getRowNum(ces_pivot);
  } else {
    last = getRowNum(ces_pivot);
    first = getRowNum(rowObj);  
  }

  unHighlightAllRows();
  ces_selectedList = new Array();
  
  for (var i=0; i < rowObj.cells.length; i++) {    
    rowObj.cells[i].className="selected";
    if (ces_lastRowObj != null) {
      if (ces_lastRowObj != rowObj) {
        ces_lastRowObj.cells[i].className="";
      }
    }
  }  
  
  // If we were shifting one way and then switched while still holding shift then swap the indexes.
  if ((last - 0) < (first - 0)) {
    var iTemp = first;
    first = last;
    last = iTemp;
  }
  
  var cid = ces_getRowItemBySource('componentid', rowObj.id);

  for (var i=(first - 0); i <= (last - 0); i++) {

    var obj2 = getRowObjectByNum(i, cid);

    if (obj2 != null) {
    
      ces_addSelectedRow(obj2);
      
      //select
      for (var k=0; k < obj2.cells.length; k++) {
        obj2.cells[k].className="selected";
      }
    }
  }  
}

function getRowObjectByNum(iNum, iCompId) {

  if (iCompId == null) {
    if (ces_activeComponentId > -1) {
      iCompId = ces_activeComponentId;
    } else {
      try {
        iCompId = ces_getRowItemBySource('componentid', ces_lastRowObj.id);
      } catch (exception) {
      
      }
    }
  }
  
  var rowObj = document.getElementById("component" + iCompId + "...document-" + iNum);

  return rowObj;
}

function getRowNum(tempObj) {
  var dash = tempObj.id.indexOf("-");
  var len = tempObj.id.length;
  return tempObj.id.substring(dash + 1, len);
}

function unHighlightAllRows() {
  for (var i=0; i < ces_selectedList.length; i++) {
    if (ces_selectedList[i] != null) {
      var tempRowObj = ces_selectedList[i];
      for (var k=0; k < tempRowObj.cells.length; k++) {    
        tempRowObj.cells[k].className="";
      }
    }    
  }
}

function ces_addSelectedRow(rowObj) {

  var iLen = ces_selectedList.length;
  if (ces_maxRowSelect != -1 && iLen >= ces_maxRowSelect) {
    var tRowObj = ces_selectedList[0];
    ces_removeFromSelected(tRowObj);
    for (var i=0; i < tRowObj.cells.length; i++) {    
      tRowObj.cells[i].className="";
    }      
  }
  ces_selectedList[iLen] = rowObj;
}

function alreadySelected(rowObj) {
  for (var i=0; i < ces_selectedList.length; i++) {
    if (ces_selectedList[i] != null) {
      if (ces_selectedList[i].id == rowObj.id) {
        return true;
      }
    }
  }
  return false;
}

function ces_removeFromSelected(rowObj) {
  for (var i=0; i < ces_selectedList.length; i++) {
    if (ces_selectedList[i] != null) {
      if (ces_selectedList[i].id == rowObj.id) {
        
        delete ces_selectedList[i]; 
        
        //create new array
        var temp = new Array();
        var x = 0;
       
        for (var n=0; n < ces_selectedList.length; n++) {
          if (ces_selectedList[n] != null) {
            temp[x] = ces_selectedList[n];          
            x++;
          }
        }
        // set to new array
        ces_selectedList = temp;
      }
    }
  } 
}

function ces_getComponentIdBySource(sourceObj) {
  var componentId = -1;
  
  var sourceId = sourceObj.id;

  if (sourceId != null) {

    // NOTE: links do not have to adhere because of parentElement
    if (sourceId.indexOf("link") > -1) {
      sourceId = sourceObj.parentElement.id;
    }   

    var featuredDocArea = sourceId.indexOf("document") > -1;
    var docPkgArea = sourceId.indexOf("tasklink") > -1 || sourceId.indexOf("r_clk") > -1;
    var breadCrumbLink = sourceId.indexOf("breadcrumb") > -1

    if (sourceId != '') {
      componentId = ces_getRowItemBySource('componentid', sourceId);
    }
  }
  return componentId;
}

function ces_trackKey(evt) {
  var keyCode = '';
  if (ces_ns6Br) {
    keyCode = evt.keyCode;
  } else {
    keyCode = event.keyCode
  }
  
  if (ces_DELETE_KEY == keyCode) {
    try {
      // Try to execute registered delete task
      if (ces_lastSrcId != null && ces_selectedList.length == 1) {
        eval('_ces_deleteTask_' + ces_activeComponentId + '()');
      }
    } catch (exception) {
    }
  }

  if (keyCode == ces_SHIFT_KEY || keyCode == ces_CTRL_KEY) {
    if (ces_maxRowSelect > 0 && keyCode == ces_SHIFT_KEY) {
      // do nothing
    } else {
      ces_selectedKey = keyCode;
    }
  }
  
  // Begin cut/copy/paste
  try {
    if (ces_selectedList.length == 1) {
			if (keyCode == ces_X_KEY && ces_lastSrcId != null) {
				//window.status = 'cut ' + ces_lastSrcId;
				eval('_ces_cutTask_' + ces_activeComponentId + '()');
			} else if (keyCode == ces_C_KEY && ces_lastSrcId != null) {
				//window.status = 'copy ' + ces_lastSrcId;
				eval('_ces_copyTask_' + ces_activeComponentId + '()');
			} else if (keyCode == ces_V_KEY) {
				//window.status = 'paste ' + ces_lastSrcId;
				eval('_ces_pasteTask_' + ces_activeComponentId + '()');
			}
		}
  } catch (exception) {
  
  } 
  
  /********************************************************
  * UP ARROW CLICKED
  ********************************************************/  
  if (keyCode == ces_ARROW_UP ) {
    ces_lastSrcId = null;
    if (ces_pivot != null) {
      
      if (ces_selectedKey == ces_SHIFT_KEY) {
        
        var oneUp;
        if (ces_selectedList && ces_selectedList.length <= 1) {
          oneUp = getRowNum(ces_pivot) - 1;
        } else {
          oneUp = getRowNum(ces_lastRowObj) - 1;
        }
        if (oneUp > -1) {
          shift('up', getRowObjectByNum(oneUp));
          ces_lastRowObj = getRowObjectByNum(oneUp);
        }
      } else {     
        var oneUp = getRowNum(ces_pivot) - 1;
        
        if (oneUp > -1)
          ces_selectRowObject(getRowObjectByNum(oneUp));
      }
    }
  /********************************************************
  * DOWN ARROW CLICKED
  ********************************************************/ 
  } else if (keyCode == ces_ARROW_DOWN ) {
  
    ces_lastSrcId = null;
  
    if (ces_pivot != null) {
      
      if (ces_selectedKey == ces_SHIFT_KEY) {
        
        var oneDown;
        if (ces_selectedList.length <= 1) {
          oneDown = ((getRowNum(ces_pivot)) - 0) + 1;
        } else {
          oneDown = ((getRowNum(ces_lastRowObj)) - 0) + 1;
        }
        if (getRowObjectByNum(oneDown) != null) {
          shift('down', getRowObjectByNum(oneDown));
          ces_lastRowObj = getRowObjectByNum(oneDown);
        }
      } else {
        var oneDown = ((getRowNum(ces_pivot)) - 0) + 1;
        if (getRowObjectByNum(oneDown) != null)
          ces_selectRowObject(getRowObjectByNum(oneDown));
      }
    }
  /********************************************************
  * RIGHT ARROW CLICKED
  ********************************************************/ 
  } else if (keyCode == ces_ARROW_RIGHT ) {
    //ces_showRightClickToolkit;
  }
  
}

function ces_unces_trackKey(evt) {

  var keyCode = '';
  if (ces_ns6Br) {
    keyCode = evt.keyCode;
  } else {
    keyCode = event.keyCode
  }

  if ((keyCode == ces_ARROW_UP || keyCode == ces_ARROW_DOWN) 
    && (ces_selectedKey == ces_SHIFT_KEY)) {
    //do nothing
  } else {
    ces_pivot = ces_lastRowObj;
    ces_selectedKey = null;
  }
}

function ces_adjustIFrameSize (iframeWindow) 
{
  if (iframeWindow.document.height) 
  {
    var iframeElement = parent.document.getElementById(iframeWindow.name);
    iframeElement.style.height = iframeWindow.document.height + 'px';
    iframeElement.style.width = iframeWindow.document.width + 'px';
  }
  else if (document.all) 
  {
    var iframeElement = parent.document.all[iframeWindow.name];
    if (iframeWindow.document.compatMode &&
        iframeWindow.document.compatMode != 'BackCompat') 
    {
      iframeElement.style.height = iframeWindow.document.documentElement.scrollHeight + 5 + 'px';
      iframeElement.style.width = iframeWindow.document.documentElement.scrollWidth + 5 + 'px';
    }
    else 
    {
      iframeElement.style.height = iframeWindow.document.body.scrollHeight + 5 + 'px';
      iframeElement.style.width = iframeWindow.document.body.scrollWidth + 5 + 'px';
    }
  }
}

// Global for brower version branching.
var Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 4));

// One object tracks the current modal dialog opened from this window.
var ces_dialogWin = new Object();

function ces_openDGDialog(url, width, height, returnFunc, args) {
	if (!ces_dialogWin.win || (ces_dialogWin.win && ces_dialogWin.win.closed)) {
		ces_dialogWin.returnFunc = returnFunc;
		ces_dialogWin.returnedValue = "";
		ces_dialogWin.args = args;
		ces_dialogWin.url = url;
		ces_dialogWin.width = width;
		ces_dialogWin.height = height;
		// Keep name unique so Navigator doesn't overwrite an existing dialog.
		ces_dialogWin.name = (new Date()).getSeconds().toString();
		// Assemble window attributes and try to center the dialog.
		if (Nav4) {
			// Center on the main window.
			ces_dialogWin.left = window.screenX + 
			   ((window.outerWidth - ces_dialogWin.width) / 2);
			ces_dialogWin.top = window.screenY + 
			   ((window.outerHeight - ces_dialogWin.height) / 2);
			var attr = "screenX=" + ces_dialogWin.left + 
			   ",screenY=" + ces_dialogWin.top + ",scrollbars=yes,resizable=yes,width=" + 
			   ces_dialogWin.width + ",height=" + ces_dialogWin.height;
		} else {
			// The best we can do is center in screen.
			ces_dialogWin.left = (screen.width - ces_dialogWin.width) / 2;
			ces_dialogWin.top = (screen.height - ces_dialogWin.height) / 2;
			var attr = "left=" + ces_dialogWin.left + ",top=" + 
			   ces_dialogWin.top + ",scrollbars=yes,resizable=yes,width=" + ces_dialogWin.width + 
			   ",height=" + ces_dialogWin.height;
		}
		
		// Generate the dialog and make sure it has focus.
		ces_dialogWin.win=window.open(ces_dialogWin.url, ces_dialogWin.name, attr);
		if(!ces_dialogWin.win)
		{
			alert("You must disable your pop-up blocker for this site.");
			return;
		}
		ces_dialogWin.win.focus();
	} else 
	{
		if(!ces_dialogWin.win)
		{
			alert("You must disable your pop-up blocker for this site.");
			return;
		}
		ces_dialogWin.win.focus();
	}
}

function ces_deadend() {
	if (ces_dialogWin.win && !ces_dialogWin.win.closed) {
		ces_dialogWin.win.focus();
		return false
	}
}

var ces_IELinkClicks;

function ces_disableForms() {
	ces_IELinkClicks = new Array();
	for (var h = 0; h < frames.length; h++) {
		for (var i = 0; i < frames[h].document.forms.length; i++) {
			for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
				frames[h].document.forms[i].elements[j].disabled = true;
			}
		}
		ces_IELinkClicks[h] = new Array();
		for (i = 0; i < frames[h].document.links.length; i++) {
			ces_IELinkClicks[h][i] = frames[h].document.links[i].onclick;
			frames[h].document.links[i].onclick = ces_deadend;
		}
		frames[h].window.onfocus = ces_checkModal;
    	frames[h].document.onclick = ces_checkModal;
	}
}

function ces_enableForms() {
	for (var h = 0; h < frames.length; h++) {
		for (var i = 0; i < frames[h].document.forms.length; i++) {
			for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
				frames[h].document.forms[i].elements[j].disabled = false;
			}
		}
		for (i = 0; i < frames[h].document.links.length; i++) {
			frames[h].document.links[i].onclick = ces_IELinkClicks[h][i];
		}
	}
}

function ces_blockEvents() {
	if (Nav4) {
		window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS);
		window.onclick = ces_deadend;
	} else {
		ces_disableForms();
	}
	window.onfocus = ces_checkModal;
}

function ces_unblockEvents() {
	if (Nav4) {
		window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS);
		window.onclick = null;
		window.onfocus = null;
	} else {
		ces_enableForms();
	}
}

function ces_checkModal() {
	setTimeout("ces_finishChecking()", 50);
	return true;
}

function ces_finishChecking() 
{
	try
	{	
		if (ces_dialogWin.win && !ces_dialogWin.win.closed) 
			ces_dialogWin.win.focus();
	} 
	catch(exception){}
}

function ces_comp_list_menuRowOn(ctrl)
{
	ctrl.style.borderColor = '#0000FF';
	ctrl.style.cursor = 'hand';
	ctrl.style.backgroundColor = '#99CCCC';	
}

function ces_comp_list_menuRowOff(ctrl)
{
	ctrl.style.borderColor = '#FFFFFF';
	ctrl.style.cursor = '';
	ctrl.style.backgroundColor = '#FFFFFF';
}


function ces_toolkit_row_mouseover(ctrl) {
	ctrl.className = 'active';
}

function ces_toolkit_row_mouseout(ctrl)
{
	ctrl.className = 'inactive';
}

function ces_appendToFunction(func,script) 
{
	try
	{
		var currScript = eval(func) ? eval(func).toString() : "";
		eval(func + " = new Function( (currScript.length ? currScript.slice(currScript.indexOf(\"{\")+1, currScript.length-2) : \"\") + script);");
	}
	catch(exception)
	{	
	}
}	

function ces_generateRightClickPostBackMenu(returnUrl, ftn, componentId, row, entityId, entityType)
{
  ces_contextMenu_GetData(componentId, row, entityId, entityType, returnUrl, ftn);
}

function ces_generateItemToolkit(returnUrl, componentId, row, entityId, entityType)
{	
  ces_contextMenu_GetData(componentId, row, entityId, entityType, returnUrl, '');
}

function ces_contextMenu_GetData(componentId, row, entityId, entityType, returnUrl, ftn)
{
  entityId = entityId.replace('&', ';amp');
  try 
  {
    eval('DocumentUtil.GetContextMenu(componentId, row, entityId, entityType, escape(returnUrl), "", ftn, ces_contextMenu_CallBack)');
  } catch (exception)
  {
    try 
    {
      eval('parent.DocumentUtil.GetContextMenu(componentId, row, entityId, entityType, escape(returnUrl), "", ftn, ces_contextMenu_CallBack)');
    } catch (exception)
    {
    }
  }
}

function ces_contextMenu_CallBack(response)
{
  var toolkitDiv = document.getElementById('ces_aj_context_menu');
  if (toolkitDiv != null)
  {
    toolkitDiv.innerHTML = response.value;
    var cX = ces_stripPx(toolkitDiv.style.left);
    var cY = ces_stripPx(toolkitDiv.style.top);
    ces_PlaceDiv(toolkitDiv, cX, cY, false);
  }
}

function ces_showflash(contentVersion, cabVersion, source, width, height, alt_img, alt_width, alt_height)
{
	var flashCanPlay = true;

	var plugin = (navigator.mimeTypes && navigator.mimeTypes['application/x-shockwave-flash']) ?
			navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin : 0;

	if (plugin) 
	{
		var words = navigator.plugins['Shockwave Flash'].description.split(' ');
		for (var i = 0; i < words.length; ++i)
		{
			if (isNaN(parseInt(words[i])))
				continue;
			var pluginVersion = words[i];
		}
		flashCanPlay = pluginVersion >= contentVersion;
	}

	if (flashCanPlay) 
	{
		var flashStr = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' +
			'codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + cabVersion + '" ';
			
		if (width != '')
			flashStr += 'width="' + width + '" ';
		
		if (height != '')
			flashStr += 'height="' + height + '"';
			
		flashStr +=
			' >' + 
			'<param name=movie value="' + source + '">' +
			'<param name=quality value=high>' +
			'<param name="wmode" value="transparent">' +
			'<embed src="' + source + '" menu=false quality=high bgcolor=#FFFFFF ' +
			'swLiveConnect=false width="' + width + '" height="' + height + '" align="right" ' +
			'type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">' +
			'</embed>' +
			'</object>';

		document.write(flashStr);
	} else
	{
		if(alt_img != '')
		{
			document.write('<a href="http://www.macromedia.com/go/getflashplayer" target="_blank">');
			document.write('<img src="' + alt_img + '" width="' + alt_width + '" height="' + alt_height + '" border="0" alt="Get Macromedia Flash Player">');
			document.write('</a>');
		}
	}
}

function ces_findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
		while(1) 
		{
			curleft += obj.offsetLeft;
			if(!obj.offsetParent)
			break;
			obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function ces_findPosY(obj)
{
	if(obj == null)
		return 0;
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function ces_getAbsTop(control)
{
	return ces_findPosY(control);
}

function ces_size_editor(editor, container)
{      
	editor.SetSize('100%', '100%');
}

function ces_resize_full(control, offset, container)
{

	if(control == null || ces_resizing == 1)
		return;
    if(!offset)
		offset = 0;
	var winheight;
	var winwidth;
	if(container == null)
	{
		if(ces_ieBr)
		{
			// Strict mode
			if(!(document.documentElement.clientWidth == 0))
			{
				winheight = document.documentElement.clientHeight;
				winwidth = document.documentElement.clientWidth;

			} else // Quirks mode
			{
				winheight = document.body.clientHeight;
				winwidth = document.body.clientWidth;
			}
		} else // w3c
		{
			winheight = window.innerHeight;
			winwidth = window.innerWidth;
		}
	}
	else
	{
		winheight = container.offsetHeight;
		winwidth = container.offsetWidth;
	}

	var winTop = ces_getAbsTop(control);
	
	try
	{
		ces_resizing = 1;	
		control.style.height=(winheight - winTop - offset) + 'px';
		ces_resizing = 0;
	}catch(exception)
	{
	}	
}

function ces_scrollTo(scrollcontrol, positioncontrol)
{
	if(scrollcontrol == null || positioncontrol == null)
		return;
	if(ces_ieBr)
		scrollcontrol.scrollTop = positioncontrol.offsetTop;
	else
		scrollcontrol.scrollTop = positioncontrol.offsetTop;
}

function ces_showProcessArea() 
{
  var areaWidth = 250;
  var areaHeight = 300;
  var winl = (document.body.offsetWidth-20 - areaWidth) / 2;
  var wint = (document.body.offsetHeight - areaHeight) / 2;
  var divObj = document.getElementById('ProcessAlert');
  if (divObj) {
    divObj.style.top = wint + 'px';
    divObj.style.left = winl + 'px';
    divObj.style.display = '';
  }
}
function ces_hideProcessArea() 
{
  var divObj = document.getElementById('ProcessAlert');
  if (divObj) {
    divObj.style.display = 'none';
  }
}

function noop()
{
}

function ces_firstSibling(control)
{
	var sibling = control.previousSibling;
	while(sibling.previousSibling != null)
	{
		sibling = sibling.previousSibling;
	}
	return sibling;
}

function ces_gridedit_status(control)
{
	window.status = ces_firstSibling(control).innerHTML;
}

var ces_storedDynContent = new Array();
function ces_generalInfoObj(id, content, clientX, clientY, justify) {
  this.id = id;
  this.content = content;
  this.clientX = clientX;
  this.clientY = clientY;
  this.justify = justify;
}

function ces_ajunpop(elemId, evt)
{

	var divObj = document.getElementById("cesDynCntDiv");
	divObj.style.display='none';
}

function ces_ajpop(elemId, evt, justify, width) 
{
	try
	{
	if(justify==null)
		justify = true;
			
	var divObj = document.getElementById("cesDynCntDiv");

	var curX = 0;
	var curY = 0;
	try 
	{
	  curX = (ces_ns6Br ? evt.pageX : event.clientX+ietruebody().scrollLeft);
	  curY = (ces_ns6Br ? evt.pageY : event.clientY+ietruebody().scrollTop);
	} 
	catch (exception) 
	{
	}

	var contains = false;
	var i=0;

	for (i=0; i < ces_storedDynContent.length; i++) {
	  var dobj = ces_storedDynContent[i];
	  if (dobj.id == elemId) {
	    contains = true;
	    break;
	  }
	}

	if (contains) 
	{
	  divObj.innerHTML = ces_storedDynContent[i].content;
	  ces_alignDiv(divObj, curX, curY, justify);
	}
	else 
	{
	  ces_storedDynContent[ces_storedDynContent.length] = new ces_generalInfoObj(elemId, null, curX, curY, justify);
	  
	  eval('DocumentUtil.ShowGenericInfo(elemId, ces_ajpop_CallBack)');
	}
	} catch(exception){}
}

function ces_showManagedLinksOnPage()
{
  var hrefs = document.getElementsByTagName("A");
  var IsLinks = false;
  
  if (hrefs) 
  {
    for (var i=0; i < hrefs.length; i++) 
    {
      try 
      {
        var href = hrefs[i].href.toLowerCase();
        if (href.indexOf("popupview.aspx") > -1 || href.indexOf("page=") > -1 || href.indexOf("documentviewer.aspx") > -1)
        {
          IsLinks = true;
          eval('DocumentUtil.ShowManagedLinkInfoAjax(href, _ces_appPath, ces_ajShowManaged_CallBack)');
        } 
      } catch (exception) 
      {
        alert('DocumentUtil is not registered properly');
        return;
      }  
    }
  }
  if (!IsLinks) 
  {
    alert('There are no managed links on this page.');
  }
}

function ces_ajShowManaged_CallBack(response) 
{
  var rval = response.value;
  var val1 = rval.split('~')[0];  
  var val2 = rval.split('~')[1]; 
  
  var hrefs = document.getElementsByTagName("A");
  if (hrefs) 
  {

    for (var i=0; i < hrefs.length; i++) 
    {
      try 
      {

        var href = hrefs[i].href.toLowerCase();
        if (href.indexOf(val1) > -1)
        {
          var img = (val2.indexOf('broken') > -1 ? "delete" : "Check_Out");
          hrefs[i].innerHTML = '<img src="/ces_components/v4/controls/images/toolbar/' + img + '.gif" border="0">&nbsp;' + val2;
        } 
      } catch (exception) 
      {

      }  
    }
  }    
}

function ces_ajdoc(elemId, evt, justify, width) 
{
  ces_ajdocFolder('doc', elemId, evt, justify, width);
}

function ces_ajfolder(elemId, evt, justify, width) 
{
  ces_ajdocFolder('folder', elemId, evt, justify, width);
}

function ces_ajdocFolder(type, elemId, evt, justify, width) 
{

	try
	{
	if(justify==null)
		justify = true;
			
	var divObj = document.getElementById("cesDynCntDiv");
	
	var curX = 0;
	var curY = 0;
	try 
	{
	  curX = (ces_ns6Br ? evt.pageX : event.clientX+ietruebody().scrollLeft);
	  curY = (ces_ns6Br ? evt.pageY : event.clientY+ietruebody().scrollTop);
	} 
	catch (exception) 
	{
	}

	var contains = false;
	var i=0;

	for (i=0; i < ces_storedDynContent.length; i++) {
	  var dobj = ces_storedDynContent[i];
	  if (dobj.id == elemId) {
	    contains = true;
	    break;
	  }
	}

	if (contains) 
	{
	  divObj.innerHTML = ces_storedDynContent[i].content;
	  ces_alignDiv(divObj, curX, curY, justify);
	}
	else 
	{
	  ces_storedDynContent[ces_storedDynContent.length] = new ces_generalInfoObj(elemId, null, curX, curY, justify);
	  
	  if (type == 'folder')
	    eval('DocumentUtil.ShowFolderCounts(elemId, ces_ajpop_CallBack)');
	  else
			eval('DocumentUtil.ShowDocumentPreview(elemId, ces_ajpop_CallBack)');
	}
	} catch(exception){}
}

function ces_ajpop_CallBack(response) 
{

	try
	{
	var divObj = document.getElementById("cesDynCntDiv");

	if(response.value == null || response.value == "null")
		return;
	divObj.style.display = '';
	divObj.innerHTML = response.value;    

	var i=0;
	for (i=0; i < ces_storedDynContent.length; i++) {
	  var dobj = ces_storedDynContent[i];
	  if (dobj.content == null) {
	    ces_storedDynContent[i].content = response.value;
	    break;
	  }
	}
	
	ces_alignDiv(divObj, ces_storedDynContent[i].clientX, ces_storedDynContent[i].clientY, ces_storedDynContent[i].justify);
	} catch(exception){}
}

function ces_alignDiv(div, clientX, clientY, justify)
{

try
{
	div.style.display='';

	if(justify == 0)
	{
		div.style.left = clientX + 15 + "px";
		div.style.top = clientY + "px";
		div.style.height='auto';
		return;
	}
	
	var curX=0;
	var curY=0;
		
	curX = (ces_ns6Br)?clientX : clientX+ietruebody().scrollLeft;		
	curY = (ces_ns6Br)?clientY : clientY+ietruebody().scrollTop;
	
		
	//Find out how close the mouse is to the corner of the window
	var rightedge=ces_ieBr&&!window.opera? ietruebody().clientWidth-clientX-offsetxpoint : window.innerWidth-clientX-offsetxpoint-20;
	var bottomedge=ces_ieBr&&!window.opera? ietruebody().clientHeight-clientY-offsetypoint : window.innerHeight-clientY-offsetypoint-20;

	var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000;

	//if the horizontal distance isn't enough to accomodate the width of the context menu
	if (rightedge<div.offsetWidth)
		//move the horizontal position of the menu to the left by it's width
		div.style.left=ces_ieBr? ietruebody().scrollLeft+clientX-div.offsetWidth+"px" : window.pageXOffsetclientX-div.offsetWidth+"px";
	else if (curX<leftedge)
		div.style.left="5px";
	else
		//position the horizontal position of the menu where the mouse is positioned
		div.style.left=curX+offsetxpoint+"px";

	if (curY < div.offsetHeight) {
		if (bottomedge<div.offsetHeight)
		div.style.top=curY-100+"px";
		else
    div.style.top=curY+offsetypoint+"px";
	}
	else if (bottomedge<div.offsetHeight)
		div.style.top=ces_ieBr? ietruebody().scrollTop+clientY-div.offsetHeight-offsetypoint+"px" : window.pageYOffset+clientY-div.offsetHeight-offsetypoint+"px";
	else
		div.style.top=curY+offsetypoint+"px";
	div.style.height='auto';
	} catch(exception){}
}

function ces_killEvent(e)
{
if(!e)e=window.event;
e.cancelBubble=true;
e.returnValue=false;
if(e.stopPropagation)e.stopPropagation();
}

function ces_ajCutCopy(item, action)
{
  try 
	{
	  eval('DocumentUtil.CutCopyDocumentItem(item, action, ces_ajCutCopy_CallBack)');
	} catch (exception) 
	{
	}
   ces_hideProcessArea();
}

function ces_ajCutCopy_CallBack(response)
{
}
