//PrintPage.js  Open's the current web page's Print Window
//PrintPreview("ApplicationTag")
//looks for the surrounding <!-- @BeginApplicationTagPrintable -->
// and <!-- @EndApplicationTagPrintable -->

function GetBase()
{
    var oBaseColl = document.all.tags('BASE');
    return ( (oBaseColl && oBaseColl.length) ? oBaseColl[0].href : 
        null );
}


function trim(str)
{
// Trims trailing and leading spaces.
	while ( str.charAt(0) == " " )
		str = str.replace(" ", "");
	i = str.length -1;	
	while (str.charAt(i) == " " )
		i = i - 1;
	str = str.substring(0, i+1);
	return str;
}

function PrintPreview(applicationtag, stylepass)
{
	styles = "";
	style = stylepass;
	
	base = "";
	if (GetBase() != null)
	{
		base = "<base href=" + GetBase() + ">"		
	}
	
	while ( (style.indexOf(",") != -1 )	)
	{
		styles += "<link rel=stylesheet type='text/css' href='";
		styles += trim(style.substring(0, style.indexOf(",")));
		style = style.substring((style.indexOf(",")+1), style.length);
		styles += "'>";
	}
	styles += "<link rel=stylesheet type='text/css' href='";
	styles += trim(style);
	styles += "'>";
	
	begintag = "<!-- @Begin" + applicationtag + "Printable -->";
	endtag = "<!-- @End" + applicationtag + "Printable -->";
	HTML = document.body.innerHTML;
	regExp = "";// + "(" + begintag + ")(*?)(" + endtag ")";
	finaldoc = "";
		
	while  ( (HTML.indexOf(begintag) != -1)   )
	{
		locOfBegin = HTML.indexOf(begintag);
		locOfEnd = HTML.indexOf(endtag);	

		while ( (locOfEnd < locOfBegin) && locOfEnd != -1)
		{
			HTML = HTML.replace(endtag, "");
			locOfEnd = HTML.indexOf(endtag);
		}
		
		if (locOfEnd == -1)
		{
			finaldoc += HTML.substring(locOfBegin, HTML.length);			
		} else {
			finaldoc += HTML.substring(locOfBegin, locOfEnd);
		}
		HTML = HTML.replace(endtag, "");
		HTML = HTML.replace(begintag, "");
	}


	if (!newWin)
	{
	 	newWin = window.open("about:blank", "_blank", "toolbar=yes,location=no,directories=no,status=no,titlebar=no,menubar=no,scrollbars=yes,resizable=no,width=700,height=400,top=0,left=0");
		if (!newWin.opener){
			newWin.opener = window;
		}
		newWin.document.open ();

		 var to_page = "<HTML><HEAD><title>"+ this.document.title +"</title>"+ base + styles + "</head><BODY BGCOLOR='white' onunload='opener.newWin=null;'><font size='2' color='#000000'><P><B>" + finaldoc + "<br><BR><center><input type='button' value='Close Preview' onclick='opener.newWin=null;window.close()'></center></FONT></B></BODY></HTML>\n";
  		newWin.document.write(to_page);
   		newWin.document.close();
	} else
		newWin.focus();
}
var newWin;