
var req;

function Initialize() {
    try {
        req=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e) {
        try {
            req=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(oc) {
            req=null;
        }
    }

    if(!req&&typeof XMLHttpRequest!="undefined") {
        req= new XMLHttpRequest();
	}
}

function SendQuery(key) {
    if (key != null && key != "" && key.length >= 3){
		
		Initialize(); var url="/getSuggestionsHandler.asp?N=0&Dx=mode+matchall&D=" + key;

		if(req!=null) {

			req.onreadystatechange = Process;
			req.open("GET", url, true);
			req.send(null);

			//timerID = window.setTimeout('SendQuery("this is junk")', 2000);

		} else {
			
		}
	}
	else{
		HideDiv("autocomplete")
	}

}

var timerID = null;
function PauseSend(key) {
	if (timerID != null) {
		window.clearTimeout(timerID);
	}
	timerID = window.setTimeout('SendQuery("'+key+'")', 1000);
}

search_focus = false;
/* Attempted to remove inline Javascript. Errors in IE6 so leaving inline for now. 
function add_to_search() {
	if(document.getElementById('search_input')) {
		document.getElementById('search_input').onfocus=function(){ 
			search_focus = true;
			if(this.value=='Part Number or Keyword') {this.value='';}
		};
		document.getElementById('search_input').onblur=function(){ 
			search_focus = false;
			if(this.value=='') { this.value='Part Number or Keyword'; }
		};
   }
}

// Can't add this listener in at least Firefox 2 through DOM0 property assignment.
if (document.addEventListener) {
  document.addEventListener('DOMContentLoaded', add_to_search, false);
} else if (document.attachEvent) {
  // optimistic that one day Internet Explorer will support this event
  document.attachEvent('onDOMContentLoaded', add_to_search);
}
*/

// Attach event for each key press inside search box.
document.onkeyup = function(event){
	if(document.getElementById('search_input') && search_focus == true){
		
		var look_ahead = document.getElementById('search_input');
		
		//reset timer
		if (timerID != null) {
		  window.clearTimeout(timerID);
		} 
		
		var evt = event || window.event;

			// Check for a few keys, backspace, delete, ctrl, left arrow, right arrow
			if (evt.keyCode != 8 && evt.keyCode != 46 && evt.keyCode != 17 && evt.keyCode != 37 && evt.keyCode != 39) {
				timerID = window.setTimeout('SendQuery("'+look_ahead.value+'")', 500);
				return false;
			}
		return true;	
	}
};

function Process() {
    if (req.readyState == 4) {
        // only if "OK"
            if (req.status == 200) {
                if(req.responseText == "" || req.responseText == "<div></div>") {
					HideDiv("autocomplete");
					document.getElementById("autocomplete").innerHTML = "";
				} else {
                    ShowDiv("autocomplete");
                    document.getElementById("autocomplete").innerHTML = req.responseText;
                }
            } else {
                document.getElementById("autocomplete").innerHTML=
					"<p>There was a problem retrieving data:<BR>"+req.statusText+"</p>";
            }
	}
}

function ShowDiv(divid) {
	if (document.layers) {
		document.layers[divid].visibility = "show";
	} else {
		document.getElementById(divid).style.visibility = "visible";
	}
}

function HideDiv(divid) {
	if (document.layers) {
		document.layers[divid].visibility = "hide";
	} else {
		document.getElementById(divid).style.visibility = "visible";
	}
}

function BodyLoad() {
    //HideDiv("autocomplete");
    document.form1.keyword.focus();
}

function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
	    now = new Date();
	    if (now.getTime() > exitTime)
		return;
	}
}

