

if (typeof(Wicket) == "undefined")
Wicket = { };
Wicket.AutoCompleteSettings = {
enterHidesWithNoSelection : false
};
Wicket.AutoComplete=function(elementId, callbackUrl, cfg, indicatorId){
var KEY_TAB=9;
var KEY_ENTER=13;
var KEY_ESC=27;
var KEY_LEFT=37;
var KEY_UP=38;
var KEY_RIGHT=39;
var KEY_DOWN=40;
var KEY_SHIFT=16;
var KEY_CTRL=17;
var KEY_ALT=18;
var selected=-1;  var elementCount=0;  var visible=0;  var mouseactive=0;		var	hidingAutocomplete=0; 
 var objonkeydown;
var objonblur;
var objonkeyup;
var objonkeypress;
var objonchange;
var objonchangeoriginal;
var objonfocus;
		var initialDelta = -1;
	var usefulDimensionsInitialized = false;
var containerBorderWidths = [0, 0];
var scrollbarSize = 0;
var selChSinceLastRender = false;
   var lastStablePopupBounds = [0, 0, 0, 0];
var ignoreOneFocusGain = false;  
		var localThrottler = new Wicket.Throttler(true);
var throttleDelay = cfg.throttleDelay;
function initialize(){
    var choiceDiv=document.getElementById(getMenuId());
if (choiceDiv != null) {
choiceDiv.parentNode.parentNode.removeChild(choiceDiv.parentNode);
}
var obj=wicketGet(elementId);
objonkeydown=obj.onkeydown;
objonblur=obj.onblur;
objonkeyup=obj.onkeyup;
objonkeypress=obj.onkeypress;
objonfocus=obj.onfocus;
 objonchangeoriginal=obj.onchange;
obj.onchange=function(event){
if(mouseactive==1)return false;
if(typeof objonchangeoriginal=="function") return objonchangeoriginal.apply(this,[event]);
}
objonchange=obj.onchange;
obj.onblur=function(event){ 
if(mouseactive==1){
ignoreOneFocusGain = true;
Wicket.$(elementId).focus();
return killEvent(event);
}
hideAutoComplete();
if(typeof objonblur=="function") return objonblur.apply(this,[event]);
}
obj.onfocus=function(event){
if (mouseactive==1) {
ignoreOneFocusGain = false;
return killEvent(event);
}
if (!ignoreOneFocusGain && cfg.showListOnFocusGain && visible==0) {
if (cfg.showCompleteListOnFocusGain) {
updateChoices(true);
} else {
updateChoices();
}
}
ignoreOneFocusGain = false;
if(typeof objonfocus=="function") return objonfocus.apply(this,[event]);
}
obj.onkeydown=function(event){
switch(wicketKeyCode(Wicket.fixEvent(event))){
case KEY_UP:
if(selected>-1) setSelected(selected-1);
if(selected==-1){
hideAutoComplete();
} else {
render(true, false);
}
if(Wicket.Browser.isSafari())return killEvent(event);
break;
case KEY_DOWN:
if(selected<elementCount-1){
setSelected(selected+1);
}
if(visible==0){
updateChoices();
} else {
render(true, false);
showAutoComplete();
}
if(Wicket.Browser.isSafari())return killEvent(event);
break;
case KEY_ESC:
hideAutoComplete();
return killEvent(event);
break;
case KEY_TAB:
case KEY_ENTER:
if(selected > -1) {
var value = getSelectedValue();
value = handleSelection(value);
hideAutoComplete();
hidingAutocomplete = 1; 
if(value) {
obj.value = value;
if(typeof objonchange=="function") objonchange.apply(this,[event]);
}
} else if (Wicket.AutoCompleteSettings.enterHidesWithNoSelection) {
hideAutoComplete();
hidingAutocomplete = 1;
}
mouseactive = 0;
if (typeof objonkeydown=="function") return objonkeydown.apply(this,[event]);
return true;
break;
default:
}
}
obj.onkeyup=function(event){
switch(wicketKeyCode(Wicket.fixEvent(event))){
case KEY_TAB:
case KEY_ENTER:
return killEvent(event);
case KEY_UP:
case KEY_DOWN:
case KEY_ESC:
case KEY_RIGHT:
case KEY_LEFT:
case KEY_SHIFT:
case KEY_ALT:
case KEY_CTRL:
break;
default:
updateChoices();
}
if(typeof objonkeyup=="function") return objonkeyup.apply(this,[event]);
}
obj.onkeypress=function(event){
if(wicketKeyCode(Wicket.fixEvent(event))==KEY_ENTER){
if(selected>-1 || hidingAutocomplete==1){
hidingAutocomplete=0;
return killEvent(event);
}
}
if(typeof objonkeypress=="function") return objonkeypress.apply(this,[event]);
}
}
function setSelected(newSelected) {
if (newSelected != selected) {
selected = newSelected;
selChSinceLastRender = true;
}
}
function handleSelection(input) {
var attr = getSelectableElement(selected).attributes['onselect'];
return attr ? eval(attr.value) : input;
}
function getSelectableElements() {
var menu = getAutocompleteMenu();
var firstChild = menu.firstChild;
var selectableElements = [];
if (firstChild.tagName.toLowerCase() == 'table') {
var selectableInd=0;
for (var i = 0; i < firstChild.childNodes.length; i++) {
var tbody = firstChild.childNodes[i];
for (var j = 0; j < tbody.childNodes.length; j++) {
selectableElements[selectableInd++]=tbody.childNodes[j];
}
}
return selectableElements;
} else {
return firstChild.childNodes;
}
}
function getSelectableElement(selected) {
var menu = getAutocompleteMenu();
var firstChild = menu.firstChild;
if (firstChild.tagName.toLowerCase() == 'table') {
var selectableInd=0;
for (var i = 0; i < firstChild.childNodes.length; i++) {
var tbody = firstChild.childNodes[i];
for (var j = 0; j < tbody.childNodes.length; j++) {
if (selectableInd==selected) {
return tbody.childNodes[j];
}
selectableInd++
}
}
} else {
return firstChild.childNodes[selected];
}
}
function getMenuId() {
return elementId+"-autocomplete";
}
function getAutocompleteMenu() {
var choiceDiv=document.getElementById(getMenuId());
if (choiceDiv==null) {
var container = document.createElement("div");
container.className ="wicket-aa-container";
if(cfg.className)
container.className += ' ' + cfg.className;
document.body.appendChild(container);
container.style.display="none";
container.style.overflow="auto";
container.style.position="absolute";
container.style.margin="0px";  container.style.padding="0px";  container.id=getMenuId()+"-container";
container.show = function() { wicketShow(this.id) };
container.hide = function() { wicketHide(this.id) };
choiceDiv=document.createElement("div");
container.appendChild(choiceDiv);
choiceDiv.id=getMenuId();
choiceDiv.className="wicket-aa";
 container.onmouseout=function() {mouseactive=0;};
container.onmousemove=function() {mouseactive=1;};
}
return choiceDiv;
}
function getAutocompleteContainer() {
var node=getAutocompleteMenu().parentNode;
return node;
}
function killEvent(event){
if(!event)event=window.event;
if(!event)return false;
if(event.cancelBubble!=null){
event.cancelBubble=true;
}
if(event.returnValue){
event.returnValue=false;
}
if(event.stopPropagation){
event.stopPropagation();
}
if(event.preventDefault){
event.preventDefault();
}
return false;
}
function updateChoices(showAll){
setSelected(-1);
if (showAll) {
localThrottler.throttle(getMenuId(), throttleDelay, actualUpdateChoicesShowAll);
} else {
localThrottler.throttle(getMenuId(), throttleDelay, actualUpdateChoices);
}
}
function actualUpdateChoicesShowAll()
{
showIndicator();
var request = new Wicket.Ajax.Request(callbackUrl+"&q=", doUpdateChoices, false, true, false, "wicket-autocomplete|d");
request.get();
}
function actualUpdateChoices()
{
showIndicator();
var value = wicketGet(elementId).value;
var request = new Wicket.Ajax.Request(callbackUrl+(callbackUrl.indexOf("?")>-1 ? "&" : "?") + "q="+processValue(value), doUpdateChoices, false, true, false, "wicket-autocomplete|d");
request.get();
}
function showIndicator() {
if (indicatorId!=null) {
Wicket.$(indicatorId).style.display='';
}
}
function hideIndicator() {
if (indicatorId!=null) {
Wicket.$(indicatorId).style.display='none';
}
}
function processValue(param) {
return (encodeURIComponent)?encodeURIComponent(param):escape(param);
}
function showAutoComplete(){
var input = wicketGet(elementId);
var container = getAutocompleteContainer();
var index=getOffsetParentZIndex(elementId);
container.show();
if (!isNaN(new Number(index))) {
container.style.zIndex=(new Number(index)+1);
}
if (!usefulDimensionsInitialized)
{
initializeUsefulDimensions(input, container);
}
if (cfg.adjustInputWidth) {
var newW = input.offsetWidth-containerBorderWidths[0];
container.style.width = (newW >= 0 ? newW : input.offsetWidth)+'px';
}
calculateAndSetPopupBounds(input, container);
if (visible == 0) {
visible = 1;
hideShowCovered(true, lastStablePopupBounds[0], lastStablePopupBounds[1], lastStablePopupBounds[2], lastStablePopupBounds[3]);
}
}
function initializeUsefulDimensions(input, container) {
usefulDimensionsInitialized = true;
 if (typeof (container.clientWidth) != "undefined" && typeof (container.clientHeight) != "undefined" && container.clientWidth > 0 && container.clientHeight > 0) {
var tmp = container.style.overflow;  container.style.overflow = "visible";
containerBorderWidths[0] = container.offsetWidth - container.clientWidth;
containerBorderWidths[1] = container.offsetHeight - container.clientHeight;
if (cfg.useSmartPositioning) {
container.style.overflow = "scroll";
scrollbarSize = container.offsetWidth - container.clientWidth - containerBorderWidths[0];
}
container.style.overflow = tmp;
}
}
function hideAutoComplete(){
visible=0;
setSelected(-1);
mouseactive=0;
var container = getAutocompleteContainer();
if (container)
{
hideShowCovered(false, lastStablePopupBounds[0], lastStablePopupBounds[1], lastStablePopupBounds[2], lastStablePopupBounds[3]);
container.hide();
if (!cfg.adjustInputWidth && container.style.width != "auto") {
container.style.width = "auto";  }
}
}
function getWindowWidthAndHeigth() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
 myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
 myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
 myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
return [ myWidth, myHeight ];
}
function getWindowScrollXY() {
var scrOfX = 0, scrOfY = 0;
if( typeof( window.pageYOffset ) == 'number' ) {
 scrOfY = window.pageYOffset;
scrOfX = window.pageXOffset;
} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
 scrOfY = document.body.scrollTop;
scrOfX = document.body.scrollLeft;
} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
 scrOfY = document.documentElement.scrollTop;
