/**
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*
* @param {radio Object} or {radio id} el
* OR
* @param {form Object} or {form id} el
* @param {radio group name} radioGroup
* 
* J'ai l'impression qu'il faut que les radio boutons aient un id !!!!
*/
function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }

    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}



function confirmMessage(text,url)
{
	   if (confirm(text))
	   {
			document.location=url;
	  	}
	}

	function showDocumentAValider(url)
	{
		window.open(url,"_blank","width=800,height=500,location=no,directories=no,menubar=yes,resizable=yes,scrollbars=yes");
	}

	
	function openInNewWindow(url)
	{
		//Pareil qu'au dessus mais pour garder la compatibilité
		window.open(url,"_blank","width=800,height=500,location=no,directories=no,menubar=no,resizable=yes");
	}	
	
	   function submitForm(theform, servlet, ok,ko)
	   {     
	   //alert('submitForm:' + theform); 		
	   	//var param1 = $(theform.id).serialize();
	   	//alert('param1' + param1);
	   	//alert('theform.id' + theform.id);
	   	//alert('gg' + $(theform.id).serialize(true));
	   	//alert('hh' + $(theform.id).serialize());
		   if (document.getElementById('tiny') != null)
		   {
		   	tinyMCE.triggerSave();
		   	//alert('save done');
		   }
		   //alert('ID:' + theform.id);
		   new Ajax.Request('/servlet/' + servlet,
			//new Ajax.Request('/tests/testQS.jsp',
			  {
			    method:'post',
			    //parameters: $('theForm').serialize(true),
			    parameters: $(theform.id).serialize(true),
			    asynchronous:false,
			    onSuccess: function(transport){
			      var response = transport.responseText;
			      //alert("Success! \n\n" + response);
			      if (response != '')
			      {
				      	//window.location.href = response;
			    	  //alert('ee');
			    	  top.location.href = response;
			      }
			      if (ok != null)
			      {
			      	ok();
			      }
			    },
			    on312: function(transport){
			    	//alert('312' + transport.responseText);
			    	
			    	Modalbox.show('/incorrectDatasForAjax2.jsp?SHOW_ONLY_ERRORS=true', {
			    		title: 'Données Incorrectes',
			    		width: 550,
			    		height: 270,
			    		slideDownDuration : 0,
			    		transitions : true,
			    		method : 'post',
			    		closeString : 'Fermer',
			    		afterLoad: function() { Modalbox.resizeToContent(); }
			    	});
			    	

			    	//displayMessage('/incorrectDatasForAjax.jsp?<%=ValidationServlet.SHOW_ONLY_ERRORS%>=true');
				    	if (ko != null)
				      	{
				      		ko();
				      	}
			    	},
			    on313: function(transport){
			    	alert(transport.responseText);
			    	if (ok != null)
			      	{
			      		ok();
			      	}
			    	}
			  }); 
			  
			  //return false;
		}
	   
	   function refreshIndicateurs(number)
	   {
		   new Ajax.Updater('indicateurs-' + number, '/jspf/admin/showIndicateurs.jsp',
					{
						method: 'post'
					});  
	   }

		function changeIndicateur(formId, reference, number, index)
		{
			submitForm(document.getElementById(formId),'com.iel.core.UpdateIndicateurInscriptionServlet');
			new Ajax.Updater('indicateur-' + number + '-' + index, '/jspf/admin/showIndicateur.jsp',
					{
						method: 'post',
						parameters : { reference : reference, index : index  }
					});
			
			
			new Ajax.Request('/servlet/com.iel.types.root.organizers.CheckLastIndicateurValidationServlet',
			{
				asynchronous:true,
				parameters: { reference : reference } ,
			    onSuccess: function(transport){
				      var response = transport.responseText;
				      //alert("Success! \n\n" + response);
				      if (response == '0')
				      {
				    	  if (confirm('Le paiement et tous les documents obligatoires nécessaires semblent être présents. Souhaitez vous valider cette inscription ?'))
				   	   	{
				    		  new Ajax.Request('/servlet/com.iel.types.root.organizers.ValidInscription',
				    					{
				    						asynchronous:false,
				    						parameters: { inscription : reference, v : 'true' } ,
				    					    onSuccess: function(transport){
				    							validAndRefresh2(number, '1');
				    						    },
			    						    onFailure: function(transport){
				    						    //en fait renvoie 313 quand ca se passe bien donc considéré comme une erreur !
				    							validAndRefresh2(number, '1');
				    						    }
				    					}
				    					);
				   	  	}
				      }
				    }
				    
			}
			);
			
		}
	   /**
	    * Pour la validation et le refresh des inscriptions
	    * @param formId
	    * @param index
	    * @return
	    */
		function validAndRefresh(formId, index)
		{
			//alert('Je vais valider:' + formId +',' +index);
			//alert(document.getElementById(formId));
			submitForm(document.getElementById(formId),'com.iel.types.root.organizers.ValidInscription');
			//alert('Je raffraichis le div:' + index);
			new Ajax.Updater('inscriptionState' + index, '/jspf/admin/showInscriptionLinks.jsp',
			{
				method: 'post'
			});
			
			new Ajax.Updater('filters' , '/adminV2/tabs/filters.jsp');
			//$('nonpaye').innerHTML ='';
			
			/*new Ajax.Updater('state0' , '/jspf/admin/getNbInscriptions.jsp?type=0');
			new Ajax.Updater('state1' , '/jspf/admin/getNbInscriptions.jsp?type=1');
			new Ajax.Updater('state2', '/jspf/admin/getNbInscriptions.jsp?type=2');
			new Ajax.Updater('state3' , '/jspf/admin/getNbInscriptions.jsp?type=3');*/
		}
		
		/**
		 * Appelé lors de la validation apres la boite de confirmation pour validation du certif en meme temps
		 * On ne peut pas rechercher le formId car il est dans la boite de dialogue qui a disparu !
		 * @param formId
		 * @param index
		 * @return
		 */
		function validAndRefresh2(index, state)
		{
			//alert('Je vais valider:' + formId +',' +index);
			//alert(document.getElementById(formId));
			//submitForm(document.getElementById(formId),'com.iel.types.root.organizers.ValidInscription');
			//alert('Je raffraichis le div:' + index);
			new Ajax.Updater('inscriptionState' + index, '/jspf/admin/showInscriptionLinks.jsp',
			{
				method: 'post'
			});
			
			new Ajax.Updater('filters' , '/adminV2/tabs/filters.jsp');
				
			//Pour savoir si le certif est desormais précisé comme fourni et conforme
			if (state == 1)
			{
				$('inscription-row-' + index).removeClassName("inscription-certif-invalide-pas-validee");
			}
			else
			{
				$('inscription-row-' + index).removeClassName("inscription-certif-invalide-pas-validee");
				$('inscription-row-' + index).addClassName("inscription-certif-invalide");
			}
			//Ca semble nul ! De toute facon, je ne cahnge pas le paiement quand je suis la
			//$('nonpaye').innerHTML ='';
			
			/*new Ajax.Updater('state0' , '/jspf/admin/getNbInscriptions.jsp?type=0');
			new Ajax.Updater('state1' , '/jspf/admin/getNbInscriptions.jsp?type=1');
			new Ajax.Updater('state2', '/jspf/admin/getNbInscriptions.jsp?type=2');
			new Ajax.Updater('state3' , '/jspf/admin/getNbInscriptions.jsp?type=3');*/
		}		
		
		function refreshEnveloppe(number)
		{
			new Ajax.Updater('enveloppe-'+number, '/jspf/admin/showEnveloppe.jsp', { method: 'post' });
		}
		
		   function insertModel()
		   {
		   			new Ajax.Request('/adminV2/tabs/getModeleContent.jsp',
		   					{
		   						asynchronous:false,
		   						parameters: { index : $('modele').value } ,
		   						onSuccess: function(transport)
		   							{
		   					   		  $('message').value = transport.responseText;
		   						    }
		   					}
		   					);
			} 
		
		   function updateColloqueCB ()
		   {
		   	var senior = $RF('senior');
		   	var societe = $RF('soumission-societe-savante');
		   	var adherent = $RF('adherent');
		   	var jeveuxadherer = $RF('je-veux-adherer');
		   	//alert('senior=' + senior +  'societe=' + societe + '   adherent=' + adherent + '    jeveuxadherer=' + jeveuxadherer);

		   	$('1').hide();
		   	$('2').hide();
		   	$('3').hide();
		   	$('4').hide();

		   	$('category-id1293184732759').value='';
		   	$('category-id1293184758265').value='';
		   	$('category-id1293184807161').value='';
		   	$('category-id1293184834446').value='';
		   	
		   	$('sfgbmougramm').hide();
		   	$('tarifgramm').hide();
		   	$('tarifautre').hide();

		   	if (senior == 'Sénior')
		   	{
		   		if (societe == 'SFGBM' || societe == 'GRAMM' )
		   		{
		   			$('tarifgramm').show();
		   			$('sfgbmougramm').show();
		   			if (adherent == 'Oui' || jeveuxadherer == 'Oui')
		   			{
		   				$('1').show();
		   				$('category-id1293184732759').value='on';
		   			}
		   			else
		   			{
		   				$('2').show();
		   				$('category-id1293184758265').value='on';
		   			}
		   		}
		   		else if (societe != '')
		   		{
		   			$('tarifautre').show();
		   			$('3').show();
		   			$('category-id1293184807161').value='on';
		   		}
		   	}
		   	else
		   	{
		   		//$('tarifautre').show();
		   		$('4').show();
		   		$('category-id1293184834446').value='on';
		   	}				
		   		
		   }
		   
		   function updateStatutOlympiades ()
		   {
			   //alert('statut=' + $('inscription-champ-1').value);
			   if ('Etudiant' == $('inscription-champ-1').value)
			   {
				   $('tarifetudiant').show();
				   $('tarifsalarie').hide();
			   		$('category-id1307965960437').value='on';
				   	$('category-id1307623405570').value='';
			   }
			   else  if ('Salarié' == $('inscription-champ-1').value)
			   {
				   $('tarifsalarie').show();
				   $('tarifetudiant').hide();
				   	$('category-id1307623405570').value='on';
			   		$('category-id1307965960437').value='';
			   }
			   else
			   {
			   		$('category-id1307965960437').value='';
				   	$('category-id1307623405570').value='';
				   $('tarifetudiant').hide();
				   $('tarifsalarie').hide();
			   }
		   }

		   
		   function setCheckedValue(radioObj, newValue)
			{
				if(!radioObj)
					return;
				var radioLength = radioObj.length;
				if(radioLength == undefined) {
					radioObj.checked = (radioObj.value == newValue.toString());
					return;
				}
				for(var i = 0; i < radioLength; i++)
				{
					radioObj[i].checked = false;
					if(radioObj[i].value == newValue.toString())
					{
						radioObj[i].checked = true;
					}
				}
			}
		   
		   

