	/************************************************************************************************************
	(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 minimumLettersBeforeLookup = 1;	// Number of letters entered before a lookup is performed.

	var ajax_list_externalFile = '/consumer/ajax/AjaxListLocations.aspx';
	var ajax_list_externalFileTreatment = '/consumer/ajax/AjaxListTreatments.aspx';
	
	var ajax_list_objects = new Array();
	var ajax_list_cachedLists = new Array();
	
	var autocomplete_cachedLists = new Array();
	var autocomplete_activeInput = false;
	var autocomplete_activeItem;
	var autocomplete_optionDivFirstItem = false;
	var autocomplete_currentLetters = new Array();
	var reva_optionDiv = false;	
	var reva_noresultDiv = false;

	var autocomplete_MSIE = false;
	if (navigator.userAgent.indexOf('MSIE') >= 0 && navigator.userAgent.indexOf('Opera') < 0) {
		autocomplete_MSIE = true;		
	}
	
	var currentListIndex = 0;
	
	function reva_getTopPos(inputObj)
	{		
	  var returnValue = inputObj.offsetTop;
	  while((inputObj = inputObj.offsetParent) != null){
	  	returnValue += inputObj.offsetTop;// +1;//(autocomplete_MSIE ? 0 : 1);
	  }
	  return returnValue;
	}
	function autocomplete_cancelEvent()
	{
		return false;
	}
	
	function reva_getLeftPos(inputObj)
	{
	  var returnValue = inputObj.offsetLeft;
	  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetLeft+1;//(autocomplete_MSIE ? 0 : 1);
	  
	  return returnValue;
	}
	
	function reva_options_hide()
	{
		if(reva_optionDiv)reva_optionDiv.style.display='none';	
		if(reva_noresultDiv)reva_noresultDiv.style.display = 'none';
	}
	
	function reva_option_setValue(e,inputObj) {

        if (!inputObj) inputObj = this;
        var tmpValue = inputObj.innerHTML;

        var temp2 = new Array();
        temp2 = inputObj.id.split(':');

        if (temp2.length == 1) {
            tmpValue = inputObj.id;
        }
        else {
            if (autocomplete_MSIE) {
                tmpValue = inputObj.innerText;
            }
            else {
                tmpValue = inputObj.textContent;
            }   
        }
        
        
        if (!tmpValue) tmpValue = inputObj.innerHTML;
        autocomplete_activeInput.value = tmpValue;
        if (document.getElementById(autocomplete_activeInput.name + '_hidden')) {
            document.getElementById(autocomplete_activeInput.name + '_hidden').value = inputObj.id;

            if (location.pathname == "/Moderator/SupplierSetupGeneral.aspx") {
                var temp = new Array();
                temp = inputObj.id.split('!');

                document.getElementById('TextBoxLocationID').value = temp[0];

            }
        }
        reva_options_hide();
	}
	
	function reva_options_rollOverActiveItem(item,fromKeyBoard)
	{
	    if (autocomplete_activeItem) autocomplete_activeItem.className = 'optionDiv';
	    
        item.className = 'optionDivSelected';
		
		autocomplete_activeItem = item;
		
		if(fromKeyBoard){
			if(autocomplete_activeItem.offsetTop>reva_optionDiv.offsetHeight){
				reva_optionDiv.scrollTop = autocomplete_activeItem.offsetTop - reva_optionDiv.offsetHeight + autocomplete_activeItem.offsetHeight + 2 ;
			}
			if(autocomplete_activeItem.offsetTop<reva_optionDiv.scrollTop)
			{
				reva_optionDiv.scrollTop = 0;	
			}
		}
	}
	
	function reva_option_list_buildList(letters,paramToExternalFile)
	{	
		reva_optionDiv.innerHTML = '';
		autocomplete_activeItem = false;
		if(autocomplete_cachedLists[paramToExternalFile][letters.toLowerCase()].length<=1){
			reva_options_hide();
			return;			
		}
		
		
		
		autocomplete_optionDivFirstItem = false;
		var optionsAdded = false;
		for(var no=0;no<autocomplete_cachedLists[paramToExternalFile][letters.toLowerCase()].length;no++){
			if(autocomplete_cachedLists[paramToExternalFile][letters.toLowerCase()][no].length==0)continue;
			optionsAdded = true;
			var div = document.createElement('DIV');
			var items = autocomplete_cachedLists[paramToExternalFile][letters.toLowerCase()][no].split(/###/gi);
			
			if(autocomplete_cachedLists[paramToExternalFile][letters.toLowerCase()].length==1 && autocomplete_activeInput.value == items[0]){
				reva_options_hide();
				return;						
			}
			
			
			div.innerHTML = items[items.length-1];
			div.id = items[0];
			div.className = 'optionDiv';
			div.onmouseover = function() { reva_options_rollOverActiveItem(this, false) }
						
			//div.onclick = reva_option_setValue;
			div.onclick = function(){
					reva_option_setValue(false,autocomplete_activeItem);
					try{document.getElementById("searchLink").focus();
					}catch(e){
					try {document.getElementById("mainSearchBox_ButtonFind").focus();} catch (ee) {	}
					}
				};
			if(!autocomplete_optionDivFirstItem)
				autocomplete_optionDivFirstItem = div;
				
			reva_optionDiv.appendChild(div);
		}	
		if(optionsAdded){
			reva_optionDiv.style.display='block';			
			reva_options_rollOverActiveItem(autocomplete_optionDivFirstItem,true);
		}
					
	}
	
	
	function reva_option_resize(inputObj)
	{
		reva_optionDiv.style.top = (reva_getTopPos(inputObj) + inputObj.offsetHeight + ajaxBox_offsetY) + 'px';
		reva_optionDiv.style.left = (reva_getLeftPos(inputObj) + ajaxBox_offsetX) + 'px';
		
	}
	
	function reva_showOptions(inputObj,paramToExternalFile,e) {

	    /*if(e.keyCode==13 || e.keyCode==9)
		{
			alert("t");
			if(inputObj.value != '')
			try{
				alert("it");
			document.getElementById("searchLink").focus();
			}catch(e)
			{
				try{
					alert("iit");
					document.getElementById("buttonAddTreatment").focus();
					//AddTreatment();
					}catch(e)
					{}
			}
			alert("ee");
			return;
			alert("e");
		}*/
		
		if(autocomplete_currentLetters[inputObj.name]==inputObj.value)return;
		if(!autocomplete_cachedLists[paramToExternalFile])autocomplete_cachedLists[paramToExternalFile] = new Array();
		autocomplete_currentLetters[inputObj.name] = inputObj.value;
		if(!reva_optionDiv){
			reva_optionDiv = document.createElement('DIV');
			reva_optionDiv.id = 'reva_listOfOptions';	
			document.body.appendChild(reva_optionDiv);
			
			
			var allInputs = document.getElementsByTagName('INPUT');
			for(var no=0;no<allInputs.length;no++){
				if(!allInputs[no].onkeyup)allInputs[no].onfocus = reva_options_hide;
			}			
			var allSelects = document.getElementsByTagName('SELECT');
			for(var no=0;no<allSelects.length;no++){
				allSelects[no].onfocus = reva_options_hide;
			}

			var oldonkeydown=document.body.onkeydown;
			if(typeof oldonkeydown!='function'){
				document.body.onkeydown=reva_option_keyNavigation;
			}else{
				document.body.onkeydown=function(){
					oldonkeydown();
				reva_option_keyNavigation() ;}
			}
			var oldonresize=document.body.onresize;
			if(typeof oldonresize!='function'){
				document.body.onresize=function() {reva_option_resize(inputObj); };
			}else{
				document.body.onresize=function(){oldonresize();
				reva_option_resize(inputObj) ;}
			}
				
		}
		
		if(inputObj.value.length<minimumLettersBeforeLookup){
			reva_options_hide();			
			if(reva_noresultDiv)
				reva_noresultDiv.style.display = 'none';
				
			return;
        }
        
        if (inputObj.value.length <= minimumLettersBeforeLookup) {
		    ajax_list_objects = new Array();
		}


		reva_optionDiv.style.width = '290px';
		reva_optionDiv.style.top = (reva_getTopPos(inputObj) + inputObj.offsetHeight + ajaxBox_offsetY + 5) + 'px';
		reva_optionDiv.style.left = (reva_getLeftPos(inputObj) + ajaxBox_offsetX - 12) + 'px';
		
		
		//create the no result div and iframe
		if ((paramToExternalFile == 'getTreatmentsByLetters' || paramToExternalFile == 'getTreatments2ByLetters') && !reva_noresultDiv)
		{
			reva_noresultDiv = document.createElement('DIV');
			reva_noresultDiv.id = 'reva_noResults';	
			reva_noresultDiv.innerHTML = "<span>You are looking for clinics and doctors</span>Please type the name of clinic/doctor or choose from the list below.";
			reva_noresultDiv.style.top = reva_optionDiv.style.top;//(reva_getTopPos(inputObj) + inputObj.offsetHeight + ajaxBox_offsetY + 3) + 'px';
			reva_noresultDiv.style.left = reva_optionDiv.style.left;//(reva_getLeftPos(inputObj) + ajaxBox_offsetX - 3) + 'px';
			document.body.appendChild(reva_noresultDiv);					
			
		}
		//hiding the no match div or iframe		
		if(reva_noresultDiv)
			reva_noresultDiv.style.display = 'none';
		
		autocomplete_activeInput = inputObj;
		reva_optionDiv.onselectstart =  autocomplete_cancelEvent;
		currentListIndex++;

			var tmpIndex=currentListIndex/1;
			reva_optionDiv.innerHTML = '';			
			
				    
			    if (paramToExternalFile == 'getTreatmentsByLetters') {

			        var ajaxIndex = ajax_list_objects.length;

			        if (inputObj.value.length >= 2) {
			            ajax_list_objects[ajaxIndex] = new sack();

			            var url = ajax_list_externalFileTreatment + '?letters=' + inputObj.value;

			            ajax_list_objects[ajaxIndex].requestFile = url; // Specifying which file to get
			            ajax_list_objects[ajaxIndex].onCompletion = function() {
			                ajax_option_list_showContent(ajaxIndex, inputObj, paramToExternalFile, tmpIndex);
			            };
			            // Specify function that will be executed after file has been found
			            ajax_list_objects[ajaxIndex].runAJAX(); 	// Execute AJAX function

			            

			        }
			    }
				else if (paramToExternalFile == 'getTreatments2ByLetters')
		        {
			        var tempList = '';
    			    if (inputObj.value != '')
			        {
			            for(_tempSelection in AutoCompleteTreatmentData)
			            {
			                if (inputObj.value.toLowerCase() == AutoCompleteTreatmentData[_tempSelection].text.substring(0,inputObj.value.length).toLowerCase())
			                {
			                    tempList += AutoCompleteTreatmentData[_tempSelection].value + "###" + AutoCompleteTreatmentData[_tempSelection].displaytext + '|';
			                }
			            }
			            
			            if (tempList == '')
			            {			               
						   if(reva_noresultDiv)
						   {
								reva_noresultDiv.innerHTML = "<span>We dont know that treatment</span>Enter a treatment e.g. 'Implants' or type 'Not Sure'.";
								reva_noresultDiv.style.display = 'block';
								
							}
							try{
								gvAjaxManager.logRequest('autocomplete=1&cid='+providersListParams.cid+'&letters='+inputObj.value);
							}catch(e)
							{} 
							return;
			            }
			                			        
			            autocomplete_cachedLists[paramToExternalFile][inputObj.value.toLowerCase()] = tempList.split('|');
		                reva_option_list_buildList(inputObj.value,paramToExternalFile);
			        }
			    }
			    else
			    {
			        var ajaxIndex = ajax_list_objects.length;
			        
    			    if (inputObj.value.length  == 2)
			        {   
			            ajax_list_objects[ajaxIndex] = new sack();

			            var ClinicTypeID = 0;

			            if (document.getElementById('treatment_hidden').value != "") {
			                ClinicTypeID = document.getElementById('treatment_hidden').value.split(':')[2];
			            }

			            var url = ajax_list_externalFile + '?cid=' + ClinicTypeID + '&dcid=' + providersListParams.dcid + '&fcid=' + providersListParams.fcid + '&letters=' + inputObj.value;
			            
	                    ajax_list_objects[ajaxIndex].requestFile = url;	// Specifying which file to get
	                    ajax_list_objects[ajaxIndex].onCompletion = function(){ 
	                                                            ajax_option_list_showContent(ajaxIndex,inputObj,paramToExternalFile,tmpIndex); 
	                                                        };	
	                                                        // Specify function that will be executed after file has been found
	                    ajax_list_objects[ajaxIndex].runAJAX();		// Execute AJAX function
	                
			        }
			        else if (inputObj.value.length  >= 2)
			        {
			            ajax_option_list_showContent(ajaxIndex,inputObj,paramToExternalFile,tmpIndex);
			        }
			        
			    }
		//}
	}
	
	function ajax_option_list_showContent(ajaxIndex, inputObj, paramToExternalFile, whichIndex)
	{
	    var counter = 0;
	    var CheckIfExistUndefined = 0;
     
        while (counter <= ajaxIndex -1)
        {
            if (ajax_list_objects[counter].responseStatus[1] != "OK")
            {
                CheckIfExistUndefined = 1;
            }
            counter = counter + 1;
        }
                
        if (CheckIfExistUndefined == 1)
        {
	        window.setTimeout(function () 
                                {
                                    ajax_option_list_showContent(ajaxIndex, inputObj, paramToExternalFile, whichIndex);
                                },300);
        }
        else
        {
            
	        if(whichIndex!=currentListIndex)return;

	        var tempList = '';

	        var reva_Match = false;

	        if (inputObj.value.length  >= 2 && ajax_list_objects != null)
	        {
                while (ajax_list_objects[ajaxIndex] == undefined && ajaxIndex >= 0)
                {
                    ajaxIndex = ajaxIndex - 1;
                }

                while (tempList == '' && ajaxIndex >= 0) {

                    if (ajax_list_objects[ajaxIndex].requestFile.indexOf("AjaxListTreatments") > 0 && paramToExternalFile == "getTreatmentsByLetters")
                    {
                        AutoCompleteCountryData = ajax_list_objects[ajaxIndex].response;
                        for (_tempSelection in AutoCompleteCountryData) {

                            if (AutoCompleteCountryData[_tempSelection].text.toLowerCase().indexOf(inputObj.value.toLowerCase()) >= 0) {
                                reva_Match = true;
                            }
                            tempList += AutoCompleteCountryData[_tempSelection].value + "###" + AutoCompleteCountryData[_tempSelection].displaytext + '|';
                        }
                    }

                    if (ajax_list_objects[ajaxIndex].requestFile.indexOf("AjaxListLocations") > 0 && paramToExternalFile == "getCountriesByLetters") 
                    {
                        AutoCompleteCountryData = ajax_list_objects[ajaxIndex].response;
                        for (_tempSelection in AutoCompleteCountryData) 
                        {
                            if (inputObj.value.toLowerCase() == AutoCompleteCountryData[_tempSelection].text.substring(0, inputObj.value.length).toLowerCase()) {
                            
                                tempList += AutoCompleteCountryData[_tempSelection].value + "###" + (AutoCompleteCountryData[_tempSelection].shortcode == "" ? "" : "<img src='/images/countries/" + AutoCompleteCountryData[_tempSelection].shortcode + ".png'> ") + '<b>' + AutoCompleteCountryData[_tempSelection].displaytext.substring(0, inputObj.value.length) + '</b>' + AutoCompleteCountryData[_tempSelection].displaytext.substring(inputObj.value.length) + '|';
                            }
                        }
                    }
                    
                    if (tempList == '') {

                        ajaxIndex = ajaxIndex - 1;
                        reva_Match = false;
                    }
                }

                if (paramToExternalFile == "getTreatmentsByLetters") {
                    if (reva_Match == false) {
                        tempList = inputObj.value + '###Search clinic or staff names for "<b>' + inputObj.value + '</b>"|' + tempList;
                    }
                    else {
                        tempList += inputObj.value + '###Search clinic or staff names for "<b>' + inputObj.value + '</b>"|';                    
                    }
                }
                
                if (tempList != '')
                {
                    autocomplete_cachedLists[paramToExternalFile][inputObj.value.toLowerCase()] = tempList.split('|');
	                reva_option_list_buildList(inputObj.value,paramToExternalFile);
	            }
	            else
	            {
	                if (reva_noresultDiv && paramToExternalFile == "getTreatmentsByLetters") 
	                {
	                    reva_noresultDiv.innerHTML = "<span>We dont know that treatment</span>Enter a treatment e.g. 'Implants' or type 'Not Sure'.";
	                    reva_noresultDiv.style.display = 'block';
	                }
	            }
            }
        }
	}
		
	function reva_option_keyNavigation(e)
	{
		
		if(document.all)e = event;
		if(!reva_optionDiv)
		{
			//alert("here2");
			return;
		}
		
		if(reva_optionDiv.style.display=='none')		
			return;
			
		if(e.keyCode==38){	// Up arrow
			if(!autocomplete_activeItem)return;
			if(autocomplete_activeItem && !autocomplete_activeItem.previousSibling)return;
			reva_options_rollOverActiveItem(autocomplete_activeItem.previousSibling,true);
		}
		
		if(e.keyCode==40){	// Down arrow
			if(!autocomplete_activeItem){
				reva_options_rollOverActiveItem(autocomplete_optionDivFirstItem,true);
			}else{
				if(!autocomplete_activeItem.nextSibling)return;
				reva_options_rollOverActiveItem(autocomplete_activeItem.nextSibling,true);
			}
		}
		if (e.keyCode == 13 || e.keyCode == 9) {	// Enter key or tab key
		    //if (autocomplete_activeItem && autocomplete_activeItem.className == 'optionDivSelected' && autocomplete_activeItem.id != 'reva_noResults') {
		    //    //reva_option_setValue(false, autocomplete_activeItem);
		    //}
			
			//if the no result div is displayed then must be search for supplier/staff name otherwise act as normal
		    if (!reva_noresultDiv || reva_noresultDiv.style.display == 'none') {
			    		            
			    if (autocomplete_activeItem && autocomplete_activeItem.className == 'optionDivSelected') 
				{					
						reva_option_setValue(false, autocomplete_activeItem);
				}
			}else
			{//better hide noresultdiv since this is a supplier/staff search since the other function won't
				reva_noresultDiv.style.display = 'none';
				reva_options_hide();
			}
			
		    if (e.keyCode == 13) {
		        try {
					
		            //if we are on the search page do the search
		            //document.getElementById("searchLink").focus();
		            //document.getElementById("searchLink").click();//won't work on a tag need to change to button
					$('input.button_search').click();										
					
		            //gvSearchFilter.doSearch();
		        } catch (e) {
		            try {
		                //ok we were not on the search page maybe index page try set focus
		                document.getElementById("mainSearchBox_ButtonFind").focus();
		                document.getElementById("mainSearchBox_ButtonFind").click();
		            }
		            catch (ee) {
		                try {
		                    //right not on either so maybe consultation form
		                    document.getElementById("buttonAddTreatment").focus();
		                    AddTreatment();
		                }
		                catch (eee) {
		                }
		            }
		        }
				
		        return false;
		    } else
		        return true;
		}
		
		if(e.keyCode==27){	// Escape key
			reva_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')reva_options_hide();
		
	}
	
	
	
