
var printablecontent = null;

/**
	Kald printpage fra originalside hvor indhold der skal printes kopieres til popupside.
	"niveau_op" er antal table tags som der skal søges op i DOMtræet fra noden
	med id="printmarker". Den fundne tabel og dens indhold kopieres til printpopup.
	portal_path_alias: portal path alias til printside som skal åbnes i popup.
*/

function printpage2(niveau_op, boolean_en_td_op, portal_path_alias) {

  var popup_width = 800;
  var popup_height = 500;

  var printmarker = document.getElementById("printmarker");

  if (!printmarker) {
    alert("Markering af printområde mangler");
  }

  var printtag = printmarker;

  for (i=0; i<niveau_op; i++) {

    while (printtag && printtag.nodeName.toLowerCase() != "table") {
      printtag = printtag.parentNode;      
      if (!printtag) alert("Ingen printbar table tag fundet " + niveau_op + " niveauer op."); 
    }

    if (i<niveau_op-1) {
      printtag = printtag.parentNode;
      if (!printtag) alert("Ingen printbar table tag fundet " + niveau_op + " niveauer op.");      
    }    
  }

  if (boolean_en_td_op) {
  
    while (printtag && printtag.nodeName.toLowerCase() != "td") {
      printtag = printtag.parentNode;
    }
  
    if (!printtag) alert("Ingen printbar TD tag fundet over " + niveau_op + " table niveauer op."); 
  }

  printablecontent = printtag.cloneNode(true);

  hideNonPrintableElements(printablecontent);

  window.open(portal_path_alias,'Printside','width='+popup_width+',height='+popup_height+',top='+
    ((parseInt(screen.height)-parseInt(popup_height))/2)+
    ',left='+((parseInt(screen.width)-parseInt(popup_width))/2)+
    ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes')
}

/**
 Hides elements which should not be included on our print-page
*/
function hideNonPrintableElements(elementColl) 
{

	if (includeInPrint(elementColl) == true) {
		
		for (var i=0;i<elementColl.childNodes.length;i++) {
		      
			// Check for the existence of child-elements and iterate through them as well
			// but only if we are not hiding the parent element
			if (includeInPrint(elementColl.childNodes[i]) == true)	{
				
				if (elementColl.childNodes[i].childNodes.length > 0) {
					hideNonPrintableElements(elementColl.childNodes[i]);
				}
				
			}
		      
		}
		
	}
	
}


/**
  Checks if an element should NOT be printed and returns true if this is the case
*/
function includeInPrint(elementToCheck) {

	if (elementToCheck.id != null && elementToCheck.id.toLowerCase().indexOf('dontprint') >= 0) {
	  // alert(elementToCheck.tagName + ' ' + elementToCheck.id + ' - dont');

	  try {
	    elementToCheck.style.display = 'none';
	  }
	  catch (ex) {}

	  return false;
	}
	else {
	  return true;
	}
	
}


/**
	Kald fra popup som skal have kopi af printbart indhold fra originalside.
	Indholdet indsættes i popupside i noden med id="destinationNode".
*/ 
function insertContent(destinationNode) {

  //document.getElementById('dest').innerHTML = window.opener.printablecontent;
  var destElement = document.getElementById(destinationNode);
  var sourceElement = window.opener.printablecontent;

  if (document.all) {
    destElement.innerHTML = sourceElement.outerHTML;
  }
  else {
    destElement.appendChild(sourceElement);      
  }

}