function ppcconversion() {
	var iframe = document.createElement('iframe');
	iframe.style.width = '0px';
	iframe.style.height = '0px';
	document.body.appendChild(iframe);
	iframe.src = 'http://www.komma-d.de/theme/templates/layout/conversion.phtml';
};


$(document).ready(function(){

	$('input#firm').bind('focus', function() {
		$("input#firm").removeClass('red');
		oldValue = $(this).val();
		$(this).val('');
	});

	$('input#firm').bind('blur', function() {
		newValue = $(this).val();
		if (newValue == '') {
			$(this).val(oldValue);
		}
	});

	$('input#name').bind('focus', function() {
		$("input#name").removeClass('red');
		oldValue = $(this).val();
		$(this).val('');
	});

	$('input#name').bind('blur', function() {
		newValue = $(this).val();
		if (newValue == '') {
			$(this).val(oldValue);
		}
	});
	
	$('input#phone').bind('focus', function() {
		$("input#phone").removeClass('red');
		oldValue = $(this).val();
		$(this).val('');
	});

	$('input#phone').bind('blur', function() {
		newValue = $(this).val();
		if (newValue == '') {
			$(this).val(oldValue);
		}
	});
	
	$('input#email').bind('focus', function() {
		$("input#email").removeClass('red');
		oldValue = $(this).val();
		$(this).val('');
	});

	$('input#email').bind('blur', function() {
		newValue = $(this).val();
		if (newValue == '') {
			$(this).val(oldValue);
		}
	});
	
	$('#message').bind('focus', function() {
		$("#message").removeClass('red');
		oldValue = $(this).val();
		/* $(this).val(''); */
	});

	$('#message').bind('blur', function() {
		newValue = $(this).val();
		if (newValue == '') {
			$(this).val(oldValue);
		}
	});

	$('input#captcha').bind('focus', function() {
		$("input#captcha").removeClass('red');
		oldValue = $(this).val();
		$(this).val('');
	});

	$('input#captcha').bind('blur', function() {
		newValue = $(this).val();
		if (newValue == '') {
			$(this).val(oldValue);
		}
	});
	



	$(".sendbutton").click(function() {
		
		var name = $("input#name").val();
		if (name == "" | name == "Name *") {
			$("input#name").val("Bitte geben Sie Ihren Namen an.");
			$("input#name").addClass('red');
			return false;
		}
		
		var phone = $("input#phone").val();
		if (phone == "" | phone == "Telefon *") {
			$("input#phone").val("Bitte geben Sie Ihre Telefonnummer an.");
			$("input#phone").addClass('red');
			return false;
		}

		var email = $("input#email").val();
		if (email == "" | email == "E-Mail *") {
			$("input#email").val("Bitte geben Sie Ihre E-Mail Adresse an.");
			$("input#email").addClass('red');
			return false;
		}
		
		if (!email == "") {
				if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
			}
			else{
				$("input#email").val("Bitte geben Sie eine gültige E-Mail Adresse an.");
				$("input#email").addClass('red');
				return false;
			}	
		}

		var message = $("#message").val();
		if (message == "") {
		/* $("#message").focus(); */
			$("#message").val("Bitte machen Sie eine Angabe in diesem Feld.");
			$("#message").addClass('red');
			return false;
		}
		
		var captcha = $("input#captcha").val();
		if (captcha == "") {
			$("input#captcha").val("Bitte übertragen Sie den Code links in dieses Feld.");
			$("input#captcha").addClass('red');
			return false;
		}

		var dataString = $("#anfrageForm").serialize();

		$.ajax({
			type: "POST",
			url: "/theme/templates/action/act_anfrage.php",
			dataType : 'json',
			data: dataString,
			success: function(data) {
				if (data.error === true){
					$('#message-box').html("<h3 class=\"yellow\">Leider war Ihr Captcha-Code nicht richtig. Versuchen Sie es bitte noch einmal.</h3>")
					.fadeIn(1500, function() {
						$('#message-box').append("");
					});
				}
				else{
					$('#contact_form').html("<div id='message'></div>");
					$('#message-box').hide();
					$('#message').html("<br /><br /><h2>Vielen Dank für Ihre Nachricht!<br/>Wir werden uns schnellst möglich mit Ihnen in Verbindung setzen.</h2>")
					.append("<p></p>")
					.hide()
					.fadeIn(1500, function() {
						$('#message').append("");
					});
					ppcconversion();
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$('#contact_form').html("<div id='message'></div>");
				$('#message').html("<br /><br /><h2>Upps... leider hat unser Server ein Problem beim Verarbeiten Ihrer Daten. Versuch Sie es bitte noch einmal oder schreiben Sie uns eine Mail an hallo@komma-d.de.</h2>")
				.append("<p></p>")
				/* .hide() */
				.fadeIn(1500, function() {
					$('#message').append("");
				});
			}
		
		});
	});
});		
