// MMSuggest template
// Copyright exorbyte GmbH, 2006. All rights reserved.

var mmURLBase = "/Export/SuggestSearch.aspx";

function mmssetup(searchBoxClientID, debugMode, language)
{
	var mmfield = document.getElementById(searchBoxClientID);
	SetupMMSuggest(
		mmfield,
		{
			// Configuring basic information:
			'requestURL': mmURLBase + "?max_results=25&Keywords_mode=APPROX&Keywords=$v$&query=$v$&bias=100&lang=" + language
			,'flags': AS_NOLOGO + AS_GROUPED + AS_NOSUBMIT + AS_NO_ROOT
			,'debug': debugMode
			// Configuring paths:
			,'iconPath': '/Images/mmsuggest/'
			// Configuring layout parameters:
			,'width': 300
			,'searchDelay': 400
			// Configurin rendering functions:
			,'suggBoxTop': function(iDiv) { return renderTopBox(iDiv); }
			//,'rowFunction': function(target, rowArray, field_index, row, iDiv) { return renderRow(target, rowArray, field_index, row, iDiv); }
			,'groupedRowFunction': function(target, rowArray, field_index, row, iDiv) 
				{
					return renderGroupedRow(target, rowArray, field_index, row, iDiv, language); 
				}
			,'suggBoxBottom': function(iDiv) { return renderBottomBox(iDiv); }
			// Configuring row and highlighted row colors:
			,'normalfg': '#123451'
			,'normalbg': 'white'
			,'highlightfg': 'white'
			,'highlightbg': '#5170a6'
			,'letterLimit': '3'
			// Configuring behaviour:
			,'onActivate': function(input, row) { return onSuggestSearchActivate(input, row); }
			// ,'beforeRequest': function(input) { return onBeforeRequest(input); }
		}
	);
}



function renderGroupedRow(target, rowArray, field_index, row, iDiv, language)
{
	try
	{
		var node = rowArray[row][2];
		var value = node.label;
		var cat = node.cat;
		var icon = target.parameters.iconPath + "ordner.gif";
		var ph = "";

		// insert placeholders depending on the node's level
		var parent = node;

		var result = true;
		// two displayed columns
		if (node.children.length > 0)
		{
		
			var ih = '<span class="suggGroupCaption"><nobr>' +
						translateGroup(language, replaceHTMLEntities(rowArray[row][0])) + 
						'&nbsp;</nobr></span>'
			result = false;
		}
		else
		{
			// ID
			var arrIDs = node.row[5].split('|');
			var strID = '';

			var ih = '<span class="suggGroupProduct"><nobr style="">' +
						replaceHTMLEntities(rowArray[row][0]) + 
						'&nbsp;</nobr></span>';
		}
		iDiv.innerHTML = '<div class="suggSides" style="">' + ih + '</div>';
		return result;
	}
	catch (E)
	{
		if (target.parameters.debug)
		{
			alert('mmGroupedRowFunction error: ' + E);
		}
	}

	return 0;
}

function translateGroup(language, text)
{
	if (language == 'DE')
	{
		switch(text)
		{
			case 'ExchangeRates':
				return 'W&auml;hrungen';
			case 'ETC':
				return 'ETCs';
			default:
				return text;
		}
	}

	switch (text)
	{
		case 'Fonds':
			return 'Funds';
		case 'Zertifikate':
			return 'Certificates';
		case 'Zertifikate-Gruppen':
			return 'Certificate Types';
		case 'Aktien':
			return 'Shares';
		case 'Anleihen':
			return 'Bonds';
		case 'Indizes':
			return 'Indices';
		case 'Rohstoffe':
			return 'Commodities'
		case 'Futures':
			return 'Futures';
		case 'ExchangeRates':
			return 'Currencies';
		case 'REITs':
			return 'REITs';
		case 'ETC':
			return 'ETCs';
	}

	return text;
}

