function ucwords (str) {
    return (str+'').replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase( ); } );
}

$(document).ready(function(){
	
	$("tr:even").addClass('alternate');
	
	$('a[rel=ext]').each(function(){
		 if($.browser.msie)
		 {
		 	$(this).attr('target','_blank');
		 }
		 else
		 {
		 	$(this).attr('onclick','window.open(\''+ $(this).attr('href') +'\'); return false; ');
		 }
	});
	
	$('form.ext').each(function(){
		var target = $(this).attr('id');
		if(target == '')
		{
			target = 'newForm';
		}
		$(this).attr('target',target);
	});
	
	$('#lookup-form').ajaxForm({
		beforeSubmit: function(){
			$('img#loading').css('visibility', 'visible');
		},
		success: function(responseText, statusText){
			$('img#loading').css('visibility', 'hidden');
			if($('div#results').css('display') == 'none')
			{
				$('div#results').slideDown('slow');
			}
			var parts = responseText.split('|');
			if(parts.length == 2)
			{
				$('div#results p.left').html('The IP Address <strong>' + $('input#ip').val() + '</strong> is located in the <strong>' + ucwords(parts[0].toLowerCase()) + '</strong> <img src="flags/' + parts[1].toLowerCase() + '.png" id="flag" alt="" />');
			}
			else
			{
				$('div#results p.left').html('Error');
			}
		}
	});
});