var ajaxHandler = null;
var ajaxHandlerTimer = null;
var ajaxHandlerCmd = null;
var ajaxIdObj = null;
var ajaxDesObj = null;
var ajaxResObj = null;
var ajaxSelPos = 0;
var ajaxOldKey = null;
var ajaxOldValue = null;

/**
 * doAjaxOnKeyUp
 * 
 * @param id
 * @param doCmd
 * @param e
 * @returns {Boolean}
 */
function doAjaxOnKeyUp(id, doCmd, e)
{

	// Load the object definition
	ajaxIdObj = getById(id);
	ajaxDesObj = getById(id + ".Des");
	ajaxResObj = getById(id + ".Res");

	// Save the old value
	if (ajaxOldKey == null)
	{
		ajaxOldKey = ajaxIdObj.value;
		ajaxOldValue = ajaxDesObj.value;
	}
	
	// Check the key
	var key = e.keyCode ? e.keyCode : e.charCode;
	if (key == 38)
	{
		doAjaxMoveUp();
		return;
	}
	else if (key == 40)
	{
		doAjaxMoveDown();
		return;
	}
	else if (key == 27)
	{
		ajaxIdObj.value = ajaxOldKey;
		ajaxDesObj.value = ajaxOldValue;
		doAjaxReset();
		return;
	}
	else
		ajaxIdObj.value = "";

	// Setup the ajax
	ajaxHandler = new Ajax();
	if (ajaxHandlerTimer != null)
	{
		clearTimeout(ajaxHandlerTimer);
		ajaxHandlerTimer = null;
	}

	// Invoke the command
	var cdKeyword = ajaxDesObj.value;
	if (cdKeyword.length == 0)
		ajaxResObj.innerHTML = "";
	else
		ajaxHandlerTimer = setTimeout("doAjaxHandler('" + doCmd + "', '" + cdKeyword + "');", 200);
	
	return true;
}

/**
 * doAjaxHandler
 * 
 * @param cdCmd
 * @param cdKeyword
 */
function doAjaxHandler(cdCmd, cdKeyword)
{
	ajaxHandlerCmd = cdCmd;
	var url = "/ajaxHandler.json?cdCmd=" + cdCmd + "&cdKeyword=" + cdKeyword;
	ajaxHandler.xmlHttp.onreadystatechange = doAjaxReadyStateChange;
	ajaxHandler.getDataAsync(url);
}

/**
 * doAjaxReadyStateChange
 */
function doAjaxReadyStateChange()
{
	if (ajaxHandler.xmlHttp.readyState == 4 && 
		ajaxHandler.xmlHttp.status == 200)
	{
		// Check if the object is still valid
		if (ajaxDesObj == null)
			return;

		var i;
		var data = ajaxHandler.xmlHttp.responseText;
		var resultList = eval(data);
		var html = "<div style=\"min-height: 100px;\">";
		
		// Check if there are results
		if (resultList.length > 0)
		{
			ajaxSelPos = 0;
			html += 
					"<select " +
					"id=\"" + ajaxIdObj.id + ".Sel\" " +
					"multiple=\"multiple\" " +
					"style=\"width: 100%; height: 100%;\" " +
					"onblur=\"doAjaxOnBlur();\" " +
					"onclick=\"doAjaxSelClick();\" " +
					"ondblclick=\"doAjaxSelDblClick();\" " +
					">";

			// Country
			if ("country" == ajaxHandlerCmd)
			{
		    	for (i = 0; i < resultList.length; i++)
		    	{
					html += "<option ";
		    		if (i == 0)
						html += "selected=\"selected\" ";
					html +=
							"value=\"" + resultList[i].cdCountry + "\">" +
							resultList[i].nmCountry +
							"</option>";
		    	}
			}
			// Security
			else if ("security" == ajaxHandlerCmd)
			{
		    	for (i = 0; i < resultList.length; i++)
		    	{
					html += "<option ";
		    		if (i == 0)
						html += "selected=\"selected\" ";
					html +=
							"value=\"" + resultList[i].cdISIN + "\">" +
							resultList[i].cdISIN + " - " + resultList[i].nmISIN +
							"</option>";
		    	}
			}
			// Portfolio
			else if ("portfolio" == ajaxHandlerCmd)
			{
		    	for (i = 0; i < resultList.length; i++)
		    	{
					html += "<option ";
		    		if (i == 0)
						html += "selected=\"selected\" ";
					html +=
							"value=\"" + resultList[i].idPortfolio + "\">" +
							resultList[i].nmPortfolio + " - " + resultList[i].idPortfolio +
							"</option>";
		    	}
			}
			// Account
			else if ("account" == ajaxHandlerCmd)
			{
		    	for (i = 0; i < resultList.length; i++)
		    	{
					html += "<option ";
		    		if (i == 0)
						html += "selected=\"selected\" ";
					html += "value=\"" + resultList[i].idAccount + "\">";
					if (resultList[i].isCompany)
						html += resultList[i].nmCompany + ", ";
					html += resultList[i].nmFirstName + " " + resultList[i].nmLastName + "</option>";
		    	}
			}
			html += "</select>";
	    }
	    else
	    	html += "NO RESULTS FOUND";

    	html += "</div>";
    	ajaxResObj.innerHTML = html;
    	ajaxResObj.style.display = "block";
	}
}

