// Start functioning when DOM is ready
$( function()
{
	// Set var with old number of tickets
	var o = 0;
	
	$('#Ticketamount').change( function()
	{
		// Get the new amount as Number object
		var n = parseInt( $(this).val() );
		
		// Check wether to add or remove tables
		if ( n < o ) {
			// Delete any tables left
			$('form[name="ticketform"] > table.userinfo:gt('+(n-1)+')').prev().remove();
			$('form[name="ticketform"] > table.userinfo:gt('+(n-1)+')').remove();
		} 
		else
		{
			// Check how many tables there are at the moment
			var t = $('form[name="ticketform"] > table.userinfo').length;
			
			// Do the cloning
			for ( var i=t; i<n; i++ ) {
				// Clone first table after the current last one
				$('<hr/>')
					.insertAfter('form[name="ticketform"] > table.userinfo:last')
					.after(
						$('form[name="ticketform"] > table.userinfo:first').clone()
					);
			}
		}
		
		// Update the background colors and the fieldname numbers
		$('form[name="ticketform"] > table.userinfo:not(:first)').each( function(i)
		{
			// Dont update even tables
			if ( i % 2 ) $(this).css( 'backgroundColor', '#fff' );
			
			// Give odd ones a gray background
			else $(this).css( 'backgroundColor', '#ddd' );
			
			// Set the new number for the fieldname
			var f = i+1;
			
			// Loop through all fields
			$('tr', this).each( function() {
				// Get the current ticket name
				var c = $('td:last input', this).attr('name');
				
				// Do the splitting
				c = c.split('tickets[')[1].split(']')[1];
				
				// Set the new ticket name
				$('td:last input', this).attr( 'name', 'tickets['+f+']'+c+']' );
			});
		});
		
		// Set o to new amount
		o = n;
	});
	
	
	$('form[name="ticketform"]').submit( function()
	{
		// set status to true by default
		var state = true;
		
		// set var for border colors
		var gray	= '#CCCCCC';
		var red		= '#E72C5A';
		
		// Loop through all fields and check their value
		$(':input', this).each( function()
		{
			// Skip if the field is 'Organisatie' or a button
			if ( $(this).attr('id') != 'Organisatie' 
					&& ! $(this).is(':submit') 
					&& $(this).attr('id') != 'Ticketamount' 
					&& $(this).attr('id') != 'factuur_adres'
					&& $(this).attr('id') != 'factuur_postcode'
					&& $(this).attr('id') != 'factuur_plaats'
					&& $(this).attr('id') != 'Tussenvoegsel' )
			{
				// Check if the value is empty or not
				if ( $(this).val() == '' ) {
					// Set state
					state = false;
					
					// Set color
					$(this).css( 'borderColor', red );
					
					// Set message
					if ( $(this).next().attr('class') != 'form_error_msg' )
						$(this).after( '<div class="form_error_msg">Verplicht veld</div>' )
				} else {
					// Remove the red border
					$(this).css( 'borderColor', gray );
					
					// Delete the message, if it isset
					if ( $(this).next().attr('class') == 'form_error_msg' )
						$(this).next().remove();
				}
			}
		});
		
		return state;
	});
});