/*
function renderGroupedRow(target, rowArray, field_index, row, iDiv)
{
	try
	{
		var node = rowArray[row][2];
		var value = node.label;
		var cat = node.cat;
		var icon = target.parameters.iconPath + "ordner.gif";
		var ph = "";

		// get node icon and image path:
		if (node.children.length == 0)
		{
			if (node.isLastChild())
			{
				icon = target.parameters.iconPath + "lastchild.gif";
			}
			else
			{
				icon = target.parameters.iconPath + "child.gif";
			}
		}
		else if (node.parent != null)
		{
			if (node.isLastChild())
			{
				ph = '<img align="middle" src="' + target.parameters.iconPath + 'lastchild.gif">';
			}
			else
			{
				ph = '<img align="middle" src="' + target.parameters.iconPath + 'child.gif">';
			}
		}
		else if (node.parent == null)
		{
			ph = '<img align="middle" width="1" height="18" src="' + target.parameters.iconPath + 'platzhalter.gif">' + ph;
		}

		// insert placeholders depending on the node's level
		var parent = node;

		for (var i = node.level - 1; i > 0; i--)
		{
			parent = parent.parent;
			if (parent.isLastChild())
			{
				ph = '<img align="middle" src="' + target.parameters.iconPath + 'platzhalter.gif">' + ph;
			}
			else
			{
				ph = '<img align="middle" src="' + target.parameters.iconPath + 'line.gif">' + ph;
			}
		}

		var result = true;
		// two displayed columns
		if (node.children.length > 0)
		{
			icon = target.parameters.iconPath + "ordner.gif";
			var ih = '<span class="suggGroupCaption"><nobr>' + ph +
						replaceHTMLEntities(rowArray[row][0]) + 
						'&nbsp;</nobr></span>'
						'<span class="suggGroupCat"><nobr>' +
						replaceHTMLEntities(cat) +
						'&nbsp;</nobr></span>';
			result = false;
		}
		else
		{
			// ID
			var arrIDs = node.row[5].split('|');
			var strID = '';
			if (arrIDs.length > 0)
			{
				if (node.row[4] == 'BASISWERT')
				{
					strID = arrIDs[2];
				}
				else if (node.row[4] == 'PRODUKT')
				{
					strID = arrIDs[4];
				}
			}
			var ih = '<span class="suggGroupProduct"><nobr style="">' + ph + '<img align="middle" src="' + icon + '">' +
						replaceHTMLEntities(rowArray[row][0]) + 
						'&nbsp;</nobr></span><span class="suggGroupCat"><nobr>' +
						replaceHTMLEntities(strID) +
						'&nbsp;</nobr></span>';
		}
		iDiv.innerHTML = '<div class="suggSides" style="">' + ih + '</div>';
		return result;
	}
	catch (E)
	{
		if (target.parameters.debug)
		{
			alert('mmGroupedRowFunction error: ' + E);
		}
	}

	return 0;
}
*/
function arrayContains(array, str)
{
	for (var z = 0; z < array.length; z++)
	{
		if (array[z] == str)
		{
			return true;
		}
	}
	return false;
}

function customOnActivate(input, row)
{
	switch (row[4])
	{
		case 'BASISWERT':
			var arrIDs = row[5].split('|');
			if (arrIDs.length > 0)
			{
				window.location.href = document.getElementById('search').file.value + '?pageid=86&pkbwnr=' + arrIDs[1];
			}
		break;
		case 'PRODUKT':
			var arrIDs = row[5].split('|');
			if (arrIDs.length > 0)
			{
				window.location.href = document.getElementById('search').file.value + '?pageid=72&pkpnr=' + arrIDs[0];
			}
		break;
	}
	return false;

	// Ändern des Wertes innerhalb der Input-Box
	var node = row[2];
	var wknString = node.row[0];
	if ((String)(node.row[5]) != "" && (arrayContains(zertifikateName, node.row[1]) || arrayContains(aktienName, node.row[1]) || arrayContains(fondsName, node.row[1])))
	{
		var split = node.row[5].split("|");
		wknString = split[0];
		if (arrayContains(fondsName, node.row[1]))
		{
			wknString = split[1];
		}
	}
	input.value = wknString;

	var split = node.row[5].split("|");
	if ((arrayContains(zertifikatGruppenName, node.row[1])))
	{
		//Bei einer Gruppe lautet die Verlinkung
		window.location.href = "showpage.asp?pageid=54&inwpnr=" + (String)(split[1]) + "&inbwpnr=" + (String)(split[2]);
	}
	else
	{
		//Bei einem Link auf ein Produkt lautet der Link
		window.location.href = "showpage.asp?pageid=84&pknr=" + (String)(split[0]) + "&inrnr=395";
	}
	return false;
}

