/********************************/
/*******start config****************/
/*******************************/
//array with suggest texts
var opttext=new Array();

//id of the textbox element
var inputID="R1"
//max size of "selectbox"
var maxcount=8
/*******************************/
/**********end config***********/
/*******************************/

opttext.sort();
var scripting=false;
//initialize:
//for mozilla
if (window.captureEvents) {
	window.captureEvents(Event.LOAD)
	window.onload=suggestInput_init;
}
//for ie
document.onreadystatechange=ieInit;
function ieInit()
{
	if (document.readyState=="complete")
	{
		document.body.onload=function() {suggestInput_init()}
	}
}

var SIs
var SItxt

var newdiv = document.getElementById("divSuggest");

var globalN=0; //how much options scrolled up
//for ie
if (document.attachEvent)
document.attachEvent("onclick",hideSelect)
// for Mozilla
if (document.captureEvents) {
        document.captureEvents(Event.CLICK);
        document.onclick = hideSelect;
}
function hideSelect() {
        newdiv.style.display="none"
}
function suggestInput_init() {
        if (document.createElement("DIV")) {
                scripting=true;
                SIs=document.createElement("SELECT")
                SIs.onkeyup=function(e){if(!e) e=event; setInputValue(this.selectedIndex,e)}
                SIs.className="select_input"
                SIs.setAttribute("id","selectInput");
                SIs.style.position="absolute"
                SIs.style.top="-9999px"
                SIs.style.left="-9999px"
                SIs.style.visibility="hidden"
                document.body.appendChild(SIs)
                SItxt =document.getElementById(inputID);
                SItxt.setAttribute("autocomplete","OFF")
		SItxt.onkeyup=function(e){ if (!e) e=event; if (this.value.length > 1) Post_XMLHttpRequest(1, '/inc/ASP/pages/xrequest_fonctions.asp', 'step=listeReference&saisie='+this.value+'&keycode='+e.keyCode, loadSuggest);}
                //SItxt.focus()
                for (i=0;i<opttext.length;i++) {
                        o=document.createElement("OPTION");;
                        o.innerHTML=opttext[i];
                        SIs.appendChild(o)
                        SIs.style.visibility="visible"; // for Opera
                }
                
                newdiv.style.backgroundColor="#ffffff"
                newdiv.style.zIndex="100"
                newdiv.style.border="1px solid black"
                newdiv.style.display="none"
                buildDiv(0)
        }
}

// XMLHttpRequest : Récupération de la liste des références correspondant à la saisie et chargement dans le tableau
function loadSuggest(l_flow) {
	var l_keycode = 0;
	var l_list = '';
	opttext.length = 0;
	if (l_flow != '') {
		if (l_flow.indexOf(';') > -1) { 
			l_keycode = l_flow.substr(0, l_flow.indexOf(';'));
			l_list = l_flow.substring(l_flow.indexOf(';') + ';'.length, l_flow.length);
		} else { l_list = l_flow; }
		if (l_list.length > l_flow.indexOf(';') + ';'.length) { 
			var l_options = l_list.split("|");
			for (i=0 ; i<l_options.length ; i++) {
				opttext.length += 1;
				opttext[(opttext.length - 1)] = l_options[i];
			}
		}
	}
	showSelection(l_keycode);
}

