function SetToKeywords(search_folder_uri){
	 $("#tbSearch").unautocomplete();
	 $("#tbSearch").autocomplete(search_folder_uri+"autocomplete/keywords.php", {
		  width: 320,  
		  minChars: 3,
          max: 300,
          matchContains: true,
		highlightCaps: true
	 });
	 $("#tbSearch").result(function(event, data, formatted) {
		  if (data)
				$(this).parent().next().find("input").val(data[1]);
	 });
	 $("#tbSearch").focus();
	 $("#tbSearch").trigger('keydown');
}

function SetToNames(search_folder_uri){
	 $("#tbSearch").unautocomplete();
	 $("#tbSearch").autocomplete(search_folder_uri+"autocomplete/listingNames.php", {
		  width: 320,  
          minChars: 3,
          max: 300,
          matchContains: true,
		highlightCaps: false
	 });
	 $("#tbSearch").result(function(event, data, formatted) {
		  if (data)
				$(this).parent().next().find("input").val(data[1]);
	 });
	 $("#tbSearch").focus();
	 $("#tbSearch").trigger('keydown');
}

function SetToCategories(search_folder_uri){
	 $("#tbSearch").unautocomplete();
	 $("#tbSearch").autocomplete(search_folder_uri+"autocomplete/categories.php", {
		  width: 320,		 
		  minChars: 3,
		  max: 300,          
          matchContains: true,
		highlightCaps: true
	 });
	 $("#tbSearch").result(function(event, data, formatted) {
		  if (data)
				$(this).parent().next().find("input").val(data[1]);
	 });
	 $("#tbSearch").focus();
	 $("#tbSearch").trigger('keydown');
}

function SetToLocation(search_folder_uri){
    $("#tbPostal").autocomplete(search_folder_uri+"autocomplete/locations.php", {
       width: 236,         
          minChars: 3,
          max: 300,          
          matchContains: true,
		highlightCaps: true
    });
    //$("#tbPostal").focus();
    $("#tbPostal").trigger('keydown'); 
}


function disableLocationRadios(){
	$("#bookID_1").attr("disabled", true);
    $("#bookID_1").attr("checked", false); 
	$("#bookID_2").attr("disabled", true);
    $("#bookID_2").attr("checked", false);   
	$("#bookID_3").attr("disabled", true);
    $("#bookID_3").attr("checked", false);   
	$("#bookID_4").attr("disabled", true);
    $("#bookID_4").attr("checked", false);   
}

function enableLocationRadios(){
	$("#bookID_1").removeAttr('disabled');
	$("#bookID_2").removeAttr('disabled');
	$("#bookID_3").removeAttr('disabled');
	$("#bookID_4").removeAttr('disabled');
}


function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateEmpty(theForm.search);
  reason += validateLocationChoice(theForm.bookID, theForm.postal);

  if (reason != "") {
	 alert("" + reason);
	 return false;
  }

  return true;
}

function validateEmpty(fld) {
	 var error = "";

	 if (fld.value.length == 0) {
		  fld.style.background = 'Yellow';
		  error = "Please enter a search term.\n"
	 } else {
		  fld.style.background = 'White';
	 }
	 return error;
}

function validateLocationChoice(btn, txtbox) {
	 var error = "";
	 var cnt = -1;
	 for (var i=btn.length-1; i > -1; i--) {
		  if (btn[i].checked) {cnt = i; i = -1;}
	 }
	 if (cnt > -1) return "";
	 else {
		  if (txtbox.value.length == 0) {
				return "Please select one of the 4 book publications or enter a location.\n";
		  } else
				return "";
	 }
}

function setBookIDFocus(bookID){
	for (i=1;i<=4;i++){
		if(i == bookID){
			document.getElementById("bookID_"+i).checked = true ;
		}else{
			document.getElementById("bookID_"+i).checked = false ;
		}
	}
}