
$().ready(function() {

	$('#contactForm').submit(function(){
		data = collectFormData('contactForm');
		$.post
		(
			'/ajax.php?q=contactMail',
			data,
			function(html){
				var res = html.split(';');
				if(res[0] == 'error'){	
					alert(res[1]);
					return false;			
				} else {
					$('#contactForm :input').val('');
					alert(res[1]);
				}
			}
		);
		return false;
	});
	
	$('#articleContactForm').submit(function(){
		data = collectFormData('articleContactForm');
		$.post
		(
			'/ajax.php?q=articleContactMail',
			data,
			function(html){
				var res = html.split(';');
				if(res[0] == 'error'){	
					alert(res[1]);
					return false;			
				} else {
					$('#articleContactForm :input').val('');
					alert(res[1]);
				}
			}
		);
		return false;
	});

});

function collectFormData(formId){
	data = new Object();
	var formValid = true;
	$('#'+formId+' input, #'+formId+' select, #'+formId+' textarea').each(function(i){
		if((this.type == 'checkbox' || this.type == 'radio')){
			if(this.checked)data[$(this).attr('name')] = this.value;
			else
				if(!data[$(this).attr('name')])
					data[$(this).attr('name')] = "0";
		}
		else{
			data[$(this).attr('name')] = $(this).val();
		}
	});
	if(formValid){
		return data;
	}else{
		return false;
	}
}
