function toCSV(elem) {
	var e;
	var doHead=true;
	var oput = "";
//cell.colSpan
	var e = document.getElementById(elem);
	var i = 1;
	//Loop over the rows
	for (var i = 0; i < e.rows.length; i++) {
		var doPrint = true;
		var r = e.rows[i];
		if (r.className.indexOf("thead") != -1) {
			//This is a header row, print it if not yet written
			doPrint = doHead;
			doHead = false;
		}
		//Now, output the columns
		if (doPrint) {
			var sep = "";
			for (var j = 0; j < r.cells.length; j++) {
				var col = r.cells[j]; 
				oput += sep + '"' + col.innerHTML.replace(/"/g, '""') + '"';//escape quotes
				sep = ",";
				//Add blank fields for spanning columns
				for (var k = 0; k < col.colSpan-1; k++) {
					oput += sep + '""';
				}
			}//end cols
			oput += "\n";
		}
	}//end rows
	return popup(oput);
}
function popup(data) {
	var generator = window.open('', 'csv', 'height=400,width=600'); 
	generator.document.write('<html><head><title>CSV</title>'); 
	generator.document.write('</head><body >'); 
	generator.document.write('<textArea cols=70 rows=15 wrap="off" onfocus="this.select();">'); 
	generator.document.write(data); 
	generator.document.write('</textArea>'); 
	generator.document.write('</body></html>'); 
	generator.document.close();
	return true; 
}

