//shorten getElementByID
function gbID(elementID){ return document.getElementById(elementID);}
//debug script for Ajax calls
function alertJAX(responseText, responseStatus) {  if (responseStatus==200) { alert(responseText); return false; } else {    alert(responseStatus +' -- Error Processing Request');  }}

// Best Ajax object to date, multiple concurrent calls with queue, function callback and notification handler
// based on The Ultimate Ajax Object
function ajaxObject(url, callbackFunction) {
  var that=this;      
  this.update = function(passNames,passValues,notifierArray,postMethod) { 
    //notifier array is [notifier element ID, loading HTML, ok HTML, bad HTML]
    that.notifier='';
    if(notifierArray[0] != ''){
    that.notifier=gbID(notifierArray[0]);
    that.notifier.innerHTML=notifierArray[1];
    }
    
    that.sendString='';
    that.cleanValue=new Array();
    for(i in passNames){
      
      passValues[i]=String(passValues[i]).replace(/&/g, "%26").replace(/\+/g,"%2B");
      that.sendString+=passNames[i]+'='+passValues[i]+'&';
      
      }
    
    if (that.updating) { return false; }
    that.AJAX = null; 

    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest(); //non IE             
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP"); //IE
    }                                             
    if (that.AJAX==null) {                             
      return false;  //no ajax
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4 && that.AJAX.status == 200) {             
          that.notifier.innerHTML=notifierArray[2];                            
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null;                
          
        } else if(that.AJAX.readyState==4 && that.AJAX.status != 200){
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null; 
          that.notifier.innerHTML=notifierArray[3];
          }                                                     
      }                                                       
      if (/post/i.test(postMethod)) {
        var uri=urlCall;
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-Length",that.sendString);
        that.AJAX.send(that.sendString);
      } else {
        var uri=urlCall+'?'+that.sendString; 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
}
searchJax=new ajaxObject('suggest.php', showSuggest);

function searchTrigger(currCount){
  if(currCount == count){
      count = 0;
      //$('#suggest').hide(500);
      searchJax.update(['try'],[document.search_form.model_no.value],['', 'processing note', 'confirmation note', 'error note'],'GET');
  }
}

var count=0;
function searchSuggest(){
var typeDelay=500; // delay in ms between keypresses
count=count+1;
setTimeout("searchTrigger('"+count+"')",typeDelay);
}

function showSuggest(responseText){
//alert(responseText);
$('#suggest').html(responseText);
$('#suggest').show(500);
$('#searchSelect').hide();
}

function hideSuggest(){
  setTimeout(function(){
    $('#suggest').hide();
    $('#searchSelect').show();
  },500);
}

function goWith(id,model){
document.search_form.model_no.value=model;
window.location="item.php?id="+id;
//document.search_form.submit();
return false;
}
