// See the tags/HTML structure in: notam_print_structure.odt

// set the checkboxes once a checkbox is selected
// ex: if a station is selected (like CYYY), check all the bulletins underlying
function notam_set_print_options(e)
   {
   // check/uncheck all the checkboxes of the section 
   currentSection = GetParentNodeWithClassName(e, "notam_print_section" );
   var el = currentSection.getElementsByTagName("div");
   var i=0;
   for (i=0; i<el.length; i++)
      {
      if (el[i].className == "notam_print_section")
         {
         if ( e.checked == true )
            {
            set_print_section_checkbox_value(el[i],true);
            }
         else
            {
            set_print_section_checkbox_value(el[i],false);
            }         
         }      
      }

   // verify if we have to check/uncheck the parent of the section:
   if ( e.checked == false ) // one element unchecked
      {
      // verify if at least one element is still checked among ALL the checkboxes,
	  // so the print button stays activated
      var list_print_checkbox = document.getElementsByName("notam_bulletin_checkbox");
      var checkbox_checked = false;
      var i=0;
      for (i=0; i<list_print_checkbox.length; i++)
         {
         if ( list_print_checkbox[i].checked == true )
            {     
            checkbox_checked = true;
            break;
            }
         }
      if ( checkbox_checked == false )
         {
         // no element checked: disable the print button
         notam_set_print_button_disabled_attribute(true);
         }

      // uncheck all parents (because one element has been unchecked)
      var parentSection = GetParentNodeWithClassName(currentSection, "notam_print_section" );
      while( parentSection != null )
         {
         set_print_section_checkbox_value(parentSection,false);
         parentSection = GetParentNodeWithClassName(parentSection, "notam_print_section" );
         }
      }
   else // one element checked
      {
      // activate the print button
      notam_set_print_button_disabled_attribute(false);

      // get the parent, verify if HIS items are ALL checked      
      var parentSection = GetParentNodeWithClassName(currentSection, "notam_print_section" );
      while( parentSection != null )
         {           
         if ( all_item_in_section_checked(parentSection) == false ) 
           {
            // no longer need to continue
            break;
            }
         else
            {
            // check the parent
            set_print_section_checkbox_value(parentSection,true);
            
            // get the parent of the parent
            parentSection = GetParentNodeWithClassName(parentSection, "notam_print_section" );
            } 
         }    
      }
   }


// Enable or disable the print buttons
function notam_set_print_button_disabled_attribute(value)
   {
   var list_print_button = document.getElementsByName("notam_print_button");

   var i=0;
   for (i=0; i<list_print_button.length; i++)
      {
      list_print_button[i].disabled = value;
      }   
   }


// Verify if all the items in the section are checked
// Return value: {true|false}
function all_item_in_section_checked(sectionNode)
   {
   el = sectionNode.childNodes;
   
   var i=0;
   for (i=0; i<el.length; i++)
      {
      if (el[i].className == "notam_print_section")
         {
         if( get_print_section_checkbox_value(el[i]) == false)
            {
            return false;
            }         
         }      
      }
   // if we made it to here, all item are checked
   return true;
   }


// Return true if the checkbox is checked; false if not
function get_print_section_checkbox_value(printSectionNode)
   {
   var checkbox = printSectionNode.getElementsByTagName("input")[0];
   return checkbox.checked;
   }


// Set a checkbox to a value, true or false
function set_print_section_checkbox_value(printSectionNode, value)
   {
   var checkbox = printSectionNode.getElementsByTagName("input")[0];
   checkbox.checked = value;
   }


// Discard every node that are not of the class: notam_print_section
function filter_print_section(nodeList)
   {
   var resultList = new Array();

   var i=0;
   for (i=0; i<nodeList.length; i++)
      {
      if( nodeList[i].className == "notam_print_section")
	 {
         resultList.push(nodeList[i]);
	 }
      }
      return resultList;
   }


// Set the flag for printing
function set_print_section_print_flag(printSectionNode, value)
   {
   var printItem = printSectionNode.getElementsByTagName("span")[0]; 
   var print_BR_list = printSectionNode.getElementsByTagName("BR"); 

   if( value == true)
      {      
      printItem.setAttribute("className","print");
      printItem.setAttribute("class","print");
      var i=0;
      for (i=0; i<print_BR_list.length; i++)
         {
         if (print_BR_list[i].id == "br_notam_station")
            {
            print_BR_list[i].setAttribute("className","print");
            print_BR_list[i].setAttribute("class","print");
            }
         }
      }
   else
      {
      printItem.setAttribute("className","noprint");
      printItem.setAttribute("class","noprint");
      var i=0;
      for (i=0; i<print_BR_list.length; i++)
         {
         if (print_BR_list[i].id == "br_notam_station")
            {
            print_BR_list[i].setAttribute("className","noprint");
            print_BR_list[i].setAttribute("class","noprint");
            }
         }
      }  
   }


function notam_validate_print_options() 
   {
   notam_print();
   }


function prepare_print_section_for_printing(print_all_section)
   {
   var list_div = print_all_section.getElementsByTagName("div");
   var print_section_list = filter_print_section(list_div);
   
   // init all section to: noprint
   var i=0;
   for (i=0; i<print_section_list.length; i++)
      {
      set_print_section_print_flag(print_section_list[i], false);
      }
   
   // if a notam_bulletin is checked: set his print flag as well as the one of the parents
   for (i=0; i<print_section_list.length; i++)
      {
      if( print_section_list[i].id == "notam_bulletin" )
         {
         if( get_print_section_checkbox_value(print_section_list[i]) == true )
            {
            set_print_section_print_flag(print_section_list[i], true);
            
            var parentSection = GetParentNodeWithClassName(print_section_list[i], "notam_print_section" );            
            
            while( parentSection != null )
               {
               set_print_section_print_flag(parentSection, true);
               parentSection = GetParentNodeWithClassName(parentSection, "notam_print_section" );
               }
            }
         }        
      }   
   }


function notam_print()
   {
   // get the principal <div> section
   var print_all_section = document.getElementById("notam_all_whole_section");
   prepare_print_section_for_printing(print_all_section);

   // never print that section
   set_print_section_print_flag(print_all_section, false);

   window.print();
   }


// This travels up from the given node until it finds a parent
// who has the given class name.
// http://www.bennadel.com/snippets/9-GetParentNodeWithClassName-objNode-strClassName-.htm
function GetParentNodeWithClassName( objNode, strClassName ){
   // Lowercase the class name for comparison.
   strClassName = strClassName.toLowerCase();
        
   // Crawl up the parent node chain. Keep crawling until we find the 
   // node with the proper class name, we hit a null node, or we hit 
   // a non-text node that has no tag name (the document object).
   for ( 
      objNode = objNode.parentNode ; 
      (
         objNode && (                   
            (objNode.tagName && (objNode.className.toLowerCase() != strClassName)) ||
            (!objNode.tagName && (objNode.nodeType != 3)) 
            )
         );
      objNode = objNode.parentNode
      )
      {
      // Nothing has to be done within in the FOR loop. We are purely
      // using the FOR loop to crawl up the DOM structure.
      }
   
   // Return the node. At this point, it might contains a valid
   // parent node, or it might be null.
   return( objNode );
   }




