//ScriptX Specific Functions
var printData;

// object to handle all the print capabilities
function printControl()
{
	var layout = "portrait";
	var pageFooter = "&d&b&bPage &p of &P";
	
	// note that the original code used mm, but the current version of the ScriptX control does not support this
	var leftMargin = "10";
	var rightMargin = "10";
	var bottomMargin = "7";
	var topMargin = "10";
	
	var copies = 1;
	
	this.Layout = layout;
	this.PageHeader;
	this.PageFooter = pageFooter;
	this.LeftMargin = leftMargin;
	this.RightMargin = rightMargin;
	this.BottomMargin = bottomMargin;
	this.TopMargin = topMargin;
	this.Copies = copies;
	
	this.Print = doPrint;
	this.Preview = doPreview;
	this.PrintReady = onPrintReady;
	
	// do a print preview
	function doPreview(pageUrl) 
	{
		this.Url = pageUrl;
		var sDialogFeatures = "dialogHeight:500px;dialogWidth:700px;status:no;help:no;center:yes;resizable:yes;scroll:yes;"
		showModalDialog("../Pages/PrintPreview.htm", this, sDialogFeatures);
	}
	
	// do a print to the printer
	function doPrint(pageUrl) 
	{
		if (document.getElementById("divPrint") == null)
		{
			document.body.insertAdjacentHTML("beforeEnd", "<div id=\"divPrint\"><iframe id=\"iframePrint\" name=\"iframePrint\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" height=\"0\" width=\"0\"></iframe></div>\n");
		}
		
		// load the print object
		var doc = document.getElementById("iframePrint").document;
		doc.open();
		doc.write("<object id=\"factory\" viewastext  style=\"display:none\" ");
		doc.write("classid=\"clsid:1663ed61-23eb-11d2-b92f-008048fdd814\" ");
		doc.write("codebase=\"../Includes/ScriptX.cab#Version=6,1,428,11\">");
		doc.write("</object>\n");
		doc.write("<body onload=\"top.frameContent.printData.PrintReady(factory, iframePrintContent)\">\n");
		doc.write("<iframe id=\"iframePrintContent\" name=\"iframePrintContent\" src=\"" + pageUrl + "\" marginheight=\"0\" marginwidth=\"0\" width=\"100%\" height=\"100%\"></iframe>");
		doc.write("</body>");
		doc.close();
	}

	// called by the print frame once the page has completed loading
	function onPrintReady(printFactory, printFrame) 
	{
		var returnCode;
		var msgNoActiveX = "The print ActiveX control could not be found.";
		var msgDocumentPrinting = "A document is currently being printed.\nPlease wait, then try again.";

		var ePrintException;
		
		// check whether the activex control exists
		try 
		{
		    if (!printFactory.object) 
			{ 
				//Works for "Do not allow ActiveX controls to run
				alert(msgNoActiveX);
				return false;
			}
		}
		catch (ePrintException) 
		{ 
			//Works for "Do not script ActiveX controls
			alert(msgNoActiveX);
			return false;
		}
		
		// set up page layout
		printFactory.printing.portrait = (this.Layout == "portrait");
		printFactory.printing.header = this.PageHeader;
		printFactory.printing.footer = this.PageFooter;
		printFactory.printing.leftMargin = this.LeftMargin;
		printFactory.printing.rightMargin = this.RightMargin;
		printFactory.printing.bottomMargin = this.BottomMargin;
		printFactory.printing.topMargin = this.TopMargin;
		
		showPleaseWait(false);

		try
	    {
	        if (this.Copies > 1) 
			{
			    for (var i = 0; i < this.Copies; i++) 
				{
					// don't prompt when printing multiples
					returnCode = printFactory.printing.Print(false, printFrame);
				}
			}
			else 
			{
				returnCode = printFactory.printing.Print(true, printFrame);
			}
		}
		catch (ePrintException) 
		{
			alert(msgDocumentPrinting);
			return false;
		}

		// clear out the print frame to avoid potential errors with multiple ScriptX objects
		try 
		{
			top.frameContent.divPrint.outerHTML = "";
		}
		catch (ex) {}
		
		return returnCode;
	}
}


