function AutoCompleteDB()
{
	// set the initial values.
	this.bEnd = false;
	this.nCount = 0;
	this.aStr = new Object;
}

AutoCompleteDB.prototype.add = function(str)
{
	// increment the count value.
	this.nCount++;

	// if at the end of the string, flag this node as an end point.
	if ( str == "" )
		this.bEnd = true;
	else
	{
		// otherwise, pull the first letter off the string
		var letter = str.substring(0,1);
		var rest = str.substring(1,str.length);
		
		// and either create a child node for it or reuse an old one.
		if ( !this.aStr[letter] ) this.aStr[letter] = new AutoCompleteDB();
		this.aStr[letter].add(rest);
	}
}

AutoCompleteDB.prototype.getCount = function(str, bExact)
{
	// if end of search string, return number
	if ( str == "" )
	{
		if ( this.bEnd && bExact && (this.nCount == 1) ) return 0;
		else return this.nCount;
	}

	// otherwise, pull the first letter off the string
	var letter = str.substring(0,1);
	var rest = str.substring(1,str.length);
	
	// and look for case-insensitive matches
	var nCount = 0;
	var lLetter = letter.toLowerCase();

	if ( this.aStr[lLetter] )
		nCount += this.aStr[lLetter].getCount(rest, bExact && (letter == lLetter));
	
	var uLetter = letter.toUpperCase();

	if ( lLetter != uLetter && this.aStr[uLetter] )
		nCount += this.aStr[uLetter].getCount(rest, bExact && (letter == uLetter));
		
	return nCount;	
}

AutoCompleteDB.prototype.getStrings = function(str1, str2, outStr)
{
	if ( str1 == "" )
	{
		// add matching strings to the array
		if ( this.bEnd ) 
			outStr.push(str2);

		// get strings for each child node
		for ( var i in this.aStr )
			this.aStr[i].getStrings(str1, str2 + i, outStr);
	}
	else
	{
		// pull the first letter off the string
		var letter = str1.substring(0,1);
		var rest = str1.substring(1,str1.length);
		
		// and get the case-insensitive matches.
		var lLetter = letter.toLowerCase();
		if ( this.aStr[lLetter] )
			this.aStr[lLetter].getStrings(rest, str2 + lLetter, outStr);

		var uLetter = letter.toUpperCase();
		if ( lLetter != uLetter && this.aStr[uLetter] )
			this.aStr[uLetter].getStrings(rest, str2 + uLetter, outStr);
	}
}


function AutoComplete(aStr, aUrls, aTrack, oText, oDiv, oSubmit, nMaxSize)
{
	// initialize member variables
	this.oStr = aStr;
	this.oUrls = aUrls;
	this.oTrack = aTrack;
	this.oText = oText;
	this.oDiv = oDiv;
	this.oSubmit = oSubmit;
	this.nMaxSize = nMaxSize;
	this.oSubmit.temp = -1;
	// preprocess the texts for fast access
	this.db = new AutoCompleteDB();
	var i, n = aStr.length;
	for ( i = 0; i < n; i++ )
	{
		this.db.add(aStr[i]);
	}
	
	// attach handlers to the text-box
	oText.AutoComplete = this;
	oText.onkeyup = AutoComplete.prototype.onTextChange;
	oText.onblur = AutoComplete.prototype.onTextBlur;
	oText.onfocus = AutoComplete.prototype.onTextFocus;
	oText.onSubmit = AutoComplete.prototype.onSubMouseClick;
	oSubmit.AutoComplete = this;
	oSubmit.onclick = AutoComplete.prototype.onSubMouseClick;
}

AutoComplete.prototype.onTextFocus = function()
{
	if(this.value=="Search Retailers" || this.value=="Search Again" || this.value=="Unknown Retailer") this.value='';
	//this.AutoComplete.onfocus();
}

AutoComplete.prototype.onTextBlur = function()
{
	if(this.value=='') this.value="Search Retailers";
	this.AutoComplete.onblur();
}

AutoComplete.prototype.onblur = function()
{
	this.oDiv.style.visibility = "hidden";
}

AutoComplete.prototype.onTextChange = function()
{
	var myText = document.getElementById('theText');
	var myDiv = document.getElementById('theDiv');
	var i;
	var myDivElements = myDiv.getElementsByTagName('div');
	// if textbox value is the name of a retailer in the autocomplete box,
	// it means the user must be scrolling through the list
	// if not, set the textbox.text variable to the actual text in the textbox
	for ( i = 0; i < myDivElements.length; i++ )
	{
		if (myText.value == myDivElements[i].retName) break;
	}
	if ( i == myDivElements.length ) myText.text = myText.value;
	
	this.AutoComplete.oSubmit.temp = -1;
	this.AutoComplete.onchange();
	
}

