var sel = null;
var res = null;
var unselect = true;
var showmore = 'Show more';

function ajaxObject(url, callbackFunction) 
{
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }
  this.update = function(passData,postMethod) 
  { 
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null;                                         
        }                                                      
      }                                                        
      that.updating = new Date();                              
      if (/post/i.test(postMethod)) {
        var uri=urlCall+'?'+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send(passData);
      } else {
        var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
}



function searchSuggest(event) 
{
	if(event == null)
		event = window.event;
	
	var keys = new Array();
	keys[13] = true;
	keys[27] = true;
	keys[38] = true;
	keys[40] = true;
	
	
	if(keys[event.keyCode] != null)
	{
		return moveSelector(event.keyCode);
	}	
	
	
	var ajax = new ajaxObject('AJAX/searchSuggest.php', handleSearchSuggest);
	var str = escape(document.getElementById('product_number').value);
	var cId = escape(document.getElementById('cId').value);
	ajax.update("data=" + str + "&cId=" + cId, "POST");
}

function checkKey(event)
{
	if(event == null)
		event = window.event;
	
	if(event.keyCode == 13 && sel != null)
		return false;
		
	return true;
}

function moveSelector(theKey)
{
	// 13 = Enter, 27 = Escape, 38 = Up, 40 = Down
	switch(theKey)
	{
		case 40:
			moveDown();
			break;
		case 38:
			moveUp();
			break;
		case 27:
			unSelect();
			break;
		case 13:
			selectIt();
			break;
	}
	
	return false;
}

function selectIt()
{
	if(sel == null)
		return null;	
	
	var value = document.getElementById('sel' + sel).innerHTML;
	document.getElementById('product_number').value = value;
	document.getElementById('search_suggest').innerHTML = '';		
	document.getElementById('search_suggest').style.display='none';
	sel = null;
		
}

function unSelect()
{
	if(!unselect)
		return null;
		
	if(sel != null)
	 document.getElementById('sel' + sel).className="suggest_link";

	document.getElementById('search_suggest').style.display="none";
	sel = null;
		
}

function moveDown()
{
	var i;
	var curSel = sel;
	if(sel == null || sel == res)
		sel = 0;
	else
		sel = sel + 1;
	
	
	//alert(sel);
	
	var theSel = document.getElementById('sel' + sel);
	if(curSel != null)
		document.getElementById('sel' + curSel).className="suggest_link";
	
	theSel.className="suggest_link_over";
}

function moveUp()
{
	var i;
	var curSel = sel;
	if(sel == null || sel == 0)
		sel = res;
	else
		sel = sel - 1;
		
	var theSel = document.getElementById('sel' + sel);
	if(curSel != null)
		document.getElementById('sel' + curSel).className="suggest_link";
	
	theSel.className="suggest_link_over";
}

function handleSearchSuggest(responseText) 
{
		sel = null;
		var ss = document.getElementById('search_suggest');
		if(responseText != "")
			ss.style.display = "block";	
		else
		{
			sel = null;
			ss.style.display = "none";	
		}
		
		ss.innerHTML = '';
		//var str = responseText.split("\n");
		
		var obj = eval(responseText);
		
		
		var x = 0;
			
		for(i in obj.titles)
		{
			var suggest = '<div id="sel' + x + '" onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
			suggest += 'class="suggest_link">' + obj.titles[i] + '</div>';
			ss.innerHTML += suggest;
			res = x;
			
			x++;	
		}
		
		
		
		
		for(i in obj.names)
		{
			var suggest = '<div id="sel' + x + '" onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
			suggest += 'class="suggest_link">' + obj.names[i] + '</div>';
			ss.innerHTML += suggest;
			res = x;
			
			x++;	
		}		
		
		
		if(obj.overall.product_names > 10 || obj.overall.product_titles > 10)
			ss.innerHTML += '<div class="showmore" onmouseover="javascript: this.className=\'showmoreActive\';" onmouseout="javascript: this.className=\'showmore\';" onmousedown="javascript: document.getElementById(\'prodSearcher\').submit();">' + showmore + '</div>';
}


function suggestOver(div_value) 
{
	div_value.className = 'suggest_link_over';
	unselect = false;
}

function suggestOut(div_value) 
{
	div_value.className = 'suggest_link';
	unselect = true;
}

function setSearch(value) 
{
	document.getElementById('product_number').value = value;
	document.getElementById('search_suggest').innerHTML = '';
	document.getElementById('search_suggest').style.display='none';
}

window.onload = function()
{
	var ss = document.getElementById('product_number');
	
	if(ss != null)
		ss.setAttribute("autocomplete", "off");
}
