jQuery(document).ready( function() {
	
	jQuery('#evite').bind('submit', validateForm);
	
	jQuery('input.select_service_time').bind('click', updateServiceTime);
	jQuery('#captcha a.reload').bind('click', reloadCaptcha);

	jQuery('#captcha input').bind('change', validateCaptchaInput);
	jQuery('#captcha input').bind('mouseleave', validateCaptchaInput);
	
	jQuery('a.add_recipient').bind('click', addRecipient); 
	bindRecipientEventHandlers();
	
	jQuery('input#sender_first_name').blur( function(event) {
		first_name = jQuery(event.target).val();
		jQuery('.sender_first_name').html(first_name);
	});
	
	
	jQuery('input#sender_last_name').blur( function(event) {
		last_name = jQuery(event.target).val();
		jQuery('.sender_last_name').html(last_name);
	});

	jQuery('input#sender_email').blur( function(event) {
		emailValue = jQuery(event.target).val();
		jQuery('.sender_email').html('&lt;' + emailValue + '&gt;');
		invitationUID = jQuery('#invitation').val();
		
		jQuery.post('/index.php', {
				eID: 'tx_cfevite_validate',
				invitation: invitationUID,
				type: 'sender',
				email: emailValue
			}, function(xml, status) {
				if(status == 'success') {
					
					valid = parseInt(jQuery(xml).find('valid').text());
					reason = jQuery(xml).find('reason').text();

					jQuery('div#sender p.error-message').remove();
					
					if(!valid && reason) {
						jQuery('div#sender').prepend('<p class="error-message">' + reason + '</p>');
						addErrorClass(jQuery('div#sender'));
					} else {
						removeErrorClass(jQuery('div#sender'));
					}
				}
			}
		);
	});
	
	jQuery('#message-body').attr("title", "Click to edit...");
	jQuery("#message-body").click( function(event) {
		
		jQuery("#message-body").css('display', 'none');
		
		filteredHTML = jQuery("#message-body").text();
		jQuery("#message-body").after('<textarea name="tx_cfevite_pi1[message]" cols="65" rows="10" class="text">' + filteredHTML + '</textarea>');
	});
	
});

function addRecipient(event) {
	count = jQuery('ul.recipients li').length + 1;	
	html = '<li><div><label for="recipient_' + count + '_first">First Name</label><input name="tx_cfevite_pi1[recipient][' + count + '][first_name]" id="recipient_' + count + '_first" class="text first_name" /><label for="recipient_' + count + '_last">Last Name</label><input name="tx_cfevite_pi1[recipient][' + count + '][last_name]" id="recipient_' + count + '_last" class="text last_name" /></div><div><label for="recipient_' + count + '_email">Email</label><input name="tx_cfevite_pi1[recipient][' + count + '][email]" id="recipient_' + count + '_email" class="text email" /></div><div class="controls"><a class="remove_recipient" title="Remove Recipient" href="#"><img src="/typo3conf/ext/cf_evite/pi1/res/application/images/cancel.png" alt="Remove Recipient" /> Remove Recipient</a>&nbsp;&nbsp;&nbsp;&nbsp; <a class="preview_recipient" title="Preview Recipient" href="#"><img src="/typo3conf/ext/cf_evite/pi1/res/application/images/preview.png" alt="Preview Recipient" /> Preview Recipient</a></div></li>';
	jQuery("ul.recipients").append(html).fadeIn('slow');
	
	bindRecipientEventHandlers();
	event.preventDefault();
}

function removeRecipient(event) {
	jQuery(this).parent().parent().remove();
	event.preventDefault();
}

function previewRecipient (event) {
	recipient = jQuery(this).parent().parent();
	
	first_name = recipient.find('.first_name').val();
	jQuery('.recipient_first_name').html(first_name);
	
	last_name = recipient.find('.last_name').val();
	jQuery('.recipient_last_name').html(last_name);

	email = recipient.find('.email').val();
	jQuery('.recipient_email').html('&lt;' + email + '&gt;');
	
	event.preventDefault();
}

function updateRecipientFirstName (event) {
	email = jQuery(event.target).val();
	jQuery('.recipient_first_name').html(email);	
}

function updateRecipientLastName (event) {
	email = jQuery(event.target).val();
	jQuery('.recipient_last_name').html(email);	
}


function validateRecipientEmail (event) {
	emailValue = jQuery(event.target).val();
	invitationUID = jQuery('#invitation').val();
	
	jQuery('.recipient_email').html('&lt;' + emailValue + '&gt;');
	
	recipient = jQuery(this).parent().parent();
	
	jQuery.post('index.php', {
			eID: 'tx_cfevite_validate',
			invitation: invitationUID,
			type: 'recipient',
			email: emailValue
		}, function(xml, status) {
			valid = parseInt(jQuery(xml).find('valid').text());
			reason = jQuery(xml).find('reason').text();

			if(status == 'success') {
				valid = parseInt(jQuery(xml).find('valid').text());
				reason = jQuery(xml).find('reason').text();

				jQuery(recipient).find('p.error-message').remove();
				if(!valid) {
					addErrorClass(recipient);
					jQuery(recipient).prepend('<p class="error-message">' + reason + '</p>');
				} else {
					removeErrorClass(recipient);
				}
			}
			
		}
	);
}

function bindRecipientEventHandlers() {
	jQuery('a.remove_recipient').bind('click', removeRecipient);
	jQuery('a.preview_recipient').bind('click', previewRecipient);
	
	jQuery('ul.recipients li .first_name').bind('blur', updateRecipientFirstName);
	jQuery('ul.recipients li .last_name').bind('blur', updateRecipientLastName);
	jQuery('ul.recipients li .email').bind('blur', validateRecipientEmail);
}

function updateServiceTime (event) {
	serviceTime = jQuery(event.target).val();
	jQuery('.service_time').html(serviceTime);	
}

function validateCaptchaInput (event) {
	inputValue = jQuery(event.target).val();
	
	jQuery.post('index.php', {
			eID: 'tx_cfevite_captcha',
			action: 'validate',
			string: inputValue
		}, function(xml, status) {
			if(status == 'success') {
				valid = parseInt(jQuery(xml).find('valid').text());
				
				if(!valid) {
					addErrorClass(jQuery('#captcha'));
				} else {
					removeErrorClass(jQuery('#captcha'));
				}
			}
		}
	);
}

function reloadCaptcha (event) {
	jQuery('#captcha img').replaceWith('<img alt="" src="/typo3conf/ext/captcha/captcha/captcha.php?' + Math.random() + '"/>');
	event.preventDefault();
}

function validateForm (event) {
	inputValue = jQuery('#captcha input').val();
	
	jQuery.post('index.php', {
			eID: 'tx_cfevite_captcha',
			action: 'validate',
			string: inputValue
		}, function(xml, status) {
			if(status == 'success') {
				valid = parseInt(jQuery(xml).find('valid').text());
				
				if(!valid) {
					addErrorClass(jQuery('#captcha'));
				}  else {
					jQuery('#evite').unbind('submit', validateForm);
					jQuery('#evite').submit();
				}
			} else {
				jQuery('#evite').unbind('submit', validateForm);
				jQuery('#evite').submit();
			}
		}
	);
	
	event.preventDefault();
	
}

function addErrorClass (elt) {
	if(elt.hasClass('error')) {
		elt.animate({ opacity: 0.5 }, function() {
			elt.animate({ opacity: 1 });
		});
	} else {
		elt.addClass('error');
	}
}

function removeErrorClass (elt) {
	elt.removeClass('error');
}

