//
// Uses AJAX to update city drop-down based on province choice
//
function subscribeToListings()
{
	type = $F('s_type');
	style = $F('s_style');
	city = $F('s_city');
	province = $F('s_province');
	min_price = $F('s_min_price');
	max_price = $F('s_max_price');
	min_size = $F('s_min_size');
	max_size = $F('s_max_size');
	min_beds = $F('s_beds');	
	min_baths = $F('s_baths');	
	
	$('notify_button').hide();
	$('notify_wait').show();
	
	if (province && province != "null")
	{
		var params = 'type='+type+'&style='+style+'&province='+province+'&city='+city+'&min_size='+min_size+'&max_size='+max_size+'&min_price='+min_price+'&max_price='+max_price+'&min_beds='+min_beds+'&min_baths='+min_baths;
		new Ajax.Request('listing_subscribe.php', {method: 'post', parameters: params, onSuccess: subscribeToListingsComplete, onFailure: subscribeToListingsFail});
	}
}

function subscribeToListingsComplete(transport)
{
	if (!transport.responseText.match(/^ERROR\:/))
	{
		$('notify_wait').hide();
		if (transport.responseText.match(/ADDED/))
		{
			$('notify_added').show();
		}
		else
		{
			$('notify_duplicate').show();
		}
	}
	else 
	{
		$('notify_button').show();
		$('notify_wait').hide();
		alert(transport.responseText);
	}
}

function subscribeToListingsFail(transport)
{
	$('notify_button').show();
	$('notify_added').hide();
	$('notify_duplicate').hide();
	$('notify_wait').hide();
	alert("An error occurred that prevented you from being subscribed. Please check your email address and/or try again later.");
}

//
// Uses AJAX to prepare and download e-book
//
function prepareListingSheet(id)
{
	if ($('listing_sheet_button_'+id)) $('listing_sheet_button_'+id).hide();
	if ($('listing_sheet_wait_'+id)) $('listing_sheet_wait_'+id).show();

	var params = 'home='+id;
	new Ajax.Request('prepare_listing_sheet.php', {method: 'get', parameters: params, onSuccess: prepareListingSheetComplete, onFailure: prepareListingSheetFail});
}

function prepareListingSheetComplete(transport)
{
	if (!transport.responseText.match(/^ERROR\:/)) 
	{
		id = transport.responseText;
		if ($('listing_sheet_button_'+id)) $('listing_sheet_button_'+id).show();
		if ($('listing_sheet_wait_'+id)) $('listing_sheet_wait_'+id).hide();
		//alert('download_listing_sheet.php?home='+id);
		window.location.href = 'download_listing_sheet.php?home='+id;	
		return null;
	}
	else 
	{
		alert(transport.responseText);
	}
}

function prepareListingSheetFail(transport)
{
	alert("Sorry, your listing sheet could not be generated due to an error with our website. Please contact technical support for assistance.");
}

//
// Uses AJAX to update city drop-down based on province choice
//
function listCities(default_city, default_option_title, default_option_value)
{
	province = $F('s_province');
	if (default_option_title==undefined) default_option_title = '';
	if (default_option_value==undefined) default_option_value = '';
	
	if (province && province != "null")
	{
		var params = 'province='+province+'&city='+default_city+'&dot='+default_option_title+'&dov='+default_option_value;
		new Ajax.Request('/includes/ajax/get_city_dropdown.php', {method: 'get', parameters: params, onSuccess: listCitiesComplete, onFailure: listCitiesFail});
	}
}

function listCitiesComplete(transport)
{
	if (!transport.responseText.match(/^ERROR\:/)) 
	{
		$('s_city').update(transport.responseText);		
	}
	else 
	{
		alert(transport.responseText);
	}
}

function listCitiesFail(transport)
{
	alert("The list of cities for the province could not be retrieved. Please contact technical support.");
}