/// These functions are used on signup and update pages to auto-check/disable children of a checked simpletag
function f_OnClickHierarchicalCheckbox(checkBox)
{
   //if (browser.IsIE())
      var parentCheckbox = checkBox;
   //else if (browser.IsFF())
   //   var parentCheckbox = e.currentTarget;
   var bNewStateChecked = parentCheckbox.checked;

   /// find the row the checkbox belongs to.
   if (browser.IsIE())
      var parentRow = parentCheckbox.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;
   else if (browser.IsFF())
      var parentRow = parentCheckbox.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;

   /// get its depth
   var parentRowDepth = parseInt(parentRow.getAttribute("depth"));
   
   /// for all sibling rows that have depth smaller than source row find checkbox and label and set them either checked/disabled or unchecked enabled
   var curRow = f_GetNextRowForRow(parentRow);
   while (curRow != null)
   {
      var curRowDepth = parseInt(curRow.getAttribute("depth"));
      if (isNaN(curRowDepth))
      {
        curRow = f_GetNextRowForRow(curRow);
        continue;
      }  
      if (curRowDepth <= parentRowDepth)
         break;

      var curRowCheckbox = f_GetCheckboxElementForRow(curRow);
      curRowCheckbox.checked = bNewStateChecked ? true : false;
      curRowCheckbox.disabled = bNewStateChecked ? true : false;
      
      var curRowLabel = f_GetLabelElementForRow(curRow);
      // curRowLabel.disabled = bNewStateChecked ? true : false;

      curRow = f_GetNextRowForRow(curRow);
   }

   function f_GetNextRowForRow(oRow)
   {
      if (browser.IsIE())
         return oRow.nextSibling;
      if (browser.IsFF())
      {
         var curRow = oRow.nextSibling;
         while(curRow != null && curRow.nodeName.toLowerCase() != "tr")
            curRow = curRow.nextSibling;

         return curRow;
      }
   }
   function f_GetNextSibling(oElem, sTag)
   {
      if (browser.IsIE())
         return oElem.nextSibling;
      if(browser.IsFF())
      {
         var curElem = oElem.nextSibling;
         while(curElem != null && curElem.nodeName.toLowerCase() != sTag)
            curElem = curElem.nextSibling;
         return curElem;
      }
   }
   function f_GetCheckboxElementForRow(oRow)
   {
      if (browser.IsIE())
         return oRow.firstChild.firstChild.cells[0].firstChild;
      if (browser.IsFF())
      {
         var table = f_GetFirstChildWithTag(f_GetFirstChildWithTag(oRow,"td"),"table");
         var cell = f_GetFirstChildWithTag(f_GetFirstChildWithTag(f_GetFirstChildWithTag(table, "tbody"), "tr"), "td"); 
         return f_GetFirstChildWithTag(cell,"input");
      }
   }
   function f_GetFirstChildWithTag(oElem, sTag)
   {
      if (!oElem.firstChild)
         return;
      var child = oElem.firstChild;
      while (child != null && child.nodeName.toLowerCase() != sTag.toLowerCase())
         child = child.nextSibling;
      return child;
   }
   function f_GetLabelElementForRow(oRow)
   {
      if (browser.IsIE())
         return oRow.firstChild.firstChild.cells[1].firstChild;
      if (browser.IsFF())
      {
         var table = f_GetFirstChildWithTag(f_GetFirstChildWithTag(oRow,"td"),"table");
         var cell = f_GetFirstChildWithTag(f_GetFirstChildWithTag(f_GetFirstChildWithTag(table, "tbody"), "tr"), "td"); 
         var cell2 = f_GetNextSibling(cell, "td");
         return f_GetFirstChildWithTag(cell2,"label");
      }
   }
}


