window.addEvent('domready', function(){
	
	/*	Initialize 'Add Recipients' features */
	
		if($('eCardCustomize')) setupCustomizer();
		if($('eCardsPreview')) setupPreview();
	
});

/*	customize-ecard.php */
	
	var eCardTemplate	= '',
		eCardConfig		= $H({});
		
	function setupCustomizer(){
		
		/*	Variables */
		
			var lastItem	= $('recipientLast'),
				i;
		
		/*	Add 5 recipients */
		
			addRecipient();
			
		/*	Store template */
		
			eCardTemplate = $('eCardTemplate').get('html');
			
		/*	Setup buttons */
		
			$('addRecipientBtn').addEvent('click', addRecipient);
			$('previewBtn').addEvent('click', refreshPreview);
			
		/*	Refresh */
		
			$$('.ecardContents').each(function(M, N) {
			
				M.setStyle('display', 'none');
			
			});
		
	}
	
	function refreshPreview(){
	
		/*	Show hidden text */
		
			$$('.ecardContents').each(function(M, N) {
			
				M.setStyle('display', '');
			
			});
		
		/*	Prepare */
		
			eCardConfig.set('contents', $('contents').getProperty('value').replace(/\n/gi, '<br />'));
			eCardConfig.set('subject', $('subject').getProperty('value'));
			eCardConfig.set('fromName', $('fromName').getProperty('value'));
			eCardConfig.set('fromEmail', $('fromEmail').getProperty('value'));
			eCardConfig.set('recipientName', '{recipientName}');
		
		/*	Publish */
		
			$('eCardTemplate').set('html', eCardTemplate.substitute(eCardConfig));
		
	}
	
	var r = 0;
	function addRecipient(){
		
		/*	Variables */
		
			var lastItem	= $('recipientLast');
			
		/*	Increment */
		
			r ++;
		
		/*	Add elements */
		
			new Element('input', {'id': 'recipientName' + r, 'class': 'recipientName textField required', 'type': 'text'}).injectInside(new Element('dd', {'class': 'first'}).injectBefore(lastItem));
			new Element('input', {'id': 'recipientEmail' + r, 'class': 'recipientEmail textField email required', 'type': 'text'}).injectInside(new Element('dd', {'class': 'second'}).injectBefore(lastItem));
			new Element('a', {'text': 'Remove', 'class': 'removeRecipientBtn', 'events': {'click': removeRecipient}}).injectInside(new Element('dd', {'class': 'third'}).injectBefore(lastItem));
		
	}
	
	function removeRecipient(e){
		
		/*	Variables */
		
			var e	= new Event(e).target;
			
		/*	Remove */
		
			e.getParent().getPrevious().destroy();
			e.getParent().getPrevious().destroy();
			e.getParent().destroy();
			
		/*	Ensure at least one recipient is available */
		
			if($$('.recipientName').length == 0) addRecipient();
		
	}
	
	function sendEcard(){
		
		/*	Variables */
		
			var valid	= true,
				data	= $H({});
		
		/*	Validate */
		
			/*	Required fields */
			
				$$('.required').each(function(field, key){
					
					field.removeClass('error');
					data.set(field.getProperty('id'), field.getProperty('value'));
					if(field.getProperty('value').trim() == ''){
						
						valid = false;
						field.addClass('error');
						
					}
					
				});
				
				if(!valid){
					
					alert('Please fill out all fields!');
					return false;
					
				}
				
			/*	Email validations */
			
				$$('.email').each(function(field, key){
					
					field.removeClass('error');
				
					if(!regex.test(field.getProperty('value').trim())){
						
						valid = false;
						field.addClass('error');
						
					}
					
				});
				
				if(!valid){
					
					alert('Please provide a valid email address for the highlighted fields!');
					return false;
					
				}
		
		/*	Submit */
		
			$('customizedData').setProperty('value', JSON.encode(data));
			return true;
		
	}
	
var regex = /^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$/;