/*
 * Specific script for GPEC submit form
 */

$('document').ready( function() {
	$('.form_step h2 a').bind('click', stepCallBack );
	
	$('#add_diploma').bind('click', addDiploma );
	$('#remove_diploma').bind('click', removeDiploma );
	$('#add_language').bind('click', addLanguage );
	$('#remove_language').bind('click', removeLanguage );
	$('#add_experience').bind('click', addExperience );
	$('#remove_experience').bind('click', removeExperience );
	
	$('input:regex(class, ezcca-gpec_submit_form_language_.*_other)').bind('keypress', otherLanguageSpecified);
	
	$('select:regex(class, ezcca-gpec_submit_form_language_[0-9]+$)').bind('change', mainLanguageSelected);
});

function mainLanguageSelected()
{
	// if main language selected, empty "other language" field
	if ($(this).val() != 'AUT')
	{
		var target = $(this).parent().attr('target');
		$('.' + target).val('');
	}
}

function otherLanguageSpecified()
{
	var target = $(this).parent().attr('target');
	
	// Change visible select list */
	$('.' + target).val('AUT');
	
	// Change hidden field
	$('.' + target).parent().children('input:first').val('AUT');
}

function stepCallBack()
{
	$('.form_step h2 a').unbind('click');
	$('.form_step h2 a').bind('click', function(){ return false; });
	
	if(!$(this).closest('.form_step').hasClass('open'))
	{
		// Close all
		$('.form_step').find('.content').slideUp('slow', function() {
			$('.form_step').addClass('closed');
			$('.form_step').removeClass('open');
		});
		
		// Open the one clicked on
		$(this).closest('.form_step').find('.content').slideDown('slow', function() { 
			$(this).closest('.form_step').removeClass('closed');
			$(this).closest('.form_step').addClass('open');
			
			// If open step 2, display rows containing data
			if( $(this).find('.line.hide').size() > 0 )
			{
				// Get hidden inputs containing data
				$hidden_inputs = $(this).find('.line.hide').find('input');
				$hidden_input_to_show = null;
				// If we get one hidden element containing data, we show the line				
				$hidden_inputs.each(function(){
					if ($(this).val() != "")
					{
						if ($(this).parents('tr.line').hasClass('hide'))
						{
							$(this).parents('tr.line').addClass('visible');
							$(this).parents('tr.line').removeClass('hide');
						}
					}
				});
				
				// Display "remove buttons" when needed
				if ($(this).find('.line.visible.diploma').size() > 1)
				{
					$('#remove_diploma').removeClass('hide');
				}
				if ($(this).find('.line.visible.language').size() > 1)
				{
					$('#remove_language').removeClass('hide');
				}
				if ($(this).find('.line.visible.experience').size() > 1)
				{
					$('#remove_experience').removeClass('hide');
				}
				
				// Hide "add buttons" when needed
				if ($('.step_two .diplomas .line.hide').length == 0)
				{
					$('#add_diploma').addClass('hide');
				}
				if ($('.step_two .languages .line.hide').length == 0)
				{
					$('#add_language').addClass('hide');
				}
				if ($('.step_two .experiences .line.hide').length == 0)
				{
					$('#add_experience').addClass('hide');
				}
				
			}
			
			$('.form_step h2 a').bind('click', stepCallBack );
		});
	}
	else
	{
		$('.form_step h2 a').bind('click', stepCallBack );
	}
	
	return false;
}

// Display next line if any to display
function addDiploma()
{
	if ($('.step_two .diplomas .line.hide').length > 0)
	{
		$('.step_two .diplomas .line.hide:eq(0)').addClass('visible');
		$('.step_two .diplomas .line.hide:eq(0)').removeClass('hide');
		
		if ($('.step_two .diplomas .line.hide').length == 0)
		{
			$('#add_diploma').addClass('hide');
		}
	}
	
	// Hide add button when no other line to display
	if ($('#remove_diploma').hasClass('hide'))
	{
		$('#remove_diploma').removeClass('hide');
	}

	return false;
}

