jq = jQuery;
//---------------------------------
function init_sidebar_tabs(){
  jq("#sidebarTabs #ulTabs a").click(
    function(){
      jq("#sidebarTabs div.tabContent").hide()
      jq("#sidebarTabs #ulTabs a").removeClass("sel");

      jq("#sidebarTabs div#"+jq(this).attr('href') ).show();
      jq(this).addClass("sel");
      return false;
    }
    );
}

//---------------------------------
function init_popin_links(){
  jq("a.popin").click(
    function(){
      if( jq("#dialogConent").length < 1 ) {
        jq('body').append('<div id="dialogConent" class="popup"><h2>Chargement en cours...</h2></div>');
      }

      jq("#dialogConent").dialog({
        bgiframe: true,
        modal : true,
        height: 600,
        width: 950,
        close : function(){
          jq("#dialogConent").remove()
        }
      });

      var sHref = jq(this).attr('href');
      jq.ajax({
        url: sHref,
        success: function(data) {
          jq("#dialogConent").html(data);
        }
      });

      return false;
    }
    );
}

// ==================================================
// BO Modules scripts
function popup(address, iWidth, iHeight) {
  var winWidth = ( iWidth != null ) ? iWidth : 950;
  var winHeight = ( iHeight != null ) ? iHeight : 600;
  var option = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width="+winWidth+",height="+winHeight;
  var neo = window.open(address, "play_list", option);
  if(neo.focus){
    neo.focus();
  }
}

/* functions boite ouverte fermée */
function show_content(boxName) {
  var obj =  jq("#"+boxName+"Content");
  var Btn = jq("#"+boxName+"Reduce");
  if(obj.css('position') == "relative") {
    if(obj.css( "display" ) =="none") {
      obj.show('slow');
      Btn.attr('src', "images/btn_down.gif");
    } else {
      obj.hide('slow');
      Btn.attr('src', "images/btn_right.gif");
    }
  } else {
    if(obj.css( "visibility" ) =="hidden") {
      obj.show('slow');
      Btn.attr('src', "images/btn_down.gif");
    } else {
      obj.hide('slow');
      Btn.attr('src', "images/btn_right.gif");
    }
  }
}

function init_evtList_links(){
  //jq(".evtListe .aEvtTitre, .evtListe .aImgEvt").click(
  jq(".evtListe .more-link, .evtListe .aImgEvt, a.showhideLink").click(
    function(){
      var sParentId = jq(this).parents("div.evtListe").attr('id');
      var sChildId = sParentId.replace("liste_", "ins_");
      jq("#"+sChildId).toggle('slow');
      return false;
    }
    )

  jq("#showHideRequeteur").click(
    function(){
      jq("#requeteur").toggle('slow');
    });
}

// DEPOT SPECTACLE -------------------------------------------------------
/* functions formulaires */
function retire_select(){
  var objin = document.getElementById("in");
  for (j=0;j<objin.options.length;j++){
    if (objin.options[j].selected == true) {
      objin.options[j]=null;
    }
  }
}
function ajout_select(){
  var obj = document.getElementById("notin");
  var obj2 = document.getElementById("in");
  if (obj) {
    for (i=0;i<obj.options.length;i++){
      //if ((obj.options[i].selected == true) && (is_in(obj.options[i].value) == false)) {
      if (obj.options[i].selected == true) {
        if (is_in(obj.options[i].value) == false) {
          var o=new Option(obj.options[i].text,obj.options[i].value);
          var indx = obj2.options.length;
          obj2.options[indx]=o;
        }
      }
    }
  }
}
function select_all() {
  var obj = document.getElementById("in");
  for(var i=0;i<obj.options.length;i++){
    if(obj.options[i].text) {
      obj.options[i].selected=true;
    }
  }
  //document.form_depot.action.value='save';
  return true;
//	alert(document.form_depot.action.value);
}

