function SetToKeywords() {
    //console.log("SetToKeywords");
	 $("#txtSearch").unautocomplete();
	 $("#txtSearch").autocomplete("/includes/autocomplete/keywords/", {
		  width: 320,  
		  minChars: 3,
          max: 300,
          matchContains: true,
		  highlightCaps: true
	 });
	// $("#txtSearch").result(function(event, data, formatted) {
		//  if (data)
			//	$(this).parent().next().find("input").val(data[1]);
	 //});
	 $("#txtSearch").focus();
	 $("#txtSearch").trigger('keydown');
}

function SetToBusinessNames() {
   // console.log("SetToBusinessNames");
    $("#txtSearch").unautocomplete();
    $("#txtSearch").autocomplete("/includes/autocomplete/businessNames/", {
        width: 320,
        minChars: 3,
        max: 300,
        matchContains: true,
        highlightCaps: false,       
        extraParams: { book: function(){ return $("input[name='ctl00$ContentPlaceHolder1$radLocation']:checked").val(); }}  
    });
   // $("#txtSearch").result(function (event, data, formatted) {
     //   if (data)
       //     $(this).parent().next().find("input").val(data[1]);
    //});
    $("#txtSearch").focus();
    $("#txtSearch").trigger('keydown');    
}

function SetToCategories() {
    //console.log("SetToCategories");
    $("#txtSearch").unautocomplete();
    $("#txtSearch").autocomplete("/includes/autocomplete/categories/", {
        width: 320,
        minChars: 3,
        max: 300,
        matchContains: true,
        highlightCaps: true
    });
   // $("#txtSearch").result(function (event, data, formatted) {
     //   if (data)
       //     $(this).parent().next().find("input").val(data[1]);
    //});
    $("#txtSearch").focus();
    $("#txtSearch").trigger('keydown');
}

function SetToLocation() {
    //console.log("SetToLocation");
    $("#txtWhere").autocomplete("/includes/autocomplete/cities/", {
        width: 236,
        minChars: 3,
        max: 300,
        matchContains: true,
		highlightCaps: true
    });
    //$("#txtWhere").focus();
    $("#txtWhere").trigger('keydown'); 
}
function disableLocationRadios(){
	$("#radLocation_0").attr("disabled", true);
	$("#radLocation_0").attr("checked", false);
	$("#radLocation_1").attr("disabled", true);
	$("#radLocation_1").attr("checked", false);
	$("#radLocation_2").attr("disabled", true);
	$("#radLocation_2").attr("checked", false);
	$("#radLocation_3").attr("disabled", true);
	$("#radLocation_3").attr("checked", false); 
}
function enableLocationRadios(){
    $("#radLocation_0").removeAttr('disabled');
    $("#radLocation_1").removeAttr('disabled');
    $("#radLocation_2").removeAttr('disabled');
    $("#radLocation_3").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;
        }
    }
}
