var error1 = 'We encountered an error while trying to communicate with the server. Please try again.';
function addImage(x) {
	var newx = x;
	new Ajax.Request('ajax_available_now.php?action=addImage&x='+x,{
		method:'get',
		onSuccess:function(transport) {
			var response = transport.responseText;
			$('imagesContainer').insert(response);
		},
		onFailure:function() { alert(error1); }
	});
	newx++;
	$('newImageLink').onclick = function() { return addImage(newx); };
	return false
}
function removeImage(x) {
	if(confirm('Are you sure you want to completely remove this image?')) {
		$('image'+x).remove();
	}
	return false;
}
function doNav(url) {
	window.location.href = url;
}
function deleteThis(element,what) {
	if(confirm('Are you sure you want to delete this '+what+'?')) {
		doNav($(element).href);
	}
	return false;
}
function addHomeImage(x) {
	var newx = x;
	new Ajax.Request('ajax_homes.php?action=addHomeImage&x='+x,{
		method:'get',
		onSuccess:function(transport) {
			var response = transport.responseText;
			$('imagesContainer').insert(response);
		},
		onFailure:function() { alert(error1); }
	});
	newx++;
	$('newImageLink').onclick = function() { return addHomeImage(newx); };
	return false
}
function removeHomeImage(x,image) {
	if(confirm('Are you sure you want to completely remove this image?')) {
		if(image) {
			new Ajax.Request('ajax_homes.php?action=removeHomeImage&image='+escape(image),{
				method:'get',
				onSuccess:function(transport) {
					var response = transport.responseText;
					if(response) {
						alert(response);
					} else {
						$('image'+x).remove();
					}
				},
				onFailure:function() { alert(error1); }
			});
		} else {
			$('image'+x).remove();
		}
	}
	return false;
}
function onFocus(element,text) {
	if($(element).value == text) {
		$(element).value = '';
		$(element).removeClassName('gray');
	}
}
function onBlur(element,text) {
	if($(element).value == '') {
		$(element).value = text;
		$(element).addClassName('gray');
	}
}
function checkDefaultImage(element) {
	if($(element).checked) {
		var checkboxes = $('ourHomesForm').getInputs('checkbox');
		checkboxes.each(function(el) {
			if(el != element) {
				$(el).checked = false;
			}
		});
	}
}