// fake print() for IE4.x
if ( !printIsNativeSupport() )
  window.print = printFrame;

// main stuff
function print_page() {
	parent.main.focus(); // Make sure the focus is on the 'main' frame
	window.print();
}

function printFrame(frame, onfinish) {

  if ( !frame ) frame = window;

  if ( printIsNativeSupport() ) {
    /* focus handling for this scope is a temporary IE5Beta workaround,
       and should be unnecessary for IE5 RTM.
    */
    var focused = document.activeElement;
    frame.focus();
    frame.self.print();
    if ( onfinish ) onfinish();
    if ( !focused.disabled ) focused.focus();
    return;
  }

  if ( frame.document.readyState !== "complete" &&
       !confirm("The document to print is not downloaded yet! Continue?") )
  {
    return;
  }

  var eventScope = printGetEventScope(frame);
  var focused = document.activeElement;

  window.printHelper = function() {
    execScript("on error resume next: printWB.ExecWB 6, 1", "VBScript");
    printFireEvent(frame, eventScope, "onafterprint");
    printWB.outerHTML = "";
    if ( onfinish ) onfinish();
    if ( !focused.disabled ) focused.focus();
    window.printHelper = null;
  }

  document.body.insertAdjacentHTML("beforeEnd",
    "<object id=\"printWB\" width=0 height=0 \
    classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\"></object>");

  printFireEvent(frame, eventScope, "onbeforeprint");
  frame.focus();
  window.printHelper = printHelper;
  setTimeout("window.printHelper()", 0);
}

// helpers
function printIsNativeSupport() {
	var agent = window.navigator.userAgent;
	var version = navigator.appVersion

	_d=document;
	ns4=(_d.layers)?true:false
	ns6=(agent.indexOf("Gecko")!=-1)?true:false
	mac=(version.indexOf("Mac")!=-1)?true:false
	mac45=(version.indexOf("MSIE 4.5")!=-1)?true:false
	if(ns6||ns4) mac=false
	opera=(agent.indexOf("Opera")!=-1)?true:false
	ns61=(parseInt(navigator.productSub)>=20010726)?true:false
	ie4=(!_d.getElementById&&_d.all)?true:false;
	ie55=((version.indexOf("MSIE 6.0")!=-1||version.indexOf("MSIE 5.5")!=-1))?true:false;
	ie50=(version.indexOf("MSIE 5.0")!=-1)?true:false;
	konq=(agent.indexOf("Konqueror")!=-1)?true:false

	return (ns4 || ns6 || ns61 || ie55 || ie50 || opera);
}

function printFireEvent(frame, obj, name) {
  var handler = obj[name];
  switch ( typeof(handler) ) {
    case "string": frame.execScript(handler); break;
    case "function": handler();
  }
}

function printGetEventScope(frame) {
  var frameset = frame.document.all.tags("FRAMESET");
  if ( frameset.length ) return frameset[0];
  return frame.document.body;
}

