
//Global variables
var g_HighlightedItem = 0; // Het item wat nu higlighted is
var g_sTextBoxID;
var g_PostbackID;
var g_bCancelSubmit;
var g_sOldTextBoxValue = "";

function asbGetXmlHttp() {
	var oXmlHttp = false;

	// -----> This method was provided from Jim Ley's website 
	/*@cc_on@*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try {
		oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			oXmlHttp = false;
		}
	}
	/*@end@*/


	if (!oXmlHttp && typeof XMLHttpRequest != 'undefined') {
		oXmlHttp = new XMLHttpRequest();
	}

	return oXmlHttp;
}


function utf8(wide) {
	var c, s;
	var enc = "";
	var i = 0;
	while (i < wide.length) {
		c = wide.charCodeAt(i++);
		// handle UTF-16 surrogates
		if (c >= 0xDC00 && c < 0xE000) continue;
		if (c >= 0xD800 && c < 0xDC00) {
			if (i >= wide.length) continue;
			s = wide.charCodeAt(i++);
			if (s < 0xDC00 || c >= 0xDE00) continue;
			c = ((c - 0xD800) << 10) + (s - 0xDC00) + 0x10000;
		}
		// output value
		if (c < 0x80) enc += String.fromCharCode(c);
		else if (c < 0x800) enc += String.fromCharCode(0xC0 + (c >> 6), 0x80 + (c & 0x3F));
		else if (c < 0x10000) enc += String.fromCharCode(0xE0 + (c >> 12), 0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F));
		else enc += String.fromCharCode(0xF0 + (c >> 18), 0x80 + (c >> 12 & 0x3F), 0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F));
	}
	return enc;
}

var hexchars = "0123456789ABCDEF";

function toHex(n) {
	return hexchars.charAt(n >> 4) + hexchars.charAt(n & 0xF);
}

var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";

function URLEncode(s) {
	var s = utf8(s);
	var c;
	var enc = "";
	for (var i = 0; i < s.length; i++) {
		if (okURIchars.indexOf(s.charAt(i)) == -1)
			enc += "%" + toHex(s.charCodeAt(i));
		else
			enc += s.charAt(i);
	}
	return enc;
}



// Over here we make a call back to our server side page and return the results from our query 
// to a DIV tag sitting under the text box
function asbGetDataFromServer(sValue, sDivID, sDataType, iMaxElements, sParameter1, sParameter2, sParameter3, sParameter4, sParameter5, iOrder, sElemExeedText, sElemHREF, sMaxPopupHeight) {
	sValue = URLEncode(sValue); // Dit is nodig voor karakters zoals ö en ä, maar ook voor +

	var oXmlHttp;
	oXmlHttp = asbGetXmlHttp();

	var sUrl;
	sUrl = ASB_GET_DATA_URL + "?TbID=" + g_sTextBoxID + "&DID=" + sDivID + "&DT=" + sDataType + "&Word=" + sValue + "&PbID=" + g_PostbackID + "&MaxE=" + iMaxElements + "&P1=" + sParameter1 + "&P2=" + sParameter2 + "&P3=" + sParameter3 + "&P4=" + sParameter4 + "&P5=" + sParameter5 + "&EET=" + sElemExeedText + "&EREF=" + sElemHREF + "&" + (new Date()).getTime();

	oXmlHttp.open("GET", sUrl, true);

	oXmlHttp.onreadystatechange = function() {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.responseText != "") {
				if (iOrder > getCurrentOrderDisplayed()) {
					setCurrentOrderDisplayed(iOrder);
					asbShowDiv(sDivID, oXmlHttp.responseText, sMaxPopupHeight);
				}
			}
			else {
				asbHideDiv(sDivID)
			}
		}
	}

	oXmlHttp.send(null)
}


function asbSetSelectedValue(sValue) {
	var hdnSelectedValue = document.getElementById(g_sTextBoxID + "_SelectedValue");
	hdnSelectedValue.value = sValue;
}

function replaceString(sString, sReplaceThis, sWithThis) {
	if (sReplaceThis != "" && sReplaceThis != sWithThis) {
		var counter = 0;
		var start = 0;
		var before = "";
		var after = "";
		while (counter < sString.length) {
			start = sString.indexOf(sReplaceThis, counter);
			if (start == -1) {
				break;
			} else {
				before = sString.substr(0, start);
				after = sString.substr(start + sReplaceThis.length, sString.length);
				sString = before + sWithThis + after;
				counter = before.length + sWithThis.length;
			}
		}
	}
	return sString;
}

function FixString(value) {
	return replaceString(value, "&amp;", "&");
}

function asbGetTextBoxValue() {
	var txtCtrl;
	txtCtrl = document.getElementById(g_sTextBoxID);
	return (txtCtrl.value);
}

function asbOnKeyPress(evt) {
	if ((asbGetKey(evt) == 13) && (g_bCancelSubmit))
		return false;

	return true;
}

