// Min/Max (Expand/Contract) Handler and helper Functions 

//
function toggleExpandContract(obj, affectedRegion, minMaxCtrlStr)
{
  // if either param is missing, return false to fallback to href call
  if (obj == null || affectedRegion == null)
    return false;

  // Grab the body that were going to hide and the chevron 
  // we are going to change
  var body = document.getElementById(affectedRegion);
  var chev = document.getElementById(affectedRegion.replace('_Body', 'Chevron'));

  // if we don't have either element, the process breaks down. Fallback to href call
  if (body == null || chev == null)
    return false;
    
  var currSetting = obj.title.toLowerCase();
  //var panelBody = getElementsWithClass(body, 'table', 'iC_DataPanel');
  var hasAlwaysShowFields = body.className && (' ' + body.className + ' ').indexOf(' iC_HasAlwaysShow ') != -1;
  
  // Check if we can even minimize the body
  if (body.className && (' ' + body.className + ' ').indexOf(' iC_AlwaysShow ') != -1)
    return true; // we can't, so just leave
 
  var minMaxCtrlName;
  
  if (minMaxCtrlStr)
    minMaxCtrlName = minMaxCtrlStr;
  else
    minMaxCtrlName = affectedRegion.replace('_Body', 'MinMax');
  
  // CO 9424: More descriptive link text
  // Chevron text will contain information descriptive of the panel, ie. "Minimize Related Data Panel".
  // Instead of setting the text to "Minimize"/"Maximize", we interchange these words and retain the panel description.
  
  if (currSetting.indexOf('minimize') != -1)
  {
    obj.title = obj.title.replace(/minimize/i, 'Maximize');
    
    if (chev) 
      chev.className = chev.className.replace('border-top-chevronUp','border-top-chevronDown');
      
    if (hasAlwaysShowFields)
      hideFields(body);
    else
      body.style.display = 'none';
    
    setFieldValue(minMaxCtrlName, 0); // 0 = Not Maximized   
	
	updateFrames(affectedRegion.replace('_SearchPage_Datapanel__Body', ''));

    return true;
  }
  else if (currSetting.indexOf('maximize') != -1)
  {
    obj.title = obj.title.replace(/maximize/i, 'Minimize');
    if (chev) chev.className = chev.className.replace('border-top-chevronDown', 'border-top-chevronUp');
    if (hasAlwaysShowFields)
      showFields(body);
    else
      body.style.display = "";
    
    setFieldValue(minMaxCtrlName, 1); // 1 = Is Maximized 
    
      updateFrames(affectedRegion.replace('_SearchPage_Datapanel__Body', ''));

    return true;
  }
  
  return false; 			
}

// Hong - CF16494: this method is used when the user clicks the popup search minimize button. 
// it will find the associate iframe and resize the iframe so it won't block the background. 
function updateFrames(clickingPanel)
{
	var allIFrames = document.getElementsByTagName("iframe");
  	
	 for (var iframeInx=0; iframeInx < allIFrames.length; iframeInx++)
	{

	    // We only need to deal with the iframes assoc. with the popups.
	    if (allIFrames[iframeInx].id.search(/PanelIFrame/) != -1 && allIFrames[iframeInx].id.search(clickingPanel) >=0)
	    {
	      var iframeID = allIFrames[iframeInx].id;
	      var panelID = iframeID.replace(/IFrame/, '');

	      try
	      {
	        panel = document.getElementById(panelID);
	        iframe = document.getElementById(iframeID);
		
	        if (iframe && panel)
	        {

		  if( is_ie )
		{
			//iframe.style.width = panel.offsetWidth;  // do not change the width, the result will be odd
			iframe.style.height = panel.offsetHeight;
		}
	        else
	        {
	            //needed for Opera because it draws the iframe overtop of the panel and displays
	            //a white square.
	            iframe.style.width = 0;
	            iframe.style.height = 0;
	            iframe.style.top = 0;
	            iframe.style.left = 0;
	          }
	          
	        }
	      }
	      catch(er)
	      {
	      }
	    }
	}
}

function setFieldValue(name, value)
{
  var field = document.getElementById(name);
 
  if (field != null)
    field.value = value;
}

function showFields(body)
{
  // do the cells first
  var trCollection = body.getElementsByTagName('tr');
  for (var i=0; i < trCollection.length; i++)
  {
    trCollection[i].style.display = '';
  }

  // then the rows
  var tdCollection = body.getElementsByTagName('td');
  for (var i=0; i < tdCollection.length; i++)
  {
    tdCollection[i].style.display = '';
  }
}

function hideFields(body)
{
    // CF14627: Get collection of group tables
    var groupCollection = getGroupTables(body);
    
    // Loop through each group and hide rows/fields
    for(var x=0; x<groupCollection.length; x++)
    { 
        var group = groupCollection[x];
      
        // Check the rows first
        var trCollection = group.getElementsByTagName('tr');
        for (var i=0; i < trCollection.length; i++)
        {
            var tr = trCollection[i];
            if ((' ' + tr.className + ' ').indexOf(' iC_AlwaysShow ') == -1)
                tr.style.display = 'none';
        }

        // then the cells
        var tdCollection = group.getElementsByTagName('td');
        for (var i=0; i < tdCollection.length; i++)
        {
            var td = tdCollection[i];
            if ((' ' + td.className + ' ').indexOf(' iC_AlwaysShow ') == -1)
            {
                td.style.display = 'none';
            }
        }
    }
}

// CF14627: Retrieve a collection of group DataPanel tables
function getGroupTables(elemStart)
{
    var finalCollection = new Array();
    
    if (elemStart)
    {
        var tagCollection = elemStart.getElementsByTagName('table');
        for (var i=0; i<tagCollection.length; i++)
        {
            var tag = tagCollection[i];
            
            if(tag.id)
            {
                if(tag.id.indexOf('GroupTable') != -1)
                { 
                    finalCollection[finalCollection.length] = tag;
                }
            }
        }
    }
    return finalCollection;
}

function getElementsWithClass(elemStart, tagName, className)
{
  var finalCollection = new Array();
  var tagCollection;
  
  if (elemStart)
  {
    if (tagName)
    {
      tagCollection = elemStart.getElementsByTagName(tagName);
      for (var i=0; i < tagCollection.length; i++)
      {
        var tag = tagCollection[i];
        if ((' ' + tag.className + ' ').indexOf(' '+className+' ') != -1)
        { 
          finalCollection[finalCollection.length] = tag;
        }
      }
    }
  }
  
  return finalCollection;
}