function mfwAlert(title,text)
{	
	alert(title + "\n\n" + text);
	/*
	var $dialog = $('<div></div>')
	.html(title+'. '+text)
	.dialog({
		autoOpen: false,
		modal:true,
		title: 'Alert'
	});
	$dialog.dialog('open');
	*/
}

function mfwData(data)
{
	if(data.status == true){							
		if(data.redirect)
			window.location = data.redirect;
		if(data.description)
			mfwAlert(data.message,data.description);
		if(data.response)
			mfwAlert('Mensaje',data.response);
		if(data.id == "all")
			form.reset();
		if(data.execute){
			eval(data.execute);
		}
		if(data.resetform){
			$('#'+data.resetform).each (function(){
			  this.reset();
			});
		}
		else if(data.id){
			$('#id'+data.id).remove(); }
	}
	else
		mfwAlert(data.message,data.description);	
}


/*****************/	
/* VALIDATION  */	
/***************/	

function ajaxForms()
{ 			
	jQuery.each($('form.ajax'),function(index, ajaxForm){

			$(this).validate({
				ignoreTitle: true,
				rules: {
					password: {
						minlength: 5	
					},
					confirmPassword: {
						required: true,
						equalTo: "#password"
					}
				},
				submitHandler: function(form){
						$.ajax({
							type: 'post',
							url: $(form).attr('action'),
							data: $(form).serialize(),
							dataType: 'json',
							error:function(){
								mfwAlert('Error',' Missing controller :(');
							},
							success: function(data){  
								mfwData(data);
							}
						});
				}
			});
	});
}

$(document).ready(function() { 
	$("a.ajaxLink").click(function(e) {
		e.preventDefault();
		if(confirm('Por favor, confirme para continuar.'))
		{
			$.post(this.href, this.rel ,
				function(data)
				{ 
					mfwData(data);
				}, "json");
		}
	});

	ajaxForms();
	
	$('form.validate').validate();

	$("form").validate();
});
