var LS_searchg; var LS_searchq; var LS_searcht; var LS_resultDiv; var LS_historyDiv; var LS_xmlHttp; var LS_timer; var LS_g = ''; var LS_q = ''; var LS_t = ''; var LS_disabled = true; var LS_hasRecord = false; var LS_visible = false; var LS_currRow = 0; var exclude_keyCode = new Array(9,13,16,17,18,19,20,27,33,34,35,36,37,38,39,40,45,46); //http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes var search_type = 'all'; var switched_tab = false; $(window).blur(function(){ switched_tab=true; }); function initLS(){ LS_searchq = document.getElementById("searchq"); LS_searchg = document.getElementById("searchg"); LS_searcht = document.getElementById("searcht"); LS_resultDiv = document.getElementById("searchres"); LS_historyDiv = document.getElementById("history"); if (LS_searchq){ var sq = $(LS_searchq); sq.unbind(); // unbind all event last attached if(window.location.pathname == '/' || window.location.pathname == '/index.php') sq.focus(); sq.bind('focus',function(e){ LS_disabled=false; if(LS_searchq.value == "") { doLS(); } }); sq.bind('blur',function(){LS_disabled=true; setTimeout("showHideLS('none')",250); switched_tab=false;}); sq.bind('keydown',function(e){switched_tab=false; return preventKeyDefault(e);}); sq.bind('keypress',function(e){ if ( e.which == 13 ){/*if(checkEnter())return false;*/} }); sq.bind('keyup',function(e){selectLS(e);}); //sq.bind('propertychange',doLS); // IE 10 has a bug making a text box disappear sq.bind('input',doLS); //firefox // this block of code is to replace on propertychange of IE /* if ( $.browser.msie ) { sq.bind('keyup', function(e) { if (e.keyCode) key = e.keyCode; else key = e.which; if (jQuery.inArray(key, exclude_keyCode) == -1) { // if the input key is not the exclude key doLS(); } }); }*/ //if (location.hash=='') LS_searchq.focus(); LS_disabled = false; } } function doLS(){ if (!LS_disabled && !switched_tab){ var g = getG(); var q = getQ(); var t = getT(); if (LS_g==g && LS_q==q && LS_q!="" && LS_t==t) return; LS_g = g; LS_q = q; LS_t = t; LS_hasRecord = false; if (LS_timer) clearTimeout(LS_timer); LS_timer = setTimeout("getLS()",350); } } function getLS(){ /*if (LS_q.length==0){ showHideLS('none'); return; }*/ LS_xmlHttp=getXmlHttpObject(); if (LS_xmlHttp==null) return; var url="/livesearch.php?g="+LS_g+"&q="+encodeURIComponent(LS_q)+"&header_type="+LS_t; url += "&rand=rand"+new Date().getTime(); LS_xmlHttp.onreadystatechange=drawLS; LS_xmlHttp.open("GET",url,true); LS_xmlHttp.send(null); } function drawLS(){ if (LS_xmlHttp.readyState==4 || LS_xmlHttp.readyState=="complete"){ if (LS_xmlHttp.responseText.trim().length==0){ LS_resultDiv.innerHTML=''; LS_hasRecord = false; showHideLS('none'); }else{ LS_resultDiv.innerHTML=LS_xmlHttp.responseText; LS_hasRecord = true; showHideLS('block'); //var searchres_left = $(LS_searchq).position().left + 8; /* if (search_type == 'product') { $("#searchres").css("left", '133px'); } else if (search_type == 'sechand') { $("#searchres").css("left", '133px'); } else { $("#searchres").css("left", '10px'); }*/ } } } function selectLS(e){ var key; if (e.keyCode) key = e.keyCode; else key = e.which; if (LS_hasRecord){ switch (key){ case 38: // Up if (LS_currRow==0){ showHideLS('none'); }else if (LS_currRow==1){ highlightLS(LS_currRow,'off','k'); resetQ(); LS_currRow = 0; }else{ highlightLS(LS_currRow-1,'on','k'); } break; case 40: // Down if (LS_visible){ highlightLS(LS_currRow+1,'on','k'); }else{ if (LS_g==getG() && LS_q==getQ()) showHideLS('block'); else doLS(); } break; case 13: // Enter showHideLS('none'); break; case 27: // Escape resetQ(); showHideLS('none'); break; } }else{ if (key==40){ // Down if (LS_g!=getG() || LS_q!=getQ()) doLS(); } } } function checkEnter(){ var obj = document.getElementById('ls_result'+LS_currRow); if(obj){ obj = $(obj); var href = obj.attr('href'); if(href) { location = href; return true; } } return false; } function highlightLS(id,status,device){ if (id>0){ var obj = document.getElementById('ls_result'+id); var fullname_obj = document.getElementById('ls_fullname'+id); if (obj){ //var obj_link = document.getElementById('ls_result_link'+id); if (status=='on'){ highlightLS(LS_currRow,'off'); if (device=='k'&&fullname_obj) setQ(getInnerText(fullname_obj)); obj.className = 'active'; // obj_link.className = 'active'; LS_currRow = id; }else{ obj.className = ''; } } } } function submitLS(obj, url){ showHideLS('none'); location.href = url; /* setQ(getInnerText(obj)); document.searchform.submit(); */ } function getG(){ return LS_searchg.value; } function getQ(){ return LS_searchq.value.ltrim(); } function getT(){ if(!LS_searcht) return ''; return LS_searcht.value.ltrim(); } function getInnerText(obj){ if (obj.innerText) return obj.innerText; else return obj.textContent; } function setQ(q){ LS_disabled = true; LS_searchq.value = q; LS_disabled = false; } function resetQ(){ LS_disabled = true; LS_searchq.value = LS_q; LS_disabled = false; } function showHideLS(s){ if (LS_disabled) s='none'; if (s=='block'){ LS_visible = true; }else{ highlightLS(LS_currRow,'off','k'); LS_visible = false; } //if (navigator.appVersion.indexOf('MSIE 6.0')>=0) showHideDummyFrame(s); LS_currRow = 0; LS_resultDiv.style.display = s; } // Fix IE div vs select box z-index problem function showHideDummyFrame(s){ df = document.getElementById('dummyFrame'); if (df){ df.style.width = LS_resultDiv.offsetWidth; df.style.height = LS_resultDiv.offsetHeight; df.style.top = LS_resultDiv.offsetTop; df.style.left = LS_resultDiv.offsetLeft; df.style.display = s; } } function preventKeyDefault(e){ if (e.keyCode==27||e.keyCode==38||e.keyCode==40){ if (e.preventDefault) e.preventDefault(); return false; } } function getXmlHttpObject(){ var xmlHttp=null; try{ // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); }catch (e){ // IE try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } function clearhistory() { document.cookie = "ui_recent_h=; expires=Sun, 03 Jun 2016 06:23:41 GMT; domain=.price.com.hk"; window.location.href = 'index.php'; } $(document).ready(initLS);