﻿var HTMLPrinterTableIsPossible = false;
var printWin = null;

/**
 * Adds leading <code>0</code>s if necessary
 * 
 *@param nr the input number.
 *@param digits the number of digits that nr should be displayed by, default is <code>2</code>
 *@return the string representation of <code>nr</code>, with at least <code>digits</code> character (zeroes might be added).
 *@version 1.0 <small>051227</small> Mats: Creation.
 **/
function digits(nr, digits){
	if (!digits){
		digits = 2;
	}
	var res = "" + nr;
	if (res.length > digits)
		return res;
	while (res.length < digits){
		res = "0" + res;
	}
	return res;
}

/**
 * Formats the given date into yyyy-MM-dd HH:mm:ss.SSS format
 * 
 *@param date the date to format
 *@return the accurate string representation of the date.
 *@version 1.0 <small>051227</small> Mats: Creation.
 **/
function formatDate(date){
	return date.getFullYear() + "-" +
		digits(date.getMonth() + 1) + "-" +
		digits(date.getDate()) + " " +
		digits(date.getHours()) + ":" +
		digits(date.getMinutes()) + ":" +
		digits(date.getSeconds()) + "." +
		digits(date.getMilliseconds(), 3);
		
}

/**
 * Puts information to debug window with <code>info</code> type
 * 
 *@param str the string to print
 *@version 1.0 <small>051227</small> Mats: Creation.
 **/
function info(str){
	var td = addText(str, "INFO");
	if (td != null)
		td.bgColor = "FFFFFF";
}

/**
 * Puts information to debug window with <code>debug</code> type
 * 
 *@param str the string to print
 *@version 1.0 <small>051227</small> Mats: Creation.
 **/
function debug(str){
	var td = addText(str, "DEBUG")
	if (td != null)
		td.bgColor = "A0FFFF";
}

/**
 * Puts information to debug window with <code>warning</code> type
 * 
 *@param str the string to print
 *@version 1.0 <small>051227</small> Mats: Creation.
 **/
function warning(str){
	var td = addText(str, "WARN");
	if (td != null)
		td.bgColor = "FFA0A0";
}

function warn(str){
    warning(str);
}

/**
 * Puts information to debug window with <code>error</code> type
 * 
 *@param str the string to print
 *@version 1.0 <small>051227</small> Mats: Creation.
 **/
function error(str){
	var td = addText(str, "ERROR");
	if (td != null)
		td.bgColor = "FF3030";
}
var reglt = new RegExp("<", "g");
var regrt = new RegExp(">", "g");
function stripString(str){
    var ret = str.replace(reglt,"&lt;"); 
    ret = ret.replace(regrt,"&gt;"); 
    return ret;
}

/**
 * Puts information to debug window with given type
 * 
 *@param str the string to print
 *@param type the type of the debug information.
 *@return the tablecell of the type.
 *@version 1.0 <small>051227</small> Mats: Creation.
 **/
function addText(str, type){
	if (!HTMLPrinterTableIsPossible)
		return null;
	//tabO = document.getElementById("debugtable");
        str = stripString(str);
	tabO = getPrintTable();
	var tr = tabO.insertRow(1);
	timeC = tr.insertCell(0);
	typeC = tr.insertCell(1);
	descC = tr.insertCell(2);
	var date = new Date();
	timeC.innerHTML = formatDate(date);
	typeC.innerHTML = type;
	descC.innerHTML = str;
	return typeC;
}

/**
 * Gets the debug table or creates it if necessary.
 *
 * @return the table of the debugwindow
 * @version 1.0 <small>051227</small> Mats: Creation
 **/
function getPrintTable(){
	try{
		if (!printWin)
			printWin = window.open("", "HTML_PRINTER");
			
		el = printWin.document.getElementById("printTable");
	}
	catch(y){ // window has been closed. Try to reopen...
		printWin = window.open("", "HTML_PRINTER");
		el = printWin.document.getElementById("printTable");
	}
	if (!el){
		printWin.document.write("<html><body><table border=\"1\" id=\"printTable\"> " +
				"<tr>" +
					"<th width=\"170\">" +
						"Tid" +
					"</th>" +
					"<th>" +
						"Typ" +
					"</th>" +
					"<th width=\"*\">" +
						"Beskrivning" +
					"</th>" +
				"</tr>" +
			"</table></body></html>");
		printWin.document.close();
		el = printWin.document.getElementById("printTable");
	}
	return el;
}
