/* later maybe put into a jquery extension */

function checkMaxChars(element, max_length) {
	var length = $(element).val().length;
	
	if (length >= max_length) {
		var trimmed = $(element).val().substr(0, max_length);
		$(element).val(trimmed);
		return false;
	}
	
	return true;
}


function validEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	return filter.test(email);
}

function createAlertPassword(title, message, scope, callback, data) {
	var xhtml = message+' <div id="msgBoxConfirmDelete">\
			<div class="item">\
				<label>Password:</label>\
				<input type="password" name="password" />\
			</div>\
			<div class="item">\
				<label>Repeat Password:</label>\
				<input type="password" name="repeat_password" />\
			</div>\
		</div>';
	Ext.Msg.show({
		title: title,
		msg: xhtml,
		buttons: Ext.Msg.OKCANCEL,
		fn: onItemDeleteCallback,
		scope: scope,
		data: data,
		callback: callback
	});
}

function onItemDeleteCallback (buttonID, text, opt) {
	if (buttonID == 'ok') {
		var password = Ext.get('msgBoxConfirmDelete').child('input[name=password]').getValue().trim();
		var repeat_password = Ext.get('msgBoxConfirmDelete').child('input[name=repeat_password]').getValue().trim();
		
		if (password != repeat_password) {
			Ext.Msg.alert('Error', 'The password aren\'t the same');
		} else if (password == '') {
			Ext.Msg.alert('Error', 'You must provide your password');
		} else {
			var handler = opt.callback;			
			handler.apply(opt.scope, [password, opt.data]);
		}
	}
}



$(document).ready(function(){
	$('.tooltip.sale.small .close-sale').click(function(){
		$.cookie('enjin_admin_sale', $(this).parent().parent().attr('sale_id'));
		$(this).parent().parent().fadeOut(350);
	});
	
    $("select[multiple].acl-widget").each(function(){
		if($(this).attr('data-mode') == 'ajax')
		{
			$(this).asmSelect({
				animate: true,
				listType: 'ul',
				onAdd: function(item)
				{
					$.ajax({
						type: "POST",
						url: "/admin/editmodule/add-acl/preset/" + $(item).parent().attr('data-preset_id'),
						data: {value: $(item).val(), access_type: $(item).parent().attr('data-access_type')},
						dataType: "json"
					});
				},
				onRemove: function(item)
				{
					$.ajax({
						type: "POST",
						url: "/admin/editmodule/remove-acl/preset/" + $(item).parent().attr('data-preset_id'),
						data: {value: $(item).val(), access_type: $(item).parent().attr('data-access_type')},
						dataType: "json"
					});
				}
			});
		}
		else
		{
			$(this).asmSelect({
				animate: true,
				listType: 'ul'
			});
		}
    });
});