
$(document).ready(function () {
	$('#contactForm').click(function (e) {
		e.preventDefault();
		// load the contact form using ajax
		$.get("../termin/contact.php", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				overlayId: 'contactModalOverlay',
				containerId: 'contactModalContainer',
				iframeId: 'contactModalIframe',
				onOpen: contact.open,
				onShow: contact.show,
				onClose: contact.close
			});
		});
	});
});

var contact = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.content.fadeIn(200, function () {
					$('#contactModalContainer #name').focus();
				});
				// resize the textarea for safari
				if ($.browser.safari) {
					$('#contactModalContainer textarea').attr({
						cols: '37',
						rows: '8'
					});
				}
			});
		});
	},
	show: function (dialog) {
		$('#contactModalContainer .send').click(function (e) {
			e.preventDefault();
			// validate form
			if (contact.validate()) {
				$('#contactModalContainer .message').fadeOut(function () {
					$('#contactModalContainer .message').removeClass('error').empty();
				});
				$('#contactModalContainer .title').html('Ihre Terminanfrage wird gesendet...');
				$('#contactModalContainer form').fadeOut(200);
				$('#contactModalContainer .content').animate({
					height: '80px'
				}, function () {
					$('#contactModalContainer .loading').fadeIn(200, function () {
						$.ajax({
							url: '../termin/contact.php',
							data: $('#contactModalContainer form').serialize() + '&action=send',
							dataType: 'html',
							complete: function (xhr) {
								$('#contactModalContainer .loading').fadeOut(200, function () {
									$('#contactModalContainer .title').html('<div align="center"><h1>Vielen Dank!</h1></div>');
									$('#contactModalContainer .message').html('Ihre Terminanfrage ist bei uns eingegangen.<br/>Wir werden uns in K&uuml;rze bei Ihnen melden.<br/>Ihr Luca Rizzo & Team').fadeIn(200);
								});
							},
							error: contact.error
						});
					});
				});
			}
			else {
				if ($('#contactModalContainer .message:visible').length > 0) {
					$('#contactModalContainer .message div').fadeOut(200, function () {
						$('#contactModalContainer .message div').empty();
						contact.showError();
						$('#contactModalContainer .message div').fadeIn(200);
					});
				}
				else {
					$('#contactModalContainer .message').animate({
						height: '100px'
					}, contact.showError);
				}
				
			}
		});
	},
	close: function (dialog) {
		dialog.content.fadeOut(300, function () {
			dialog.container.fadeOut(300, function () {
				dialog.overlay.fadeOut(300, function () {
					$.modal.remove(dialog);
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	validate: function () {
		contact.message = '';
		if (!$('#contactModalContainer #name').val()) {
			contact.message += 'Bitte geben Sie Ihren Vornamen ein<br>';
		}
		
		if (!$('#contactModalContainer #nachname').val()) {
			contact.message += 'Bitte geben Sie Ihren Nachnamen ein<br>';
		}
		
		var email = $('#contactModalContainer #email').val();
		if (!email) {
			contact.message += 'Bitte geben Sie Ihre eMailadresse ein<br>';
		}
		
		else {
			// Regex from: http://regexlib.com/REDetails.aspx?regexp_id=599
			var filter = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$/;
			if (!filter.test(email)) {
				contact.message += 'Im Feld eMail stimmt etwas nicht<br>';
			}
		}

		var telefon = $('#contactModalContainer #telefon').val();
		if (!telefon) {
			contact.message += 'Bitte geben Sie Ihre Telefonnummer ein<br>';
		}
		
		else { 
			// Regex from: http://regexlib.com/REDetails.aspx?regexp_id=599
			var filter = /^[0-9-()/+.]*$/;
			if (!filter.test(telefon)) {
				contact.message += 'Im Feld Telefonnummer stimmt etwas nicht<br>';
			}
		}
		
		if (!$('#contactModalContainer #wunsch').val()) {
			contact.checkbox.wunsch += 'Bitte geben Sie eine wunsch ein<br>';
		}

		if (contact.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
		

	},
	showError: function () {
		$('#contactModalContainer .message').html($('<div class="error"></div>').append(contact.message)).fadeIn(200);
	}
};