function removeDiploma()
{
	// Display add button if it was hidden
	if ($('#add_diploma').hasClass('hide'))
	{
		$('#add_diploma').removeClass('hide');
	}
	
	// Empty and remove lines excepted the last one
	if($('.step_two .diplomas .line:visible').length >= 2)
	{	
		// Store select lists
		fields_to_empty = $('.step_two .diplomas .line.visible:last').find('select');
		
		// Empty all fields of the last line
		$('.step_two .diplomas .line.visible:last').find(':input').each(function(index) {
			$(this).val('') ;
		});
		
		// Special treatment for IE6 : empty select list
		for(var i=0; i < fields_to_empty.length; i++)
		{
			fields_to_empty[i].value = '';
			if(fields_to_empty[i].type == 'select-one')
			{
				fields_to_empty[i].selectedIndex = 0;
			}
		}
		
		// Hide last line
		$('.step_two .diplomas .line.visible:last').addClass('hide');
		$('.step_two .diplomas .line.visible:last').removeClass('visible');
	}
	if($('.step_two .diplomas .line.visible').length == 1)
	{
		$('#remove_diploma').addClass('hide');
	}
	

	return false;
}

// Display next line if any to display
function addLanguage()
{
	if ($('.step_two .languages .line.hide').length > 0)
	{
		$('.step_two .languages .line.hide:eq(0)').addClass('visible');
		$('.step_two .languages .line.hide:eq(0)').removeClass('hide');
		
		if ($('.step_two .languages .line.hide').length == 0)
		{
			$('#add_language').addClass('hide');
		}
	}
	
	// Hide add button when no other line to display
	if ($('#remove_language').hasClass('hide'))
	{
		$('#remove_language').removeClass('hide');
	}

	return false;
}

function removeLanguage()
{
	// Display add button if it was hidden
	if ($('#add_language').hasClass('hide'))
	{
		$('#add_language').removeClass('hide');
	}
	
	// Empty and remove lines excepted the last one
	if($('.step_two .languages .line:visible').length >= 2)
	{	
		// Store select lists
		fields_to_empty = $('.step_two .languages .line.visible:last').find('select');
		
		// Empty all fields of the last line
		$('.step_two .languages .line.visible:last').find(':input').each(function(index) {
			$(this).val('') ;
		});
		
		// Special treatment for IE6 : empty select list
		for(var i=0; i < fields_to_empty.length; i++)
		{
			fields_to_empty[i].value = '';
			if(fields_to_empty[i].type == 'select-one')
			{
				fields_to_empty[i].selectedIndex = 0;
			}
		}
		
		// Hide last line
		$('.step_two .languages .line.visible:last').addClass('hide');
		$('.step_two .languages .line.visible:last').removeClass('visible');
	}
	if($('.step_two .languages .line.visible').length == 1)
	{
		$('#remove_language').addClass('hide');
	}

	return false;
}

// Display next line if any to display
function addExperience()
{
	if ($('.step_two .experiences .line.hide').length > 0)
	{
		$('.step_two .experiences .line.hide:eq(0)').addClass('visible');
		$('.step_two .experiences .line.hide:eq(0)').removeClass('hide');
		
		// Hide add button when no other line to display
		if ($('.step_two .experiences .line.hide').length == 0)
		{
			$('#add_experience').addClass('hide');
		}
	}
	
	if ($('#remove_experience').hasClass('hide'))
	{
		$('#remove_experience').removeClass('hide');
	}

	return false;
}

function removeExperience()
{
	// Display add button if it was hidden
	if ($('#add_experience').hasClass('hide'))
	{
		$('#add_experience').removeClass('hide');
	}
	
	// Empty and remove lines excepted the last one
	if($('.step_two .experiences .line:visible').length >= 2)
	{	
		// Store select lists
		fields_to_empty = $('.step_two .experiences .line.visible:last').find('select');
		
		// Empty all fields of the last line
		$('.step_two .experiences .line.visible:last').find(':input').each(function(index) {
			$(this).val('') ;
		});
		
		// Special treatment for IE6 : empty select list
		for(var i=0; i < fields_to_empty.length; i++)
		{
			fields_to_empty[i].value = '';
			if(fields_to_empty[i].type == 'select-one')
			{
				fields_to_empty[i].selectedIndex = 0;
			}
		}
		
		// Hide last line
		$('.step_two .experiences .line.visible:last').addClass('hide');
		$('.step_two .experiences .line.visible:last').removeClass('visible');
	}
	if($('.step_two .experiences .line.visible').length == 1)
	{
		$('#remove_experience').addClass('hide');
	}

	return false;
}