AutoComplete.prototype.onSubMouseClick = function()
{
	if (this.temp != -1)
	{
		this.AutoComplete.oText.value = "Search Again";
		//this.AutoComplete.oText.value = this.AutoComplete.oUrls[this.temp];
		var newWindow = window.open(this.AutoComplete.oUrls[this.temp], '_blank');
		pageTracker._trackPageview(this.AutoComplete.oTrack[this.temp]);
            newWindow.focus();
	}
	else
	{
		var myDiv = document.getElementById('theDiv');
		var myDivElements = myDiv.getElementsByTagName('div');
		var FoundRetailer = false;
		for ( var i = 0; i < myDivElements.length; i++ )
		{
			if (this.AutoComplete.oText.value.toLowerCase() == myDivElements[i].retName.toLowerCase())
			{
				FoundRetailer = true;
				this.AutoComplete.oSubmit.temp = myDivElements[i].temp;
				this.AutoComplete.oText.value = "Search Again";
				var newWindow = window.open(this.AutoComplete.oUrls[this.temp], '_blank');
				pageTracker._trackPageview(this.AutoComplete.oTrack[this.temp]);
				newWindow.focus();
			}
		}
		if (!FoundRetailer)
		{
			this.AutoComplete.oText.value = "Unknown Retailer";
		}
	}
	this.temp = -1;
}

AutoComplete.prototype.onDivMouseDown = function()
{
	this.AutoComplete.oText.value = this.retName;
	this.AutoComplete.oSubmit.temp = this.temp;
	this.AutoComplete.oSubmit.clicked = true;
}

AutoComplete.prototype.onDivMouseOver = function()
{
	var myDiv = document.getElementById('theDiv');
	var myDivElements = myDiv.getElementsByTagName('div');
	for ( var i = 0; i < myDivElements.length; i++ )
	{
		if (myDivElements[i].className == "AutoCompleteHighlight")
		{
			myDivElements[i].className = "AutoCompleteBackground";
		}
	}
	this.className = "AutoCompleteHighlight";
	this.AutoComplete.oText.value = this.retName;
	this.AutoComplete.oSubmit.temp = this.temp;
}

AutoComplete.prototype.onDivMouseOut = function()
{
	if (!this.AutoComplete.oSubmit.clicked)
	{
		this.className = "AutoCompleteBackground";
		this.AutoComplete.oText.value = this.AutoComplete.oText.text;
		this.AutoComplete.oSubmit.temp = -1;
	}
	else this.AutoComplete.oSubmit.clicked = true;
}