function f_DelayedMarkCheckedHierarchicalCheckbox(sCheckboxId)
{
   if (browser.IsIE())
      window.attachEvent("onload", f_DelayedMarkCheckedCheckbox);
   else if (browser.IsFF())
      window.addEventListener("load", f_DelayedMarkCheckedCheckbox, false);

   function f_DelayedMarkCheckedCheckbox()
   {
      var checkbox = document.getElementById(sCheckboxId);
      if (checkbox)
         f_OnClickHierarchicalCheckbox(checkbox);
   }
}

/// these functions are used to expand/collapse item contents in xsl stylesheet
function f_ToggleElement(event, oTogglerElement)
{
  var oToggledElement = document.getElementById(oTogglerElement.getAttribute("toggleElementId"));
  var oToggledImgElement = document.getElementById(oTogglerElement.getAttribute("toggleImgId"));
  var oToggledLabelElement = document.getElementById(oTogglerElement.getAttribute("toggleLabelId"));
  
  if (oTogglerElement.getAttribute("expanded") == null)
    oTogglerElement.setAttribute("expanded", "false");

  var bIsExpanded = oTogglerElement.getAttribute("expanded");
  if (bIsExpanded == "true")
  {
    f_CollapseSection(oTogglerElement, oToggledElement, oToggledImgElement, oToggledLabelElement);
  }
  else
  {
    f_ExpandSection(oTogglerElement, oToggledElement, oToggledImgElement, oToggledLabelElement);
  }
}

function f_ExpandSection(oTogglerElement, oToggledElement, oToggledImgElement, oToggledLabelElement)
{
   if (oToggledElement.runtimeStyle)
      oToggledElement.runtimeStyle.display = "block";
   else
      oToggledElement.style.display = "block";

   if (oToggledImgElement)
   {
      oToggledImgElement.src = oToggledImgElement.getAttribute("collapseImgSrc");
   }

   if (oToggledLabelElement)
   {
      oToggledLabelElement.innerHTML = oToggledLabelElement.getAttribute("collapseText");
   }
   oTogglerElement.setAttribute("expanded", "true");
}

function f_CollapseSection(oTogglerElement, oToggledElement, oToggledImgElement, oToggledLabelElement)
{
   if (oToggledElement.runtimeStyle)
      oToggledElement.runtimeStyle.display = "none";
   else
      oToggledElement.style.display = "none";
      
   if (oToggledImgElement)
   {
      oToggledImgElement.src = oToggledImgElement.getAttribute("expandImgSrc");
   }

   if (oToggledLabelElement)
   {
      oToggledLabelElement.innerHTML = oToggledLabelElement.getAttribute("expandText");
   }
   oTogglerElement.setAttribute("expanded", "false");
}

/// these functions used for subscribing to desktop readers behind the scenes...

function f_SubscribeToDesktopReader(feedUrl, originalUrl, bDoNotNotify)
{
   var iframe = document.createElement("IFRAME");
   with (iframe)
   {
      style.display = "none";
      if (browser.IsIE())
      {
         attachEvent("onreadystatechange", f_OnReadyStateChangeSubscribeFrame);
      }
      else if (browser.IsFF())
      {
         addEventListener("load", f_OnLoadSubscribeFrame, false);
      }
      src = feedUrl;
   }

   document.body.appendChild(iframe);
   return true;

   function f_OnReadyStateChangeSubscribeFrame()
   {  
      if (iframe.readyState == "complete")
      {  
         if (!bDoNotNotify)
            alert(f_GetSubscribedToDesktopReaderMessage());
         bDoNotNotify = true;
         setTimeout(f_do, 10);
      }
   }

   function f_OnLoadSubscribeFrame()
   {
      var ifrm = iframe;
      if (!bDoNotNotify)
         alert(f_GetSubscribedToDesktopReaderMessage());
      setTimeout(f_do, 10);
   }

   function f_do()
   {
      document.location = originalUrl;
   }
}

function f_GetSubscribedToDesktopReaderMessage()
{
   return "You should now be subscribed to the feed.\nIf you are not subscribed, please make sure your RSS reader is installed and running, then click subscribe again.";
}
