$(function(){
	
	$('form').live('submit', function(){
		var errors;
		var myElem;
		
		$(this).find('.required').each(function(){
			if(jQuery.trim($(this).val()).length == 0){
				errors = true;
				$(this).parent().addClass('error');
				$('.errorContainer2').fadeIn();
				
			}else{
				if($(this).attr('title') != $(this).val()){
					$(this).parent().removeClass('error');
					
					
					if($(this).attr('type') == 'checkbox'){
						if(!$(this).is(':checked')){
							errors = true;
							$(this).parent().addClass('error');
							$('.errorContainer2').fadeIn();
						}
					}
					
					if($(this).hasClass('emailField')){
						var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
						if(reg.test($(this).val()) == false){
							errors = true;
							$(this).parent().addClass('error');
							$('.errorContainer2').fadeIn();
						}
						}
				}else{
					errors = true;
					$(this).parent().addClass('error');
					$('.errorContainer2').fadeIn();
				}
				
				if(errors != true){
					errors = false;
					$('.errorContainer2').fadeOut();
				}else{
					errors = true;
				}
			}
			
		});
		
		formName = $(this).attr('id');
		if(!errors){
			$('#'+formName+' .errorContainer').fadeOut();
			$('#'+formName+' .loadingForm').fadeIn();
			formSubmitUrl = $(this).attr('action');
			form = $(this);
			$.ajax({
				type: "POST",
				url: formSubmitUrl,
				data: $(this).serialize(),
				success: function(data){
					if(data == true)
						$('#'+formName+' .loadingForm, #'+formName+' .errorContainer').fadeOut('fast', function(){$('#'+formName+' .successForm').fadeIn('fast', function(){ $('#'+formName+' .formContainer').slideUp(); }); });
					else
						$('#'+formName+' .loadingForm').fadeOut('fast', function(){$('#'+formName+' .errorContainer').fadeIn(); });
				}
			});
			return false;
		
		}else{
			$('#'+formName+' .errorContainer').fadeIn();
		}
		
		return false;
	});
});
