/**
 * Sort the initiatives
 * @param champs
 * @param sens
 * @return
 */
function sort(champs,sens)
{
	$('#order_by').val(champs);
	$('#sens').val(sens);
	$('#search').submit();
}

/**
 * Paginate the initiatives
 * @param page
 * @return
 */
function pagination(page)
{
	$('#page').val(page);
	$('#search').submit();
}

/**
 * Send a mail to the admin
 * @return
 */
function validateContact()
{
	var msg = '';
	
	if(!validateEmail($('#email').val()))
	{
		msg += 'Votre adresse mail est incorrecte.<br/>';
	}

	if(tinyMCE.get('subject').getContent() == '')
	{
		msg += 'Votre message est vide.';
	}
		
	if(msg != '')
	{
		 $('#errorcontact').html(msg);
	}
	else
	{
		$('#errorcontact').html('');
		$('#sendcontact').submit();
	}
}

/**
 * Validate fields of an initiative
 * @return
 */
function validateFiche()
{
	var msg = '';
	
	if($('#email').val() != '')
	{
		if(!validateEmail($('#email').val()))
		{
			msg += 'L\'adresse mail est incorrecte.<br/>';
		}
	}
	if($('#telephone').val() != '')
	{
		if(!validateNumeric($('#telephone').val()))
		{
			msg += 'Le numéro de téléphone doit être un champ numérique.<br/>';
		}
	}
	if($('#fax').val() != '')
	{
		if(!validateNumeric($('#fax').val()))
		{
			msg += 'Le fax doit être un champ numérique.<br/>';
		}
	}
	if(!validateNumeric($('#annee_creation').val()))
	{
		msg += 'L\'année de création doit être un champ numérique.<br/>';
	}
	if($('#effectif').val() != '')
	{
		if(!validateNumeric($('#effectif').val()))
		{
			msg += 'L\'effectif doit être un champ numérique.<br/>';
		}
	}
	
	if(msg != '')
	{
		$('#errorcontact').html(msg);
	}
	else
	{
		$('#errorcontact').html('');
		$('#formentreprise').submit();
	}
}

/**
 * Validate an email
 * @param email
 * @return
 */
function validateEmail(email)
{
	var reg = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");

	if(reg.test(email))
	{
		return(true);
	}
	else
	{
		return(false);
	}
}

/**
 * Validate a numeric field
 * @param number
 * @return
 */
function validateNumeric(number)
{
	if(number == 0)
	{
		return false;
	}
	e = new RegExp("^[0-9]*$");

	if (!e.test(number))
		return false;
	else
		return true;
}
