// Javascript file

var TableExporter = function(tableId) {
	this.table = document.getElementById(tableId);
	this.tableArray = this.parseTableToArray();
}// End TableExporter

TableExporter.prototype = {
	parseTableToArray: function() {
		var rows = this.table.getElementsByTagName("tr");
		var aTable = new Array();
		for ( i = 0; i <= rows.length -1; i ++ ) {
			tr = rows[i];
			aTable[i] = new Array();
			if ( tr && tr.childNodes ) {
				for ( j = 0; j <= tr.childNodes.length -1; j ++ ) {
					cell = tr.childNodes[j];	
					if ( cell.nodeName == "TD" || cell.nodeName == "TH") {
						//console.log('node %s with content%s',cell.nodeName,cell.innerHTML);
						aTable[i].push(cell.innerHTML);
						if (cell.colspan && cell.colspan >= 1) {
							this.padArray(aTable,cell.colspan -1);
						}// End if
					}// End if
				}// End for
			}// End if
		}// End if
		return aTable;	
	},// End parseTableToArray
	
	padArray: function(arr,num) {
		for ( k = 1; k <= num; k ++ ) {
			arr.push(" ");
		}// End for
	},// End padArray
	
	_toCSV: function() {
		var csv = "";
		for ( i = 0; i <= this.tableArray.length -1; i++) {
			csv += this.tableArray[i].join(',') + "\n";
		}// End for
		return csv
	},// End _toCSV
	
	_toText: function() {
		var output = "<table id=" + this.table.id + ">";
		output += this.table.innerHTML;
		output += "</table>";
		return output;
	}// End _toTable
}// End TableExporter.prototype

var ClipBoard = function() {
	if ( window.netscape ) {	
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		this.clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
   		if (!this.clip) alert('Clipboard not supported!');
		
		this.trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!this.trans) alert('There was a problem with the clipboard transfer interface');

		this.trans.addDataFlavor('text/unicode');

		this.str = new Object();
		this.len = new Object();

		this.str = Components.classes["@mozilla.org/supports-string;1"]                .createInstance(Components.interfaces.nsISupportsString);
	}// End if
}// End ClipBoard

ClipBoard.prototype = {
	copy: function(text) {
		if (window.clipboardData) {
			window.clipboardData.setData("Text", text);
		} else if ( window.netscape ) {
			try {
				this.str.data = text;
				this.trans.setTransferData("text/unicode",this.str,text.length*2);
				var clipid = Components.interfaces.nsIClipboard;
				this.clip.setData(this.trans,null,clipid.kGlobalClipboard);
				
			} catch (e) {
				alert("Exception found. If error is permission related, you will need to enable your netscape (eg firefox) browser to use the clipboard.\n\n" + e);
				return false;
			}
		} else {
			alert('Copy failed!');
			return false;
		}// End if
		alert('Copied to clipboard successfully!');
		return true;
	}// End copy
}// End ClipBoard.prototype
