$(function() {
	// header.php
	$('#dialog').dialog({
		autoOpen: false,
		show: 'blind',
		hide: 'explode'
	});
	// Menus
	var accordion = $("#MenuMonCompte");
	var index = $.cookie("MenuMonCompte");
	var active;
	if (index !== null) {
		active = accordion.find("h3:eq(" + index + ")");
	} else {
		active = 0;
	}
	accordion.accordion({
		header: "h3",
		alwaysOpen: false,
		active: active,
		autoHeight: false,
		navigation: true,
		change: function(event, ui) {
				var index = $(this).find("h3").index ( ui.newHeader[0] );
				$.cookie("MenuMonCompte", index, {
					path: "/"
				});
			}
	});
	
	// formInscritpion.php
	$("#tabsIdentification").tabs({
		cache: true,
		cookie: { expires: 30 }
	});
	
	// Mémorisation des coordonnées
	$('#formCoordonnees').submit(function(){
		$('#formCoordonnees').ajaxSubmit({ 
			url			: '/includes/action_post.php',    	// override for form's 'action' attribute 
			type			: 'post',	       				// 'get' or 'post', override for form's 'method' attribute 
			success		: function(msg){
				$('#dialog').dialog({
					title: 'Confirmation',
					buttons: {
						Ok: function() {
							$(this).dialog('close');
						}
					},
					open: function(event, ui){
						$('#DialogMessage').html(msg);
					}
				});
				$('#dialog').dialog('open');
				return false;
			}
		});
		return false;
	});
	
	// Mémorisation des infos bancaire
	$('#formInfoBancaire').submit(function(){
		$('#formInfoBancaire').ajaxSubmit({ 
			url			: '/includes/action_post.php',    	// override for form's 'action' attribute 
			type			: 'post',	       				// 'get' or 'post', override for form's 'method' attribute 
			success		: function(msg){
				$('#dialog').dialog({
					title: 'Confirmation',
					buttons: {
						Ok: function() {
							$(this).dialog('close');
						}
					},
					open: function(event, ui){
						$('#DialogMessage').html(msg);
					}
				});
				$('#dialog').dialog('open');
				return false;
			}
		});
		return false;
	});

	// affiliation.php Liste des commission
	jQuery("#ListeCommission").jqGrid({ 
		url:'JQGrid/Commissions.php', 
		datatype: "json", 
		colNames:['Date','N°','Description', 'Prix', '%','Montant'], 
		colModel:[ 
				{name:'Date',index:'Date', width:85}, 
				{name:'IDFacture',index:'IDFacture', width:30, align:"center"}, 
				{name:'Description',index:'Description', width:380}, 
				{name:'PrixProduit',index:'PrixProduit', width:80, align:"right"}, 
				{name:'TauxComm',index:'TauxComm', width:40, align:"right"}, 
				{name:'Montant',index:'Montant', width:80, align:"right"}
				], 
		rowNum:50, 
		height:240,
		rowList:[10,20,30,50,100], 
		pager: '#PagerCommission', 
		sortname: 'Date', 
		viewrecords: true, 
		sortorder: "desc", 
		caption:"Relever de compte", 
			footerrow : true, 
			userDataOnFooter : true, 
			altRows : true  
	}); 
	jQuery("#ListeCommission").jqGrid('navGrid','#PagerCommission',{edit:false,add:false,del:false}); 	
	
	// affiliation_CG.php
	$('#formDevenirAffilie').submit(function(){
		if($('#CGDevenirAffilie').is(':checked')==false){
			$('#dialog').dialog({
				title: 'Attention !',
				buttons: {
					Ok: function() {
						$(this).dialog('close');
					}
				},
				open: function(event, ui){
					$('#DialogMessage').html('Vous devez accepter les conditions');
				}
			});
			$('#dialog').dialog('open');
			return false;
		}else{
			$('#formDevenirAffilie').ajaxSubmit({ 
				url			: '/includes/action_post.php',    	// override for form's 'action' attribute 
				type			: 'post',	       				// 'get' or 'post', override for form's 'method' attribute 
				success		: function(msg){
					if(msg==1){
						$('#dialog').dialog({
							title: 'Confirmation',
							buttons: {
								Ok: function() {
									$(this).dialog('close');
								}
							},
							open: function(event, ui){
								$('#DialogMessage').html('Votre demande est bien enregistrée et sera étudié pas nos services, vous recevrez la réponse dans un délai de 48h maximum.');
								$('#FormulaireDevenirAffilie').hide('slow');
							}
						});
						$('#dialog').dialog('open');
						return false;
					}else{
						$('#dialog').dialog({
							title: 'Erreur !',
							buttons: {
								Ok: function() {
									$(this).dialog('close');
								}
							},
							open: function(event, ui){
								$('#DialogMessage').html(msg);
							}
						});
						$('#dialog').dialog('open');
						return false;
					}
				} 
			});
			return false;				
		}
		return false;				
	});
});


