// ************** TYPE AHEAD SCRIPTS ************** //var typeAheadHiglightColor;var typeAheadNormalColor;var objOptions;var objSrchFld;var strMasterFldName;var KEYUP;var KEYDOWN;var KEYENTER;var KEYTAB;var Hcolor;var Ncolor;//Hcolor = "#" + document.forms[0].Hcolor.value;//Ncolor =  "#" + document.forms[0].Ncolor.value//typeAheadHiglightColor = Hcolor;//typeAheadNormalColor = Ncolor;KEYUP = 38;KEYDOWN = 40;KEYENTER = 13;KEYTAB = 9;function validateItemNoTypeAhead(){	var doc = document.forms[0];		strSelfVal = "";	strPrevOption = "";	if ( doc.Self ) 	{		strSelfVal = trimString ( getFieldValue(doc.Self) ) ; 	}		if ( strSelfVal == "Self"  )	{		strPrevOption = "0";		doc.PaymentType.value = "Credit Card";	}	else	{		strPrevOption = "";	}	if ( strPrevOption== "" ) 	{		var strValue = trimString ( getFieldValue(doc.PaymentTypeOptions) ) ; 		doc.PaymentType.value = strValue;	}	if ( checkRequiredFields( doc ) == false )     	return false     else     	return true}function cleanSrchText ( strToClean ){if ( strToClean == '' ) return strToCleanreturn strToClean.replace( /\*/g , '' ).replace( /\^/g , '' ).replace( /\(/g , '' ).replace( /\)/g , '' ).replace(/\./g , '' ).replace(/\s\s/g,'').replace(/\\/g , '' ).replace(/\[/g , '' ).replace(/\]/g  , '' ).replace( /\//g , '' )}function displayTypeAheadDiv ( flag ){	if ( flag )	{		objOptions.style.display = 'block';	}	else	{		objOptions.innerHTML = ''		objOptions.style.display = 'none'	}}function entrySelected(strSelectedText){		objSrchFld.value = strSelectedText		displayTypeAheadDiv ( false )		if ( objSrchFld.name == 'TopXPSearchField' )		{			var objFld = document.getElementById ( 'XPSrchQuery' )			objFld.value = strSelectedText//			var retvalue = ShowValuePicker('Buyer','frmBuyerSingleValuePicker',ContactDBPath,'BuyerInfo','vBuyerLookupByLastName','0~^1~^2~^3','6','','','')		}		else if ( objSrchFld.name == 'TopXPSearchField' || objSrchFld.name == 'XPSrchQuery' )		{			if ( objSrchFld.name == 'TopXPSearchField' ) document.forms[0].XPSrchQuery.value = objSrchFld.value//			doXPSearch();		}		else if ( objSrchFld.name.match(/^item/i) )		{			objSrchFld.value = objSrchFld.value.split ( ' - ' )[0]			getProductDetailsByItemNo( objSrchFld.name.replace( /itemNo/i , '' ) );		}		return true}function getSelectedSpanNum (div){	var count = -1;	var spans = div.getElementsByTagName("SPAN");	  	if (spans) 	{		for (var i = 0; i < spans.length; i++) 		{			count++;			if (spans[i].style.backgroundColor != div.style.backgroundColor)			{			return count;			}		}	}return -1;}function setSelectedSpan (div, spanNum ){	var thisSpan;	var spans = div.getElementsByTagName("SPAN");		if (spans) 	{		for (var i = 0; i < spans.length; i++) 		{    			if (i == spanNum) 			{					thisSpan = spans[i];								thisSpan.style.backgroundColor = typeAheadHiglightColor			} 			else			{				var thisUnSelSpan = spans[i];			thisUnSelSpan.style.backgroundColor = typeAheadNormalColor				}		}	}	return thisSpan;}function keyPressHandler(strSrchFldName,key){objSrchFld = document.getElementById ( strSrchFldName )var selNum = getSelectedSpanNum(objOptions);var selSpan = setSelectedSpan(objOptions, selNum);if ( !key || key == '' ){	if  ( window.event ) key = window.event.keyCode	else key = event.which}	if ( ( key == KEYENTER ) || ( key == KEYTAB ) )	{	if ( selSpan )		{		entrySelected(selSpan.firstChild.nodeValue );		}	}	else	{		if ( key == KEYUP )			{				if ( selNum == -1 )				{				var spans = objOptions.getElementsByTagName("SPAN");				selNum = spans.length				}			selSpan = setSelectedSpan ( objOptions, selNum - 1 );			}				if ( key == KEYDOWN )			{						selSpan = setSelectedSpan ( objOptions, selNum + 1 );			}	}}function showCreateDiv(divObj,posObj){	var x = posObj.offsetLeft;	var y = posObj.offsetTop + posObj.offsetHeight;	var parent = posObj;	while (parent.offsetParent) 	   {     	 parent = parent.offsetParent;	      x += parent.offsetLeft;     	 y += parent.offsetTop;	    }	divObj.style.display = 'block'	divObj.style.position = "absolute";	divObj.style.left = x+ "px";	divObj.style.top = y + "px";	divObj.style.zIndex=1000;}function returnEntries(strSrchFldName,key) {try{var KEYHOME = 36;var KEYEND = 35;var KEYLEFT = 37;var KEYRIGHT = 39;var KEYSPACE = 32;//----------- Below keys handled in keyPressHandler functionif ( ( key == KEYENTER ) || ( key == KEYTAB ) || ( key == KEYUP ) || ( key == KEYDOWN ) || ( key == KEYHOME ) || ( key == KEYEND ) || ( key == KEYLEFT ) || ( key == KEYRIGHT ) || ( key == KEYSPACE ) || ( key == 0 ) ) {	return ''}var doc = document.forms[0];//----------- Get handle to various field objects used in type aheadobjSrchFld = document.getElementById ( strSrchFldName )if ( strSrchFldName == 'TopXPSearchField' ){var objLastLength = document.getElementById ( 'XPSrchQueryLL' )var objLastIndex = document.getElementById ( 'XPSrchQueryLI' )var objMasterList = document.getElementById ( 'XPSrchQueryML' )}else{var objLastLength = document.getElementById ( strSrchFldName + 'LL' )var objLastIndex = document.getElementById ( strSrchFldName + 'LI' )var objMasterList = document.getElementById ( strSrchFldName + 'ML' )}var strEntered = objSrchFld.valuevar intCurLength = parseInt ( strEntered.toString().length , 10 )//----------- Do Type ahead only when..if ( intCurLength < 1  ){	objLastLength.value = ''	objLastIndex.value = ''	displayTypeAheadDiv ( false )	return ''}//----- Define search criteriavar strRegExp = eval ( "/^" + cleanSrchText ( strEntered ) + "/i" )//----- Get master list to search inif ( strSrchFldName.match ( /^itemno/i ) ){objMasterList.value = objMasterList.value.replace ( /~\^/g , ' - ' )}var strVals = objMasterList.valuevar arrVals = strVals.split('; ')arrVals = getUniqueValues ( arrVals )//----- get last search results if anyvar intLastIndex = parseInt ( objLastIndex.value , 10 )var intLastLength = parseInt ( objLastLength.value , 10 )objLastLength.value = intCurLength.toString()var strNewIndex = ''var arrValsDisp = new Array()var intArrIdx = 0//----- Define style of entriesvar strOptionSPAN = '<span id=lookupSpan style="background-color:' + typeAheadNormalColor + ';width:100%;cursor:pointer;clear:both;float:left;font-family:Verdana;font-size:10px;line-height: 15px;"  onMouseOver="javascript:setSelectedSpan(objOptions,-1);this.style.backgroundColor=\'' + typeAheadHiglightColor+'\'" onMouseOut="javascript:this.style.backgroundColor=\'' + typeAheadNormalColor + '\'" onclick="javascript:if(entrySelected(this.firstChild.nodeValue))doXPSearch();;">'//----- Search and get the entriesif ( objLastIndex.value == '' ) //first search{		for ( intCount = 0 ; intCount < arrVals.length ; intCount++ )	{	if ( cleanSrchText ( arrVals[intCount] ) .match( strRegExp ) )	{		if ( strNewIndex == '' ) strNewIndex = intCount.toString()		arrValsDisp[intArrIdx] = strOptionSPAN + arrVals[intCount]		intArrIdx++		}	}}else if ( intLastLength > intCurLength ) //backspace pressed	{		//Search index--		var continueLoop = true		var match = false		if ( intLastIndex > 0 )		{			for ( intCount = intLastIndex -1; ( intCount >= 0 && continueLoop ) ; intCount-- )			{				if ( cleanSrchText ( arrVals[intCount] ) .match( strRegExp ) )				{				match = true				strNewIndex = intCount.toString()				arrValsDisp[intArrIdx] = strOptionSPAN + arrVals[intCount]				intArrIdx++				}				else				{				if ( match )					{					arrValsDisp.reverse() //order to be reversed as we searched in index-- order					continueLoop = false					}				}			}		}		if ( strNewIndex == '' ) strNewIndex = intLastIndex				//Search index++		var continueLoop = true		var match = false		for ( intCount = intLastIndex ; ( intCount < arrVals.length && continueLoop ) ; intCount++ )		{			if ( cleanSrchText ( arrVals[intCount] ) .match( strRegExp ) )			{			match = true			arrValsDisp[intArrIdx] = strOptionSPAN + arrVals[intCount]			intArrIdx++			}			else			{			if ( match ) continueLoop = false			}					}	}else if ( intLastLength < intCurLength ) //new chars added	{		var continueLoop = true		var match = false		for ( intCount = intLastIndex ; ( intCount < arrVals.length && continueLoop ); intCount++ )		{			if ( cleanSrchText ( arrVals[intCount] ) .match( strRegExp ) )			{			if ( strNewIndex == '' ) strNewIndex = intCount.toString()			arrValsDisp[intArrIdx] = strOptionSPAN + arrVals[intCount]			intArrIdx++			}			else			{			if ( match ) continueLoop = false			}		}	}else if ( intLastLength == intCurLength ) return ''	//----- Display results	if ( strNewIndex.toString() == '' )	{		objLastLength.value = ''		objLastIndex.value = ''		displayTypeAheadDiv ( false )	}	else	{		objLastIndex.value = strNewIndex		arrValsDisp[0] = arrValsDisp[0].replace ( 'background-color:'+typeAheadNormalColor , 'background-color:' + typeAheadHiglightColor)		if ( strSrchFldName == 'XPSrchQuery' )			objOptions.style.width = '200px';		else if ( strSrchFldName == 'TopXPSearchField' )			objOptions.style.width = '500px';		else			objOptions.style.width = '500px';		//else if( strSrchFldName == 'Parish' ) objOptions.style.width = '450px';		//else if( strSrchFldName.match(/^itemno/i)){		//	objOptions.style.width = '350px';		//	objLastLength.value = ''		//	objLastIndex.value = ''		//	}		objOptions.innerHTML = arrValsDisp.join('</span>')		showCreateDiv ( objOptions , objSrchFld )			}}catch(e){alert(e);}}// ************** END OF TYPE AHEAD SCRIPTS ************** //// To remove duplicate entries in an arrayfunction getUniqueValues(mixedData){	var uniqueValuesList=new Array()		mixedData = mixedData.sort();	idx = 0;	prevValue = mixedData[0];	currValue = mixedData[0];	for (i=0; i<mixedData.length; i++)	{ 		currValue = mixedData[i];		if (currValue != prevValue)		{			idx++;			count=idx	 		}			uniqueValuesList[idx] = currValue;	prevValue = currValue;	}	return uniqueValuesList}
