$(document).ready(function(){
    $('.peform .submit').click(function(){
	var form = $(this).parents('form:first');
	// Clear former errors
	$('ul.error').empty();
	
	// Get the form data
	var data1 = form.serializeJSON();
	var data2 = form.find('[required]').serializeRequiredJSON();
	var data = $.extend({}, data1, {required: data2});
	
	// Get the AJAX url
	var url = 'http://' + window.location.hostname + '/?type=1299485812&tx_peproducts_pi1[action]=processForm';
	
	// Post the call
	$.post(url, data, function(e){
	    if(e.error){
		// Add error messages
		form.prepend('<ul class="error"">' + e.error + '</ul>');
	    }
	    if(e.success){
		// Add a message
		form.before('<div class="succes">' + e.success + '</div>');
		// Remove the form
		form.remove();
	    }
	});
    });
});

(function( $ ){
    $.fn.serializeJSON=function() {
	var json = {};
	jQuery.map($(this).serializeArray(), function(n, i){
	    json[n['name']] = n['value'];
        });
	return json;
    };
})( jQuery );

(function( $ ){
    $.fn.serializeRequiredJSON=function() {
	var json = {};
	jQuery.map($(this).serializeArray(), function(n, i){
	    json[i] = n['name'];
        });
	return json;
    };
})( jQuery );
