//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]+'&';
      
      }
	  */
	  passValues[0]=String(passValues[0]).replace(/&/g, "%26").replace(/\+/g,"%2B");
      that.sendString+=passNames[0]+'='+passValues[0]+'&';
    
    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 () { };
}
searchJaxName=new ajaxObject('suggest_name.php', showSuggestName);

function searchTriggerName(currCount){
  if(currCount == count){
      count = 0;
      //$('#suggest').hide(500);
	  
      searchJaxName.update(['name'],[document.search_form.name.value],['', 'processing note', 'confirmation note', 'error note'],'GET');
  }
}

var count=0;
function searchSuggestName(){
	var typeDelay=500; // delay in ms between keypresses
	count=count+1;
	setTimeout("searchTriggerName('"+count+"')",typeDelay);
}

function showSuggestName(responseText){
	//alert(responseText);
	$('#suggestName').html(responseText);
	$('#suggestName').show(500);
	//$('#collectionSelect').hide();
	//$('#furnitureTypeSelect').hide();
}

function hideSuggestName(){
  setTimeout(function(){
    $('#suggestName').hide();
    //$('#collectionSelect').show();
	//$('#furnitureTypeSelect').show();  
  },500);
}

function goWithName(id,name){
	document.search_form.name.value='  '+name;
	window.location="search_results_detail.php?id="+id;
	//document.search_form.submit();
	return false;
}

