﻿// Printer Friendly Logic
// Written 08/02/2008 by @Work

function LoadPrintDoc(Target)
{
    var win = window.opener ? window.opener : window.dialogArguments, c;
  
   // if (!win)
   // {
    //    win = findWin();
   // }
  
   if (win)
   {
        PrintCtl = win.PrintCtl;
        if (PrintCtl.printObj)
        {
        
            //filter out href's and onclick's because this is a print window only and we do not want 
            //people navigating away inside it.
            SourceHTML = win.PrintCtl.printObj.innerHTML;
            
            var instr = '.';
            
            //We need to do this in two steps. Because we will be replaceing the string "onclick" with the string "onclick"
            //this causes a loop, so we replace with a placeholder then replace the placeholder.
            while (instr)
            {
                SourceHTML = SourceHTML.replace(/onclick=/,"##onclick##=");
                instr = SourceHTML.match('onclick=');
            }
            instr = '.';
            while (instr)
            {
                SourceHTML = SourceHTML.replace(/##onclick##=/,"onclick='return false;' oc-replaced=");
                instr = SourceHTML.match('##onclick##');
            }            
            /////////////////
            instr = '.';
            while (instr)
            {
                SourceHTML = SourceHTML.replace(/href=/,"##href##=");
                instr = SourceHTML.match('href=');
            } 
            instr = '.';
            while (instr)
            {
                SourceHTML = SourceHTML.replace(/##href##=/,"href='javascript:void(0)' hr-replaced=");
                instr = SourceHTML.match('##href##');
            }                        
            ////////////////
            
            TarObj = document.getElementById(Target);
            TarObj.innerHTML = SourceHTML;
            
            window.print();
        }
   }
}

//function findWin()
//{
//    var c;
//    // Check parents
//    c = window;
//    while (c && (c = c.parent) != null) {
//        if (typeof(c.PrintCtl) != "undefined")
//            return = c;
//    }

//    // Check openers
//    c = w;
//    while (c && (c = c.opener) != null) {
//        if (typeof(c.PrintCtl) != "undefined")
//            return = c;
//    }

//    // Try top
//    if (typeof(top.PrintCtl) != "undefined")
//        return = top;        
//}

function PrintFriendly(TargetPage, SourceDiv, myWidth, myHeight)
{
    PrintCtl = new Print_Ctl();
    PrintCtl.printObj = document.getElementById(SourceDiv);
    window.open(TargetPage,'','scrollbars=yes,resizable=yes,width=' + myWidth + ',height=' + myHeight + ',left='+((screen.availWidth/2)-(myWidth/2))+',top='+((screen.availHeight/2)-(myHeight/2))+'');
    return false;
}

var PrintCtl;
function Print_Ctl(){
};