//-----------------------------------------------------------------------------
// previewHTML.js :: used by /kbase/add_document.jsp
// 2004.11.23 - written by rk7138 for SBC Internet Services
//-----------------------------------------------------------------------------
// getBoxHTML (title, content, doc)
//-----------------------------------------------------------------------------
// -- creates an HTML preview of a document object
// -- assumes that title, content and doc are populated.
//
//    title:String
//    content:String [ allows HTML.  fails with large strings ]
//    doc:filepath/filename.ext
//
// -- can be called via showPopUp(formId),showPopUp_parm(title, content, doc) and showPopUp_JSP(contentID)
//    showPopUp(formId):  passes in a formId with title, content and doc elements
//    showPopUp_parm(title, content, doc):  bypass formId
//    showPopUp_JSP(contentID):  jsp will take contentID, query database and present preview HTML
//-----------------------------------------------------------------------------
var prevent_extensions = false;
var allowed_extensions = [ 
	".pdf", ".xls", ".csv", ".doc", ".ppt", ".prj", ".vso", ".html", ".js", ".css", ".dat", ".bat", ".exe", ".bin",
	".txt", ".xml", ".cgi", ".class", ".htm", ".jpg", ".bmp", ".gif", ".swf"
];

var width = 500;
var height = 400;
var screen_width = screen.width;
var screen_height = screen.height;
var top = (screen_height - height)/2;
var left = (screen_width - width)/2;

var content_maxlength = 4000;

var titleName = 'title';
var contentName = 'content';
var documentName = 'document';

var docRoot = '/NDWS';
var imgRoot = docRoot + '/img/buttons';
var closemeGIF = imgRoot + '/close.gif';
var documentGIF = imgRoot + '/document.gif';

var kbquery = docRoot + '/secure/user/kb/document?id=';
var targetFile = docRoot + '/admin/content_mgr/preview.html';
var targetFileJSP = docRoot + '/admin/content_mgr/previewJSP.html?id=';
var targetName = 'child';

var ERROR_DISALLOWED_EXTENSIONS = "ERROR ::\nOnly these file extensions are allowed:\n";
var ERROR_MISSING_VALUES = "ERROR ::\nPlease provide values for all fields.";
var ERROR_TOO_LONG = "ERROR ::\nThe content body exceeds the maximum allowed length of " + content_maxlength + " characters.\nPlease shorten the amount of text in the content body and try again.";
var month_abbr = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
	
function $isAllowedExtension(givenDocName){
	var status = false;
	
	if (prevent_extensions == true){		
		for (var i = 0; i < allowed_extensions.length; i++){
			if (givenDocName.indexOf(allowed_extensions[i]) != -1){ status = true; break; }	
		}
	}
	else {
		status = true;
	}
	
	return status;
}

function showPopUp(formId){
	var formObj = document.getElementById(formId);
	var title = formObj[titleName].value;
	var content = formObj[contentName].value;
	var doc = formObj[documentName].value;

	$showPopUp(title, content, doc);
}

function showPopUp_parm(title, content, doc){
	$showPopUp(title, content, doc);	
}

function showPopUp_JSP(contentID){
	var target = targetFileJSP + contentID;
	
	childObj = window.open(target, targetName, 'height=' + height + ',width=' + width + ',top=' + top + ',left=' + left + ',toolbar=0,scrollbars=yes,resizable=yes');
	childObj.focus();
}

function $showPopUp(title, content, doc){
	if (content.length > 4000){ alert(ERROR_TOO_LONG); return false; }
	if (content.length > 0 && title.length > 0 && doc.length > 0){
		if ($isAllowedExtension(doc)){
			childObj = window.open(targetFile, targetName, 'height=' + height + ',width=' + width + ',top=' + top + ',left=' + left + ',toolbar=0,scrollbars=yes,resizable=yes');
			childObj.document.write(getBoxHTML(title, content, doc, true));
			childObj.focus();
		}
		else {
			alert(ERROR_DISALLOWED_EXTENSIONS + allowed_extensions);
		}
	}
	else {
		alert(ERROR_MISSING_VALUES);	
	}
}

function showDiv(formId, divId, parentId){
	var formObj = document.getElementById(formId);
	var title = formObj[titleName].value;
	var content = formObj[contentName].value;
	var doc = formObj[documentName].value;

	var elObj = document.getElementById(divId);
	
	elObj.innerHTML = getBoxHTML(title, content, doc, false);
	
	if (document.getElementById(parentId).style.display="none")
		document.getElementById(parentId).style.display="block";
}

function getBoxHTML (title, content, doc, bHTMLPage){
	var dateObj = new Date();
	var dateValue = dateObj.getFullYear() + "-" + month_abbr[dateObj.getMonth()] + "-" + dateObj.getDate();
	var show_doc = (doc.search(/\d/gi)== -1)
		? escape(doc)
		: kbquery + doc;
	
	var html = "";

	if (bHTMLPage == true){
		html += "<html>\n";
		html += "    <head>\n";	
		html += "        <link rel='stylesheet' href='" + docRoot + "/common/css/winie.css' type='text/css' />\n";
		html += "        <link rel='stylesheet' href='" + docRoot + "/common/css/p1.css' type='text/css' />\n";
		html += "        <link rel='stylesheet' href='" + docRoot + "/common/css/kbase_results.css' type='text/css' />\n";
		html += "        <title>Content Preview</title>\n";
		html += "    </head>\n";
		html += "    <body style='background: white;'>\n";
	}
	
		html += "        <div class='overview'>\n";
		html += "            <div><span class='BlueSectionLink'>" + title + "</span></div>\n";
		html += "                <table border='0' cellpadding='2' cellspacing='2' width='100%' style='padding: 2px; border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC;'>\n";
		html += "                <tr valign='top'>\n";
		html += "                    <td colspan='2'>\n";
		
		if (doc)
			html += "                        <div class='floatleft WhiteTableSubHead'><a href='file://" + escape(doc) + "' target='_blank'><img src='" + documentGIF + "' border='0' style='float: left;'/>More Info Available</a></div>\n";		
	
		html += "                        <span class='DefaultGrayText'>" + content + "</span>\n";
		html += "                    </td>\n";
		html += "                </tr>\n";
		html += "				 <tr>\n";
		html += "                    <td class='DefaultGrayText'><i>Last Updated: " + dateValue + "</i></td>\n";
		html += "                    <td align='right'><span class='GraySideText' style='cursor:hand; cursor:pointer' onclick=\"expandcontent(\'previewParent\');\">CLOSE<img src='" + closemeGIF + "' border='0' style='padding-left: 2px;'/></span></td>\n";
		html += "                </tr>\n";
		html += "                </table>\n";							
		html += "        </div>\n";
	
	if (bHTMLPage == true){	
		html +=     "</body>\n";
		html += "</html>\n";
	}
	return html;	
}