var DataForm = {
	initialize: function() {
		//$('.sourcepage').val(location.href);

		$('#newsletterbox form').submit(function() {
			if (ProcessForm.validate($(this).parents('div').attr('id'))) {
				ProcessForm.send('/gezochtmaatjes2/gm_data.asp',$(this).parents('div').attr('id'));
			}
			return false;
		});
		
		$('#projectbox .newsletter').click(function() {
			ProcessForm.open($(this).attr('class'));
			return false;
		});

		$('#newsletterbox .close').click(function() {
			ProcessForm.close($(this).parents('div').attr('id'));
			return false;
		});
	}
};

var MailForm = {
	
	initialize: function() {
		$('.sourcepage').val(location.href);
	
		$('#sendbox form').submit(function() {
			if (ProcessForm.validate($(this).parents('div').attr('id'))) {
				ProcessForm.send('/gezochtmaatjes2/gm_mail.asp', $(this).parents('div').attr('id'));
			}
			return false;
		});
		
		$('#projectbox .send').click(function() {
			ProcessForm.open($(this).attr('class'));
			return false;
		});

		$('#sendbox .close').click(function() {
			ProcessForm.close($(this).parents('div').attr('id'));
			return false;
		});
	}
};

var ProcessForm = {
	initialize: function() {	
	},

	validate: function(type) {
		
		if ($('#' + type + ' form input[type=text][value=]').length > 0) {
			$('#' + type + ' .error-required').show();
			return false;
		}

		$('#' + type + ' .error-required').hide();

		var email = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
		var emailError = false;

		$('#' + type + ' form input.validate.email').each(function() {
			if (email.test($(this).val()) == false) {
				emailError = true;
			}
		});

		if (emailError) {
			$('#' + type + ' .error-email').show();
			return false;
		}
		
		$('#' + type + ' .error-email').hide();
		return true;
		
	},
	
	open: function(type) {
		$('#' + type + 'box').slideDown('normal', function() {
			$('#' + type + 'box form input[type=text]').get(0).focus();
		});
	},

	close: function(type) {
		$('#' + type).slideUp('fast');
	},
	
	send: function(file, type) {
		
		var data = $('#' + type + ' form').serialize();
		
		var callback = function(result) {

			var result = Math.floor(result);

			if (result == 0) {
				ProcessForm.close(type);
			}

			if (result == 1) {
				$('#' + type + ' .msg-sent').show();
				
				$('#' + type + ' form').css('visibility', 'hidden');
				
				window.setTimeout(function() {
					ProcessForm.close(type);
					window.setTimeout(function() {
						$('#' + type + ' .msg-sent').hide();
						$('#' + type + '  form').css('visibility', 'visible').get(0).reset();
					}, 1000);
				}, 2000);
			}
		}
		
		$.post(file, data, callback);
	}
};

$(function() { ProcessForm.initialize() });
$(function() { DataForm.initialize() });
$(function() { MailForm.initialize() });