//================================================================================
// JS        : FONCTIONS COMMUNES AU PAGES D'INSCRIPTION
// FICHIER   : inscription.js
//--------------------------------------------------------------------------------
// Philippe  12/11/07  Particularités de ulla
// Philippe   9/11/07  Reprise d'inscription
// Philippe  20/09/07  Création
//================================================================================
var global_champ_apres_code='#nom';


function validation_saisie() {
  $('#action').val('CREER');
  document.form_saisie.submit();
} // validation_saisie

function voir_conditions(p_popup) {
  if (p_popup) {
    $('#action').val('ACCES_POP');
    var l_win=window.open("","ConditionsAcces","scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,width=800,copyhistory=yes,height=600");
    document.form_saisie.target="ConditionsAcces";
  } else $('#action').val('ACCES');
  document.form_saisie.submit();
} // voir_conditions

function voir_vieprivee(p_popup) {
  if (p_popup) {
    $('#action').val('VIEPRIVEE_POP');
    var l_win=window.open("","ViePrivee","scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,width=800,copyhistory=yes,height=600");
    document.form_saisie.target="ViePrivee";
  } else $('#action').val('VIEPRIVEE');
  document.form_saisie.submit();
} // voir_conditions

function saisie_pays() {
  if ($('#pays').val() == 45) {
    $('#code_postal').attr('disabled',false);
    $('#code_postal').focus();
  } else {
    $('#code_postal').val('');$('#zone_ville').html('&nbsp;');$('#ref_commune').val(0);
    $('#code_postal').attr('disabled',true);
    $('#jour').focus();
  }
} // saisie_pays

function saisie_commune(p_event) {
  if (!p_event) p_event=window.event;
  if (p_event.keyCode) l_code=p_event.keyCode;
  else l_code=p_event.which;
  if ((l_code == 13) || (l_code == 8)) return(true)
  if (((l_code < 48) || (l_code > 57)) && (l_code > 31)) { p_event.cancelBubble=true;return(false);}
} // saisie_commune

function recupere_commune(p_script,p_auto) {
  $.getJSON(p_script,{ action: 'COMMUNE', code_postal: $('#code_postal').val(), autorisation_action: p_auto },
	    function(p_json) {
	      if (p_json.length == 1) {
	    	  $('#zone_ville').html(p_json[0].nom);
	    	  $('#ref_commune').val(p_json[0].commune);
	    	  $('#CCO').val(p_json[0].commune);

	      } else {
	    	  l_html="<select id='choix_commune' name='choix_commune' onChange='$(\"#CCO\").val($(\"#choix_commune\").val());$(\"#ref_commune\").val($(\"#choix_commune\").val());'>";
	    	  for (var l_index=0;l_index < p_json.length;l_index++) l_html+="<option value='"+p_json[l_index].commune+"'>"+p_json[l_index].nom+"</option>";
	    	  l_html+="</select>";
	    	  $('#zone_ville').html(l_html);
	    	  $('#ref_commune').val(p_json[0].commune);
	    	  $('#CCO').val(p_json[0].commune);

	      }
	    });
} // recupere_commune

function validation_commune(p_event,p_script,p_auto) {
  if (!p_event) p_event=window.event;
  if (p_event.keyCode) l_code=p_event.keyCode;
  else l_code=p_event.which;
  if (l_code == 13) { $(global_champ_apres_code).focus();return(false);}
  if ($('#code_postal').val().length < 5) { $('#zone_ville').html('&nbsp;');$('#ref_commune').val(0);return(false);}
  if (((l_code < 48) || (l_code > 57)) && ((l_code < 96) || (l_code > 105))) return(false);
  // ---- Recherche de la commune ----
  recupere_commune(p_script,p_auto);
} // validation_commune

function deja_inscrit() {
  if ($("#nom_inscrit").val().length == 0) { $("#nom_inscrit").focus();return(false); }
  if ($("#mdp_inscrit").val().length == 0) { $("#mdp_inscrit").focus();return(false); }
  $("#form_inscrit").submit();
  return(true);
} // deja_inscrit

function verifie_email_perdu(p_email) {
  if (p_email.length < 5) {
    alert("Veuillez indiquer un email valide du type xxxx@yyy.zz");
    return false;
  }
  // Rajout de . pour gerer email du type yahoo.co.uk
  verif=/^[a-zA-Z.0-9_-]+@[a-zA-Z0-9-.]{2,}[.][a-zA-Z]{2,3}$/;
  if (!verif.exec(p_email)) {
    alert("Adresse incomplète: un email est du type xxxx@yyy.zz");
    return false;
  }
  return true;
} // verifie_email_perdu