function renderTopBox(iDiv)
{
	var spanMiddlePartWidth = target.parameters.width;
	var html = '<div class="suggBorder">'
		+ '<span style="width: ' + spanMiddlePartWidth + 'px; visibility:hidden" class="suggMiddlePart"></span>'
		+	'</div>';
	iDiv.innerHTML = html;
	return true;
}

function renderBottomBox(iDiv)
{
	var spanMiddlePartWidth = target.parameters.width;
	var html = '<div class="suggBorder">'
		+ '<span style="width: ' + spanMiddlePartWidth + 'px;  visibility:hidden" class="suggMiddlePart"></span>'
		+ '</div>';
	iDiv.innerHTML = html;
	return true;
}

function getSelectedRowData(row, rows)
{
	var rowFound = null;
	var argument = "";
	// Find a row with the same category name in the original rows.
	// Prefer the shortest matching row.
	if (row[1] == '')
	{
		for(var i = 0; i < rows.length; i++) {
			if(rows[i][1].indexOf(row[0]) == 0 && (rowFound == null || rowFound[1] > rows[i][1])) {
				rowFound = rows[i];
			}
		}
	}
	
	argument = row.join(';');
	
	
	return argument;
}

function citiRowFunction(target, rowArray, field_index, row, iDiv) {
	try {
		var node = rowArray[row][2];
		var value = node.label;
		var cat = node.cat;
		var icon = target.parameters.iconPath + "ordner.gif";
		var ph = "";
		if (node.children.length == 0) {
			if (node.isLastChild())
				icon = target.parameters.iconPath + "lastchild.gif";
			else
				icon = target.parameters.iconPath + "child.gif";
		} else if (node.parent != null) {
			if (node.isLastChild())
				ph = "<img align='middle' src='" + target.parameters.iconPath + "lastchild.gif'>";
			else
				ph = "<img align='middle' src='" + target.parameters.iconPath + "child.gif'>";
		} else if (node.parent == null) {
			ph = '<img align="middle" width="1" height="18" src="' + target.parameters.iconPath + 'platzhalter.gif">' + ph;
		}

		// insert placeholders depending on the node's level
		var parent = node;
		for (var i = node.level - 1; i > 0; i--) {
			parent = parent.parent;
			if (parent.isLastChild()) {
				ph = "<img align='middle' src='" + target.parameters.iconPath + "platzhalter.gif'>" + ph;
			} else {
				ph = "<img align='middle' src='" + target.parameters.iconPath + "line.gif'>" + ph;
			}
		}
		var result = true;
		// two displayed columns
		if (node.children.length > 0) {
			icon = target.parameters.iconPath + "ordner.gif";
			var ih = "<span class='suggGroupCaption'><nobr>" + ph +
						replaceHTMLEntities(rowArray[row][0]) + 
						"&nbsp;&nbsp;</nobr></span>";
		} else {
			// ID
			var arrIDs = node.row[5].split('|');
			var strID = '';
			if (arrIDs.length > 0) {
				if (node.row[4] == 'BASISWERT') {
					strID = arrIDs[2];
				} else if (node.row[4] == 'PRODUKT') {
					strID = arrIDs[4];
				}
			}
			var ih = "<span class='suggGroupProduct'><nobr>" + ph + "<img align='middle' src='" + icon + "'>" +
						replaceHTMLEntities(rowArray[row][0]) + 
						"&nbsp;&nbsp;</nobr></span><span class='suggGroupCat'><nobr>" +
						replaceHTMLEntities(strID) +
						"&nbsp;&nbsp;</nobr></span>";
		}
		iDiv.innerHTML = "<div class='suggSides'>"+ih+"</div>";
		return result;
	} catch (E) {
		if (target.parameters.debug)
			alert("mmGroupedRowFunction error: " + E);
	}
	
	return 0;
}
