// Jobsearch autocomplete Script

$(document).ready(function(){

	$("#jsearch_kword").focus(function() {
		if($(this).val()=="Keyword") {
			$(this).val("");
			$(this).removeClass("inactive");	
		}		 
	});
	$("#jsearch_kword").blur(function() {
		if(!$(this).val()) {
			$(this).val("Keyword");
			$(this).addClass("inactive");	
		}
	});
	
	$("#jsearch_kword").autocomplete('ajax/keywords.php', {
		width: 190,
		multiple: true,
		matchContains: true,
		formatItem: formatItem,
		formatResult: formatResult
	});
	
	
	$("#jsearch_kword").result(function(event, data, formatted) {
		var hidden = $(this).parent().next().find(">:input");
		hidden.val( (hidden.val() ? hidden.val() + ";" : hidden.val()) + data[1]);
	});
	

	$("#jsearch_placename").focus(function() {
		if($(this).val()=="Location") {
			$(this).val("");
			$(this).removeClass("inactive");
		}		 
	});
	$("#jsearch_placename").blur(function() {
		if(!$(this).val()) {
			$(this).val("Location");
			$(this).addClass("inactive");	
		}
	});
	
	$("#jsearch_placename").autocomplete('ajax/geonames_autocomplete.php', {
		width: 190,
		multiple: false,
		matchContains: true,
		formatItem: formatItem,
		formatResult: formatResult
	});
	
	
	$("#jsearch_placename").result(function(event, data, formatted) {
		var hidden = $(this).parent().next().find(">:input");
		hidden.val( (hidden.val() ? hidden.val() + ";" : hidden.val()) + data[1]);
	});
	
	
	// make sure to clear "Keyword" and "Location" from fields on submit
	$("#jsearch_kword").parent().submit(function() {
		if($("#jsearch_placename").val()=="Location") {
			$("#jsearch_placename").val("");
		} 
		if($("#jsearch_kword").val()=="Keyword") {
			$("#jsearch_kword").val("");
		} 
	});
	
});


function formatItem(row) {
		// return row[0] + " (<strong>id: " + row[1] + "</strong>)";
		return row[0];
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}