function is_in(value){
  var objin = document.getElementById("in");
  for (k=0;k<objin.options.length;k++){
    if (objin.options[k].value == value) {
      return true;
    }
  }
  return false;
}
function retire_selectpush(){
  var objin = document.getElementById("inpush");
  for (j=0;j<objin.options.length;j++){
    if (objin.options[j].selected == true) {
      objin.options[j]=null;
    }
  }
}
function ajout_selectpush(){
  var obj = document.getElementById("notinpush");
  var obj2 = document.getElementById("inpush");
  if (obj) {
    for (i=0;i<obj.options.length;i++){
      //if ((obj.options[i].selected == true) && (is_in(obj.options[i].value) == false)) {
      if (obj.options[i].selected == true) {
        if (is_in(obj.options[i].value) == false) {
          var o=new Option(obj.options[i].text,obj.options[i].value);
          var indx = obj2.options.length;
          obj2.options[indx]=o;
        }
      }
    }
  }
}

function select_allpush() {
  var obj = document.getElementById("inpush");
  for(var i=0;i<obj.options.length;i++){
    if(obj.options[i].text) {
      obj.options[i].selected=true;
    }
  }
  //document.form_depot.action.value='save';
  return true;
//	alert(document.form_depot.action.value);
}

function is_inpush(value){
  var objin = document.getElementById("inpush");
  for (k=0;k<objin.options.length;k++){
    if (objin.options[k].value == value) {
      return true;
    }
  }
  return false;
}
// EVENEMENTS ----------------------------------------------
function verif_formulaire_evenement() {
  if(document.form_depot.manif_nom.value == "")  {
    alert("Veuillez entrer un intitulé !");
    document.form_depot.manif_nom.focus();
    return false;
  }
  if(document.form_depot.manif_dateDebut.value== "" )  {
    alert("Veuillez entrer une date de début !");
    document.form_depot.manif_dateDebut.focus();
    return false;
  }
  if(document.form_depot.manif_email.value== "" && document.form_depot.manif_tel.value== "")  {
    alert("Veuillez entrer le mail et/ou le téléphone ! ");
    document.form_depot.manif_email.focus();
    return false;
  }
  if(document.form_depot.manif_ville.value== "" )  {
    alert("Veuillez entrer la ville où se déroule l'évènement !");
    document.form_depot.manif_ville.focus();
    return false;
  }
  if(document.form_depot.manif_code.value== "" )  {
    alert("Veuillez entrer le code postal où se déroule l'évènement !");
    document.form_depot.manif_code.focus();
    return false;
  }
  if(document.form_depot.submit_nom.value== "" )  {
    alert("Veuillez entrer votre nom !");
    document.form_depot.submit_nom.focus();
    return false;
  }
  if(document.form_depot.submit_email.value== "" )  {
    alert("Veuillez entrer votre email !");
    document.form_depot.submit_email.focus();
    return false;
  }
  select_all();
  select_allpush();
}


