/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2007 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: contact.js 39 2007-10-26 20:46:32Z emartin24 $
 *
 */

$(document).ready(function () {
	
	$('#tellForm a[name="tell-a-friend"]').click(function (e) {
		e.preventDefault();
		// load the contact form using ajax
		$.get("data/tell-a-friend.php", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				overlayId: 'tellModalOverlay',
				containerId: 'tellModalContainer',
				iframeId: 'tellModalIframe',
				onOpen: tell.open,
				onShow: tell.show,
				onClose: tell.close
			});
		});
	});
});

var tell = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
												   
				dialog.content.fadeIn(200, function () {
					$('#tellModalContainer #yourname').focus();
				});
				
				
				
			});
		});
	},
	show: function (dialog) {
		$('#tellModalContainer .send').click(function (e) {
			e.preventDefault();
			// validate form
			if (tell.validate()) {
				$('#tellModalContainer .message').fadeOut(function () {
					$('#tellModalContainer .message').removeClass('error').empty();
				});
				$('#tellModalContainer .title').html('Sende...');
				$('#tellModalContainer form').fadeOut(200);
				$('#tellModalContainer .content').animate({
					height: '80px'
				}, function () {
					$('#tellModalContainer .loading').fadeIn(200, function () {
						$.ajax({
							url: 'data/tell-a-friend.php',
							data: $('#tellModalContainer form').serialize() + '&action=send',
							dataType: 'html',
							complete: function (xhr) {
								$('#tellModalContainer .loading').fadeOut(200, function () {
									$('#tellModalContainer .title').html('Vielen Dank!');
									$('#tellModalContainer .message').html(xhr.responseText).fadeIn(200);
								});
							},
							error: tell.error
						});
					});
				});
			}
			else {
				if ($('#tellModalContainer .message:visible').length > 0) {
					$('#tellModalContainer .message div').fadeOut(200, function () {
						$('#tellModalContainer .message div').empty();
						tell.showError();
						$('#tellModalContainer .message div').fadeIn(200);
					});
				}
				else {
					$('#tellModalContainer .message').animate({
						height: '30px'
					}, tell.showError);
				}
				
			}
		});
	},
	close: function (dialog) {
		dialog.content.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.remove(dialog);
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	validate: function () {
		tell.message = '';
		/*if (!$('#tellModalContainer #name').val()) {
			tell.message += 'Name is required. ';
		}*/

		var youremail = $('#tellModalContainer #youremail').val();
		var theiremail = $('#tellModalContainer #theiremail').val();
		var yourname = $('#tellModalContainer #yourname').val();
		var theirname = $('#tellModalContainer #theirname').val();
		
		 
		if (!theiremail) {
			
			tell.message = 'Bitte geben Sie die E-Mail-Adresse des Empf&auml;ngers ein.';
			$('#tellModalContainer #theiremail').focus();
			
		} 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(theiremail)) {
				tell.message = 'Die E-Mail-Adresse ist ung&uuml;ltig. ';
				$('#tellModalContainer #theiremail').focus();
			}
			
		}
		
		if (!theirname) {
			
			tell.message = 'Bitte geben Sie den Empf&auml;ngernamen ein.';
			$('#tellModalContainer #theirname').focus();
			
		}


		if (!youremail) {
			
			tell.message = 'Bitte geben Sie eine E-Mail-Adresse ein.';
			$('#tellModalContainer #youremail').focus();
			
		} 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(youremail)) {
				tell.message = 'Die E-Mail-Adresse ist ung&uuml;ltig.';
				$('#tellModalContainer #youremail').focus();
			}
			
		}
		
		if (!yourname) {
			
			tell.message = 'Bitte geben Sie Ihren Namen ein.';
			$('#tellModalContainer #yourname').focus();
		}
		
		/*if (!$('#tellModalContainer #message').val()) {
			tell.message += 'Message is required.';
		}*/

		if (tell.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	showError: function () {
		$('#tellModalContainer .message').html($('<div class="error"></div>').append(tell.message)).fadeIn(200);
	}
};