// Functions called in form_validation.js
function validateDiplomas()
{
	var valid = true;
	var $diplomalines = $('table.diplomas tr.diploma');
	if ($diplomalines.size() > 0)
	{
		$diplomalines.each(function(index) {
			var lineEmpty = true;
			
			// Check if line is non empty
			$(this).find('input').each(function() {
				if($(this).val() != '')
				{
					lineEmpty = false;
				}
			});
			
			// If line is non empty, check if content is ok
			if (lineEmpty == false)
			{
				// Content is ok if date, label, speciality, school and city are nonempty
				if ($(this).find('input[id*="birthday_month"]').val() == ''
					|| $(this).find('input[id*="birthday_year"]').val() == ''
					|| $(this).find('input[id*="diplomes"]').val() == ''
					|| $(this).find('input[id*="speciality"]').val() == ''
					|| $(this).find('input[id*="school"]').val() == ''
					|| $(this).find('input[id*="city"]').val() == ''
				   )
				{
					// If it is not, alert message, open step2 tab and form is not valid
					valid = false;
					
					alert('Ligne de diplôme invalide : une ligne dont la saisie est commencée doit contenir ' +
						  'une date d\'obtention, un libellé, une spécialité, une école et une ville');
					// Open step2 tab
					$('.form_step:eq(1) h2 a').click();
				}		
			}
		});
	}
	
	return valid;
}
function validateLanguages()
{
	var valid = true;
	var $languagelines = $('table.languages tr.language');
	if ($languagelines.size() > 0)
	{
		$languagelines.each(function(index) {
			var lineEmpty = true;
			
			// Check if line is non empty
			$(this).find('input').each(function() {
				if($(this).val() != '')
				{
					lineEmpty = false;
				}
			});
			
			// If line is non empty, check if content is ok
			if (lineEmpty == false)
			{
				// Content is ok if (language or other language) and  practice and level are nonempty
				if ( ($(this).find('input[id*="langues"]').val() == '' && $(this).find('input[id*="other"]').val() == '' )
					|| $(this).find('input[id*="pratiques"]').val() == ''
					|| $(this).find('input[id*="niveaux"]').val() == ''
				   )
				{
					// If it is not, alert message, open step2 tab and form is not valid
					valid = false;
					
					alert('Ligne de langue invalide : une ligne dont la saisie est commencée doit contenir ' +
						  'une langue ou une autre langue, une pratique et un niveau');
					// Open step2 tab
					$('.form_step:eq(1) h2 a').click();
				}
				// If "Autres" is selected in select list and nothing has been typed in "other language" field, alert message, open step2 tab and form is not valid
				else if ($(this).find('input[id*="langues"]').val() == 'AUT' && $(this).find('input[id*="other"]').val() == '')
				{
					// If it is not, alert message, open step2 tab and form is not valid
					valid = false;
					
					alert('Ligne de langue invalide : vous avez sélectionné "Autre langue" mais n\'avez pas spécifié laquelle. Merci de compléter.');
					
					// Open step2 tab
					$('.form_step:eq(1) h2 a').click();
				}
			} 
		});
	}
	
	return valid;
}
function validateExperiences()
{
	var valid = true;
	var $experiencelines = $('table.experiences tr.experience');
	if ($experiencelines.size() > 0)
	{
		$experiencelines.each(function(index) {
			var lineEmpty = true;
			
			// Check if line is non empty
			$(this).find('input').each(function() {
				if($(this).val() != '')
				{
					lineEmpty = false;
				}
			});
			
			// If line is non empty, check if content is ok
			if (lineEmpty == false)
			{
				// Content is ok if date_begin, post, employer and cancer activity are nonempty
				if ($(this).find('input[id*="birthday_month_date_begin"]').val() == ''
					|| $(this).find('input[id*="birthday_year_date_begin"]').val() == ''
					|| $(this).find('input[id*="post"]').val() == ''
					|| $(this).find('input[id*="employer"]').val() == ''
					|| $(this).find('input[id*="activite_cancero"]').val() == ''
				   )
				{
					// If it is not, alert message, open step2 tab and form is not valid
					valid = false;
					
					alert('Ligne d\'expérience invalide : une ligne dont la saisie est commencée doit contenir ' +
						  'une date de début non vide, un emploi, un employeur et une activité en cancérologie');
					// Open step2 tab
					$('.form_step:eq(1) h2 a').click();
				}		
			}
		});
	}
	
	return valid;
}

