
var CSV_DELIMITER = ',';
var CSV_ENCLOSURE = '"';

function ParseCSV(str) {
	var rows = new Array();
	var length = str.length;


	var quoted = false;

	var row = 0, col = 0;
	rows[0] = new Array();
	for(var i = 0; i < length; i++) {
		var ch = str.substring(i,i+1);
		var nextCh = str.substring(i+1,i+2);
		if(!rows[row][col]) {
			rows[row][col] = '';
		}
		if(ch.match(/\n/) && !quoted) {
			col = 0;
			row++;
			rows[row] = new Array();
		} else if(ch == CSV_ENCLOSURE && !quoted) {
			quoted = true;
		} else if(ch == CSV_ENCLOSURE && quoted && nextCh == CSV_DELIMITER) {
			// Do nothing
			quoted = false;
		} else if(ch == CSV_DELIMITER && !quoted) {
			col++;
			quoted = false;
		} else if(quoted && ch == CSV_ENCLOSURE && nextCh == CSV_ENCLOSURE) {
			rows[row][col]+=CSV_ENCLOSURE;
			i++;
		} else {
			rows[row][col]+=ch;
		}
	}
	return rows;
}