scrOfX = document.documentElement.scrollLeft;
}
return [ scrOfX, scrOfY ];
}
function calculateAndSetPopupBounds(input, popup)
{
var leftPosition=0;
var topPosition=0;
var inputPosition=getPosition(input);
if (cfg.useSmartPositioning) {
  if (popup.style.width == "auto") {
popup.style.left = "0px";  popup.style.top = "0px";
}
var windowScrollXY = getWindowScrollXY();
var windowWH = getWindowWidthAndHeigth();
var windowScrollX = windowScrollXY[0];
var windowScrollY = windowScrollXY[1];
var windowWidth = windowWH[0];
var windowHeight = windowWH[1];
var dx1 = windowScrollX + windowWidth - inputPosition[0] - popup.offsetWidth;
var dx2 = inputPosition[0] + input.offsetWidth - popup.offsetWidth - windowScrollX;
if (popup.style.width == "auto" && dx1 < 0 && dx2 < 0) {
   var newW = popup.offsetWidth + Math.max(dx1, dx2) - containerBorderWidths[0];
popup.style.width = (newW >= 0 ? newW : popup.offsetWidth + Math.max(dx1, dx2))+'px';
dx1 = windowScrollX + windowWidth - inputPosition[0] - popup.offsetWidth;
dx2 = inputPosition[0] + input.offsetWidth - popup.offsetWidth - windowScrollX;
}
var dy1 = windowScrollY + windowHeight - inputPosition[1] - input.offsetHeight - popup.offsetHeight;
var dy2 = inputPosition[1] - popup.offsetHeight - windowScrollY;
if (dy1 < 0 && dy2 < 0) {
 var newH = popup.offsetHeight + Math.max(dy1, dy2) - containerBorderWidths[1];
popup.style.height = (newH >= 0 ? newH : popup.offsetHeight + Math.max(dy1, dy2))+'px';
var dy1 = windowScrollY + windowHeight - inputPosition[1] - input.offsetHeight - popup.offsetHeight;
var dy2 = inputPosition[1] - popup.offsetHeight - windowScrollY;
}
 if (dx1 < 0 && dx1 < dx2) {
if (dy1 < 0 && dy1 < dy2) {
 leftPosition = inputPosition[0] + input.offsetWidth - popup.offsetWidth;
topPosition = inputPosition[1] - popup.offsetHeight;
} else {
 leftPosition = inputPosition[0] + input.offsetWidth - popup.offsetWidth;
topPosition = inputPosition[1] + input.offsetHeight;
}
} else {
if (dy1 < 0 && dy1 < dy2) {
 leftPosition = inputPosition[0];
topPosition = inputPosition[1] - popup.offsetHeight;
} else {
 leftPosition = inputPosition[0];
topPosition = inputPosition[1] + input.offsetHeight;
}
}
if (popup.style.width == "auto") {
var newW = popup.offsetWidth - containerBorderWidths[0];
popup.style.width = (newW >= 0 ? newW : popup.offsetWidth)+'px';
}
} else {
leftPosition = inputPosition[0];
topPosition = inputPosition[1] + input.offsetHeight;
}
popup.style.left=leftPosition+'px';
popup.style.top=topPosition+'px';
if (visible == 1 &&
(lastStablePopupBounds[0] != popup.offsetLeft ||
lastStablePopupBounds[1] != popup.offsetTop ||
lastStablePopupBounds[2] != popup.offsetWidth ||
lastStablePopupBounds[3] != popup.offsetHeight)) {
hideShowCovered(false, lastStablePopupBounds[0], lastStablePopupBounds[1], lastStablePopupBounds[2], lastStablePopupBounds[3]);  hideShowCovered(true, popup.offsetLeft, popup.offsetTop, popup.offsetWidth, popup.offsetHeight);  }
lastStablePopupBounds = [popup.offsetLeft, popup.offsetTop, popup.offsetWidth, popup.offsetHeight];
}
function getPosition(obj) {
var leftPosition = obj.offsetLeft || 0;
var topPosition = obj.offsetTop || 0;
obj = obj.offsetParent;
while (obj && obj != document.documentElement && obj != document.body) {
topPosition += obj.offsetTop || 0;
topPosition -= obj.scrollTop || 0;
leftPosition += obj.offsetLeft || 0;
leftPosition -= obj.scrollLeft || 0;
obj = obj.offsetParent;
}
return [leftPosition,topPosition];
}
function doUpdateChoices(resp){
 var input=wicketGet(elementId);
if ((Wicket.Focus.getFocusedElement() != input) || !cfg.showListOnEmptyInput && (input.value==null || input.value=="")) {
hideAutoComplete();
Wicket.Ajax.invokePostCallHandlers();
hideIndicator();
return;
}
var element = getAutocompleteMenu();
if (!cfg.adjustInputWidth && element.parentNode && element.parentNode.style.width != "auto") {
element.parentNode.style.width = "auto";  selChSinceLastRender = true;  }
element.innerHTML=resp;
var selectableElements = getSelectableElements();
if(selectableElements) {
elementCount=selectableElements.length;
var clickFunc = function(event) {
mouseactive = 0;
var value = getSelectedValue();
var input = wicketGet(elementId);
if(value = handleSelection(value)) {
input.value = value;
if(typeof objonchange=="function") objonchange.apply(input,[event]);
}
hideAutoComplete();
if (Wicket.Focus.getFocusedElement() != input) {
ignoreOneFocusGain = true;
input.focus();
}
};
var mouseOverFunc = function(event) {
setSelected(getElementIndex(this));
render(false, false);  showAutoComplete();
};
for(var i = 0;i < elementCount; i++) {
var node = selectableElements[i];
node.onclick = clickFunc;
node.onmouseover = mouseOverFunc;
}
} else {
elementCount=0;
}
if(elementCount>0){
if(cfg.preselect==true){
setSelected(0);
} 
showAutoComplete();
} else {
hideAutoComplete();
}
render(false, true);
scheduleEmptyCheck();
Wicket.Log.info("Response processed successfully.");
Wicket.Ajax.invokePostCallHandlers();
hideIndicator();
 if(Wicket.Browser.isIE()) {
var range = document.selection.createRange();
if (range != null)
range.select();
}
}
function scheduleEmptyCheck() {
window.setTimeout(function() {
var input=wicketGet(elementId);
if (!cfg.showListOnEmptyInput && (input.value==null || input.value=="")) {
hideAutoComplete();
}
}, 100);
}
function getSelectedValue(){
var element=getAutocompleteMenu();
var selectableElement = getSelectableElement(selected);
var attr=selectableElement.attributes['textvalue'];
var value;
if (attr==undefined) {
value=selectableElement.innerHTML;
} else {
value=attr.value;
}
return stripHTML(value);
}
function getElementIndex(element) {
var selectableElements = getSelectableElements();
for(var i=0;i<selectableElements.length;i++){
var node=selectableElements[i];
if(node==element)return i;
}
return -1;
}
function stripHTML(str) {
return str.replace(/<[^>]+>/g,"");
}
function adjustScrollOffset(menu, item) {  if (item.offsetTop + item.offsetHeight > menu.scrollTop + menu.offsetHeight) {
menu.scrollTop = item.offsetTop + item.offsetHeight - menu.offsetHeight;
} else
 if (item.offsetTop < menu.scrollTop) {
menu.scrollTop = item.offsetTop;
}
}
function render(adjustScroll,adjustHeight){
var menu=getAutocompleteMenu();
var height=0;
var node=getSelectableElement(0);
var re = /\bselected\b/gi;
var sizeAffected = false;
for(var i=0;i<elementCount;i++)
{
var origClassNames = node.className;
var classNames = origClassNames.replace(re, "");
if(selected==i){
classNames += " selected";
if (adjustScroll) adjustScrollOffset(menu.parentNode, node);
}
if (classNames != origClassNames) {
node.className = classNames;
}
if (cfg.maxHeight > -1)
height+=node.offsetHeight;
node = node.nextSibling;
}
if (cfg.maxHeight > -1) {
if (initialDelta == -1)
{
 initialDelta = menu.parentNode.offsetHeight - height;
}
if (height + initialDelta > cfg.maxHeight) {
var newH = cfg.maxHeight - containerBorderWidths[1];
menu.parentNode.style.height = (newH >= 0 ? newH : cfg.maxHeight) + "px";
sizeAffected = true;
} else if (menu.parentNode.style.height != "auto") {   if (adjustHeight)
{
menu.parentNode.style.height = "auto";  }
sizeAffected = true;
}
}
if (cfg.useSmartPositioning && !cfg.adjustInputWidth && menu.parentNode.style.width != "auto" && selChSinceLastRender) {
 selChSinceLastRender = false;
menu.parentNode.style.width = "auto";
sizeAffected = true;
}
if (sizeAffected) calculateAndSetPopupBounds(wicketGet(elementId), menu.parentNode);  }
 function getStyle(obj,cssRule) {
var cssRuleAlt = cssRule.replace(/\-(\w)/g,function(strMatch,p1){return p1.toUpperCase();});
var value=obj.style[cssRuleAlt];
if (!value) {
if (document.defaultView && document.defaultView.getComputedStyle) {
value = document.defaultView.getComputedStyle(obj,"").getPropertyValue(cssRule);
}
else if (obj.currentStyle)
{
value=obj.currentStyle[cssRuleAlt];
}
}
return value;
}
function isVisible(obj) {
return getStyle(obj,"visibility");
}
function getOffsetParentZIndex(obj) {
obj=typeof obj=="string"?Wicket.$(obj):obj;
obj=obj.offsetParent;
var index="auto";
do {
var pos=getStyle(obj,"position"); 
if(pos=="relative"||pos=="absolute"||pos=="fixed") {
index=getStyle(obj,"z-index");
}
obj=obj.offsetParent; 
} while (obj && index == "auto");
return index;
}
function hideShowCovered(popupVisible, acLeftX, acTopY, acWidth, acHeight) {
if (!cfg.useHideShowCoveredIEFix || (!/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent))) {
return;
}
var hideTags=new Array("select","iframe","applet");
var acRightX = acLeftX + acWidth;
var acBottomY = acTopY + acHeight;
for (var j=0;j<hideTags.length;j++) {
var tagsFound=document.getElementsByTagName(hideTags[j]);
for (var i=0; i<tagsFound.length; i++){
var tag=tagsFound[i];
var p=getPosition(tag);  var leftX=p[0];
var rightX=leftX+tag.offsetWidth;
var topY=p[1];
var bottomY=topY+tag.offsetHeight;
if (!tag.wicket_element_visibility) {
tag.wicket_element_visibility=isVisible(tag);
}
if (popupVisible==0 || (leftX>acRightX) || (rightX<acLeftX) || (topY>acBottomY) || (bottomY<acTopY)) {
tag.style.visibility = tag.wicket_element_visibility;
} else {
tag.style.visibility = "hidden";
}
}
}
}
initialize();
}

