$(function(){
	var REP_Install = '/';

	/*
	Bouton ui
	*/
	$( "button, input:submit", ".formAlignBlock" ).button();
	$( "a .button" ).button();
	//
	$( "a", "button .formAlignBlock" ).click(function() { return false; });

	/*
	Tabs de base
	*/
	$("#tabs").tabs({
		cookie: { expires: 30 }
	});

	/*
	Inscription
	*/
	$('#formInscription').submit(function(){
		if($.trim($('#Nom').val())==''){
			alert('Veuillez renseigner votre nom.');
			$('#Nom').focus();
			return false;
		}else if($.trim($('#Prenom').val())==''){
			alert('Veuillez renseigner votre Prénom.');
			$('#Prenom').focus();
			return false;
		}else if($.trim($('#Email').val())==''){
			alert('Veuillez renseigner votre email.');
			$('#Email').focus();
			return false;
		}else{
			$(this).ajaxSubmit({ 
				url			: REP_Install+'api-erprequest.php?ajax=ok',    	// override for form's 'action' attribute 
				type		: 'post',	       				// 'get' or 'post', override for form's 'method' attribute 
				dataType	: 'json',
				success	: function(msg){
					dialog(msg);
					return false;
				}
			});
			return false;
		}
	});
	/*
	Vérification du formulaire d'identification en envoi ajax
	*/
	$('#formClientEnregistrer').submit(function(){
		if($.trim($('#Login').val())==''){
			alert('Veuillez renseigner votre login (email).');
			$('#Login').focus();
			return false;
		}else if($.trim($('#Passe').val())==''){
			alert('Veuillez renseigner votre mot de passe.');
			$('#Passe').focus();
			return false;
		}else{
			$(this).ajaxSubmit({ 
				url			: REP_Install+'api-erprequest.php?ajax=ok',    	// override for form's 'action' attribute 
				type		: 'post',	       				// 'get' or 'post', override for form's 'method' attribute 
				dataType	: 'json',
				success	: function(msg){
					if(msg.id!=0){
						dialog(msg);
					}else{
						dialog(msg);
						return false;
					}
				}
			});
			return false;
		}
	});
	/*
	Envoi du mot de passe
	*/
	$('#formForgotPass').submit(function(){
		if($.trim($('#EmailForgot').val())==''){
			alert('Veuillez renseigner votre email.');
			$('#EmailForgot').focus();
			return false;
		}else{
			$(this).ajaxSubmit({ 
				url			: REP_Install+'api-erprequest.php?ajax=ok',    	// override for form's 'action' attribute 
				type		: 'post',	       				// 'get' or 'post', override for form's 'method' attribute 
				dataType	: 'json',
				success	: function(msg){
					if(msg.id!=0){
						dialog(msg);
					}else{
						dialog(msg);
						return false;
					}
				}
			});
			return false;
		}
	});
	
	$('#btnNouveauCompte').click(function(){
		$(location).attr('href',"inscription.php");
	});


	/*
	Inscription NewsLetters
	*/
	$('#formInscriptionNewsLetters').submit(function(){
		if($.trim($('#NewsPrenom').val())==''){
			alert('Veuillez renseigner votre prénom.');
			$('#NewsPrenom').focus();
			return false;
		}else if($.trim($('#NewsEmail').val())==''){
			alert('Veuillez renseigner votre email.');
			$('#NewsEmail').focus();
			return false;
		}else{
			$(this).ajaxSubmit({ 
				url			: REP_Install+'api-erprequest.php?ajax=ok',    	// override for form's 'action' attribute 
				type		: 'post',	       				// 'get' or 'post', override for form's 'method' attribute 
				dataType	: 'json',
				success	: function(msg){
					if(msg.id!=0){
						dialog(msg);
					}else{
						dialog(msg);
						return false;
					}
				}
			});
			return false;
		}
	});



	/*
	Vérification du formulaire de contact en envoi ajax
	*/
	$('#formErpContact').submit(function(){
		if($.trim($('#formErpContactNom').val())==''){
			alert('Veuillez renseigner votre nom.');
			$('#formErpContactNom').focus();
			return false;
		}else if($.trim($('#formErpContactPrenom').val())==''){
			alert('Veuillez renseigner votre prénom.');
			$('#formErpContactPrenom').focus();
			return false;
		}else if($.trim($('#formErpContactEmail').val())==''){
			alert('Veuillez renseigner votre email.');
			$('#formErpContactEmail').focus();
			return false;
		}else if($.trim($('#formErpContactObjet').val())==''){
			alert('Veuillez renseigner un Objet.');
			$('#formErpContactObjet').focus();
			return false;
		}else if($.trim($('#formErpContactMessage').val())==''){
			alert('Veuillez renseigner votre message.');
			$('#formErpContactMessage').focus();
			return false;
		}else{
			$(this).ajaxSubmit({ 
				url			: REP_Install+'api-erprequest.php?ajax=ok',    	// override for form's 'action' attribute 
				type		: 'post',	       				// 'get' or 'post', override for form's 'method' attribute 
				dataType	: 'json',
				success	: function(msg){
					if(msg.id==54){
						$('#divformErpContact').html(msg.msg);
						dialog(msg);
					}else{
						dialog(msg);
						return false;
					}
				}
			});
			return false;
		}
	});
	
});

/*
Gestion de la boite de dialogue des erreurs est des messages
*/
function dialog(msg){
	$('#dialog').dialog({
		title: msg.type,
		autoOpen: false,
		show: 'blind',
		hide: 'explode',
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		},
		open: function(event, ui){
			$('#DialogMessage').html(msg.msg);
			$('#DialogID').html('id '+msg.type+' : '+msg.id);
		},
		close: function(event, ui) {
			if(typeof(msg.urlredirect)=='undefined'){ 
				return true;
			}else{
				window.location.href = msg.urlredirect;	
				return false;
			}
		},
		Cancel: function() {
			$(this).dialog('close');
		}
	});
	$('#dialog').dialog('open');
}
