//main function to be called opens new window and outputs content sections as appropriate
function printerFriendlyWindow(contentsections,headerstring,outputlinkhref){
var contentarray;
var outputstring='';
var loopcount;
var loopcount2;
var printerfriendlywindow;
var errormessage;
var contentpointer;

//open window to output printerfriendly content
printerfriendlywindow=window.open('','printerfriendlywindow');
if(contentsections==''||contentsections==null){//if no contentsections were passed error outfunction
errormessage='<h1>No content sections passed to Printer Friendly JS Function</h1>';
printerfriendlywindow.document.write(errormessage);
return 0;}
if(headerstring==null){
headerstring='';
}
if(outputlinkhref==null){
outputlinkhref='N';
}
else{
outputlinkhref=outputlinkhref.toUpperCase();
}
contentarray=contentsections.split(",");
for(loopcount=0;loopcount<contentarray.length;loopcount++){
 contentpointer=document.getElementById(contentarray[loopcount]);
 if(contentpointer==null){
  contentpointer=document.getElementsByTagName(contentarray[loopcount]);
  if(contentpointer==null||contentpointer.length==0){
   errormessage='<h1>&quot;'+contentarray[loopcount]+'&quot; is not an element ID or an a collection of HTML tags in this document</h1>';
   printerfriendlywindow.document.write(errormessage);
   return 0;
  }
  else{//document has collection of tags
   for(loopcount2=0;loopcount2<contentpointer.length;loopcount2++){
    outputstring=outputstring+contentsectiontooutput(contentpointer[loopcount2],outputlinkhref);
   }
  }
 }
 else{
  //get output from content and add to output string
  outputstring=outputstring+contentsectiontooutput(contentpointer,outputlinkhref);
 }
}
printerfriendlywindow.document.write(html_head());
printerfriendlywindow.document.write(html_body_tag());
printerfriendlywindow.document.write(headerstring);
printerfriendlywindow.document.write(outputstring);
printerfriendlywindow.document.write(end_html_doc());

return 1;
}

function html_head(){
var title;
var printstring;
title=document.getElementsByTagName('TITLE');
printstring="<html><head><title>";
printstring=printstring+title[0].text;
printstring=printstring+"</title></head>";
return printstring;
}

function html_body_tag(){
return '<body>';
}


function end_html_doc(){
return '</body></html>';
}

function contentsectiontooutput(nodepointer,outputlinkhref){
var printstring=""; //return to have output
var loopcount; 
 if(nodepointer.hasChildNodes){ //if node has children loop go down to the children
  for(loopcount=0;loopcount<nodepointer.childNodes.length;loopcount++){
   printstring=printstring+printchildnodes(nodepointer.childNodes[loopcount],outputlinkhref)
  }
 }
 return printstring;
}

function printchildnodes(nodepointer,outputlinkhref){
var printstring=""; //return to have output
var loopcount; 
 printstring=printstring+printnodevalues(nodepointer); //output parent node first
 if(nodepointer.hasChildNodes){ //if node has children loop go down to the children
  for(loopcount=0;loopcount<nodepointer.childNodes.length;loopcount++){
   printstring=printstring+printchildnodes(nodepointer.childNodes[loopcount],outputlinkhref)
  }
 }
 printstring=printstring+printendtag(nodepointer,outputlinkhref);
 return printstring;
}

//function prints out HTML tags w/ attributes or text node values 
//1/11/2005 kfly- Due to errors being caused by the output of the contentEditable attribute in HTML, a check has been established and it will no longer be an attribute that will be output.
function printnodevalues(nodepointer){//output tag or content out to browser
 var printstring='';
 var attributeloop;
 var attributesarray;
 switch(nodepointer.nodeType){
 case 1: //is an element node
 printstring=printstring+'<'+nodepointer.nodeName;
 attributesarray=nodepointer.attributes;
 if(nodepointer.attributes!=null){
  for(attributeloop=0;attributeloop<attributesarray.length;attributeloop++){
   if(attributesarray[attributeloop].nodeValue!=null&&attributesarray[attributeloop].nodeValue!=''&&attributesarray[attributeloop].nodeName!='contentEditable'){
    printstring=printstring+' '+attributesarray[attributeloop].nodeName+'=\"'+attributesarray[attributeloop].nodeValue+'\"';
   }
  }
 }
 printstring=printstring+'>';
 break;
 case 3: //is an text node
 printstring=printstring+nodepointer.nodeValue;
 break;
 default: 
 }
 return printstring;
}

function printendtag(nodepointer,outputlinkhref){//ouput ending tag if needed
var printstring='';
var tagsneedingending=new Array('A','ABBR','ACRONYM','ADDRESS','APPLET','B','BDO','BIG','BLOCKQUOTE','BODY','BUTTON','CAPTION','CENTER','CITE','CODE','COLGROUP','DD','DEL','DFN','DIV','DL','DT','EM','FILEDSET','FONT','FORM','FRAMESET','H1','H2','H3','H4','H5','H6','HEAD','HTML','I','IFRAME','INS','KBD','LABEL','LEGEND','LI','MAP','MENU','NOFRAMES','NOSCRIPT','OBJECT','OL','OPTGROUP','OPTION','P','PRE','Q','S','SAMP','SCRIPT','SELECT','SMALL','SPAN','STRIKE','STRONG','STYLE','SUB','SUP','TABLE','TBODY','TD','TEXTAREA','TFOOT','THEAD','TITLE','TR','TT','U','UL','VAR');
var loopcount;
 switch(nodepointer.nodeType){
 case 1: //is an element node
 for(loopcount=0;loopcount<tagsneedingending.length;loopcount++){
  //printstring=printstring+nodepointer.nodeName;
  //printstring=printstring+tagsneedingending[loopcount].value+'\n'
  if(tagsneedingending[loopcount]==nodepointer.nodeName){
   if(nodepointer.nodeName=='A'&&outputlinkhref=='Y'){
   printstring=printstring+' <span style=\"font-size: 8pt\;\">('+nodepointer.href+')</span>';
   }
   printstring=printstring+'</'+nodepointer.nodeName+'>';
   break;
  }  
 }
 break;
 case 3: //is an text node
 break;
 default: 
 }
 return printstring;
}

