var CollapsiblePanel_ActivePanel = null;
var CollapsiblePanel_ActiveHeight = 0;
var CollapsiblePanel_ActiveTotalHeight = 0;
var CollapsiblePanel_ActiveLinesToDisplay = 20;
var CollapsiblePanel_TimerID = 0;

function ExpandCollapse (cpName) {
   var cp = document.getElementById(cpName);
   if (cp == null) return false;

   var bCanCollapse = (cp.attributes['CanCollapse'].value == 'True');
   if (!bCanCollapse) return false;

   var cpFixedId = cp.id.split(':').join('_');

   var title = document.getElementById(cpFixedId + '_title');
   var hid = document.getElementById(cpFixedId + '_hid');
   var lnk = document.getElementById(cpFixedId + '_lnkToggle');
   var imb = document.getElementById(cpFixedId + '_imbToggle');
   var pnl = document.getElementById(cp.id + '_pnl');
   if (pnl == null) return false;

   var bCollapsed = (pnl.style.display == 'none');
   var sOnToggle;

   if (bCollapsed) sOnToggle = cp.attributes['OnExpand'].value;
      else sOnToggle = cp.attributes['OnCollapse'].value;

   if ((sOnToggle != null) && (sOnToggle != '')) {
      // if toggle method returns false, cancel the toggle
      if (!eval(sOnToggle)) return false;
   }

   var sTitle;
   var sImageUrl;
   var sLinkText;
   var sCssTitle;

/*
   var bSlide = (cp.attributes['SlideEnabled'].value.toLowerCase() == 'true');
   var nSlideSpeed = parseInt(cp.attributes['SlideSpeed'].value);
   var nSlideLines = parseInt(cp.attributes['SlideLines'].value);
*/

   if (bCollapsed) {
      sTitle = cp.attributes['ExpandedTitle'].value;
      sCssTitle = cp.attributes['CssExpandedTitle'].value;
      sImageUrl = cp.attributes['CollapseImageUrl'].value;
      sLinkText = cp.attributes['CollapseText'].value;
   }
   else {
      sTitle = cp.attributes['CollapsedTitle'].value;
      sCssTitle = cp.attributes['CssCollapsedTitle'].value;
      sImageUrl = cp.attributes['ExpandImageUrl'].value;
      sLinkText = cp.attributes['ExpandText'].value;
   }

   if (title != null) {
      title.innerHTML = '&nbsp;' + sTitle;
      title.attributes['class'].value = sCssTitle;
   }

   if (hid != null) hid.value = bCollapsed ? '0' : '1';
   if (lnk != null) lnk.value = sLinkText;

   if (imb != null) {
      imb.src = sImageUrl;
      imb.title = sLinkText;
      imb.alt = sLinkText;
   }

   pnl.style.display = bCollapsed ? '' : 'none';
   if (bCollapsed && (document.body.clientHeight < (findPosY(pnl) + pnl.clientHeight))) pnl.scrollIntoView(false);

   // here's where we'll check for & do the postback
   var s = bCollapsed ? 'Expand' : 'Collapse';
   var bPostBack = (cp.attributes['PostBackOn' + s].value.toLowerCase() == 'true');
   if (bPostBack) __doPostBack(cpName, s);

   return false;
}

function CPanel_SetCollapsability(cpName, bCanCollapse) {
   var cp = document.getElementById(cpName);
   if (cp != null) cp.attributes['CanCollapse'].value = (bCanCollapse) ? 'True' : 'False';
}

// methods to check contents of a panel
function CPTextCheck(sCPName, sTXTName, sSuffix) {
   // find the panel
   var cpn = document.getElementById(sCPName);
   if (cpn == null) { alert('CollapsiblePanel not found'); return false; }

   // extract the parent hierarchy from the panel's name
   var i = sCPName.lastIndexOf(':');
   var s = (i < 0) ? '' : sCPName.substring(0, i).split(':').join('_') + '_';

   // find the text box
   var txt = document.getElementById(s + sTXTName);
   if (txt == null) { alert('TextBox not found'); return false; }

   // get the current text and remove trailing spaces
   var sText = txt.value;
   while (sText.charAt(sText.length - 1) == ' ') sText = sText.substring(0, sText.length - 1);

   // since this runs before the panel does its thing, we cannot update the title directly, so we update the attributes
   // (always use the attributes property for custom attributes -- it works with all browsers)
   if ((sSuffix == null) || (sSuffix == '')) sSuffix = '*';
   s = (sText != '') ? sSuffix : '';
   var sTitle = cpn.attributes['ExpandedTitle'].value;

   // remove suffix from current title, if it exists
   if (sTitle.substring(sTitle.length - sSuffix.length, sTitle.length) == sSuffix)
      sTitle = sTitle.substring(0, sTitle.length - sSuffix.length);

   // update both titles
   cpn.attributes['ExpandedTitle'].value = sTitle + s;
   cpn.attributes['CollapsedTitle'].value = sTitle + s;

   // allow the toggle action to continue
   return true;
}

