$(document).ready( function()
{

	var fields = new Array("#name","#email","#message","#captcha", "#subject");
	
	 
	
	$('#refreshCaptcha').click( function()
	{
		$('img[alt="captcha"]').attr('src', 'captcha.php?'+ new Date().getTime());
		
	});
	
	function checkForm(fields)
	{
		var check = true;
		for(key in fields)
		{
			$(fields[key]).css({'border':'1px inset silver'});
			if($(fields[key]).val() == '')
			{
				$(fields[key]).css({'border':'1px solid red'});
				check = false;
			}
		}
		return check;
	}
			
			
			
	function resetForm(fields)
	{
		for(key in fields)
		{
			$(fields[key])
				.css({'border':'1px inset silver'})
				.val('');
		}
	}
	
	$('#contact').submit(function(e) 
	{
		//alert("maaah!");
		e.preventDefault();
		
		//resetForm(fields);
		if(checkForm(fields))
		{
			$('#contact').ajaxSubmit({
				url:"/service.php",
				type:'post',
				dataType: 'json',
				//resetForm: true,
				success: function(data)
				{
					
					if(data.type == 'error') 
					
					{
					    alert("Programmfehler, Reparatur folgt (Onlineredaktion EKHN)");
						if(data.msg == 'captcha')
						   
							$('#captcha').css({'border':'1px solid red'});
						
						else if(data.msg == 'email')
							$('#email').css({'border':'1px solid red'});
					}
					else if(data.type == 'success')
					{
						resetForm(fields);
						alert("Vielen Dank fuer Ihre Nachricht!");
					}
				}
			});
		}
		else
		{
			alert("Bitte füllen Sie alle markierten Felder aus");
		}
		e.preventDefault();
	});		
});


