function tooltip(message, x, y) {
	var box = document.getElementById('tooltip');
	box.style.left = x+'px';
	box.style.top = y+'px';
	box.innerHTML = message;
	box.style.display = 'block';
}

function get_coords(item) {
	var ptr = item;
	var x=-3, y=-3;
	while (ptr!=null) {
		x+=ptr.offsetLeft;
		y+=ptr.offsetTop;
		ptr = ptr.offsetParent;
	}
	return [x,y];
}

function scroll_to_item(item,offset) {
	var coords = get_coords(document.getElementById(item));
	if (coords[1] > window.innerHeight)
		window.scrollTo(0, coords[1]+offset);
}

function tooltip_over(item, message) {
	var ptr = item;
	var x=-3, y=-3;
	while (ptr!=null) {
		x+=ptr.offsetLeft;
		y+=ptr.offsetTop;
		ptr = ptr.offsetParent;
	}
	tooltip(message, x, y);
}

hold_tooltip = false;
function clear_tooltip(force) {
	var box = document.getElementById('tooltip');
	if (force) { hold_tooltip = false; }
	if (!hold_tooltip) {
	    box.style.display = 'none';
	}
}

function str_dots(string, len) {
	if (string.length > len) {
		return string.substring(0, len-3) + '...';
	} else {
		return string;
	}
}


function ajaxify_form(form) {
	form.action = '?action=ajax_save';
	var save_result = $('save_result');
	form.set('send',{
		evalScripts: true,
		onFailure: function(xhr) {
			alert('Something with wrong, please try again. If the problem persists, contact YGMTRC support.');
		},
		onSuccess: function(resp) {
			save_result.removeClass('ajax_loading');
			save_result.set('html', resp);
			
			if ($('save_success')) {
				var save_success_fx = new Fx.Morph($('save_success'), {duration:2000, wait:true});
				save_success_fx.start({}).chain(function(){this.start({'opacity':0})});
			}
		}
	});
	
	form.addEvent('submit', function(e) {
		// Prevent the submit event
		new Event(e).stop();
		save_result.empty().addClass('ajax_loading');
		
		// Clear any boxes that were turned red
		$$('input').each(function(i) {i.removeClass('bad_input');});
		$$('select').each(function(s) {s.removeClass('bad_input');});
		
		// Send the form
		this.send();
	});
}
