$(document).ready(function(){
	
	
	/**
	 *@desc Fonction ajax qui envoie l'adresse mail du nouvel entrant
	 */
	$('a#vipSendInscription').each(function(){
		$(this).click(function(){
			var ajaxargs_args = new Array();
			ajaxargs_args["email"] = $('#vipEmailNew').val();
			ajaxargs_args["majeurCGU"] = $('#vipCguAccept:checked').length;
			ajaxargs_args["vipCouponId"] = $('#vipCouponId').val();
			ajaxargs_args["mtcmk"]=$('#vipMtcmk').val();
			param = {ajaxfile:"sendInscription", ajaxfunc:"sendInscription", ajaxargs:ajaxargs_args};
			ajaxargs = $.getArgs(param);
			$.ajax({
				 type: "POST",
				 url: "/ajax/ajax.php",
				 data: ajaxargs,
				 success: function(msg){showInfoBubble(msg);}
			});
		});
	});
	
	/**
	 *@desc Fonction ajax qui envoie l'adresse mail pour la personne qui a oublié son mdp
	 */
	$('a#vipSendPass').each(function(){
		$(this).click(function(){
			var ajaxargs_args = new Array();
			ajaxargs_args["email"] = document.getElementById ( 'vipForgotPassEmail' ).value;
			param = {ajaxfile:"sendForgottenPassword", ajaxfunc:"sendForgottenPassword", ajaxargs:ajaxargs_args};
			ajaxargs = $.getArgs(param);
			$.ajax({
				 type: "POST",
				 url: "/ajax/ajax.php",
				 data: ajaxargs,
				 success: function(msg){showInfoText(msg);}
			});
		});
	});
	
	$('input#email').click(function(){
		hideAllInfoBubbles();
	});
	
	$('input#vipForgotPassEmail').click(function(){
		hideAllInfoTexts();
	});
});

/**
 * @desc Affiche l'info-bulle demandée par la variable flag
 * @var flag string type d'info-bulle a afficher
 */
function showInfoBubble ( flag ) {
	hideAllInfoBubbles ();
	switch ( flag ) {
	case 'OK' :
		$('div#vipBlocFormInscription').hide();
		$('div#vipBlocInscriptionOk').show();
		break;
	case 'ACCEPT_TU' :
		$('div#vipBubbleAcceptCgu').show();
		break;
	case 'ALREADY_ASSIGNED' :
		$('div#vipBubbleExistEmail').show();
		break;
	case 'INVALID_EMAIL' :
		$('div#vipBubbleInvalidEmail').show();
		break;
	case 'MAIL_EMPTY' :
		$('div#vipBubbleEmptyEmail').show();
		break;
	case 'INTERNAL_ERROR' :
	default :
		$('div#vipBubbleInternalError').show();
		break;
	}
}

/**
 * @desc Masque toutes les infobulles
 */
function hideAllInfoBubbles () {
	$('div.vipBubbleVerticalError').hide(); 
	$('div.vipBubbleHorizontalError').hide(); 
	$('div.vipBubbleDiagonalError').hide();
}

/**
 * Affiche le texte indiquant l'état d'avancement du mail de mot de passe oublié
 * @var flag string type de texte a afficher
 */
function showInfoText ( flag ) {
	hideAllInfoTexts ();
	switch ( flag ) {
	case 'OK' :
		$('p#vipSendPassOk').show();
		setTimeout("toggleForgottenPassword()",3000);
		break;
	case 'INTERNAL_ERROR' :
	case 'INVALID_EMAIL' :
	case 'MAIL_EMPTY' :
	default :
		$('p#vipErrorSendPass').show();
		break;
	}
}

/**
 * Affiche ou masque le calque contenant le formulaire permettant d'envoyer le mail de rappel du mot de passe
 */
function toggleForgottenPassword() {
	$(".jq_showHide").toggle();
}

/**
 * @desc Masque toutes les infos texte
 */
function hideAllInfoTexts () { 
	$('p.vipTxtErrorB').hide();
}