function verif_form_admin_evenement() {
  if(document.form_depot.manif_nom.value == "")  {
    alert("Veuillez entrer un intitulé !");
    document.form_depot.manif_nom.focus();
    return false;
  }
  if(document.form_depot.manif_dateDebut.value== "" )  {
    alert("Veuillez entrer une date de début !");
    document.form_depot.manif_dateDebut.focus();
    return false;
  }
  select_all();
  select_allpush();
}
// /EVENEMENTS ----------------------------------------------
//
// EMPLOIS ----------------------------------------------
function verif_formulaire_off() {
  if(document.form_depot.of_emploi_nom.value == "")  {
    alert("Veuillez entrer un intitul� de poste !");
    document.form_depot.of_emploi_nom.focus();
    return false;
  }
  if(document.form_depot.of_emploi_type_id.value== "0" )  {
    alert("Veuillez entrer un secteur !");
    document.form_depot.of_emploi_type_id.focus();
    return false;
  }
  if(document.form_depot.of_emploi_structure.value== "" )  {
    alert("Veuillez entrer le nom de la structure !");
    document.form_depot.of_emploi_structure.focus();
    return false;
  }
  if(document.form_depot.of_emploi_codecp.value== "" )  {
    alert("Veuillez entrer un Code Postal !");
    document.form_depot.of_emploi_codecp.focus();
    return false;
  }
  if(document.form_depot.of_emploi_ville.value== "" )  {
    alert("Veuillez entrer une Ville !");
    document.form_depot.of_emploi_ville.focus();
    return false;
  }

  if(document.form_depot.of_emploi_type_contrat.value== "" )  {
    alert("Veuillez entrer un type de contrat !");
    document.form_depot.of_emploi_type_contrat.focus();
    return false;
  }

  if(document.form_depot.of_emploi_date_limite.value== "" )  {
    alert("Veuillez entrer une date limite de candidature !");
    document.form_depot.of_emploi_date_limite.focus();
    return false;
  }
  if(document.form_depot.of_emploi_adresse_envoi.value== "" )  {
    alert("Veuillez entrer une adresse d'envoi des candidatures !");
    document.form_depot.of_emploi_adresse_envoi.focus();
    return false;
  }

  if(document.form_depot.of_emploi_submit_nom.value== "" )  {
    alert("Veuillez entrer votre nom !");
    document.form_depot.of_emploi_submit_nom.focus();
    return false;
  }

  if(document.form_depot.of_emploi_submit_email.value == "") {
    alert("Veuillez entrer votre adresse �lectronique !");
    document.form_depot.of_emploi_submit_email.focus();
    return false;
  }
  if(document.form_depot.of_emploi_submit_email.value.indexOf('@') == -1) {
    alert("Votre adresse n'est pas une adresse �lectronique!");
    document.form_depot.of_emploi_submit_email.focus();
    return false;
  }
  select_all();
}
function verif_formulaire_dem() {
  if(document.form_depot.dem_emploi_nom.value == "")  {
    alert("Veuillez entrer un intitulé !");
    document.form_depot.dem_emploi_nom.focus();
    return false;
  }
  if(document.form_depot.dem_emploi_type_id.value== "0" )  {
    alert("Veuillez entrer un secteur !");
    document.form_depot.dem_emploi_type_id.focus();
    return false;
  }
  if(document.form_depot.dem_emploi_submit_nom.value == "")  {
    alert("Veuillez entrer votre nom !");
    document.form_depot.dem_emploi_submit_nom.focus();
    return false;
  }
  if(document.form_depot.dem_emploi_submit_prenom.value == "")  {
    alert("Veuillez entrer votre prénom !");
    document.form_depot.dem_emploi_submit_prenom.focus();
    return false;
  }

  if(document.form_depot.dem_emploi_codecp.value == "")  {
    alert("Veuillez entrer un Code Postal !");
    document.form_depot.dem_emploi_codecp.focus();
    return false;
  }
  if(document.form_depot.dem_emploi_ville.value == "")  {
    alert("Veuillez entrer une Ville !");
    document.form_depot.dem_emploi_ville.focus();
    return false;
  }
  if(document.form_depot.dem_emploi_submit_email.value == "") {
    alert("Veuillez entrer votre adresse électronique !");
    document.form_depot.dem_emploi_submit_email.focus();
    return false;
  }
  if(document.form_depot.dem_emploi_submit_email.value.indexOf('@') == -1) {
    alert("Ce n'est pas une adresse électronique !");
    document.form_depot.dem_emploi_submit_email.focus();
    return false;
  }
  if(document.form_depot.dem_emploi_date_debut.value == "") {
    alert("Veuillez entrer une date de disonibilité !");
    document.form_depot.dem_emploi_date_debut.focus();
    return false;
  }
  select_all();
}
// /EMPLOIS ----------------------------------------------

