/*
$Id: Printable.js,v 1.1 2006/01/26 14:47:12 baileyjs Exp $
Author: Jeremy Bailey
Date: 11/09/2005
Company: SourceOne Network, Inc
Description: 
Prints the content of the page opener using Javascript. 
This prevents the query string and FORM posting problem of the the past.

The basic idea is that we grab all the HTML from our parent, then look for the <crawl start>/<crawl stop> tags. 
Everything between them is GOOD content, everything outside is header/footer.  We do some string magic and write to 
the contents of our own div, and voila, we have just the content! 
*/

function AlariusPrintable() {
	var PrintWindow;
	AlariusPrintableWin = window.open("","Printable","width=600,height=400,left=200,scrollbars,resizable");
	PrintWindow=AlariusPrintableWin.document.open();
	PrintWindow.writeln('<html>\n<head>\n<title>' + document.title + ' </title>');
	for (var i=0; i <= document.styleSheets.length-1; i++) {
		PrintWindow.writeln('<link href='+ document.styleSheets[i].href + ' rel="styleSheet" type="text/css">');
	}
	PrintWindow.writeln('</head>\n<body>');
	PrintWindow.writeln('<style>');
	// Styles for printer page - START
	PrintWindow.writeln('.AlariusPrintableToolbar {');
	PrintWindow.writeln('text-align: right;');
	PrintWindow.writeln('}');
	PrintWindow.writeln('.AlariusPrintableContent {');
	PrintWindow.writeln('}');
	// Styles for printer page - END
	PrintWindow.writeln('</style>\n');
	PrintWindow.writeln('<div id="AlariusPrintableToolbar" class="AlariusPrintableToolbar">');
	PrintWindow.writeln('<a href="javascript:void(0);" OnClick="window.opener.AlariusPrintablePrint();">Print This Page</a>&nbsp;&nbsp;');
	PrintWindow.writeln('</div>');
	PrintWindow.writeln('<div id="AlariusPrintableContent" class="AlariusPrintableContent"> ' + AlariusPrintableCopyContent() + '</div>');
	PrintWindow.writeln('</body>\n</html>\n');
	PrintWindow=AlariusPrintableWin.document.close();
	// We had a problem with the disable link script running too soon, before the links populated
	// So, we "delay" it by shoving it through setTimeout();
	window.setTimeout('AlariusPrintableDisableActions()', 0);
	AlariusPrintableWin.focus();
}

function AlariusPrintableCopyContent() {
	var Content,startpos,endpos,offset,starttag,endtag;
	// IE and Firefox treat the content differently, so we need different tags and offsets
	if(document.all) {
		starttag = '<CRAWL start';
		endtag = '<CRAWL stop';
		offset = 13;
	} else {
		starttag = '<crawl start';
		endtag = '<crawl stop';
		offset = 16;
	}
	// Grab the parent's content and find our start/end tags
	Content = document.body.innerHTML;
	startpos = Content.indexOf(starttag);
	endpos = Content.indexOf(endtag);
	if(startpos < 0 || endpos < 0) {
		// return a nice error if something went wrong
		return "Error capturing text";
	} else {
		// reset the content
		return Content.substr(startpos+offset,endpos-startpos-offset);
		// Disable any links (they shouldn't be followed within the popup)
	}
}
function AlariusPrintablePrint() {
	AlariusPrintableWin.print();
	AlariusPrintableWin.close();
}
function AlariusPrintableDisableActions() {
	// Loop through the links, disable them
	for (i = 0; i < AlariusPrintableWin.document.links.length; i++) {
		if(AlariusPrintableWin.document.links[i].parentNode.id != 'AlariusPrintableToolbar') {
			// Only disable other links
			AlariusPrintableWin.document.links[i].onclick = function() { return false };
			AlariusPrintableWin.document.links[i].href = "javascript:void(0)";
		}
	}
	// Loop through the forms, disable the buttons (we don't disable the whole form, since it looks bad)
	for (var i = 0; i < AlariusPrintableWin.document.forms.length; i++) {
		for (var j = 0; j < AlariusPrintableWin.document.forms[i].elements.length; j++) {
			if(AlariusPrintableWin.document.forms[i].elements[j].type == 'submit' || AlariusPrintableWin.document.forms[i].elements[j].type == 'button') {
				// submit buttons and regular buttons are cadidates
				// Note: I tried altering the action or onSubmit() but that didn't seem to work
				AlariusPrintableWin.document.forms[i].elements[j].disabled = true;
			}
		}
	}
} 