function asbOnMouseClick(nMenuIndex, sTextBoxID, sDivID, sPostbackID) {
	g_HighlightedItem = nMenuIndex;
	g_sTextBoxID = sTextBoxID;
	g_PostbackID = sPostbackID;

	asbSelectItem(sDivID);
}


function asbOnMouseOver(nMenuIndex, sTextBoxID, sPostbackID) {
	g_sTextBoxID = sTextBoxID;
	g_PostbackID = sPostbackID;

	asbHighlightItem(nMenuIndex);
}

function getCurrentOrderDisplayed() {
	var hdnSelectedValue = document.getElementById(g_sTextBoxID + "_LD");
	return parseInt(hdnSelectedValue.value);
}
function setCurrentOrderDisplayed(iOrder) {
	var hdnSelectedValue = document.getElementById(g_sTextBoxID + "_LD");
	hdnSelectedValue.value = iOrder;
}
function incrementAndGetOrderSent() {
	var hdnSelectedValue = document.getElementById(g_sTextBoxID + "_LS");
	var iCurrentOrder = parseInt(hdnSelectedValue.value);
	iCurrentOrder++;
	hdnSelectedValue.value = iCurrentOrder;
	return iCurrentOrder;
}

function asbOnKeyUp(sTextBoxID, sDiv, sDataType, evt, maxelements, param1, param2, param3, param4, param5, sPostbackID, sElemExeedText, sElemHREF, sMaxPopupHeight) {
	g_sTextBoxID = sTextBoxID;
	g_PostbackID = sPostbackID;

	var nKey;
	nKey = asbGetKey(evt);

	//Skip up/down/enter
	if ((nKey != 38) && (nKey != 40) && (nKey != 13)) {
		var sNewValue;
		sNewValue = asbGetTextBoxValue();

		if (sNewValue.length > 0) {

			// AH: Het is mogelijk om de parameters een bestaand veld uit te laten lezen door het id geprefixed met : als parameter mee te geven
			if (String(param1).substring(0, 1) == ":")
				param1 = document.getElementById(param1.substring(1, param1.length)).value;
			if (String(param2).substring(0, 1) == ":")
				param2 = document.getElementById(param2.substring(1, param2.length)).value;
			if (String(param2).substring(0, 1) == ":")
				param3 = document.getElementById(param3.substring(1, param3.length)).value;
			if (String(param3).substring(0, 1) == ":")
				param4 = document.getElementById(param4.substring(1, param4.length)).value;
			if (String(param4).substring(0, 1) == ":")
				param5 = document.getElementById(param5.substring(1, param5.length)).value;

			var requestNumberSent = incrementAndGetOrderSent()
			asbGetDataFromServer(sNewValue, sDiv, sDataType, maxelements, param1, param2, param3, param4, param5, requestNumberSent, sElemExeedText, sElemHREF, sMaxPopupHeight)
		}

		if (g_sOldTextBoxValue != sNewValue)
			asbSetSelectedValue("");
	}
}


function asbOnKeyDown(sTextBoxID, sDiv, evt, sPostbackID) {

	g_sTextBoxID = sTextBoxID;
	g_PostbackID = sPostbackID;

	//Save current text box value before key press takes affect
	g_sOldTextBoxValue = asbGetTextBoxValue();

	var nKey;
	nKey = asbGetKey(evt);

	//Detect if the user is using the down button
	if (nKey == 38) //Up arrow
	{
		asbMoveDown()
	}
	else if (nKey == 40) //Down arrow
	{
		asbMoveUp()
	}
	else if (nKey == 13) //Enter
	{
		if (asbIsVisibleDiv(sDiv)) {

			asbSelectItem(sDiv);

			//Only works in IE
			evt.cancelBubble = true;

			if (evt.returnValue) evt.returnValue = false;
			if (evt.stopPropagation) evt.stopPropagation();

			// Door onderstaande zin te commenten reageert een submit al met eerste enter
			g_bCancelSubmit = true;
		}
		else {
			g_bCancelSubmit = false;
		}
	}
	else {
		asbHideDiv(sDiv);
	}

	return true ;
}


function asbGetSelMenuItemDiv() {
	return asbGetMenuItemDiv(g_HighlightedItem);
}


function GetDivMenuItemID(nMenuItem) {
	return (g_sTextBoxID + "_mi_" + nMenuItem);
}


function asbGetMenuItemDiv(nMenuItem) {
	var sDivMenuItemID;
	sDivMenuItemID = GetDivMenuItemID(nMenuItem);

	return document.getElementById(sDivMenuItemID)
}


function asbMoveUp() {
	var nMenuItem;
	nMenuItem = g_HighlightedItem + 1;

	//Check if menu item exists
	if (asbGetMenuItemDiv(nMenuItem))
		asbHighlightItem(nMenuItem)
}


