function getDimensions( elm ) {
	var box = { x:0, y:0, w:0, h:0 };
	if(document.getBoxObjectFor) {
		var boxRef = document.getBoxObjectFor(elm);
		box.x = boxRef.x;
		box.y = boxRef.y;
		box.w = boxRef.width;
		box.h = boxRef.height;
	}
	else if(elm.getBoundingClientRect) {
		var rxIE50 = /MSIE 5\.0/g;
		var boxRef = elm.getBoundingClientRect();
		box.x = boxRef.left;
		box.y = boxRef.top;
		box.w = (boxRef.right - boxRef.left);
		box.h = (boxRef.bottom - boxRef.top);
		if(document.compatMode && document.compatMode != 'BackCompat') {
			
			box.x += document.documentElement.scrollLeft - 2;
			box.y += document.documentElement.scrollTop - 2;
		}
		else if(!gClientIsIE5) {
			box.x += document.body.scrollLeft - 2;
			box.y += document.body.scrollTop - 2;
		}
	}
	else {
		box.w = elm.offsetWidth;
		box.h = elm.offsetHeight;
		while(elm) {
			box.x += elm.offsetLeft;
			box.y += elm.offsetTop;
			if(elm.offsetParent) 
				elm = elm.offsetParent;
			else
				break;
		}
	}
	var cc;
	if(cc = document.getElementById('bodyconstraint'))
		box.x -= cc.offsetLeft;
	return box;
}

var DOM = {
	isParentOf: function( parentElm, contextElm) {
		while(contextElm && (contextElm != parentElm))
			contextElm = contextElm.parentNode;
		return (contextElm == parentElm);
	},
	getParentOrSelf: function( contextElm, nodeName ) {
		nodeName = nodeName.toLowerCase();
		while(contextElm.nodeName.toLowerCase() != nodeName && contextElm.parentNode)
			contextElm = contextElm.parentNode;
		return contextElm;
	},
	addClass: function( elm, className ) {
		elm.className += ' '+className;
	},
	removeClass: function( elm, className) {
		var classMatch = new RegExp('\\b'+className+'\\b', 'g');
		if(classMatch.test(elm.className))
			elm.className = elm.className.replace(classMatch, ' ');
	}	
};

var gClientIsGecko = (window.controllers) ? true : false;
var gClientIsOpera = (window.opera) ? true : false;
var gClientIsIE    = (document.all && !gClientIsOpera) ? true : false;
var gClientIsIE5   = (gClientIsIE && /MSIE 5\.0/.test(navigator.appVersion)) ? true : false;
var gClientIsMac   = (/Mac/.test(navigator.appVersion)) ? true : false;

function SloppyRegExp( matchOn, mods ) {
	var cm, matched, upper, i, realMatch = '', sub = b25.Bkhtl.sub;
	for(var c = 0; c < matchOn.length; c++) {
		matched = false;
		upper = isUpperCase(matchOn.charAt(c));
		cm = matchOn.charAt(c).toLowerCase();
		for(i in sub) {
			if(sub[i].indexOf(cm) != -1) {
				cm = '[' + sub[i] + ']';
				matched = true;
				break;
			}
		}
		if(upper)
			cm = cm.toUpperCase();
		realMatch += cm + ' ?'; 
	}
	return new RegExp('(' + realMatch + ')', mods);
}

function isUpperCase( text ) {
	return (text == text.toUpperCase());
}


function highlightSloppy( text, matchOn ) {
	var sloppyMatch = SloppyRegExp(matchOn, 'i');
	if(sloppyMatch.test(text)) {
		var hlss = sloppyMatch.exec(text)[1];
		return text.replace(sloppyMatch, hlss);
	}
	else
		return text;
}




if(!b25)
	var b25 = {};


