
var isSafari=0;function addListner(keywordobj,discountobj){if(discountobj!=null)
var oTextbox=new AutoSuggestControl(document.getElementById("items"),new MakeItemSuggestions(keywordobj,discountobj));else
var oTextbox=new AutoSuggestControl(document.getElementById("items"),new MakeItemSuggestions(keywordobj,null));isSafari=navigator.userAgent.indexOf("Safari");}
var INPUT_NAME_PREFIX='items';var TABLE_NAME='itemTable';var ROW_BASE=1;var hasLoaded=false;function MakeItemSuggestions(keywordobj,discountobj){keywordArray=new Array();if(keywordobj!=null){for(var i=0;i<keywordobj.length;i++){keywordArray.push(keywordobj[i].keyword_name);}}
this.keywords=keywordArray;this.discounts=discountobj;}
MakeItemSuggestions.prototype.requestSuggestions=function(oAutoSuggestControl,bTypeAhead){var keysarray=searchjsonobj(this.keywords,this.discounts,oAutoSuggestControl.textbox.value);oAutoSuggestControl.autosuggest(keysarray,false);};function AutoSuggestControl(oTextbox,oProvider){this.cur=-1;this.layer=null;this.provider=oProvider;this.textbox=oTextbox;this.keywordHolder=document.getElementById('keywordPlaceHolder');this.priceHolder=document.getElementById('keywordPricePlaceHolder');this.discountIdHolder=document.getElementById('discountIdHolder');this.init();}
AutoSuggestControl.prototype.autosuggest=function(aSuggestions,bTypeAhead){try{if(aSuggestions!=null){if(aSuggestions.length>0){if(bTypeAhead){this.typeAhead(aSuggestions[0].substring(0,aSuggestions[0].length-2));}
this.showSuggestions(aSuggestions);}else{this.hideSuggestions();}}}catch(e){}};AutoSuggestControl.prototype.createDropDown=function(){var oThis=this;this.layer=document.createElement("div");this.layer.className="suggestions";this.layer.style.visibility="hidden";this.layer.style.width=this.textbox.offsetWidth;this.layer.onmousedown=this.layer.onmouseup=this.layer.onmouseover=function(oEvent){oEvent=oEvent||window.event;var oTarget=oEvent.target||oEvent.srcElement;if(oEvent.type=="mousedown"){var propertyArray=getTargetProps(oTarget);if(propertyArray[0]!=-1){oThis.textbox.value=propertyArray[0];oThis.keywordHolder.value=propertyArray[1];oThis.priceHolder.value=propertyArray[2];oThis.discountIdHolder.value=propertyArray[3];oThis.hideSuggestions();}}else if(oEvent.type=="mouseover"){oThis.highlightSuggestion(oTarget);}else{oThis.textbox.focus();}};document.body.appendChild(this.layer);};function getTargetProps(target){var props=new Array();if(target.id=='items'){props[0]=target.firstChild.nodeValue;return props;}else{if(target.className=='current'){props[0]=target.childNodes[1].firstChild.nodeValue;props[1]=target.childNodes[5].getAttribute('value');props[2]=target.childNodes[6].getAttribute('value');props[3]=target.childNodes[7].getAttribute('value');return props;}else if(target.className=='suggestions'){props[0]=-1;return props;}else{props[0]=target.parentNode.childNodes[1].firstChild.nodeValue;props[1]=target.parentNode.childNodes[5].getAttribute('value');props[2]=target.parentNode.childNodes[6].getAttribute('value');props[3]=target.parentNode.childNodes[7].getAttribute('value');return props;}}}
AutoSuggestControl.prototype.getLeft=function(){var oNode=this.textbox;var iLeft=0;while(oNode.tagName!="BODY"&&oNode.tagName!="HTML"){iLeft+=oNode.offsetLeft;oNode=oNode.offsetParent;}
return iLeft;};AutoSuggestControl.prototype.getTop=function(){var oNode=this.textbox;var iTop=0;while(oNode.tagName!="BODY"&&oNode.tagName!="HTML"){iTop+=oNode.offsetTop;oNode=oNode.offsetParent;}
return iTop;};AutoSuggestControl.prototype.handleKeyDown=function(oEvent){switch(oEvent.keyCode){case 38:this.previousSuggestion();break;case 40:this.nextSuggestion();break;case 13:this.hideSuggestions();break;case 27:this.hideSuggestions();break;}};AutoSuggestControl.prototype.handleKeyUp=function(oEvent){var iKeyCode=oEvent.keyCode;if(iKeyCode==8||iKeyCode==46){this.provider.requestSuggestions(this,false);}else if(iKeyCode<32||(iKeyCode>=33&&iKeyCode<46)||(iKeyCode>=112&&iKeyCode<=123)){}else{this.provider.requestSuggestions(this,true);}};AutoSuggestControl.prototype.hideSuggestions=function(){this.layer.style.visibility="hidden";};AutoSuggestControl.prototype.highlightSuggestion=function(oSuggestionNode){for(var i=0;i<this.layer.childNodes.length;i++){var oNode=this.layer.childNodes[i];if(oNode==oSuggestionNode){setClassAttribute(oNode,'current');}else if(oNode.className=="current"){if(oNode.childNodes.length>1)
setClassAttribute(oNode,'discountDiv');else
oNode.className="";}}};AutoSuggestControl.prototype.init=function(){var oThis=this;this.textbox.onkeyup=function(oEvent){if(!oEvent){oEvent=window.event;}
oThis.handleKeyUp(oEvent);};this.textbox.onkeydown=function(oEvent){if(!oEvent){oEvent=window.event;}
oThis.handleKeyDown(oEvent);};this.textbox.onblur=function(){oThis.hideSuggestions();};this.createDropDown();};AutoSuggestControl.prototype.nextSuggestion=function(){var cSuggestionNodes=this.layer.childNodes;if(cSuggestionNodes.length>0&&this.cur<cSuggestionNodes.length-1){var oNode=cSuggestionNodes[++this.cur];this.highlightSuggestion(oNode);if(oNode.childNodes.length>1){this.textbox.value=oNode.childNodes[1].firstChild.nodeValue
this.keywordHolder.value=oNode.childNodes[5].getAttribute('value');this.priceHolder.value=oNode.childNodes[6].getAttribute('value');this.discountIdHolder.value=oNode.childNodes[7].getAttribute('value');}else
this.textbox.value=oNode.firstChild.nodeValue;}};AutoSuggestControl.prototype.previousSuggestion=function(){var cSuggestionNodes=this.layer.childNodes;if(cSuggestionNodes.length>0&&this.cur>0){var oNode=cSuggestionNodes[--this.cur];this.highlightSuggestion(oNode);if(oNode.childNodes.length>1){this.textbox.value=oNode.childNodes[1].firstChild.nodeValue
this.keywordHolder.value=oNode.childNodes[5].getAttribute('value');this.priceHolder.value=oNode.childNodes[6].getAttribute('value');this.discountIdHolder.value=oNode.childNodes[7].getAttribute('value');}else
this.textbox.value=oNode.firstChild.nodeValue;}};AutoSuggestControl.prototype.selectRange=function(iStart,iLength){if(this.textbox.createTextRange){var oRange=this.textbox.createTextRange();oRange.moveStart("character",iStart);oRange.moveEnd("character",iLength-this.textbox.value.length);oRange.select();}else if(this.textbox.setSelectionRange){this.textbox.setSelectionRange(iStart,iLength);}
this.textbox.focus();};AutoSuggestControl.prototype.showSuggestions=function(aSuggestions){var oDiv=null;var emptyNode=document.createTextNode("");this.layer.innerHTML="";for(var i=0;i<aSuggestions.length;i++){if(aSuggestions[i].item_name!=null){try{var discountDiv=document.createElement('div');setClassAttribute(discountDiv,'discountDiv');if(aSuggestions[i].logo!=null&&aSuggestions[i].logo!=''){logo=document.createElement('img');logo.setAttribute('src',aSuggestions[i].logo);setClassAttribute(logo,'suggestlogo');discountDiv.appendChild(logo);}else{logo=document.createElement('img');logo.setAttribute('src','../Graphics/star_filler.gif');logo.setAttribute('alt',aSuggestions[i].merchatname);setClassAttribute(logo,'suggestlogo');discountDiv.appendChild(logo);}
nameP=document.createElement('p');setClassAttribute(nameP,'suggestname');nameP.appendChild(document.createTextNode(aSuggestions[i].item_name));discountDiv.appendChild(nameP);if(aSuggestions[i].description!=null){descP=document.createElement('p');setClassAttribute(descP,'suggestdescription');descP.appendChild(document.createTextNode(aSuggestions[i].description));discountDiv.appendChild(descP);}
if(aSuggestions[i].merchantname!=null){storename=document.createElement('p');setClassAttribute(storename,'suggestmerchantname');storename.appendChild(document.createTextNode(aSuggestions[i].merchantname));discountDiv.appendChild(storename);}
if(aSuggestions[i].merchant_address!=null){storename=document.createElement('p');setClassAttribute(storename,'suggestmerchantname');storename.appendChild(document.createTextNode(aSuggestions[i].merchant_address));discountDiv.appendChild(storename);}
var keyholder=document.createElement('input');keyholder.setAttribute('type','hidden');keyholder.setAttribute('value',aSuggestions[i].matchedkeyword);discountDiv.appendChild(keyholder);var priceholder=document.createElement('input');priceholder.setAttribute('type','hidden');priceholder.setAttribute('value',aSuggestions[i].matchedkeyandprice);discountDiv.appendChild(priceholder);var idholder=document.createElement('input');idholder.setAttribute('type','hidden');idholder.setAttribute('value',aSuggestions[i].discountid);discountDiv.appendChild(idholder);this.layer.appendChild(discountDiv);}catch(exp){alert(exp);}}else{item_name=aSuggestions[i];if(item_name!=null){oDiv=document.createElement("div");oDiv.setAttribute('id','items');}
oDiv.appendChild(document.createTextNode(item_name));this.layer.appendChild(oDiv);}}
var theLeft=this.getLeft();var theTop=this.getTop();if(navigator.appName=="Microsoft Internet Explorer"){this.layer.style.left=(theLeft)+'px';this.layer.style.top=(theTop+20)+'px';}else if(isSafari>1){this.layer.style.left=(theLeft)+'px';this.layer.style.top=(theTop+this.textbox.offsetHeight)+'px';}else{this.layer.style.left=(theLeft)+'px';this.layer.style.top=(theTop+this.textbox.offsetHeight)+'px';}
this.layer.style.visibility='visible';this.layer.style.background="#fff";this.layer.style.color="#000000";this.layer.style.width=((this.textbox.getAttribute('size')*6)+125)+'px';};AutoSuggestControl.prototype.typeAhead=function(sSuggestion){if(this.textbox.createTextRange||this.textbox.setSelectionRange){var iLen=this.textbox.value.length;this.textbox.value=sSuggestion;this.selectRange(iLen,sSuggestion.length);}};function searchjsonobj(keywords,discounts,needle){var discountArray=new Array();var limit=4;if(discounts!=null){numDiscounts=discounts.length;if(needle.length>1&&numDiscounts>0)
var discountArray=searchDiscounts(discounts,needle);limit=2;}
var n=keywords.length;var start=indexOf(keywords,needle);var newarray=new Array();var counter=0;for(var i=start;i<n;i++){if(keywords[i]){if(needle.toLowerCase()==keywords[i].substr(0,needle.length).toLowerCase()&&counter<limit){newarray.push(keywords[i]);counter++;}else{var totalArray=new Array();return totalArray.concat(discountArray,newarray.sort());}}}
return null;}
function searchDiscounts(discounts,needle){var discountArray=new Array();for(var i=0;i<discounts.length;i++){var matchedkeyword=searchKeyword(discounts[i].keywords,needle);if(matchedkeyword!=null){var keyArray=matchedkeyword.split('->');discounts[i].matchedkeyword=keyArray[0];discounts[i].matchedkeyandprice=keyArray[1];discountArray.push(discounts[i]);}}
discountArray.sort(callbackFuncSortByPrice);discountArray.reverse();return discountArray;}
function callbackFuncSortByPrice(a,b){if(a.matchedkeyandprice==b.matchedkeyandprice)
return 0;return(a.matchedkeyandprice<b.matchedkeyandprice)?-1:1;}
function searchKeyword(keywords,needle){var keywordArray=keywords.split('_');for(var i=0;i<keywordArray.length;i++){if(keywordArray[i]!=null){if(keywordArray[i].toLowerCase().indexOf(needle,0)==0){return keywordArray[i];}}}
return null;}
function indexOf(keywords,srch){for(var index=0,len=keywords.length;index<len;index++){if(keywords[index].substr(0,srch.length).toLowerCase()==srch.toLowerCase())return index;}
return-1;}
