/**
 * Sends a message to the server without expecting a response and then refreshes
 * the page.
 * @param string url The url to send the message to
 */
function systemMessage(url) {
	var urlParts=url.split('?');
	new Ajax.Request(urlParts[0], {
		method: 'post',
		parameters: urlParts[1],
		onSuccess: function() {
			window.location.reload();
		}
	});
}

function submitFormAsync(url, form) {
	new Ajax.Request(url, {
		postBody: form.serialize()
	});
}

/**
 * Positions the info box next to the button but makes sure that it stays on the
 * screen and doesn't cover up the button.
 * The box is by default displayed to the right of the button.
 * @param button The button used to open the box when clicked
 * @param box The box that needs to be positioned 
 */
function positionInfoBox(button, box) {
	if (!$(box).positioned) {
		var boxDims = $(box).getDimensions();
		var parentDims = document.viewport.getDimensions()
		var buttonDims = $(button).getDimensions();
		var buttonPos = $(button).viewportOffset();
	
		//distance from parent right edge to button right edge
		buttonPos.right = parentDims.width - (buttonPos.left + buttonDims.width);
		//distance from parent bottom edge to button bottom edge
		buttonPos.bottom = parentDims.height - (buttonPos.top + buttonDims.height);
	
		//distance to offset the top of the box by from the top parent edge
		var offsetTop = buttonDims.height;
		//distance to offset the left of the box by from the left parent edge
		var offsetLeft = buttonDims.width;
		
		//if the box will not fit between the right side of the button and the 
		// right container edge
		if (buttonPos.right < boxDims.width) {
			offsetLeft -= boxDims.width - buttonPos.right + 5;
		}
	
		if (buttonPos.bottom < boxDims.height) {
			offsetTop -= boxDims.height - buttonPos.bottom + 5;
		}
		
		$(box).clonePosition(button, {setWidth: false, setHeight: false, offsetLeft: offsetLeft, offsetTop: offsetTop});
		$(box).positioned = true;
	}
}