jQuery(function($){
	$('#validate').click(function onClick(){
		hideAlert();
	});
	
	var killInterval = null;
	
	function hideAlert() {
		clearInterval(killInterval);
		$('#validate').fadeOut();
	}
	
	function popupAlert(text, el) {
		if (text === false)
			return;
		
		var errorIcon = $('<img />').attr({
			src: 'https://ipp.tradingplaces.com/static/images/icons/misc/deactivate.png',
			width: 16,
			height: 16
		}).css({
			width: 16,
			height: 16,
			float: 'left',
			padding: 5
		});
		
		$('#validate').html(text).css({
			top:  el.offset().top - 30,
			left: el.offset().left
		}).prepend(errorIcon).fadeIn('slow');
	
		killInterval = setInterval(function(){
			hideAlert();
		}, 5000);
	}
	
	var RX_EMAIL = /^(\w+[\-\.])*\w+@(\w+\.)+[A-Za-z]+$/;
	
	Admin.validate = function validate(value, el, type, onError) {				
		switch (type)
		{
			case 'email':
				if (!RX_EMAIL.test(value))
					popupAlert(onError(), el);
				
				break;
			case 'number':
				if (isNaN(value))
					popupAlert(onError(), el);
				
				break;
			default:
				if (typeof type == 'function' && !type(value))
					popupAlert(onError(), el);
				else if (typeof type == 'number' && value.length < type)
					popupAlert(onError(), el);
		}
		
	};

});