	/************************************************************************************************************
	(C) www.dhtmlgoodies.com, April 2006

	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.

	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.

	Thank you!

	www.dhtmlgoodies.com
	Alf Magne Kalleland

	************************************************************************************************************/

	var ajaxBox_offsetX = 0;
	var ajaxBox_offsetY = 0;
	var list_externalFile = '/inc/ajax-list-countries.php';	// Path to external file
	var minimumLettersBeforeLookup = 1;	// Number of letters entered before a lookup is performed.

//	alert(list_externalFile);
	
	var list_objects = new Array();
	var list_cachedLists = new Array();
	var list_activeInput = false;
	var list_activeItem;
	var list_optionDivFirstItem = false;
	var list_currentLetters = new Array();
	var optionDiv = false;
	var optionDiv_iframe = false;

	var list_MSIE = false;
	if(navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Opera')<0)list_MSIE=true;

	var currentListIndex = 0;

	function getTopPos(inputObj)
	{

	  var returnValue = inputObj.offsetTop;
	  while((inputObj = inputObj.offsetParent) != null){
	  	returnValue += inputObj.offsetTop;
	  }
	  return returnValue;
	}
	function list_cancelEvent()
	{
		return false;
	}

	function getLeftPos(inputObj)
	{
	  var returnValue = inputObj.offsetLeft;
	  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetLeft;

	  return returnValue;
	}

	function option_setValue(e,inputObj)
	{
		if(!inputObj)inputObj=this;
		var tmpValue = inputObj.innerHTML;
		if(list_MSIE)tmpValue = inputObj.innerText;else tmpValue = inputObj.textContent;
		if(!tmpValue)tmpValue = inputObj.innerHTML;
		list_activeInput.value = tmpValue;
		if(document.getElementById(list_activeInput.name + '_hidden'))document.getElementById(list_activeInput.name + '_hidden').value = inputObj.id;

  		var f1=setTimeout('list_activeInput.focus()',1);
  		var f2=setTimeout('list_activeInput.value = list_activeInput.value',1);

		options_hide();
	}

	function options_hide()
	{
		if(optionDiv)optionDiv.style.display='none';
		if(optionDiv_iframe)optionDiv_iframe.style.display='none';
	}

	function options_rollOverActiveItem(item,fromKeyBoard)
	{
		if(list_activeItem)list_activeItem.className='optionDiv';
		item.className='optionDivSelected';
		list_activeItem = item;

		if(fromKeyBoard){
			if(list_activeItem.offsetTop>optionDiv.offsetHeight){
				optionDiv.scrollTop = list_activeItem.offsetTop - optionDiv.offsetHeight + list_activeItem.offsetHeight + 2 ;
			}
			if(list_activeItem.offsetTop<optionDiv.scrollTop)
			{
				optionDiv.scrollTop = 0;
			}
		}
	}

	function option_list_buildList(letters,paramToExternalFile)
	{

		optionDiv.innerHTML = '';
		list_activeItem = false;
		if(list_cachedLists[paramToExternalFile][letters.toLowerCase()].length<=1){
			options_hide();
			return;
		}



		list_optionDivFirstItem = false;
		var optionsAdded = false;
		for(var no=0;no<list_cachedLists[paramToExternalFile][letters.toLowerCase()].length;no++){
			if(list_cachedLists[paramToExternalFile][letters.toLowerCase()][no].length==0)continue;
			optionsAdded = true;
			var div = document.createElement('DIV');
			var items = list_cachedLists[paramToExternalFile][letters.toLowerCase()][no].split(/###/gi);

			if(list_cachedLists[paramToExternalFile][letters.toLowerCase()].length==1 && list_activeInput.value == items[0]){
				options_hide();
				return;
			}


			div.innerHTML = items[items.length-1];
			div.id = items[0];
			div.className='optionDiv';
			div.onmouseover = function(){ options_rollOverActiveItem(this,false) }
			div.onclick = option_setValue;
			if(!list_optionDivFirstItem)list_optionDivFirstItem = div;
			optionDiv.appendChild(div);
		}
		if(optionsAdded){
			optionDiv.style.display='block';
			if(optionDiv_iframe)optionDiv_iframe.style.display='';
			options_rollOverActiveItem(list_optionDivFirstItem,true);
		}

	}

	function option_list_showContent(ajaxIndex,inputObj,paramToExternalFile,whichIndex)
	{
		if(whichIndex!=currentListIndex)return;
		var letters = inputObj.value;
		var content = list_objects[ajaxIndex].response;
		var elements = content.split('|');
		list_cachedLists[paramToExternalFile][letters.toLowerCase()] = elements;
		option_list_buildList(letters,paramToExternalFile);

	}

	function option_resize(inputObj)
	{
		optionDiv.style.top = (getTopPos(inputObj) + inputObj.offsetHeight + ajaxBox_offsetY) + 'px';
		optionDiv.style.left = (getLeftPos(inputObj) + ajaxBox_offsetX) + 'px';
		if(optionDiv_iframe){
			optionDiv_iframe.style.left = optionDiv.style.left;
			optionDiv_iframe.style.top = optionDiv.style.top;
		}

	}

	function showOptions(inputObj,paramToExternalFile,e)
	{
		if(e.keyCode==13 || e.keyCode==9)return;
		if(list_currentLetters[inputObj.name]==inputObj.value)return;
		if(!list_cachedLists[paramToExternalFile])list_cachedLists[paramToExternalFile] = new Array();
		list_currentLetters[inputObj.name] = inputObj.value;
		if(!optionDiv){
			optionDiv = document.createElement('DIV');
			optionDiv.id = 'listOfOptions';
			document.body.appendChild(optionDiv);

			if(list_MSIE){
				optionDiv_iframe = document.createElement('IFRAME');
				optionDiv_iframe.border='0';
				optionDiv_iframe.style.width = optionDiv.clientWidth + 'px';
				optionDiv_iframe.style.height = optionDiv.clientHeight + 'px';
				optionDiv_iframe.id = 'listOfOptions_iframe';

				document.body.appendChild(optionDiv_iframe);
			}

			var allInputs = document.getElementsByTagName('INPUT');
			for(var no=0;no<allInputs.length;no++){
				if(!allInputs[no].onkeyup)allInputs[no].onfocus = options_hide;
			}
			var allSelects = document.getElementsByTagName('SELECT');
			for(var no=0;no<allSelects.length;no++){
				allSelects[no].onfocus = options_hide;
			}

			var oldonkeydown=document.body.onkeydown;
			if(typeof oldonkeydown!='function'){
				document.body.onkeydown=option_keyNavigation;
			}else{
				document.body.onkeydown=function(){
					oldonkeydown();
				option_keyNavigation() ;}
			}
			var oldonresize=document.body.onresize;
			if(typeof oldonresize!='function'){
				document.body.onresize=function() {option_resize(inputObj); };
			}else{
				document.body.onresize=function(){oldonresize();
				option_resize(inputObj) ;}
			}

		}

		if(inputObj.value.length<minimumLettersBeforeLookup){
			options_hide();
			return;
		}


		optionDiv.style.top = (getTopPos(inputObj) + inputObj.offsetHeight + ajaxBox_offsetY) + 'px';
		optionDiv.style.left = (getLeftPos(inputObj) + ajaxBox_offsetX) + 'px';
		if(optionDiv_iframe){
			optionDiv_iframe.style.left = optionDiv.style.left;
			optionDiv_iframe.style.top = optionDiv.style.top;
		}

		list_activeInput = inputObj;
		optionDiv.onselectstart =  list_cancelEvent;
		currentListIndex++;
		if(list_cachedLists[paramToExternalFile][inputObj.value.toLowerCase()]){
			option_list_buildList(inputObj.value,paramToExternalFile,currentListIndex);
		}else{
			var tmpIndex=currentListIndex/1;
			optionDiv.innerHTML = '';
			var ajaxIndex = list_objects.length;
			list_objects[ajaxIndex] = new sack();
			var url = list_externalFile + '?' + paramToExternalFile + '=1&letters=' + inputObj.value.replace(" ","+");
			list_objects[ajaxIndex].requestFile = url;	// Specifying which file to get
			list_objects[ajaxIndex].onCompletion = function(){ option_list_showContent(ajaxIndex,inputObj,paramToExternalFile,tmpIndex); };	// Specify function that will be executed after file has been found
			list_objects[ajaxIndex].runAJAX();		// Execute AJAX function
		}


	}

	function option_keyNavigation(e)
	{
		if(document.all)e = event;

		if(!optionDiv)return;
		if(optionDiv.style.display=='none')return;

		if(e.keyCode==38){	// Up arrow
			if(!list_activeItem)return;
			if(list_activeItem && !list_activeItem.previousSibling)return;
			options_rollOverActiveItem(list_activeItem.previousSibling,true);
		}

		if(e.keyCode==40){	// Down arrow
			if(!list_activeItem){
				options_rollOverActiveItem(list_optionDivFirstItem,true);
			}else{
				if(!list_activeItem.nextSibling)return;
				options_rollOverActiveItem(list_activeItem.nextSibling,true);
			}
		}

		if(e.keyCode==13 || e.keyCode==9){	// Enter key or tab key
			if(list_activeItem && list_activeItem.className=='optionDivSelected')option_setValue(false,list_activeItem);
			if(e.keyCode==13)return false; else return true;
		}
		if(e.keyCode==27){	// Escape key
			options_hide();
		}
	}


	document.documentElement.onclick = autoHideList;

	function autoHideList(e)
	{
		if(document.all)e = event;

		if (e.target) source = e.target;
			else if (e.srcElement) source = e.srcElement;
			if (source.nodeType == 3) // defeat Safari bug
				source = source.parentNode;
		if(source.tagName.toLowerCase()!='input' && source.tagName.toLowerCase()!='textarea')options_hide();

	}