
jQuery(function() { 
	
	jQuery(".sender").focus();
	
/*	However, it's this textarea's contents which is sent when the form is submitted. 
	Consequently its contents has to be updated before the form submission.
	With jQuery Form plugin, 
	here's how to update the textareas before the form fields are serialized and sent via Ajax:
	*/
	
	jQuery('#contact_form').submit(function() { 
	    // submit the form 
	    jQuery(this).ajaxSubmit({ 
			beforeSubmit: validateComment,
			url:"/ajax.php",
			success: showResponse
		}); 
	    return false; 
	});

	function showResponse(responseText, statusText)  { 
		jQuery('#contact_form').resetForm(); // clear form		
		jQuery('#contact_form').clearForm(); // clear form		
		//tinyMCE.getInstanceById('msg').setContent(''); // tinyMCE clear
		//tinyMCE.init({cleanup: true});
		jQuery.jGrowl(responseText, { header: 'Köszönjük!', position: 'top-left' });
		jQuery.unblockUI();
		return false;
	}
	
	function validateComment(formData, jqForm, options) { 
		jQuery.blockUI();
	    var form = jqForm[0]; 
	
	    if (!form.sender.value || !form.subject.value || !form.address.value) { 
			jQuery.jGrowl("Az összes mező kitöltése kötelező.", { header: 'Figyelmeztetés', theme: 'attention' });
			jQuery.unblockUI();
	        return false; 
	    } 
	
	    if (form.sender.value) { 
			var filter= /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
			if (filter.test(form.sender.value)) {
				return true; }
			else {
				jQuery.jGrowl("Helytelen e-mail cím!", { header: 'Figyelmeztetés', theme: 'attention' });
				jQuery.unblockUI();
	        	return false; }
	    }	
	}
	
});
