$(document).ready(function() {
	$("#url").focus();
	
	$('#shorterform').submit(function(e) {
		e.preventDefault();
		
		url = $('#shorterform input').val();
		if(url == '') {
			show_error('URL Empty');
		} else {
			get_url(url);
		}
	});
	
	
});

function get_url(url) {
	$.post('index.php', {
		api: "get_url",
		data: url
	}, function(data) {
		if(data == 'Invalid URL') {
			show_error(data);
		} else {
			show_short(data);
		}
	});
}

function show_short(data) {
	$('#shorterurl').css('color', 'white');
	$('#shorterurl').html(data);
}

function show_error(error) {
	$('#shorterurl').css('color', 'red');
	$('#shorterurl').html(error);
}
