/***
 * This javascript file contains function which is used to get Auto suggest drop down search result
 * @author     		MediTab Software Inc. 
 * @copyright  		1997-2005 The MediTab Software Inc.
 * @license    		http://www.php.net/license/3_0.txt  PHP License 3.0
 * @version   	 	Release: 1.0
 * @PHP version  	5.x
*/	

	var medBox_offsetX = 0;
	var medBox_offsetY = 0;
	var med_list_externalFile = 'index.php?file=med_common_suggest';	// Path to external file
	var minimumLettersBeforeLookup = 1;	// Number of letters entered before a lookup is performed.
	
	var med_list_objects = new Array();
	var med_list_cachedLists = new Array();
	var med_list_activeInput = false;
	var med_multi_suggest = false;
	var med_list_activeItem;
	var med_suggest_hidden = null;
	var med_callback_function	=	null;
	var med_list_optionDivFirstItem = true;
	var med_list_currentLetters = new Array();
	var med_optionDiv = false;
	var med_optionDiv_iframe = false;

	var med_list_MSIE = false;
	if(navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Opera')<0)med_list_MSIE=true;
	
	var currentListIndex = 0;
	
	function med_getTopPos(inputObj)
	{
		
	  var returnValue = inputObj.offsetTop;
	  while((inputObj = inputObj.offsetParent) != null){
	  	returnValue += inputObj.offsetTop;
	  }
	  return returnValue;
	}
	function med_list_cancelEvent()
	{
		return false;
	}
	
	function med_getLeftPos(inputObj)
	{
	  var returnValue = inputObj.offsetLeft;

	  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetLeft;
	  
	  return returnValue;
	}
	
	function med_option_setValue(e,inputObj)
	{
		if(!inputObj)inputObj=this;
		var tmpValue = inputObj.innerHTML;
		

		if(med_list_MSIE)tmpValue = inputObj.innerText;else tmpValue = inputObj.textContent;
		if(!tmpValue)tmpValue = inputObj.innerHTML;
		
		var strPosSemiColon	=	med_list_activeInput.value.lastIndexOf(";");
		var strOldValue = "";
		
		//If Semicolon found, then remove all after last semicolon
		if(med_multi_suggest)
		{
			if(strPosSemiColon > 0)
				tmpValue	=	med_list_activeInput.value.substring(0,strPosSemiColon+1)+tmpValue;
		
			tmpValue	=	tmpValue+"; ";
		}
		
		//Define the Fix maxlength
		if(!med_multi_suggest && med_suggest_hidden!=null)
			med_list_activeInput.maxLength = tmpValue.length;
			
		//Assign text value to textbox
		med_list_activeInput.value = tmpValue;
		
		//Assign ClientId to Hidden variable
		if(med_suggest_hidden!=null)
		{
			if(med_multi_suggest)
			{
				if(getElement(med_suggest_hidden).value != "")
					getElement(med_suggest_hidden).value = getElement(med_suggest_hidden).value+","+inputObj.id;
			}
			else
			{
					getElement(med_suggest_hidden).value = inputObj.id;
			}
		}
		if(med_callback_function!=null)
			eval(window.med_callback_function);
		med_options_hide();
	}
	
	function med_options_hide()
	{
		if(med_optionDiv)med_optionDiv.style.display='none';	
		if(med_optionDiv_iframe)med_optionDiv_iframe.style.display='none';
	}

	function med_options_rollOverActiveItem(item,fromKeyBoard)
	{
		if(med_list_activeItem)med_list_activeItem.className='optionDiv';
		item.className='optionDivSelected';
		med_list_activeItem = item;
		
		if(fromKeyBoard){
			if(med_list_activeItem.offsetTop>med_optionDiv.offsetHeight){
				med_optionDiv.scrollTop = med_list_activeItem.offsetTop - med_optionDiv.offsetHeight + med_list_activeItem.offsetHeight + 2 ;
			}
			if(med_list_activeItem.offsetTop<med_optionDiv.scrollTop)
			{
				med_optionDiv.scrollTop = 0;	
			}
		}
	}
	
	function med_option_list_buildList(letters,paramToExternalFile)
	{
		
		med_optionDiv.innerHTML = '';
		med_list_activeItem = false;
		if(med_list_cachedLists[paramToExternalFile][letters.toLowerCase()].length<=1){
			med_options_hide();
			return;			
		}
		
		
		
		med_list_optionDivFirstItem = false;
		var optionsAdded = false;
		for(var no=0;no<med_list_cachedLists[paramToExternalFile][letters.toLowerCase()].length;no++){
			if(med_list_cachedLists[paramToExternalFile][letters.toLowerCase()][no].length==0)continue;
			optionsAdded = true;
			var div = document.createElement('DIV');
			var items = med_list_cachedLists[paramToExternalFile][letters.toLowerCase()][no].split(/###/gi);
			
			if(med_list_cachedLists[paramToExternalFile][letters.toLowerCase()].length==1 && med_list_activeInput.value == items[0]){
				med_options_hide();
				return;						
			}
			
			
			div.innerHTML = items[items.length-1];
			div.id = items[0];
			div.className='optionDiv';
			div.onmouseover = function(){ med_options_rollOverActiveItem(this,false) }
			div.onclick = med_option_setValue;
			if(!med_list_optionDivFirstItem)med_list_optionDivFirstItem = div;
			med_optionDiv.appendChild(div);
		}	
		if(optionsAdded){
			med_optionDiv.style.display='block';
			if(med_optionDiv_iframe)med_optionDiv_iframe.style.display='';
			med_options_rollOverActiveItem(med_list_optionDivFirstItem,true);
		}
					
	}
	
	function med_option_list_showContent(medIndex,inputObj,paramToExternalFile,whichIndex)
	{
		if(whichIndex!=currentListIndex)return;
		var letters = inputObj.value;
		var content = med_list_objects[medIndex].response;
		var elements = content.split('|');
		med_list_cachedLists[paramToExternalFile][letters.toLowerCase()] = elements;
		med_option_list_buildList(letters,paramToExternalFile);
		
	}
	
	function med_option_resize(inputObj)
	{
		med_optionDiv.style.top = (med_getTopPos(inputObj) + inputObj.offsetHeight + medBox_offsetY) + 'px';
		med_optionDiv.style.left = (med_getLeftPos(inputObj) + medBox_offsetX) + 'px';
		if(med_optionDiv_iframe){
			med_optionDiv_iframe.style.left = med_optionDiv.style.left;
			med_optionDiv_iframe.style.top = med_optionDiv.style.top;			
		}		
		
	}
	
	function med_showOptions(inputObj,paramToExternalFile,e,strHiddenControl,strCallBackFunName,blnMultiple)
	{
	
		med_suggest_hidden		=	strHiddenControl;
		med_callback_function	=	strCallBackFunName;
		if(blnMultiple != undefined && blnMultiple != null)
			med_multi_suggest		=	blnMultiple;
		
		if(e.keyCode==13 || e.keyCode==9)return;
		if(med_list_currentLetters[inputObj.name]==inputObj.value)return;
		if(!med_list_cachedLists[paramToExternalFile])med_list_cachedLists[paramToExternalFile] = new Array();
		med_list_currentLetters[inputObj.name] = inputObj.value;
		if(!med_optionDiv){
			med_optionDiv = document.createElement('DIV');
			med_optionDiv.id = 'med_listOfOptions';	
			document.body.appendChild(med_optionDiv);
			
			if(med_list_MSIE){
				med_optionDiv_iframe = document.createElement('IFRAME');
				med_optionDiv_iframe.border='0';
				med_optionDiv_iframe.style.width = med_optionDiv.clientWidth + 'px';
				med_optionDiv_iframe.style.height = med_optionDiv.clientHeight + 'px';
				med_optionDiv_iframe.id = 'med_listOfOptions_iframe';
				
				document.body.appendChild(med_optionDiv_iframe);
			}
			
			var allInputs = document.getElementsByTagName('INPUT');
			for(var no=0;no<allInputs.length;no++){
				if(!allInputs[no].onkeyup)allInputs[no].onfocus = med_options_hide;
			}			
			var allSelects = document.getElementsByTagName('SELECT');
			for(var no=0;no<allSelects.length;no++){
				allSelects[no].onfocus = med_options_hide;
			}

			var oldonkeydown=document.body.onkeydown;
			if(typeof oldonkeydown!='function'){
				document.body.onkeydown=med_option_keyNavigation;
			}else{
				document.body.onkeydown=function(){
					oldonkeydown();
				med_option_keyNavigation() ;}
			}
			var oldonresize=document.body.onresize;
			if(typeof oldonresize!='function'){
				document.body.onresize=function() {med_option_resize(inputObj); };
			}else{
				document.body.onresize=function(){oldonresize();
				med_option_resize(inputObj) ;}
			}
				
		}
		
		if(inputObj.value.length<minimumLettersBeforeLookup){
			med_options_hide();
			return;
		}
				

		med_optionDiv.style.top = (med_getTopPos(inputObj) + inputObj.offsetHeight + medBox_offsetY) + 'px';
		med_optionDiv.style.left = (med_getLeftPos(inputObj) + medBox_offsetX) + 'px';
		if(med_optionDiv_iframe){
			med_optionDiv_iframe.style.left = med_optionDiv.style.left;
			med_optionDiv_iframe.style.top = med_optionDiv.style.top;			
		}
		
		med_list_activeInput = inputObj;
		med_optionDiv.onselectstart =  med_list_cancelEvent;
		currentListIndex++;
		if(med_list_cachedLists[paramToExternalFile][inputObj.value.toLowerCase()]){
			med_option_list_buildList(inputObj.value,paramToExternalFile,currentListIndex);			
		}else{
			var tmpIndex=currentListIndex/1;
			med_optionDiv.innerHTML = '';
			var medIndex = med_list_objects.length;
			med_list_objects[medIndex] = new sack();
			
			strContent = new String(getElement(inputObj.id).value);
			strPosSemiColon	=	strContent.lastIndexOf(";");
			if(strPosSemiColon > 0 && med_multi_suggest)
			{
				strKeyWord = inputObj.value.substring(strPosSemiColon+1,inputObj.value.length)
			}
			else
			{
				strKeyWord = inputObj.value;
			}
		
			var url = med_list_externalFile + '&' + paramToExternalFile + '=1&suggest_keyword=' + Trim(strKeyWord).replace(" ","+");
			
			med_list_objects[medIndex].requestFile = url;	// Specifying which file to get
			med_list_objects[medIndex].onCompletion = function(){ med_option_list_showContent(medIndex,inputObj,paramToExternalFile,tmpIndex); };	// Specify function that will be executed after file has been found
			med_list_objects[medIndex].runAJAX();		// Execute med function		
		}
		
			
	}
	
	function med_option_keyNavigation(e)
	{
		if(document.all)e = event;
		
		if(!med_optionDiv)return;
		if(med_optionDiv.style.display=='none')return;
		
		if(e.keyCode==38){	// Up arrow
			if(!med_list_activeItem)return;
			if(med_list_activeItem && !med_list_activeItem.previousSibling)return;
			med_options_rollOverActiveItem(med_list_activeItem.previousSibling,true);
		}
		
		if(e.keyCode==40){	// Down arrow
			if(!med_list_activeItem){
				med_options_rollOverActiveItem(med_list_optionDivFirstItem,true);
			}else{
				if(!med_list_activeItem.nextSibling)return;
				med_options_rollOverActiveItem(med_list_activeItem.nextSibling,true);
			}
		}
		
		if(e.keyCode==13 || e.keyCode==9){	// Enter key or tab key
			if(med_list_activeItem && med_list_activeItem.className=='optionDivSelected')med_option_setValue(false,med_list_activeItem);
			if(e.keyCode==13)return false; else return true;
		}
		if(e.keyCode==27){	// Escape key
			med_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')med_options_hide();
		
	}
