function setScrollTitle(str, rewrited) {
	$('scrollTitle').update('<a href="' + rewrited + '">' + str + '</a>');
}

function ajaxSubmit(form, url) {
	if ($('activity_indicator') != null) $('activity_indicator').title = 'hidden';
	
	idx = '';
	if (arguments.length >= 4) idx = arguments[3];

	successAction = null;
	if (arguments.length >= 3) successAction = arguments[2];
	
	$('button' + idx).hide();
	$('loader' + idx).show();		
	new Ajax.Request(url, {
		parameters: Form.serialize($(form)),
		onComplete: function(t) {
			if ($('activity_indicator') != null) $('activity_indicator').title = '';
			
			$('button' + idx).show();
			$('loader' + idx).hide();
			var status = t.responseText.replace(/^\s+|\s+$/g, '');
			if (status == 'ok' && successAction != null) successAction();
			if (status != 'ok') alert(status);
		}
	});
}

function doLogin() {
	ajaxSubmit('form_login', '/index.php?obj=member&action=sign_in', function() {
		if ($('form_login').redirect.value)
			location.href = '/' + $('form_login').redirect.value;
		else {
			$('member_auth').update('<img src=images/ajax_indicator.gif>');
			new Ajax.Updater('member_auth', '/index.php?obj=member&action=auth', { evalScripts: true });
		}
	}, 1);
}

function doFPass() {
	ajaxSubmit('form_fpass', '/index.php?obj=member&action=fpass', function() {
		alert('An e-mail with your password has been sent to your e-mail address !');
		$('member_auth').update('<img src=images/ajax_indicator.gif>');
		new Ajax.Updater('member_auth', '/index.php?obj=member&action=auth', { evalScripts: true });
	}, 1);
}

function saveSearchListener() {
	if ($(document.form_search) == null) return;
	
	for (i = 0; i < document.form_search.elements.length; i++) 
		if (document.form_search.elements[i].lang != 'ignore')
			new Event.observe(document.form_search.elements[i], 'change', saveSearch);
}

function saveSearch() {
	new Ajax.Request('/index.php?obj=search&action=save', { parameters: Form.serialize(document.form_search) });
}

function submitSearch() {
	msg = '';
	
	if ($('search_area').visible() && document.form_search.areas.value == '')
		msg += 'You have to select at least one neighborhood !\n';
	if ($('search_zip').visible() && document.form_search.codes.value == '')
		msg += 'You have to select at least one zip code !\n';
	if ($('search_address').visible() && document.form_search.street_name.value == '')
		msg += 'You have to enter a street name !\n';
	if ($('search_suburb').visible() && document.form_search.suburbs.value == '')
		msg += 'You have to select at least one suburb !\n';

	ret = false;
	for (var i = 0; i < document.form_search.elements.length; i++)
		if (document.form_search.elements[i].name == 'type[]' && document.form_search.elements[i].checked) {
			ret = true;
			break;
		}
	if (!ret)
		msg += 'You have to select at least one property type !';
		
	if (msg)
		alert(msg);
	else
		location.href = '/listings';
}

function switchSearch(mode) {
	if (searchMode == mode) return;
	
	new Effect.BlindUp('search_' + searchMode, { 
		afterFinish: function() { 
			$('search_' + searchMode).update(''); 
			
			new Ajax.Updater('search_' + mode, '/index.php?obj=search&action=' + mode, { 
				evalScripts: true,
				onComplete: function() {
					new Effect.BlindDown('search_' + mode, { queue: 'end' });
					new Effect.ScrollTo('label_' + mode, { queue: 'end' });
					searchMode = mode;
				}
			})
		} 
	});
}