var ac_options = {minChars: 0, matchContains: true, max: 20, scroll: false};

$('#url').autocomplete(url_history, ac_options);
$('#url_dropdown').click(function() {
	$('#url').val('');
	// 3 delayed clicks to trigger Autocomplete to display dropdown (weird but it works)
	setTimeout(function() { $('#url').click(); }, 100);
	setTimeout(function() { $('#url').click(); }, 150);
	setTimeout(function() { $('#url').click(); }, 200);
});

$('#bcc').autocomplete(bcc_history, ac_options);
$('#bcc_dropdown').click(function() {
	$('#bcc').val('');
	// 3 delayed clicks to trigger Autocomplete to display dropdown (weird but it works)
	setTimeout(function() { $('#bcc').click(); }, 100);
	setTimeout(function() { $('#bcc').click(); }, 150);
	setTimeout(function() { $('#bcc').click(); }, 200);
});

$('#from').autocomplete(from_history, ac_options);
$('#from_dropdown').click(function() {
	$('#from').val('');
	// 3 delayed clicks to trigger Autocomplete to display dropdown (weird but it works)
	setTimeout(function() { $('#from').click(); }, 100);
	setTimeout(function() { $('#from').click(); }, 150);
	setTimeout(function() { $('#from').click(); }, 200);
});

$('#reply_to').autocomplete(reply_to_history, ac_options);
$('#reply_to_dropdown').click(function() {
	$('#reply_to').val('');
	// 3 delayed clicks to trigger Autocomplete to display dropdown (weird but it works)
	setTimeout(function() { $('#reply_to').click(); }, 100);
	setTimeout(function() { $('#reply_to').click(); }, 150);
	setTimeout(function() { $('#reply_to').click(); }, 200);
});

$('#cron_urls').focus(function() {
	if ($(this).val() == 'Enter one URL per line...') $(this).val('');
});

function editTemplate(id, subject, message) {
	$('#template_header').html('Edit Template #' + id + ' <span class="cancel">(<a href="javascript:cancelEditTemplate();">Cancel</a>)</span>');
	$('#template_id').val(id);
	$('#template_subject').val(subject);
	$('#template_message').val(message);
	$('#template_submit').val('Update');				
}			

function cancelEditTemplate() {
	$('#template_header').html('Add New Template');
	$('#template_id').val(0);
	$('#template_subject').val('');
	$('#template_message').val('');
	$('#template_submit').val('Save');	
}

function editCron(id, url, from, reply_to, bcc, template_id, interval) {
	$('#cron_header').html('Edit Cron #' + id + ' <span class="cancel">(<a href="javascript:cancelEditCron();">Cancel</a>)</span>');
	$('#cron_id').val(id);
	$('#cron_urls').val(url);
	$('#cron_from').val(from);
	$('#cron_reply_to').val(reply_to);
	$('#cron_bcc').val(bcc);
	$('#cron_template_id option[value="' + template_id + '"]').attr('selected', 'selected');
	$('#cron_interval').val(interval);
	$('#cron_submit').val('Update');
	location.href = '#cron_form';
}

function cancelEditCron() {
	$('#cron_header').html('Add New Cron');
	$('#cron_id').val(0);
	$('#cron_urls').val('Enter one URL per line...');
	$('#cron_from').val('');
	$('#cron_reply_to').val('');
	$('#cron_bcc').val('');
	$('#cron_template_id option:eq(0)').attr('selected', 'selected');
	$('#cron_interval').val(30);
	$('#cron_submit').val('Save');
}

$('#tabs').tabs().show();
$('#loading').hide();
