function SavedDocument(defaultPage, originalHTML, productCSS, filename){
this.commonCSS  ="";
this.productCSS =productCSS;
this.defaultPage=defaultPage;
this.HTMLDocType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">";
this.reportHTML ="";
this.windowProperties  = "top=0, left=200, width=400, height=300, addressbar=yes, menubar=yes, scrollbars=yes, resizable=yes";
this.originalHTML =originalHTML;
this.defaultFilename =filename;
SavedDocument.prototype.openSaveableReport=_openSaveableReport;
SavedDocument.prototype.setProductCSS =_setProductCSS;
SavedDocument.prototype.setWindowProperties=_setWindowProperties;
SavedDocument.prototype.customiseReport =_customiseReport;
SavedDocument.prototype.getReportHTML =_getReportHTML;
SavedDocument.prototype.removeHyperlinks=_removeHyperlinks;
this.reportHTML=__buildSaveableReport(this.HTMLDocType,this.originalHTML,this.commonCSS,this.productCSS);
function _getReportHTML(){
return this.reportHTML;
}
function _customiseReport(){
return this.reportHTML;
}
function _openSaveableReport(){
var saveButtonHTML=__buildSaveButton(this.defaultFilename);
var saveWindow = window.open( this.defaultPage, "saveWindow", this.windowProperties);
saveWindow.document.write( this.reportHTML );
saveWindow.document.body.insertAdjacentHTML( "AfterBegin", saveButtonHTML );
saveWindow.document.close();
}
function _setWindowProperties(newProperties){
this.windowProperties=newProperties;
}
function _setProductCSS(style){
this.productCSS=style;
}
function _removeHyperlinks(){
this.reportHTML = this.reportHTML.replace( /<a/gi, "<u" );
this.reportHTML = this.reportHTML.replace( /<\/a/gi, "<\/u" );
}
function __buildSaveableReport(HTMLDocType,originalHTML,commonCSS,productCSS){
var headpos;
var reportHTML="";
reportHTML=HTMLDocType+originalHTML;
reportHTML=__removeJavaScript(reportHTML);
headpos = reportHTML.indexOf( "</HEAD>" );
if(headpos == -1 ){ headpos = reportHTML.indexOf( "</head>" );}
if(headpos!=-1){
reportHTML=reportHTML.substring( 0, headpos )+commonCSS+productCSS+reportHTML.substring( headpos );
}
return reportHTML;
}
function __removeJavaScript(reportHTML){
reportHTML=reportHTML.replace( /\t/g, "" );
reportHTML = reportHTML.replace( / {2,}/g, " " );
reportHTML=reportHTML.replace( /<SCRIPT.*\/SCRIPT>/gi, "" );
reportHTML = reportHTML.replace( /<SCRIPT/gi, "<!-- <SCRIPT" );
reportHTML = reportHTML.replace( /\/SCRIPT>/gi, "/SCRIPT> -->" );
reportHTML=reportHTML.replace( /onload=.*\)(;)*/gi, "" );
return reportHTML;
}
function __buildSaveButton(defaultFilename){
if(!defaultFilename){
defaultFilename=document.getElementsByTagName( 'title' )[ 0 ].text;
}
defaultFilename=defaultFilename.replace( /[^A-Za-z0-9_ #&+,;=\^\[\]{}()~`!@$%'-]/g, "" );
defaultFilename='\''+defaultFilename+'\'';
var saveButtonHTML = '<p align="center"><input type="button" onclick="document.execCommand(\'SaveAs\',null,' + defaultFilename + ');window.close();" class="savebutton" value="Save Report"></p>';
return saveButtonHTML;
}
}