b25.Bkhtl = {
    BS_URI:'cs2.php?srch_key=',
    
	BS_TIMEOUT: 100,
	BS_INITIALIZED: false,
	sub: [
		'aàáâãäåæāăą',
		'iìíîïīĭĩ',
		'eèéêëēĕėęě',
		'oòóôõöōŏő',
		'uyùúûüýũūŭůű',
		' ?'
	],
	request: null,
	timeout: false,
	element: null,
	popup: null,
	append: null,
	popupActive: false,
	lastValue: '',
	init: function( elm ) {
		if(!b25.Bkhtl.popup) {
			var contentBody = document.getElementById('bodyconstraint') || document.body;
			
			var markup = '<table cellspacing="0" cellpadding="0"><tr><td></td></tr></table>';
			if(document.all && !window.opera)
				markup = '<iframe style="position:absolute;width:1000px;height:1000px;" frameborder="0"></iframe>' + markup;
			b25.Bkhtl.popup = document.createElement('div');
			b25.Bkhtl.popup.setAttribute('id', 'Bkhtl_popup');
			b25.Bkhtl.popup.style.display = 'none';
			b25.Bkhtl.popup.innerHTML = markup;
			contentBody.appendChild(b25.Bkhtl.popup);
			b25.Bkhtl.append = b25.Bkhtl.popup.getElementsByTagName('td')[0];
		}
		b25.Bkhtl.element = elm;
		b25.Bkhtl.BS_INITIALIZED = true;
	},
	initRequestObject: function() {
		b25.Bkhtl.request = null;
		var req;
		try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch(err) {
			try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch(err) { req = null; }
		}
		if(!req && (typeof(XMLHttpRequest)!="undefined")) {
			req = new XMLHttpRequest();
		}
		b25.Bkhtl.request = req;
	},
	handleKeyNav: function( evt, isUp ) {
		if(gClientIsIE5) return true;
		if(evt.keyCode) {
			switch(evt.keyCode) {
				case 9: 
					if(b25.Bkhtl.popup.selectedBkhtlion)
						b25.Bkhtl.popup.selectedBkhtlion.onmousedown();
					break;
				case 13: 
					if(isUp)
						return false;
					if(b25.Bkhtl.popup.selectedBkhtlion) {
						b25.Bkhtl.popup.selectedBkhtlion.onmousedown();
						if(evt.preventDefault) evt.preventDefault();
						return false;
					}
					break;
				case 27: 
					b25.Bkhtl.__hidePopup();
					b25.Bkhtl.lastValue = b25.Bkhtl.element.value;
					break;
				case 38: 
					if(b25.Bkhtl.popupActive)
						b25.Bkhtl.__previousBkhtlion();
					if(evt.preventDefault) evt.preventDefault();
					break;
				case 40: 
					if(b25.Bkhtl.popupActive)
						b25.Bkhtl.__nextBkhtlion();
					if(evt.preventDefault) evt.preventDefault();
					break;
				default:
					return true;
					break;
			}
		}
	},
	handleActivity: function( elm, evt ) {
        //elm.className = "search_start";
		if(gClientIsIE5) return;
		if(!b25.Bkhtl.BS_INITIALIZED)
			b25.Bkhtl.init(elm);
		if(elm.value == '') {
			b25.Bkhtl.__hidePopup();
			b25.Bkhtl.lastValue = elm.value;
		}
		else if(elm.value != b25.Bkhtl.lastValue) {
			if(b25.Bkhtl.timeout)
				clearTimeout(b25.Bkhtl.timeout);
			b25.Bkhtl.timeout = setTimeout(b25.Bkhtl.requestBkhtlion, b25.Bkhtl.BS_TIMEOUT)
		}
	},
	requestBkhtlion: function() {
		if(b25.Bkhtl.element.value=='') {
			b25.Bkhtl.__hidePopup();
			return;
		}
		b25.Bkhtl.initRequestObject();
		var req = b25.Bkhtl.request,
		    url = b25.Bkhtl.BS_URI + encodeURIComponent(b25.Bkhtl.element.value);
		req.onreadystatechange = b25.Bkhtl.responseHandler;
		
        req.open('GET', url, true);
		req.send('');
		b25.Bkhtl.lastValue = b25.Bkhtl.element.value;
	},
	responseHandler: function() {
		if(b25.Bkhtl.request.readyState==4) {
			switch(b25.Bkhtl.request.status) {
				case 200: 
					b25.Bkhtl.parseResponse(b25.Bkhtl.request.responseText);
					break;
				case 404: 
				default:
					window.status = 'b25 Bkhtl error: Server returned HTTP code ' + b25.Bkhtl.request.status;
					break;
			}
		}
	},
	parseResponse: function( response ) {
		var append = b25.Bkhtl.append,
		    elm = b25.Bkhtl.element;
		var BkhtlList = [];
		var rl = response.split('\n');
		for(var i=0; rl[i]; i+=2)
			BkhtlList.push({
				n: rl[i],
                d: rl[i+1]
				
			});
		
		if(BkhtlList.length) {
			var spt = '';
			b25.Bkhtl.Bkhtlions = [];
			for(var i=0; BkhtlList[i]; i++) {
				b25.Bkhtl.Bkhtlions[i] = BkhtlList[i].n;
				spt += '<div class="Bkhtl_entry" onmouseover="b25.Bkhtl.__setActiveBkhtlion(this)" onmouseout="b25.Bkhtl.__unsetActiveBkhtlion(this)" onmousedown="b25.Bkhtl.__useBkhtlion(' + i + ')">';
				spt += highlightSloppy(BkhtlList[i].n, b25.Bkhtl.lastValue);
				spt += '<div class="Bkhtl_dist"> ' + BkhtlList[i].d + '</div>';
				spt += '</div>';
			}
			append.innerHTML = spt;
			b25.Bkhtl.__showPopup();
		}
		else {
			b25.Bkhtl.__hidePopup();
		}
		b25.Bkhtl.popup.selectedBkhtlion = null;
	},
	__setActiveBkhtlion: function( elm ) {
		if(b25.Bkhtl.popup.selectedBkhtlion)
			b25.Bkhtl.__unsetActiveBkhtlion(b25.Bkhtl.popup.selectedBkhtlion);
		DOM.addClass(elm, 'Bkhtl_entry_selected');
		b25.Bkhtl.popup.selectedBkhtlion = elm;
	},
	__unsetActiveBkhtlion: function( elm ) {
		DOM.removeClass(elm, 'Bkhtl_entry_selected');
	},
	__nextBkhtlion: function() {
		if(b25.Bkhtl.popup.selectedBkhtlion && b25.Bkhtl.popup.selectedBkhtlion.nextSibling)
			var newBkhtlion = b25.Bkhtl.popup.selectedBkhtlion.nextSibling;
		else 
			var newBkhtlion = b25.Bkhtl.append.firstChild;
		b25.Bkhtl.__setActiveBkhtlion(newBkhtlion);
	},
	__previousBkhtlion: function() {
		if(b25.Bkhtl.popup.selectedBkhtlion && b25.Bkhtl.popup.selectedBkhtlion.previousSibling)
			var newBkhtlion = b25.Bkhtl.popup.selectedBkhtlion.previousSibling;
		else 
			var newBkhtlion = b25.Bkhtl.append.lastChild;
		b25.Bkhtl.__setActiveBkhtlion(newBkhtlion);
	},
	__useBkhtlion: function( index ) {
        if (b25.Bkhtl.Bkhtlions[index] != "Destination Not Found")
        {
		    b25.Bkhtl.element.value = b25.Bkhtl.Bkhtlions[index];
        }
        else
        {
            b25.Bkhtl.element.value = "";
        }
		b25.Bkhtl.__hidePopup();
		
		
	},
	__showPopup: function() {
		if(b25.Bkhtl.element.value=='') {
			b25.Bkhtl.__hidePopup();
			return;
		}
		var elm = b25.Bkhtl.element,
			popup = b25.Bkhtl.popup;
		var elmPos = getDimensions(elm);
		var left = elmPos.x, top = elmPos.y + (elm.offsetHeight);
		popup.style.left = left + 'px';
		popup.style.top  = top + 'px';
		popup.style.display = 'none'; 
		popup.style.display = 'block';
		b25.Bkhtl.popupActive = true;
	},
	__hidePopup: function() {
		if(gClientIsIE5) return;
		var hideIt = function() {
			b25.Bkhtl.popup.style.display = 'none';
		}
		setTimeout(hideIt, 100);
		b25.Bkhtl.popupActive = false;
		
		b25.Bkhtl.popup.selectedBkhtlion = null;
		b25.Bkhtl.lastValue = '';
	}
};


