Event.observe(window, 'load', function() 
{
	var ecardie = new Ecard();
	
});


var Ecard = Class.create(
{
	initialize: function()
	{
		this.recipients = new Array();
		this.sendername = '';
		this.sendermail = '';
		this.personalmessage = '';
		this.selectedindex = '';

		this.addBehaviour();
	},
	addBehaviour: function()
	{
		if ($('addtorecipients'))
		{
			Event.observe('addtorecipients', 'click', this.addOption.bindAsEventListener(this, false));
			Event.observe('modifyinlist', 'click', this.addOption.bindAsEventListener(this, true));
			Event.observe('deleteinlist', 'click', this.deleteOption.bindAsEventListener(this));
			Event.observe('recipients', 'click', this.clickToModify.bindAsEventListener(this));
			Event.observe('allrecipients', 'click', this.selectAll.bindAsEventListener(this));
			Event.observe('submit', 'click', this.checkAllInputs.bindAsEventListener(this));
		}
	},
	addOption: function(event, modify)
	{
		
		if((!$('recipientname').value.strip().empty()) && (!$('recipientmail').value.strip().empty()))
		{
			var allowedtoad = true;
			var inputvalue  = $('recipientname').value.strip()+'|'+$('recipientmail').value.strip();

			if(this.recipients.length > 0)
			{
				for(var i=0; i < this.recipients.length; i++)
				{
					if(this.recipients[i].value == inputvalue)
					{
						allowedtoad = false;
					}
				}
				if(allowedtoad)
				{
					if(modify)
					{
						this.recipients[this.selectedindex] = inputvalue;
						this.selectedindex = '';
					}
					else
					{
						this.recipients.push(inputvalue);
						this.selectedindex = ''; 
					}
				}	
			}
			else
			{
				if(!modify)
				{
					this.recipients.push(inputvalue);
					this.selectedindex = '';
				}
			}

			$('recipientname').value = '';
			$('recipientmail').value = '';
			$('recipientname').focus();
			
		}
	this.createoptions();
	this.selectAll();
	},
	clickToModify: function(event)
	{
		if(!event.element().value.empty())
		{
			var splittedval = event.element().value.split('|');

			$('recipientname').value = splittedval[0];
			$('recipientmail').value = splittedval[1];
			$('recipientname').focus();

			if(event.element().index == undefined)
			{
				this.selectedindex = this.recipients.length -1;
			}
			else
			{
				this.selectedindex = event.element().index;
			}
		}
	},
	deleteOption: function(event)
	{
		var newrecipients = new Array();

		for(var i = 0; i < this.recipients.length; i++ )
		{
			if(i != this.selectedindex)
			{
				newrecipients.push(this.recipients[i]);
			}
		}

		$('recipientname').value = '';
		$('recipientmail').value = '';
		$('recipientname').focus();

		this.recipients = newrecipients;

		this.selectedindex = ''; 
		this.createoptions();
	},
	createoptions: function()
	{
		var indinodes = new Array();
		// recipients leegmaken.
		$('recipients').length = 0;
		
		for(var i = 0; i < this.recipients.length; i++ )
		{
			var valueman = this.recipients[i].split('|');
			indinodes[i]	=	Builder.node( 'option', { value: this.recipients[i]}, valueman[0]+' - '+valueman[1]);
			$('recipients').insert(indinodes[i], 'after'); 
		}
	},
	selectAll: function()
	{
		for(var i=0; i < $('recipients').options.length; i++)
		{
			$('recipients').options[i].selected = true;
		}
	},
	checkAllInputs: function(event)
	{
		var errors = new Array();
		
		if($('sendername').value.strip().empty())
		{
			errors.push('naam verzender ontbreekt');
		}

		if($('sendermail').value.strip().empty())
		{
			errors.push('emailadres verzender ontbreekt');
		}

		if($('recipients').options.length == 0 && $('recipientname').value.length == 0 && $('recipientmail').value.length == 0)
		{
			errors.push('geen ontvangers ingevuld');
		}

		if($('personalreaction').value.strip().empty())
		{
			errors.push('persoonlijk bericht ontbreekt');
		}
		
		if(errors.length > 0)
		{
			alert(errors);
			Event.stop(event); 
		}
	}
}
);