AutoComplete.prototype.onchange = function()
{
	// if the textbox.text variable is the same as the text in the actual textbox,
	// update the box
	if ( this.oText.text == this.oText.value )
	{
		//var txt = this.oText.value;
		var txt = this.oText.text;
		
		// count the number of strings that match the text-box value
		var nCount = this.db.getCount(txt, true);
		  
		// if a suitable number then show the popup-div
		if ( (this.nMaxSize == -1 ) || ((nCount < this.nMaxSize) && (nCount > 0)) )
		{
			// clear the popup-div.
			while ( this.oDiv.hasChildNodes() )
				this.oDiv.removeChild(this.oDiv.firstChild);
				
			// get all the matching strings from the AutoCompleteDB
			var aStr = new Array();
			this.db.getStrings(txt, "", aStr);
			
			// add each string to the popup-div
			var i, n = aStr.length;
			for ( i = 0; i < n; i++ )
			{
				var oDiv = document.createElement('div');
				this.oDiv.appendChild(oDiv);
				oDiv.className = "AutoCompleteBackground";
				oDiv.innerHTML = aStr[i];
				oDiv.retName = aStr[i];
				oDiv.temp = getIndex(aStr[i], this.oStr);
				oDiv.onmousedown = AutoComplete.prototype.onDivMouseDown;
				oDiv.onmouseover = AutoComplete.prototype.onDivMouseOver;
				oDiv.onmouseout = AutoComplete.prototype.onDivMouseOut;
				oDiv.AutoComplete = this;
			}
			this.oDiv.style.visibility = "visible";
		}
		else // hide the popup-div
		{
		this.oDiv.innerHTML = "";
		this.oDiv.retName = "";
		this.oDiv.style.visibility = "hidden";
		}
	}
	
	$(document).keyup(function(e) {
      switch(e.keyCode) { 
         // User pressed "up" arrow
         case 38:
            //navigate('up');
			
			var myDiv = document.getElementById('theDiv');
			var myText = document.getElementById('theText');
			var mySubmit = document.getElementById('theSubmit');
			var myDivElements = myDiv.getElementsByTagName('div');
			var SetHighlight = false;
			// search for placement of scroll
			for (var i = 0; i < myDivElements.length; i++ )
			{
				if (myText.value == myDivElements[i].retName)
				{
					// remove highlight from previous retailer
					myDivElements[i].className = "AutoCompleteBackground";
					// if previous retailer was not first in box, highlight the retailer above
					if (i != 0) 
					{						
						myDivElements[i-1].className = "AutoCompleteHighlight";
						myText.value = myDivElements[i-1].retName;
						mySubmit.temp = myDivElements[i-1].temp;
						SetHighlight = true;
						break;
					}
					// if so, put the text that created the orginal autocomplete box in the textbox
					else
					{
						myText.value = myText.text;
						SetHighlight = true;
					}
				}
			}
			// if there is no retailer highlighted, highlight the last retailer
			if (!SetHighlight)
			{
				myDivElements[myDivElements.length-1].className = "AutoCompleteHighlight";
				myText.value = myDivElements[myDivElements.length-1].retName;
				mySubmit.temp = myDivElements[myDivElements.length-1].temp;
			}
			// clear the keyCode so a click doesn't register more than once
			e.keyCode = null;
         break;
         // User pressed "down" arrow
         case 40:
			var myDiv = document.getElementById('theDiv');
			var myText = document.getElementById('theText');
			var mySubmit = document.getElementById('theSubmit');
			var myDivElements = myDiv.getElementsByTagName('div');
			var SetHighlight = false;
			// search for placement of scroll
			for ( var i = 0; i < myDivElements.length; i++ )
			{
				if (myText.value == myDivElements[i].retName)
				{
					// remove highlight of previous retailer
					myDivElements[i].className = "AutoCompleteBackground";
					// if previous retailer was not last, highlight the retailer below
					if (i != myDivElements.length - 1)
					{
						myDivElements[i+1].className = "AutoCompleteHighlight";
						myText.value = myDivElements[i+1].retName;
						mySubmit.temp = myDivElements[i+1].temp;
						SetHighlight = true;
					}
					// if so, put the text that created the orginal autocomplete box in the textbox 
					else
					{
						myText.value = myText.text;
						SetHighlight = true;
					}
					break;
				}
			}
			// if there is no retailer highlighted, highlight the first retailer in the autocomplete box
			if (!SetHighlight)
			{
				myDivElements[0].className = "AutoCompleteHighlight";
				myText.value = myDivElements[0].retName;
				mySubmit.temp = myDivElements[0].temp;
			}
			// clear the keyCode so a click doesn't register more than once
			e.keyCode = null;
         break;
         // User pressed "enter"
         case 13:
			var myDiv = document.getElementById('theDiv');
			var myText = document.getElementById('theText');
			var mySubmit = document.getElementById('theSubmit');
			var myDivElements = myDiv.getElementsByTagName('div');
			var SetHighlight = false;
			var RetailerFound = false;
			for ( var i = 0; i < myDivElements.length; i++ )
			{
				if (myText.value.toLowerCase() == myDivElements[i].retName.toLowerCase())
				{
					RetailerFound = true;
					//myText.value = "Search Again";
					mySubmit.temp = myDivElements[i].temp;
					myDiv.style.visibility = "hidden";
					//myText.text = myText.value;
					//pageTracker._trackPageview(mySubmit.AutoComplete.oTrack[mySubmit.temp]);
					//var newWindow = window.open(mySubmit.AutoComplete.oUrls[mySubmit.temp], "_blank" );
					//window.location = mySubmit.AutoComplete.oUrls[mySubmit.temp];
				}
			}
			//if (!RetailerFound)
			//{
			//	myText.value = "Unknown Retailer";
			//}
			//mySubmit.temp = -1;
			// clear the keyCode so a click doesn't register more than once
            e.keyCode = null;
         break;
      }
	})
}

function getIndex(str, list)
{
	for(i=0; i<list.length; i++)
	{
		if (list[i] == str)
			return i;
	}
}

