new Ajax.Autocompleter("venue_search", "venue_search_results", "/london/sublisting/ajax_venue_search/", {callback:resetID, afterUpdateElement: processItem, minChars: 2, frequency:0.1, indicator:'search_spinner' });

// limit the description field to 650 characters

Event.observe('short_desc', 'keydown', trimDescription);
Event.observe('short_desc', 'keyup', trimDescription);	
Event.observe('short_desc', 'change', trimDescription);
		
function trimDescription() {
	
	var trimlength = 350;
	
	if (trimlength - $F('short_desc').length < 0) {
		var leng = 0;
	}
	else {
		var leng = trimlength - $F('short_desc').length;
	}
	
	$('remaining').update("(" + leng + " characters left)");
	document.iform.short_desc.value = ($F('short_desc').truncate(trimlength, ''));
	
}

// check the venue postcode (for a user-entered venue) against the DB, flag errors

if($('user_venue_postcode')){
	Event.observe('user_venue_postcode', 'change', function(event){
		$('postcode_indicator').innerHTML = '<img src="/img/ajax_loader.gif" />&nbsp;Checking...';
		
		var url = '/london/sublisting/checkPostcode';
		
		trigger = new Ajax.Request( url, {
			method: 'get',
			parameters: {
				postcode: $F('user_venue_postcode')
			},
			onSuccess: function( transport ) {
	
				// search has returned either a postcode (successful), -1 (search returned false), or 0 (entry too short)
				
				var q = transport.responseJSON.resultset.result;
				
				if (q == 0) {
					$('postcode_indicator').innerHTML = "Postcode too short";
					new Effect.Highlight('user_venue_postcode', {startcolor:'#ff2200', endcolor:'#ffffff', duration:1})
					}
				else if (q == -1) {
					$('postcode_indicator').innerHTML = "Postcode not found";
					new Effect.Highlight('user_venue_postcode', {startcolor:'#ff2200', endcolor:'#ffffff', duration:1})
					}
				else {
					document.iform.user_venue_postcode.value = q;
					$('postcode_indicator').innerHTML = "Postcode verified";
					new Effect.Highlight('user_venue_postcode', {startcolor:'#22ff00', endcolor:'#ffffff', duration:1})
					}
				
			},
			onFailure: function() {
				
			}
		});	
		
	});
}


// get the id of the venue from a successful lookup, save in a hidden form field
// change the colour of input field to indicate that it's been set by a lookup

function processItem(text, li) {
    document.iform.venue_id.value = li.id;
    //document.getElementById('venue_search').value += ' (id: ' + li.id + ')';
    $('venue_search').className = "idDefined";
}

// Autocompleter callback, needed to reset the colour of the field when the user edits the field,
// and to reset the venue_id hidden field

function resetID(q, d) {
	document.iform.venue_id.value = "";
	$('venue_search').className = "";
	
	return ("venue_search=" + q.value);
}

// hide / show for the auto / manually entered venue plus secret location

function toggleUserVenue(v) {
	
	if (v == "a" && $('vsearchfield').style.display == "none") {
		
		$('vsearchfield').style.display = "";
		$('vmanualfield').style.display = "none";
	}
	
	else if (v == "m" && $('vmanualfield').style.display == "none"){
		
		$('vmanualfield').style.display = "";
		$('vsearchfield').style.display = "none";
		
		document.iform.venue_id.value = "";
		$('venue_search').value = "";
		$('venue_search').className = "";
	}
	
	else if (v == "s") {
		$('vsearchfield').style.display = "none";
		$('vmanualfield').style.display = "none";
		document.iform.venue_id.value = "";
		$('venue_search').value = "";
		$('venue_search').className = "";
	}
	
}

// toggle the user category input box when "other" is selected from the category dropdown

function toggleUserGenre() {

	if (document.iform.genre_id.value == 999 && $('user_genre').style.display == "none") {
		 $('user_genre').style.display = "";
		}
	else {
		$('user_genre').style.display = "none";
		}
		
}

// hide / show some fields for recurring event

function toggleRecurring() {
	
	var r = document.iform.recurring_event;
	if (r.checked) {
		$('date_end').style.display = "";
		$('recurrence_interval').style.display = "";
		$('recurrence_caption').style.display = "";
		$('date_end_caption').style.display = "";
	}
	else {
		$('date_end').style.display = "none";
		$('recurrence_interval').style.display = "none";
		$('recurrence_caption').style.display = "none";		
		$('date_end_caption').style.display = "none";
	}
	
	
}

// submit form, needed as the date fields need to be re-enabled

function submitForm() {
	$('date_end_input').disabled = false;
	$('date_event_input').disabled = false;
	document.iform.submit();
}

Calendar.setup({
	inputField	 :	"date_event_input",			// id of the input field
	ifFormat	 :	"%e %b %Y",					// format of the input field
	button		 :	"date_event_handle",		// trigger for the calendar (button ID)
	showsTime 	 : false
});

Calendar.setup({
	inputField	 :	"date_end_input",			// id of the input field
	ifFormat	 :	"%e %b %Y",					// format of the input field
	button		 :	"date_end_handle",			// trigger for the calendar (button ID)
	showsTime 	 : false
});