function buildDiv(n) {
        if (n>SIs.childNodes.length) return false;
        for (i=0;i<newdiv.childNodes.length;i++)
        {newdiv.removeChild(newdiv.childNodes[i]);i--}
        if (n>0) {// insert top "..." - div
                d1=document.createElement("DIV");
                d1.id="lessDiv"
                d1.style.width="100%"
                d1.style.fontSize="0.8em"
                d1.onmouseover=function() {this.style.backgroundColor="#000099";this.style.color="#ffffff"}
                d1.onmouseout=function() {this.style.backgroundColor="#ffffff";this.style.color="#686868"}
                d1.onclick=function() {buildDiv(n-1);
                        d1.style.backgroundColor="#000099";d1.style.color="#ffffff"
                }
                d1.innerHTML="......";
                newdiv.appendChild(d1);
        }
        m=(maxcount<SIs.childNodes.length)?(maxcount):(SIs.childNodes.length)
        for(i=0;i<m;i++) {
                d=document.createElement("DIV")
                d.style.width="100%"
                d.style.fontSize="0.8em"
                d.onmouseover=function() {
                        this.style.backgroundColor="#000099";this.style.color="#ffffff";
                        SItxt.value=this.innerHTML
                }
                d.onmouseout=function() {this.style.backgroundColor="#ffffff";this.style.color="#686868"}
                d.onclick=function() {SItxt.value=this.innerHTML;newdiv.style.display="none"}
                try {d.innerHTML=SIs.childNodes[i+n].innerHTML;}
                catch(err) {}
                newdiv.appendChild(d)
        };
        globalN=n;
        if (SIs.childNodes.length-n>maxcount) {// insert bottom "..." - div
                d2=document.createElement("DIV");
                d2.id="moreDiv"
                d2.style.width="100%"
                d2.style.fontSize="0.8em"
                d2.onmouseover=function() {this.style.backgroundColor="#000099";this.style.color="#ffffff"}
                d2.onmouseout=function() {this.style.backgroundColor="#ffffff";this.style.color="#686868"}
                d2.onclick=function() {
                        buildDiv(n+1); d2.style.backgroundColor="#000099";d2.style.color="#ffffff"
                }
                d2.innerHTML="......"; d2.style.backgroundColor="#ffffff";d2.style.color="#686868"
                newdiv.appendChild(d2) }
}

function setInputValue(m,ev) {
        if (!scripting) return;
        isLess=(document.getElementById("lessDiv"))?(1):(0)
        if (m>globalN+maxcount+isLess+1) {m=globalN+maxcount;SIs.selectedIndex=m}
        if (m<isLess) {m=globalN-1;SIs.selectedIndex=globalN-1}
        a=SIs.childNodes[m].innerHTML
        SItxt.value=a;
        try {
                if (newdiv.childNodes[m-globalN+isLess]) {  if (newdiv.childNodes[m-globalN+isLess].id=="moreDiv") {  buildDiv(globalN+1);
                                newdiv.childNodes[maxcount].style.backgroundColor="#000099";
                                newdiv.childNodes[maxcount].style.color="#ffffff"
                                return
                        }
                }
        } catch (err) {}
        try {
                if (newdiv.childNodes[m-globalN+isLess]) {  if (newdiv.childNodes[m-globalN+isLess].id=="lessDiv") {  buildDiv(globalN-1);
                                isLess_new=(document.getElementById("lessDiv"))?(1):(0)
                                newdiv.childNodes[isLess_new].style.backgroundColor="#000099";
                                newdiv.childNodes[isLess_new].style.color="#ffffff"
                                return
                        }
                }
        } catch (err) {}
        try {
                for (i=0;i<newdiv.childNodes.length;i++)
                {
                        newdiv.childNodes[i].style.backgroundColor="#ffffff";
                        newdiv.childNodes[i].style.color="#686868";
                }
                newdiv.childNodes[m-globalN+isLess].style.backgroundColor="#000099";
                newdiv.childNodes[m-globalN+isLess].style.color="#ffffff"
        } catch(err) {};
        
        if ((ev.keyCode!=40) && (ev.keyCode!=38) && (ev.keyCode!=0)) // if not arrow down, arrow up or mouseclick
        {
                newdiv.style.display="none"
                SItxt.focus();
        }
}

function showSelection(evkeyCode) { 
        if (!scripting) return;
        if (evkeyCode==40) { //with arrow down comes into suggestion select
                if (SIs.childNodes.length>0)
                {
                        newdiv.childNodes[0].style.backgroundColor="#000099"
                        newdiv.childNodes[0].style.color="#ffffff"
                        SItxt.value=SIs.childNodes[0].innerHTML;
                        try {SIs.focus();} catch(err){}
                        SIs.childNodes[0].selected=true;
                }
               return            
	}
        
        for (i=0;i<SIs.childNodes.length;i++) {SIs.removeChild(SIs.childNodes[i]);i--}
        for(i=0;i<opttext.length;i++)
        {
		oOption = document.createElement("OPTION");
		SIs.appendChild(oOption)
		oOption.innerHTML = opttext[i];
        }
        if (SIs.childNodes.length>0)  {
                newdiv.style.display=""
                buildDiv(0)
        } else newdiv.style.display="none";
        SItxt.focus()
}