// STAGES ----------------------------------------------
function verif_formulaire_stage() {
  if(document.form_depot.form_nom.value == "")  {
    alert("Veuillez entrer un intitulé de stage !");
    document.form_depot.form_nom.focus();
    return false;
  }
  /*if(document.form_depot.branche.value== "" )  {
   alert("Veuillez entrer un secteur !");
   document.form_depot.branche.focus();
   return false;
  }*/
  if(document.form_depot.dt_deb.value== "" )  {
    alert("Veuillez entrer une date de début !");
    document.form_depot.dt_deb.focus();
    return false;
  }
  if(document.form_depot.nom_etab.value== "" )  {
    alert("Veuillez entrer le nom de la structure organisatrice !");
    document.form_depot.nom_etab.focus();
    return false;
  }
  if(document.form_depot.email.value== "" && document.form_depot.telephone.value== "")  {
    alert("Veuillez entrer le mail et/ou le téléphone ! ");
    document.form_depot.email.focus();
    return false;
  }
  if(document.form_depot.ville.value== "" )  {
    alert("Veuillez entrer la ville où se déroule le stage !");
    document.form_depot.ville.focus();
    return false;
  }
  if(document.form_depot.codecp.value== "" )  {
    alert("Veuillez entrer le code postal où se déroule le stage !");
    document.form_depot.codecp.focus();
    return false;
  }
  if(document.form_depot.submit_nom.value== "" )  {
    alert("Veuillez entrer votre nom !");
    document.form_depot.submit_nom.focus();
    return false;
  }
  if(document.form_depot.submit_email.value== "" )  {
    alert("Veuillez entrer votre email !");
    document.form_depot.submit_email.focus();
    return false;
  }
  /* if(document.form_depot.collecteur.value== "" )  {
   alert("Veuillez entrer le collecteur - Vous ou votre structure !");
   document.form_depot.collecteur.focus();
   return false;
  }*/
  select_all();
}
// /STAGES ----------------------------------------------


// FORMATION ----------------------------------------------
function verif_formulaire_formation() {
  if(document.form_depot.nom_etab.value == "")  {
    alert("Veuillez entrer un nom pour l'établissement !");
    document.form_depot.nom_etab.focus();
    return false;
  }
  if(document.form_depot.ville.value == "")  {
    alert("Veuillez entrer une ville !");
    document.form_depot.ville.focus();
    return false;
  }
  if(document.form_depot.codecp.value == "")  {
    alert("Veuillez entrer un code postal !");
    document.form_depot.codecp.focus();
    return false;
  }
  if(document.form_depot.form_nom.value == "")  {
    alert("Veuillez entrer un intitulé de formation !");
    document.form_depot.form_nom.focus();
    return false;
  }
  select_all();
}
// /FORMATION ----------------------------------------------

// ==================================================
function reset_form(form){
  // iterate over all of the inputs for the form
  // element that was passed in
  jq(':input', form).each(function() {
    var type = this.type;
    var tag = this.tagName.toLowerCase(); // normalize case
    // it's ok to reset the value attr of text inputs,
    // password inputs, and textareas
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = "";
    // checkboxes and radios need to have their checked state cleared
    // but should *not* have their 'value' changed
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    // select elements need to have their 'selectedIndex' property set to -1
    // (this works for both single and multiple select elements)
    else if (tag == 'select')
      //this.selectedIndex = -1;
      this.selectedIndex = 0;
  });
};
//---------------------------------
jq(document).ready(
  function(){
    jq("#aShowAgenda").click(
      function(){
        jq("#popin_calendar").toggle('slow');
      }
      );
    jq(".resetRequeteur").click(
      function(){
        reset_form( jq("#requeteur") );
     });
    init_sidebar_tabs();
    init_popin_links();

    jq(".datepicker").datepicker(jq.datepicker.regional['fr']);

    init_evtList_links();

  }
  )