	/************************************************************************************************************
	(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 ajax_list_externalFile = 'service/ajaxkeywordlist';	// Path to external file
	var minimumLettersBeforeLookup = 1;	// Number of letters entered before a lookup is performed.
	
	var ajax_list_objects = new Array();
	var ajax_list_cachedLists = new Array();
	var ajax_list_activeInput = false;
	var ajax_list_activeItem;
	var ajax_list_optionDivFirstItem = false;
	var ajax_list_currentLetters = new Array();
	var ajax_optionDiv = false;
	var ajax_optionDiv_iframe = false;
	var autohide =0;
	var ajax_list_MSIE = false;
	var  check1=1;
	var is_initialize=0;
	if(navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Opera')<0)ajax_list_MSIE=true;
	
	var currentListIndex = 0;
	
	function ajax_getTopPos(inputObj)
	{
		
	  var returnValue = inputObj.offsetTop;
	  while((inputObj = inputObj.offsetParent) != null){
	  	returnValue += inputObj.offsetTop;
	  }
	  return returnValue;
	}
	function ajax_list_cancelEvent()
	{
		return false;
	}
	
	function ajax_getLeftPos(inputObj)
	{
	  var returnValue = inputObj.offsetLeft;
	  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetLeft;
	  
	  return returnValue;
	}
	
	function ajax_option_setValue(e,inputObj)
	{
		if(!inputObj)inputObj=this;
		var tmpValue = inputObj.innerHTML;
		if(ajax_list_MSIE)tmpValue = inputObj.innerText;else tmpValue = inputObj.textContent;
		if(!tmpValue)tmpValue = inputObj.innerHTML;
		ajax_list_activeInput.value = tmpValue;
		var items = inputObj.id.split(/###/gi);
		
		//if(document.getElementById(ajax_list_activeInput.name + '_hidden'))document.getElementById(ajax_list_activeInput.name + '_hidden').value = inputObj.id;
		if(document.getElementById(ajax_list_activeInput.name + '_hidden'))document.getElementById(ajax_list_activeInput.name + '_hidden').value = items[0];
		if(document.getElementById(ajax_list_activeInput.name + '_amount'))document.getElementById(ajax_list_activeInput.name + '_amount').value = items[1];
		if(document.getElementById(ajax_list_activeInput.name + '_status'))document.getElementById(ajax_list_activeInput.name + '_status').value = items[2];
		if(document.getElementById(ajax_list_activeInput.name + '_id'))document.getElementById(ajax_list_activeInput.name + '_id').value = items[3];
		if(document.getElementById(ajax_list_activeInput.name + '_isuserkeyword'))document.getElementById(ajax_list_activeInput.name + '_isuserkeyword').value = items[4];
		if(document.getElementById(ajax_list_activeInput.name + '_adv_keyword_status'))document.getElementById(ajax_list_activeInput.name + '_adv_keyword_status').value = items[5];
		
		showStatus();
		ajax_options_hide();
	}
	
	function ajax_options_hide()
	{
		if(ajax_optionDiv)ajax_optionDiv.style.display='none';	
		if(ajax_optionDiv_iframe)ajax_optionDiv_iframe.style.display='none';
	}

	function ajax_options_rollOverActiveItem(item,fromKeyBoard)
	{
		if(ajax_list_activeItem)ajax_list_activeItem.className='optionDiv';
		item.className='optionDivSelected';
		ajax_list_activeItem = item;
		
		if(fromKeyBoard){
			if(ajax_list_activeItem.offsetTop>ajax_optionDiv.offsetHeight){
				ajax_optionDiv.scrollTop = ajax_list_activeItem.offsetTop - ajax_optionDiv.offsetHeight + ajax_list_activeItem.offsetHeight + 2 ;
			}
			if(ajax_list_activeItem.offsetTop<ajax_optionDiv.scrollTop)
			{
				ajax_optionDiv.scrollTop = 0;	
			}
		}
	}
	
	function ajax_option_list_buildList(letters,paramToExternalFile,variableValue,variableValue2,match_string)
	{
		
		if(!match_string){
			match_string=letters;
		}

		ajax_optionDiv.innerHTML = '';
		ajax_list_activeItem = false;
		if(ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2][match_string.toLowerCase()].length<=1){
			ajax_options_hide();
			//return;
		}
		
		
		
		ajax_list_optionDivFirstItem = false;
		var optionsAdded = false;
		var showoption=0;

		if(document.getElementById(ajax_list_activeInput.name + '_hidden'))document.getElementById(ajax_list_activeInput.name + '_hidden').value ="";
		if(document.getElementById(ajax_list_activeInput.name + '_amount'))document.getElementById(ajax_list_activeInput.name + '_amount').value = ""; 
		if(document.getElementById(ajax_list_activeInput.name + '_status'))document.getElementById(ajax_list_activeInput.name + '_status').value = ""; 
		if(document.getElementById(ajax_list_activeInput.name + '_id'))document.getElementById(ajax_list_activeInput.name + '_id').value = ""; 
		if(document.getElementById(ajax_list_activeInput.name + '_isuserkeyword'))document.getElementById(ajax_list_activeInput.name + '_isuserkeyword').value = "";
		if(document.getElementById(ajax_list_activeInput.name + '_adv_keyword_status'))document.getElementById(ajax_list_activeInput.name + '_adv_keyword_status').value = "";
		showStatus();
		for(var no=0;no<ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2][match_string.toLowerCase()].length;no++){
		
			if(ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2][match_string.toLowerCase()][no].length==0)continue;
			optionsAdded = true;
			var div = document.createElement('DIV');
			//alert(ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2][match_string.toLowerCase()][no]);
			var items = ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2][match_string.toLowerCase()][no].split(/###/gi);
			
			if(ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2][match_string.toLowerCase()].length==1 && ajax_list_activeInput.value == items[0]){
				ajax_options_hide();
				return;
			}
			//alert(letters.toLowerCase()+"=="+items[items.length-1].toLowerCase()+"=="+items[items.length-1].toLowerCase().indexOf(letters.toLowerCase()));
			//alert(items[items.length-1].toLowerCase());
			//alert(items[items.length-1].toLowerCase().indexOf(match_string.toLowerCase()));
			if(items[items.length-1].toLowerCase().indexOf(letters.toLowerCase())==0){
				if(items[items.length-1].toLowerCase()==letters.toLowerCase()){
					if(document.getElementById(ajax_list_activeInput.name + '_hidden'))document.getElementById(ajax_list_activeInput.name + '_hidden').value = items[0]; 
					if(document.getElementById(ajax_list_activeInput.name + '_amount'))document.getElementById(ajax_list_activeInput.name + '_amount').value = items[1];
					if(document.getElementById(ajax_list_activeInput.name + '_status'))document.getElementById(ajax_list_activeInput.name + '_status').value = items[2];
					if(document.getElementById(ajax_list_activeInput.name + '_id'))document.getElementById(ajax_list_activeInput.name + '_id').value = items[3];
					if(document.getElementById(ajax_list_activeInput.name + '_isuserkeyword'))document.getElementById(ajax_list_activeInput.name + '_isuserkeyword').value = items[4];
					if(document.getElementById(ajax_list_activeInput.name + '_adv_keyword_status'))document.getElementById(ajax_list_activeInput.name + '_adv_keyword_status').value = items[5];
					showStatus();
				}
				//if(document.getElementById(ajax_list_activeInput.name + '_hidden'))document.getElementById(ajax_list_activeInput.name + '_hidden').value = inputObj.id; 
				//this condition  for if keywork are reserve//
				/*if(items[0]!='1'){
					showoption=1;
					div.innerHTML = items[items.length-1];
					div.id = items[0]+"###"+items[1]+"###"+items[2]+"###"+items[3];
					div.className='optionDiv';
					div.onmouseover = function(){ ajax_options_rollOverActiveItem(this,false) }
					div.onclick = ajax_option_setValue;
					if(!ajax_list_optionDivFirstItem){
						ajax_list_optionDivFirstItem = div;
					}
					ajax_optionDiv.appendChild(div);
				}*/
			}
		}
		if(autohide){
			showoption=0;
		}
		if(!showoption){
			ajax_options_hide();
				return;
		}
		if(optionsAdded){
			
			ajax_optionDiv.style.display='block';
			if(ajax_optionDiv_iframe)ajax_optionDiv_iframe.style.display='';
			//ajax_options_rollOverActiveItem(ajax_list_optionDivFirstItem,true);
		}
			
	}
	
	function ajax_option_list_showContent(ajaxIndex,letters,paramToExternalFile,variableValue,variableValue2,whichIndex,inputObj)
	{
		
		if(whichIndex!=currentListIndex){
			check1=1;
			//alert(2);
			return;
		}else{
			//var letters1 = inputObj.value;
			var content = ajax_list_objects[ajaxIndex].response;
			var elements = content.split('|');

			ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2][letters.toLowerCase()] = elements;
			ajax_option_list_buildList(letters,paramToExternalFile,variableValue,variableValue2);
			
			var match_string="";
			for(i=0;i<inputObj.value.toLowerCase().length;i++){

				 match_string +=inputObj.value.toLowerCase().charAt(i);
				//alert(match_string)
					//alert(ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2][match_string]);
				if(ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2][match_string]){
					ajax_option_list_buildList(inputObj.value,paramToExternalFile,variableValue,variableValue2,match_string);
				}
			}



			check1=1;
		}

	}
	
	function ajax_option_resize(inputObj)
	{
		ajax_optionDiv.style.top = (ajax_getTopPos(inputObj) + inputObj.offsetHeight + ajaxBox_offsetY) + 'px';
		ajax_optionDiv.style.left = (ajax_getLeftPos(inputObj) + ajaxBox_offsetX) + 'px';
		if(ajax_optionDiv_iframe){
			ajax_optionDiv_iframe.style.left = ajax_optionDiv.style.left;
			ajax_optionDiv_iframe.style.top = ajax_optionDiv.style.top;
		}
	}
	function list_initialize(paramToExternalFile,variableValue,variableValue2){
		
		/*for (var p in Array.prototype){
			ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2][p] = undefined;			
		}
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['concat'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['every'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['filter'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['join'] = undefined;		
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['map'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['pop'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['push'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['reverse'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['shift'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['slice'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['splice'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['some'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['unshift'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['sort'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['watch'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['unwatch'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['constructor'] = undefined;
		ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]['reduce'] = undefined;*/
	}
	function ajax_showOptions(inputObj,paramToExternalFile,e,variableName,variableValue,autoHide)
	{
		var variableName2='';
		var variableValue2='';
		var Abstract = new Object();

			
		autohide=autoHide;

		if(e.keyCode==40 ||e.keyCode==38 ||e.keyCode==13 || e.keyCode==9)
		{
			return;
		}
		//if(ajax_list_currentLetters[inputObj.name]==inputObj.value)return;
		if(!ajax_list_cachedLists[paramToExternalFile]){
			ajax_list_cachedLists[paramToExternalFile]= new Array();
		}
		if(!ajax_list_cachedLists[paramToExternalFile][variableValue]){
			ajax_list_cachedLists[paramToExternalFile][variableValue]= new Array();
		}
		if(!ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2]){
			ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2] = new Array();
		}
		//showStatus();
		ajax_list_currentLetters[inputObj.name] = inputObj.value;
		if(!ajax_optionDiv){
			ajax_optionDiv = document.createElement('DIV');
			ajax_optionDiv.id = 'ajax_listOfOptions';
			document.body.appendChild(ajax_optionDiv);
			if(ajax_list_MSIE){
				ajax_optionDiv_iframe = document.createElement('IFRAME');
				ajax_optionDiv_iframe.border='0';
				ajax_optionDiv_iframe.style.width = ajax_optionDiv.clientWidth + 'px';
				ajax_optionDiv_iframe.style.height = ajax_optionDiv.clientHeight + 'px';
				ajax_optionDiv_iframe.id = 'ajax_listOfOptions_iframe';
				document.body.appendChild(ajax_optionDiv_iframe);
			}
			var allInputs = document.getElementsByTagName('INPUT');
			for(var no=0;no<allInputs.length;no++){
				if(!allInputs[no].onkeyup)allInputs[no].onfocus = ajax_options_hide;
			}
			var allSelects = document.getElementsByTagName('SELECT');
			for(var no=0;no<allSelects.length;no++){
				allSelects[no].onfocus = ajax_options_hide;
			}

			var oldonkeydown=document.body.onkeydown;
			if(typeof oldonkeydown!='function'){
				document.body.onkeydown=ajax_option_keyNavigation;
			}else{
				document.body.onkeydown=function(){
				oldonkeydown();
				ajax_option_keyNavigation() ;}
			}
			var oldonresize=document.body.onresize;
			if(typeof oldonresize!='function'){
				document.body.onresize=function() {ajax_option_resize(inputObj); };
			}else{
				document.body.onresize=function(){oldonresize();
				ajax_option_resize(inputObj) ;}
			}
		}

		if(inputObj.value.length<minimumLettersBeforeLookup){
			ajax_options_hide();
			return;
		}

		ajax_optionDiv.style.top = (ajax_getTopPos(inputObj) + inputObj.offsetHeight + ajaxBox_offsetY) + 'px';
		ajax_optionDiv.style.left = (ajax_getLeftPos(inputObj) + ajaxBox_offsetX) + 'px';
		if(ajax_optionDiv_iframe){
			ajax_optionDiv_iframe.style.left = ajax_optionDiv.style.left;
			ajax_optionDiv_iframe.style.top = ajax_optionDiv.style.top;			
		}
		
		ajax_list_activeInput = inputObj;
		ajax_optionDiv.onselectstart =  ajax_list_cancelEvent;

		var check=1;
		var match_string='';
		
		if(!is_initialize){
			list_initialize(paramToExternalFile,variableValue,variableValue2);
			is_initialize=1;
		}
		var dd = ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2][inputObj.value];
		
		if(dd) {
			
			dd=dd.toString();				
			if(dd.indexOf('function')==0) {
				ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2][inputObj.value] = undefined;
			}
			
		}
		if(check1){
			
			check1=0;
			
		for(i=0;i<inputObj.value.length;i++){

			match_string +=inputObj.value.toLowerCase().charAt(i);
			match_string = match_string.toString();
			
			
			/*
			 * function join() {
    [native code]
}
			 */
			//alert(ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2][match_string]);
			if(ajax_list_cachedLists[paramToExternalFile][variableValue][variableValue2][match_string]){
				
					ajax_option_list_buildList(inputObj.value,paramToExternalFile,variableValue,variableValue2,match_string);
				check=0;
				check1=1;
			}
		}

		ajax_options_hide();// for hiding the pre suggestion box

		if(check ){
			currentListIndex++;
			var tmpIndex=currentListIndex/1;
			ajax_optionDiv.innerHTML = '';
			var ajaxIndex = ajax_list_objects.length;
		
			ajax_list_objects[ajaxIndex] = new sack();
			var url = ajax_list_externalFile + '?' + paramToExternalFile + '=1&letters=' + inputObj.value.replace(" ","+");
			ajax_list_objects[ajaxIndex].requestFile = url;	// Specifying which file to get
			ajax_list_objects[ajaxIndex].setVar(variableName,variableValue);
			//ajax_list_objects[ajaxIndex].setVar(variableName2,variableValue2);
			var input_letters=inputObj.value;
			ajax_list_objects[ajaxIndex].onCompletion = function(){ ajax_option_list_showContent(ajaxIndex,input_letters,paramToExternalFile,variableValue,variableValue2,tmpIndex,inputObj); };	// Specify function that will be executed after file has been found
			ajax_list_objects[ajaxIndex].runAJAX();		// Execute AJAX function		
		}
	}
	}

	function ajax_option_keyNavigation(e)
	{
		if(document.all)e = event;
		
		if(!ajax_optionDiv)return;
		if(ajax_optionDiv.style.display=='none')return;
		
		if(e.keyCode==38){	// Up arrow
			if(!ajax_list_activeItem)return;
			if(ajax_list_activeItem && !ajax_list_activeItem.previousSibling)return;
			ajax_options_rollOverActiveItem(ajax_list_activeItem.previousSibling,true);
		}
		
		if(e.keyCode==40){	// Down arrow
			if(!ajax_list_activeItem){
				ajax_options_rollOverActiveItem(ajax_list_optionDivFirstItem,true);
			}else{
				if(!ajax_list_activeItem.nextSibling)return;
				ajax_options_rollOverActiveItem(ajax_list_activeItem.nextSibling,true);
			}
		}
		
		if(e.keyCode==13 || e.keyCode==9){	// Enter key or tab key
			if(ajax_list_activeItem && ajax_list_activeItem.className=='optionDivSelected')ajax_option_setValue(false,ajax_list_activeItem);
			if(e.keyCode==13)return false; else return true;
		}
		if(e.keyCode==27){	// Escape key
			ajax_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')ajax_options_hide();
		
	}
