var Inscription = Class.create({
	initialize: function(){
	
	},
	chargeNewClub: function(){
		var saisie = prompt("Indiquer le nom du club", "");
	       if (saisie != null) {
	           var url = 'module/Inscription/chargeNewClub.php';
	    	   var obj = new Object();
	           obj.method = 'post';
	           obj.parameters = 'club=' + saisie + '&uti_id=' + $F('f_uti_id');
	           obj.onComplete = function(rep){
	        	   var r = rep.responseText;
	        	   if(r == '@'){
	        		   // dans ce cas tout va bien on ajoute le club et on le selectionne
	        		   var contenu = '<option value="' + saisie + '">' + saisie + '</option>';
	        		   $('f_ins_club').insert({bottom:contenu});
	        	   }
	           };
	           var myAjax = new Ajax.Request(url,obj);
	       }
	},
	
	modifieNomEquipe: function(ach_id){
		var saisie = prompt("Indiquer le nouveau nom pour l'équipe", $('nom_equipe_'+ach_id).innerHTML);
	       if (saisie != null) {
	           var url = 'module/Inscription/modifieNomEquipe.php';
	    	   var obj = new Object();
	           obj.method = 'post';
	           obj.parameters = 'nom=' + saisie.encode() + '&ach_id=' + ach_id;
	           obj.onComplete = function(rep){
	        	   var r = rep.responseText;
	        	   if(r != '#'){
	        		   // dans ce cas tout va bien on ajoute le club et on le selectionne
	        		   $('nom_equipe_'+ach_id).update(saisie);
	        	   } else {
	        		   alert(r.substring(1));
	        	   }
	           };
	           var myAjax = new Ajax.Request(url,obj);
	       }
	},
	
	paiement: function(typePaiement,variable,valeur,ck){
		switch(typePaiement){
			case 'CB':
				var parametre = "am=" + $('totalNet').value + "&" + variable + "=" + valeur;
				var newfen = window.open("module/Banque/call_request_bq.php?" + parametre,
						                 "banque",
						                 "width=800,height=740,menubar=no, status=no, menubar=no,scrollbars=yes");
				break;
			case 'credit':
				var _w = new Attente();
				_w.show();
				var url = 'module/Inscription/paiementCredit.php';
				var obj = new Object();
				obj.method = 'post';
		        obj.parameters = "am=" + $('totalNet').value + "&" + variable + "=" + valeur + '&ck=' + ck;
		        obj.onComplete = function(rep){
		        	var r = rep.responseText;
		        	// Indique un problème
		        	if(r.substring(0,1) != '@'){
		        		alert(r.substring(1));
		        		_w.hide();
		        	} else {
		        		window.location = 'index.php?load=panier&' + r.substring(1);
		        	}
		        };
		        var myAjax = new Ajax.Request(url,obj);   
				break;
		}
		return;
	},
	
	recharger: function(){
		window.location = '../../index.php' + (window.location.search).gsub('&mode=CB','');
	},
	
	modifier: function(){
		var _w = new Attente();
		_w.show();
		var url = 'module/Inscription/modifier.php';
		var obj = new Object();
		obj.method = 'post';
		
		obj.parameters = 'ins_categorie=' + $('categorie').value + '&ins_club=' + $('club').value + 
		                 '&ins_repas_coureur=' + (($('ins_repas_coureur').checked == true)? 'oui' : '');
		
		obj.parameters += '&nb_repas_accompagnant=' + $('nb_repas_accompagnant').value;
		obj.parameters += '&nb_repas_enfant=' + $('nb_repas_enfant').value;
		obj.parameters += '&ins_objectif=' + $('f_ins_objectif').value;
		if($('ins_sup_num1') != undefined){
			obj.parameters += '&ins_sup_num1=' + $('ins_sup_num1').value;
		}
		if($('ins_sup_check1') != undefined){
			obj.parameters += '&ins_sup_check1=' + (($('ins_sup_check1').checked == true)? '1' : '0');
		}
		if($('ins_sup_checknum1') != undefined){
			obj.parameters += '&ins_sup_checknum1=' + (($('ins_sup_checknum1').checked == true)? '1' : '0');
		}
		if($('ins_sup_text1') != undefined){
			obj.parameters += '&ins_sup_text1=' + $('ins_sup_text1').value;
		}
		if($('f_ins_navette') != undefined){
			obj.parameters += '&ins_navette=' + (($('f_ins_navette').checked == true)? '1' : '0');
		}
		if($('ins_id') != undefined) obj.parameters += '&ins_id=' + $('ins_id').value;
		if($('ach_id') != undefined) obj.parameters += '&ach_id=' + $('ach_id').value;

		obj.onComplete = function(){window.location.reload();};
		var myAjax = new Ajax.Request(url,obj);
	},
	
	deplacer: function(pan_id,ach_id,sens){
		var _w = new Attente();
		_w.show();
		var url;
		if(sens == 'haut'){
			url = 'module/Inscription/remonter.php';
		} else {
			url = 'module/Inscription/descendre.php';
		}
		var obj = new Object();
		obj.method = 'post';
		obj.parameters = 'pan_id=' + pan_id + '&ach_id=' + ach_id;
		obj.onComplete = function(){window.location.reload();};
		var myAjax = new Ajax.Request(url,obj);
	},
	
	recherche: function(typeRecherche){
		$('ecran').update('');
		if($('f_recherche').value != ''){
			$('preload').setStyle({'display': 'table-row'});
			$('bouton_recherche').setStyle({'display' : 'none'});
			$('bouton_valide').setStyle({'display' : 'none'});
			var ecran = '';
			var url = 'module/Inscription/recherche.php';
			var obj = new Object();
			obj.method = 'post';
			obj.parameters = 'recherche=' + $('f_recherche').value + '&type=' + typeRecherche;
			obj.onComplete = function(rep){
				$('preload').setStyle({'display': 'none'});
				var r = rep.responseText;
				if(r == '@'){
					switch(typeRecherche){
						case 'ffa':
							ecran ='<font style="color: red;font-weight: bold;font-size: 11px;">' + 
							       'Le numéro de licence FFA n\'est pas valide.</font>';
							break;
						case 'yrun':
							ecran ='<font style="color: red;font-weight: bold;font-size: 11px;">' + 
						       	   'Ce code YRUN ne correspond pas à un coureur.</font>';
							break;
					}
					$('bouton_recherche').setStyle({'display' : 'table-row'});
				} else {
					$('f_retour').value = r;
					var rArr = r.split(';');
					ecran = '<table class="retourFFA"><tr><th>Nom</th><td>' + rArr[1] + '</td></tr>' +
					        '<tr><th>Prénom</th><td>' + rArr[2] + '</td></tr>' + 
					        '<tr><th>Catégorie</th><td>' + rArr[3] + '</td></tr>' + 
					        '<tr><th>Club</th><td>' + rArr[4] + '</td></tr>';
					
					if(rArr.length == 7){
						ecran += '<tr><th>Ville</th><td>' + rArr[5] + '</td></tr>';
					}
					
					ecran += '</table>';
					$('bouton_valide').setStyle({'display' : 'table-row'});
				}
				$('ecran').update(ecran);
			};
			var myAjax = new Ajax.Request(url, obj);
		} else {
			ecran = '<font style="color: red;font-weight: bold;font-size: 11px;">' +
			        'Vous devez saisir un numéro de licence FFA</font>';
			$('bouton_recherche').setStyle({'display' : 'table-row'});
			$('ecran').update(ecran);
		}
	},
	
	initRecherche: function(){
		var obj = (window.location.search.substring(1)).toQueryParams();
		if(obj.hasOwnProperty('uti_id')){
			window.location = 'index.php' + (window.location.search).gsub('&uti_id=' + obj.uti_id,'');
		} else {
			window.location = 'index.php' + window.location.search;
		}
	},
	
	valide: function(){
		$('preload').setStyle({'display': 'table-row'});
		$('bouton_valide').setStyle({'display' : 'none'});
		var url = 'module/Inscription/valider.php';
		var obj = new Object();
		obj.method = 'post';
		obj.parameters = 'recherche=' + $('f_retour').value;
		obj.onComplete = function(rep){
			$('preload').setStyle({'display': 'none'});
			var r = rep.responseText;
			var obj = (window.location.search.substring(1)).toQueryParams();
			var newUrl = window.location.search;
			if(obj.hasOwnProperty('uti_id')){
				newUrl = 'index.php' + (newUrl).gsub('&uti_id=' + obj.uti_id,'&uti_id=' + r);
			} else {
				newUrl = 'index.php' + newUrl + '&uti_id=' + r;
			}
			//ami
			if(obj.hasOwnProperty('ami')){
				newUrl = newUrl.gsub('&ami=1','');
			}
			window.location = newUrl;
		};
		var myAjax = new Ajax.Request(url, obj);
	},
	
	update: function(r){
		var rep = r.id.split('#');
		
		if(rep.length == 6){
			$('f_retour').value = r.id.gsub('#',';') + ';YRUN';
			ecran = '<table class="retourFFA"><tr><th>Nom</th><td>' + rep[1] + '</td></tr>' +
			        '<tr><th>Prénom</th><td>' + rep[2] + '</td></tr>' + 
			        '<tr><th>Catégorie</th><td>' + rep[3] + '</td></tr>' +
			        '<tr><th>Club</th><td>' + rep[4] + '</td></tr>' +
			        '<tr><th>Ville</th><td>' + rep[5] + '</td></tr>' +
			        '</table>';
			$('bouton_valide').setStyle({'display' : 'table-row'});
			$('bouton_recherche').setStyle({'display' : 'none'});
			$('ecran').update(ecran);
			
		} else {
			ecran = '<font style="color: red;font-weight: bold;font-size: 11px;">' + 
	       	   		'Ce code YRUN ne correspond pas à un coureur.</font>';
			$('bouton_recherche').setStyle({'display' : 'table-row'});
		}
		$('ecran').update(ecran);
	},
	
	updateSimple: function(r){
		var rep = r.id.split('#');
		
		if(rep.length == 6){
			var obj = (window.location.search.substring(1)).toQueryParams();
			// suppression de la notion d'ami
			var url;
			if(obj.hasOwnProperty('uti_id')){
				url = 'index.php' + (window.location.search).gsub('&uti_id=' + obj.uti_id,'&uti_id=' + rep[0]);
			} else {
				url = 'index.php' + window.location.search + '&uti_id=' + rep[0];
			}
			url = url.gsub('&ami=1','');
			window.location = url;
		} 
	},
	
	supprimer: function(id){
		if(confirm('Confirmez-vous la suppression de cette inscription ?')){
			_a_ = new Attente();
			_a_.show();
			var url = 'module/Inscription/supprimer.php';
			var obj = new Object();
			obj.method = 'post';
			obj.parameters = 'id=' + id;
			obj.onComplete = function(rep){
				var r = rep.responseText;
				if(r.substr(0,1) == '#'){
					alert(r.substr(1));
					_a_.hide();
				} else {
					if(r.substr(0,1) == '@'){
						window.location = r.substr(1);
					}
				}
			};
			var myAjax = new Ajax.Request(url,obj);
		}
	},
	
	toogleVille: function(){
		if($('f_pay_id').value == 'FR'){
			$('f_uti_ville_etr').value = '';
			$('tr_f_uti_ville_etr').setStyle({display: 'none'});
			$('tr_f_uti_ville').setStyle({display: 'table-row'});
		} else {
			$('f_uti_ville').value = '';
			$('tr_f_uti_ville_etr').setStyle({display: 'table-row'});
			$('tr_f_uti_ville').setStyle({display: 'none'});
		}
	}
});