function asbMoveDown() {
	var nMenuItem;
	nMenuItem = g_HighlightedItem - 1;

	if (nMenuItem != 0)
		asbHighlightItem(nMenuItem)
}

//Highlights a div (MouseOver, ArrowUP, ArrowDown)
function asbHighlightItem(nMenuItem) {
	var divMenuItem;
	divMenuItem = asbGetMenuItemDiv(nMenuItem)

	if (divMenuItem) {
		if (nMenuItem != g_HighlightedItem) {
			asbUnhighlightSelMenuItem();
			g_HighlightedItem = nMenuItem;
			divMenuItem.className = "asbSelMenuItem"
		}
	}
}


function asbUnhighlightSelMenuItem() {
	var divMenuItem;
	divMenuItem = asbGetSelMenuItemDiv()

	if (divMenuItem) {
		divMenuItem.className = "asbMenuItem"
	}
}

//Detects what key was pressed
function asbGetKey(evt) {
	evt = (evt) ? evt : (window.event) ? event : null;
	if (evt) {
		var cCode = (evt.charCode) ? evt.charCode :
					((evt.keyCode) ? evt.keyCode :
					((evt.which) ? evt.which : 0));
		return cCode;
	}
}

function asbSelectItem(sDivID) {
	//asbSetTextBoxValue(1);

	var divMenuItem;
	divMenuItem = asbGetSelMenuItemDiv();

	if (divMenuItem) {
		var sMenuItemValueID;
		sMenuItemValueID = GetDivMenuItemID(g_HighlightedItem) + "_value";
		var hdnMenuItemValue = document.getElementById(sMenuItemValueID);

		if (hdnMenuItemValue) {
			//Set selected value of control to the value of selected menu item
			asbSetSelectedValue(hdnMenuItemValue.value);
		}

		// Update de textbox value
		var txtCtrl;
		txtCtrl = document.getElementById(g_sTextBoxID);
		
		/* Dit is de enige uitzondering op onze standaard ASB control (het verwijderen alles wat achter een { komt */
		var txt;
		txt = FixString(divMenuItem.innerHTML);
		var strt = txt.indexOf(" {", 0);
		if (strt > -1)
			txt = txt.substr(0, strt);
		txtCtrl.value = txt.replace(/<.*?>/g, '');

		// Doe een postback als dat opgegeven is
		if (g_PostbackID != '') {
			if (asbIsVisibleDiv(sDivID)) {
				var hdnSelectedValue = document.getElementById(g_sTextBoxID + "_SelectedValue");
				if (hdnSelectedValue.value != '') {
					// Do postback
					__doPostBack(g_PostbackID, hdnSelectedValue.value);
				}
			}
		}
	}
	else {
		// Enter zonder item! Select het eerste item uit pulldown als ingetypte gelijk is
		if (asbGetTextBoxValue().toUpperCase() == asbGetMenuItemDiv(1).innerHTML.toUpperCase()) {
			asbHighlightItem(1);
			asbSelectItem(sDivID)
		}

	}
	// En klap de div dicht
	asbHideDiv(sDivID);
}

function asbBlur(sDivID) {
	//asbHideDiv(sDivID);
}

function asbHideDiv(sDivID) {
	document.getElementById(sDivID).style.visibility = 'hidden';
	g_HighlightedItem = 0;
}

function asbIsVisibleDiv(sDivID) {
	if (document.getElementById(sDivID).style.visibility == 'hidden') {
		return false;
	}
	else {
		return true;
	}
}


function asbShowDiv(sDivID, sDivContent) {
	var divMenu;
	divMenu = document.getElementById(sDivID);

	var sInnerHtml;

	//Use IFrame of the same size as div		
	if (asbIsIE()) {
		sInnerHtml = "<div id='" + sDivID + "_content' style='z-index:9050; position:asbolute;'>";
		sInnerHtml += sDivContent;
		sInnerHtml += "</div><iframe id='" + sDivID + "_iframe' src='about:blank' frameborder='1' scrolling='no'></iframe>";
	}
	else {
		sInnerHtml = sDivContent;
	}

	divMenu.innerHTML = sInnerHtml;

	if (asbIsIE()) {
		var divContent;
		divContent = document.getElementById(sDivID + "_content");

		var divIframe;
		divIframe = document.getElementById(sDivID + "_iframe");

		//Remember display type
		divContent.className = "asbMenu";
		divMenu.className = "asbMenuBase";

		divIframe.style.width = divContent.offsetWidth + 'px';
		divIframe.style.height = divContent.offsetHeight + 'px';
		divIframe.marginTop = "-" + divContent.offsetHeight + 'px';

	}

	divMenu.style.visibility = 'visible';
}

function asbIsIE() {
	return (navigator.appName == "Microsoft Internet Explorer");
}

function asbIsNav() {
	return (navigator.appName == "Netscape");
}

