// (c) Vitaliy Filippov 2011-2015 // @license MPL 2.0 http://www.mozilla.org/MPL/2.0/ // http://yourcmc.ru/wiki/SimpleAutocomplete var SimpleAutocomplete=function(b,a,c){if(typeof(b)=="string"){b=document.getElementById(b)}if(!c){c={}}this.options=c;this.input=b;this.a=a;this.b=c.multipleDelimiter;this.c=c.multipleListener;this.d=c.onChangeListener;this.e=c.emptyText;this.f=c.prompt;this.g=c.delay;this.h=c.moreMarker;this.i=c.idField;this.j=c.persist;this.k=c.useTab&&true;this.l=c.className||"hintLayer";if(this.i&&typeof(this.i)=="string"){this.i=document.getElementById(this.i)}if(this.h===undefined){this.h="#MORE"}if(this.g===undefined){this.g=300}this.m=0;this.n=null;this.o=[];this.p=[];this.q=0;this.r=-1;this.s=false;this.t=null;this.u()};SimpleAutocomplete.prototype.u=function(){var d=this.input;var a=SimpleAutocomplete.v;this.w=this.input.id+a.length;a.push(this);var c=this.x=document.createElement("div");c.className=this.l;if(!this.j){c.style.display="none";c.style.position="absolute";c.style.zIndex=1000;document.body.insertBefore(c,document.body.childNodes[0])}else{d.nextSibling?d.parentNode.insertBefore(c,d.nextSibling):d.parentNode.appendChild(c)}d.SimpleAutocomplete_input=this;c.SimpleAutocomplete_layer=this;if(typeof d.autocomplete!=="undefined"){d.autocomplete="off";addListener(window,"beforeunload",function(){d.autocomplete="on"})}var b=this;this.y("keydown",function(e){return b.z(e)});this.y("keyup",function(e){return b.aa(e)});this.y("change",function(){return b.onChange()});this.y("focus",function(){return b.ab()});this.y("blur",function(){return b.ac()});addListener(c,"mousedown",function(e){return b.ad(e)});this.onChange(true)};SimpleAutocomplete.prototype.replaceItems=function(c,a){if(!a){this.x.scrollTop=0;this.r=0;this.p=[];if(!c||c.length==0){if(this.e){this.x.innerHTML='
'+this.e+"
"}else{this.ae()}return}while(this.r'+this.f+"":"";this.af()}if(this.b){var e={};var b=this.input.value.split(this.b);for(var d=0;d=0){var d=this.am();if(d){d.className=this.p[this.r][2]?"hintDisabledItem":"hintItem"}}this.r=a;b.className="hintActiveItem";return false};SimpleAutocomplete.prototype.am=function(a){if(a==null){a=this.r}if(a<0){return null}return document.getElementById(this.w+"_item_"+this.r)};SimpleAutocomplete.prototype.selectItem=function(a){if(this.p[a][2]){return false}if(this.h&&this.p[a][1]==this.h){this.p.splice(a,1);var b=document.getElementById(this.w+"_item_"+a);b.parentNode.removeChild(b);this.m++;this.onChange(true);return}if(!this.b&&!this.c){this.input.value=this.p[a][1];if(this.i){this.i.value=this.p[a][3]}this.hide()}else{document.getElementById(this.w+"_check_"+a).checked=this.p[a][3]=!this.p[a][3];if(this.c&&!this.c(this,a,this.p[a])){return}this.an(a)}this.t=this.input.value;if(this.d){this.d(this,a,this.p[a])}};SimpleAutocomplete.prototype.an=function(c){var b=this.input.value.split(this.b);for(var d=0;d=0;d--){if(b[d]==this.p[c][1]){b.splice(d,1)}}this.input.value=b.join(this.b+" ")}else{var e={};for(var d=0;da){this.x.style.right=(a-b.left-this.input.offsetWidth)+"px";this.x.style.left=""}return true}};SimpleAutocomplete.prototype.ae=function(){this.s=true;this.hide()};SimpleAutocomplete.prototype.af=function(){this.s=false;if(this.ao){this.show()}};SimpleAutocomplete.prototype.ah=function(a){return stopEvent(a||window.event,false,true)};SimpleAutocomplete.prototype.ad=function(a){a=a||window.event;if(this.ao){this.q++}return stopEvent(a,true,false)};SimpleAutocomplete.prototype.ai=function(a){return this.al(a)};SimpleAutocomplete.prototype.aj=function(b,c){var a=parseInt(c.id.substr(this.w.length+6));this.selectItem(a);return true};SimpleAutocomplete.prototype.onChange=function(c){var b=this.input.value.trim();if(!c){this.m=0}if(b!=this.t||c){if(this.t!==null&&this.i){this.i.value=""}this.t=b;if(!this.g||c){this.a(this,b,this.m)}else{if(!this.n){var a=this;this.n=setTimeout(function(){a.a(a,a.t,a.m);a.n=null},this.g)}}}return true};SimpleAutocomplete.prototype.aa=function(a){a=a||window.event;if(a.keyCode==38||a.keyCode==40){this.show()}if(a.keyCode==38||a.keyCode==40||a.keyCode==10||a.keyCode==13||a.keyCode==9&&this.k){if(this.x.style.display==""){return stopEvent(a,true,true)}else{return true}}this.onChange();return true};SimpleAutocomplete.prototype.z=function(e){if(this.x.style.display=="none"){return true}e=e||window.event;if(e.keyCode==38){this.ak(-1)}else{if(e.keyCode==40){this.ak(1)}else{if(e.keyCode==10||e.keyCode==13||e.keyCode==9&&this.k){if(this.r>=0){this.selectItem(this.r)}return stopEvent(e,true,true)}else{if(e.keyCode==27){this.hide();return stopEvent(e,true,true)}else{return true}}}}if(this.r>=0){var f=this.am();var b=this.x;var a=getOffset(f).top+b.scrollTop-b.style.top.substr(0,b.style.top.length-2);var d=f.scrollHeight;if(a+d-b.offsetHeight>b.scrollTop){b.scrollTop=a+d-b.offsetHeight}else{if(a