/*****************************************************************************
'*                                                                           *
'* Fichier     : scripts/libAutoComplete.js                                  *
'* Application : Portail                                                     *
'* Société     : DoubleTrade                                                 *
'* Auteur      : http://www.sitepoint.com                                    *
'* Créé le     : 07 mars 2005                                                *
'* Date Modif  : Raison Modif                                                *
'*                                                                           *
'* Description :                                                             *
'*                                                                           *
'****************************************************************************/

var LAC_isIE = null;
var LAC_isMoz = null;

function LAC_Init(oTextbox) {
	if (LAC_isIE == null || LAC_isMoz == null) {
		LAC_isIE = (document.selection != null);
		LAC_isMoz = (oTextbox.setSelectionRange != null);	
	}
}

function LAC_textboxSelect (oTextbox, iStart, iEnd) {

   switch(arguments.length) {
       case 1:
           oTextbox.select();
           break;

       case 2:
           iEnd = oTextbox.value.length;
           /* falls through */
           
       case 3:          
           if (LAC_isIE) {
               var oRange = oTextbox.createTextRange();
               oRange.moveStart("character", iStart);
               oRange.moveEnd("character", -oTextbox.value.length + iEnd);      
               oRange.select();                                              
           } else if (LAC_isMoz){
               oTextbox.setSelectionRange(iStart, iEnd);
           }                    
   }

   oTextbox.focus();
}

function LAC_textboxReplaceSelect (oTextbox, sText) {

   if (LAC_isIE) {
       var oRange = document.selection.createRange();
       oRange.text = sText;
       oRange.collapse(true);
       oRange.select();                                
   } else if (LAC_isMoz) {
       var iStart = oTextbox.selectionStart;
       oTextbox.value = oTextbox.value.substring(0, iStart) + sText + oTextbox.value.substring(oTextbox.selectionEnd, oTextbox.value.length);
       oTextbox.setSelectionRange(iStart + sText.length, iStart + sText.length);
   }

   oTextbox.focus();
}

function LAC_autocompleteMatch (sText, arrValues) {

   for (var i=0; i < arrValues.length; i++) {
       if (arrValues[i].indexOf(sText) == 0) {
           return arrValues[i];
       }
   }

   return null;

}

function LAC_autocomplete(oTextbox, oEvent, arrValues) {
	LAC_Init(oTextbox);
	
	if (LAC_isIE || LAC_isMoz) {

		switch (oEvent.keyCode) {
		    case 38: //up arrow  
		    case 40: //down arrow
		    case 37: //left arrow
		    case 39: //right arrow
		    case 33: //page up  
		    case 34: //page down  
		    case 36: //home  
		    case 35: //end                  
		    case 13: //enter  
		    case 9: //tab  
		    case 27: //esc  
		    case 16: //shift  
		    case 17: //ctrl  
		    case 18: //alt  
		    case 20: //caps lock
		    case 8: //backspace  
		    case 46: //delete
		        return true;
		        break;

		    default:
		        LAC_textboxReplaceSelect(oTextbox, String.fromCharCode(LAC_isIE ? oEvent.keyCode : oEvent.charCode));
		        var iLen = oTextbox.value.length;

		        var sMatch = LAC_autocompleteMatch(oTextbox.value, arrValues);

		        if (sMatch != null) {
		            oTextbox.value = sMatch;
		            LAC_textboxSelect(oTextbox, iLen, oTextbox.value.length);
		        }  
			        
		        return false;
		}
	}
}
