function qm_setRecentQuotes(cookieName, latestQuote){
	var MAXIMUM_RECENT_QUOTES = 10;

	QMCI(function(){
		var cookieValue = QMCI.cookie(cookieName);
		
		//Check if symbol valid
		if(latestQuote != null){
				
			//New Cookie
			if (cookieValue == null || cookieValue.length < 1) {
				cookieValue = latestQuote;	
			} else {
				var isInCookie = false;
				var cookieValueArray = cookieValue.split("|");
				for (var j =0; j<cookieValueArray.length; j++) {
					if (cookieValueArray[j] == latestQuote) {
						isInCookie = true;
					}
				}
				
				//Check if current value in the string already
				//if (!cookieValue.match(new RegExp(latestQuote,'i') )){
				if (!isInCookie) {
					cookieValueArray.unshift(latestQuote);
						
					//Split if > MAXIMUM_RECENT_QUOTES
					if (cookieValueArray.length > MAXIMUM_RECENT_QUOTES) {
						cookieValueArray = cookieValueArray.slice(0,MAXIMUM_RECENT_QUOTES);
					}
				} else {
					for (var i =0; i<cookieValueArray.length; i++) {
						if(cookieValueArray[i] == latestQuote) {
							cookieValueArray = qm_recentQuotesHelper(i,cookieName);
							cookieValueArray.unshift(latestQuote);
						}
					}
				}	
				
				cookieValue = cookieValueArray.toString().replace(/\,/g , "|");
			}
			
			QMCI.cookie(cookieName,cookieValue, {expires:10,path:'/'});
		}
			
	});
}

function qm_createRecentQuotesTable(id, cookieName, noRecentQuotesString, uniqueSuffix, closeString) {
	qm_createRecentQuotesTable(id, cookieName, noRecentQuotesString, uniqueSuffix, "qm_maintext", "recentQuotesTable", closeString);
}
function qm_createRecentQuotesTable(id, cookieName, noRecentQuotesString, uniqueSuffix, className, tblId,closeString){		
	var tableId = tblId;
	var recentQuotesArray = null;
	QMCI(function(){
		var cookieValue = QMCI.cookie(cookieName);

		if (cookieValue!=null) {
			recentQuotesArray = cookieValue.split("|");
		}

		var recentQuotesDiv = document.getElementById(id);
			
		var tbl = document.createElement("table");
		var tblBody = document.createElement("tbody");
			
		tbl.id = tableId;
		tbl.width = "100%";
			
		if (recentQuotesDiv != null) {
			if ( recentQuotesArray != null) {
				var rows = new Array();
				var symbolCells = new Array();
				var nameCells = new Array();
				var delCells = new Array();
				var tempString = new Array();
				
				var hideRowColSpan;
				for (var i = 0; i< recentQuotesArray.length; i++) {
					//Saved because onclick cannot access the recentQuotesArray
					tempString[i] = recentQuotesArray[i];
					var tempArray = tempString[i].split("_");
					rows[i] = document.createElement("tr");

					symbolCells[i] = document.createElement("td");
		            var symbolTextLink = document.createElement("A");
		            symbolTextLink.setAttribute("href", "javascript:qm_submit_form" + uniqueSuffix + "('" + tempArray[0] +"')" );
		            symbolTextLink.innerHTML = tempArray[0];
		            
		            symbolCells[i].appendChild(symbolTextLink);
		            rows[i].appendChild(symbolCells[i]);

		            nameCells[i] = document.createElement("td");
		            var nameCellText = document.createTextNode(tempArray[1]);
		            nameCells[i].appendChild(nameCellText);
		            rows[i].appendChild(nameCells[i]);

		            delCells[i] = document.createElement("td");
		            var delCellText = document.createTextNode("X");
		            delCells[i].style.cursor = 'pointer';
		            delCells[i].style.textAlign = 'right';
		            delCells[i].appendChild(delCellText);

		            delCells[i].onclick = function() {qm_deleteRecentQuotes(this, cookieName, noRecentQuotesString, tblId)};
		            
		            rows[i].className = className;
		            rows[i].appendChild(delCells[i]);
		            rows[i].id = "recentQuoteRow_" +i;

		            tblBody.appendChild(rows[i]);
				}
				
				hideRowColSpan = "3";
			} else {
				var row = document.createElement("tr");
				var cell = document.createElement("td");
	            var cellText = document.createTextNode("No recent quotes");
	            cell.appendChild(cellText);
	            row.appendChild(cell);
	            tblBody.appendChild(row);
	            row.className = className;
	            
	            hideRowColSpan = "1";
			}
			
			var hideCell = document.createElement("td");
			var hideCellText = document.createTextNode(closeString);
			hideCell.colSpan = hideRowColSpan;
			hideCell.style.textAlign = "left";
			hideCell.onclick = function() {toggle(id)};
			hideCell.style.cursor = "pointer";
			hideCell.appendChild(hideCellText);
			hideCell.style.borderTop="1px solid rgb(204, 204, 204)";
			var hideRow = document.createElement("tr");
			hideRow.className = className;
			hideRow.appendChild(hideCell);
			tblBody.appendChild(hideRow);
			
			//Append to div tag
			tbl.appendChild(tblBody);
			recentQuotesDiv.appendChild(tbl);
		}
	});
}

function qm_deleteRecentQuotes(delCell, cookieName, noRecentQuotesString, tblId){
	QMCI(function(){
		var i = delCell.parentNode.rowIndex;
		
		var cookieValueArray = qm_recentQuotesHelper(i, cookieName)
		
		cookieValue = cookieValueArray.toString().replace(/\,/g , "|");	
		QMCI.cookie(cookieName,cookieValue, {expires:10,path:'/'});
		//Delete from table
		var tbl = document.getElementById(tblId);
		
		//2 Rows because of the last row to hide the table is considered a row
		if(tbl.rows.length == 2){
			var row = tbl.rows[0];
            while (row.childNodes[0]) {
                row.removeChild(row.childNodes[0]);
            }
            
            var cell = document.createElement("td");
            var cellText = document.createTextNode(noRecentQuotesString);
            cell.appendChild(cellText);
            row.appendChild(cell);
		}else{
			tbl.deleteRow(i);
		}
	});
}

function qm_recentQuotesHelper(i, cookieName){
	//Delete From Cookie
	var cookieValue = QMCI.cookie(cookieName);
	var cookieValueArray = cookieValue.split("|");
	
	//i at the front
	if(i==0){
		cookieValueArray = cookieValueArray.slice(1);
	}else{
		//End
		if(i==cookieValueArray.length-1){
			cookieValueArray = cookieValueArray.slice(0,i);
		}else{
			//Middle
			var temp = cookieValueArray.slice(0,i);
			var temp2 = cookieValueArray.slice(i+1,cookieValueArray.length);
			cookieValueArray = temp.concat(cookieValueArray.slice(i+1,cookieValueArray.length));
		}
	}
	
	return cookieValueArray;
}

function toggle(id){
	document.getElementById(id).style.display = (document.getElementById(id).style.display == 'block') ? 'none' : 'block';
}

QMCI(document).ready(function() {
	qm_setRecentQuotes(COOKIE_NAME, latestQuote);
	qm_createRecentQuotesTable(divId, COOKIE_NAME, noRecentQuotesString, uniqueSuffix, contentClassName, tblId, closeString);
});




