/** * @author Gproxy Design Inc. * @copyright (c) 2009, Gproxy Design Inc. All rights reserved. * @version 2.5 * * Gproxy doesn't allow to copy or change this code without Gproxy authorization. * See http://www.gproxy.com/licenses/license01.pdf for the full license governing this code. */ // global variables var intQioListTotal = 0; var intQioListCurrent = -1; var intQioDelay = 500; var strQioSearchId = null; var strQioResultsId = null; var objQioSearchField = null; var objQioResultsDiv = null; /** * Sets the autcomplet functionality for the search field * @param {Object} strFieldId * @param {Object} strResultsId */ function qioSetAutoComplete(strFieldId, strResultsId, strSiteNumber){ // initialize vars strQioSearchId = "#" + strFieldId; strQioResultsId = "#" + strResultsId; // create the results div $j("body").append('
'); // register mostly used vars objQioSearchField = $j(strQioSearchId); objQioResultsDiv = $j(strQioResultsId); // reposition div qioRepositionResultsDiv(); // on blur listener objQioSearchField.blur(function(){ setTimeout("qioClearAutoComplete()", 200) }); // on key up listener objQioSearchField.keyup(function(e){ // get keyCode (window.event is for IE) var keyCode = e.keyCode || window.event.keyCode; var strLastVal = objQioSearchField.val(); // check an treat up and down arrows if (qioUpDown(keyCode)) { return; } // check for an ENTER or ESC if (keyCode == 13 || keyCode == 27) { qioClearAutoComplete(); return; } // if is text, call with delay setTimeout(function(){ qioAutoComplete(strLastVal, strSiteNumber) }, intQioDelay); }); } /** * Treat the auto-complete action (delayed function) * @param {Object} strLastValue * @param {Object} strSiteNumber */ function qioAutoComplete(strLastValue, strSiteNumber){ // get the field value var strPart = objQioSearchField.val(); // if it's empty clear the resuts box and return if (strPart == '') { qioClearAutoComplete(); return; } // if it's equal the value from the time of the call, allow if (strLastValue != strPart) { return; } qioGetResults(strPart, strSiteNumber); } /** * Clear auto complete box */ function qioClearAutoComplete(){ objQioResultsDiv.html(''); objQioResultsDiv.css("display", "none"); } /** * Reposition the results div accordingly to the search field */ function qioRepositionResultsDiv(){ // get the field position var objSearchFieldOffset = objQioSearchField.offset(); var intSearchFieldTop = objSearchFieldOffset.top; var intSearchFieldLeft = objSearchFieldOffset.left; // get the field size var intSearchFieldHeight = objQioSearchField.height(); var intSearchFieldWidth = objQioSearchField.width(); var intBodyHeigth = $j('body').height(); var intResultsDivHeight = objQioResultsDiv.height(); var intLimit = (intSearchFieldTop + intSearchFieldHeight + intResultsDivHeight + 4); if (intLimit > intBodyHeigth) { var intResultsDivTop = (intSearchFieldTop - intResultsDivHeight - 4); } else { var intResultsDivTop = intSearchFieldTop + intSearchFieldHeight + 4; } // apply the css styles - optimized for Firefox objQioResultsDiv.css("position", "absolute"); objQioResultsDiv.css("left", intSearchFieldLeft - 2); objQioResultsDiv.css("top", intResultsDivTop); objQioResultsDiv.css("width", intSearchFieldWidth + 200); } /** * Treat up and down key strokes defining the next selected element * @param {Object} keyCode */ function qioUpDown(keyCode){ if (keyCode == 40 || keyCode == 38) { if (keyCode == 38) { // keyUp if (intQioListCurrent == 0 || intQioListCurrent == -1) { intQioListCurrent = intQioListTotal - 1; } else { intQioListCurrent--; } } else { // keyDown if (intQioListCurrent == intQioListTotal - 1) { intQioListCurrent = 0; } else { intQioListCurrent++; } } // loop through each result div applying the correct style objQioResultsDiv.children().each(function(i){ if (i == intQioListCurrent) { objQioSearchField.val(this.childNodes[0].childNodes[0].nodeValue); this.className = "qio_selected"; } else { this.className = "qio_unselected"; } }); return true; } else { // reset intQioListCurrent = -1; return false; } } function qioGetResults(strSearch,strSiteNumber){ var src = '/app/site/hosting/scriptlet.nl?script=customscript_gpr_qio_getresults_ss&deploy=customdeploy_gpr_qio_getresults_ss&itemsearch=' + escape(strSearch) + '&resultvar=objQioResultsDiv&itemscountvar=intQioListTotal&sitenumber='+strSiteNumber; var scriptFile = document.createElement('script'); scriptFile.setAttribute('type', 'text/javascript'); if (scriptFile.readyState) { //IE scriptFile.onreadystatechange = function(){ if (scriptFile.readyState == "loaded" || scriptFile.readyState == "complete") { scriptFile.onreadystatechange = null; qioShowResults(); } }; } else { //Others scriptFile.onload = function(){ qioShowResults(); }; } document.getElementById("qio_search").style.background = "url(/site/addons/qio/qio_ajax_loader.gif) 100% 1px no-repeat"; scriptFile.setAttribute('src', src); document.getElementsByTagName('head')[0].appendChild(scriptFile); } function qioShowResults(){ qioRepositionResultsDiv(); document.getElementById("qio_search").style.background = "none"; if (intQioListTotal > 0) { objQioResultsDiv.css("display", "block"); // for all divs in results var objDivs = $j(strQioResultsId + " > div"); // on mouse over clean previous selected and set a new one objDivs.mouseover(function(){ objDivs.each(function(){ this.className = "qio_unselected"; }); this.className = "qio_selected"; }); // on click copy the result text to the search field and hide objDivs.click(function(){ objQioSearchField.val(this.childNodes[0].childNodes[0].nodeValue); document.forms['form_qio'].itemid.value = this.id; document.forms['form_qio'].buyid.value = this.id; qioClearAutoComplete(); }); } else { objQioResultsDiv.css("display", "block"); setTimeout("qioClearAutoComplete()",3000); } } function qioAddToCart(strFormId){ if (document.forms[strFormId].qty.value > 0) { if (document.forms[strFormId].itemid.value != '' && document.forms[strFormId].itemid.value != null) { document.forms[strFormId].submit(); }else{ alert('Please type the item SKU in the imput box.'); } } else { document.forms[strFormId].qty.value = '1'; alert('Invalid number (must be greater than 0).'); } }