/**
 * doAjaxOnBlur
 */
function doAjaxOnBlur()
{
	var objSel = getById(ajaxIdObj.id + ".Sel");
	if (objSel != null)
	{
		ajaxIdObj.value = objSel.options[ajaxSelPos].value;
		ajaxDesObj.value = objSel.options[ajaxSelPos].text;
	}
	else
	{
		ajaxIdObj.value = "";
		ajaxDesObj.value = "";
	}
	doAjaxReset();
}

/**
 * doAjaxRemoveBlur
 */
function doAjaxRemoveBlur()
{
	if (ajaxDesObj != null)
		ajaxDesObj.onblur = null; 
}

/**
 * doAjaxAddBlur
 */
function doAjaxAddBlur()
{
	if (ajaxDesObj != null)
		ajaxDesObj.onblur = doAjaxOnBlur; 
}

/**
 * doAjaxReset
 */
function doAjaxReset()
{
	// Clear the timer
	if (ajaxHandlerTimer != null)
	{
		clearTimeout(ajaxHandlerTimer);
		ajaxHandlerTimer = null;
	}
	// Empty the html selection box and hide it
	ajaxResObj.innerHTML = "";
	ajaxResObj.style.display = "none";

	// Clear all the main variable
	ajaxHandler = null;
	ajaxHandlerTimer = null;
	ajaxHandlerCmd = null;
	ajaxIdObj = null;
	ajaxDesObj = null;
	ajaxResObj = null;
	ajaxSelPos = 0;
	ajaxOldKey = null;
	ajaxOldValue = null;
}

/**
 * doAjaxMoveUp
 */
function doAjaxMoveUp()
{
	// Get the object
	var objSel = getById(ajaxIdObj.id + ".Sel");
	if (objSel == null)
		return;
	
	// Clear the selection 
	var i;
	for (i = 0; i < objSel.length; i++)
		if (objSel.options[i].selected)
			objSel.options[i].selected = false;

	// Move up the selection
	ajaxSelPos--;
	if (ajaxSelPos < 0)
		ajaxSelPos = 0;
	objSel.options[ajaxSelPos].selected = true;;
	ajaxIdObj.value = objSel.options[ajaxSelPos].value;
	ajaxDesObj.value = objSel.options[ajaxSelPos].text;
}

/**
 * doAjaxMoveDown
 */
function doAjaxMoveDown()
{
	// Get the object
	var objSel = getById(ajaxIdObj.id + ".Sel");
	if (objSel == null)
		return;

	// Clear the selection 
	var i;
	for (i = 0; i < objSel.length; i++)
		if (objSel.options[i].selected)
			objSel.options[i].selected = false;

	// Move down the selection
	ajaxSelPos++;
	if (ajaxSelPos >= objSel.length)
		ajaxSelPos = objSel.length - 1;
	objSel.options[ajaxSelPos].selected = true;;
	ajaxIdObj.value = objSel.options[ajaxSelPos].value;
	ajaxDesObj.value = objSel.options[ajaxSelPos].text;
}

/**
 * doAjaxSelClick
 */
function doAjaxSelClick()
{
	var objSel = getById(ajaxIdObj.id + ".Sel");
	if (objSel == null)
		return;
	ajaxSelPos = objSel.selectedIndex;
	ajaxIdObj.value = objSel.options[ajaxSelPos].value;
	ajaxDesObj.value = objSel.options[ajaxSelPos].text;
}

/**
 * doAjaxSelClick
 */
function doAjaxSelDblClick()
{
	doAjaxSelClick();
	doAjaxReset();
}

