
function all_checkboxes_state(state, form_id, submit_id, option_id) {
      var select_submit = window.document.getElementById(submit_id);
      //var select_all = window.document.getElementById('all');
	var cases = document.forms[form_id];
	var nb = 0;
	var allnb = 0;
      for (i=0;i<cases.length;i++) {
        if(cases[i].type=='checkbox' && cases[i].id!='all'){ // On cherche toutes les cases à cocher sauf la case 'all'
          // On coche toutes les cases
          if(state=='check')  {
            cases[i].checked = true;
          }
          else{
            // On décoche toutes les cases
            if(state=='uncheck') cases[i].checked = false;
          }
          // On compte le nomber de case cochées
          if(cases[i].checked) nb++;
          allnb++;
        }
      }

      // On coche ou décoche la case all
      //if(allnb==nb) select_all.checked = true;
      //else select_all.checked = false;

      // On active, ou désactive le bouton submit en fonction du nombre de case à cocher
      if(nb>0 && $('#'+option_id).val()!='') {select_submit.disabled = false;}
      else     {select_submit.disabled = true;}

}

function check_radio(input_id,value){
    $('#'+input_id+'[value='+value+']').attr('checked','checked');
}

function save_radio_value(input_id){
    value = $('input[@name='+input_id+']:checked').attr('value');
    sto(input_id,value);
    //alert(value);

}

function save_value(input_id){
    value = $('#'+input_id).val();
    sto(input_id,value);
    //alert(value);
}

// Fonction pour resoumettre le tableau des candidature en fonction du master choisi
function resubmit_applications_table(input_id,url){
    value = $('#'+input_id).val();
    sto(input_id,value);
    document.location.replace(url);
    //alert(value);
}

function change_decision(userid,input_id,target_div){
    decision_id = $('#'+input_id).val();
    var url_update = 'ajax_actions.php?action=update_decision&cin='+userid+'&decision_id='+decision_id;
    $('#'+target_div).load(url_update);


}

function change_course_option(input_id,target_div){
    value_id = $('#'+input_id).val();
    var url_update = 'ajax_actions.php?action=update_responsable_name&course_id='+value_id;
    $('#'+target_div).load(url_update);

}

// Fonction pour redirectionner la page vers une autre on lui ajoutant la variable page_id dans l'url
function go_to_page(url, get_var, input_id){
    var input_value = document.getElementById(input_id).value;
    var new_url = url + '&' + get_var +'=' + input_value;
    document.location.replace(new_url);
    //alert(new_url);
}

//
// // Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}

// Example:
// alert( readCookie("myCookie") );
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}


function sto(nom,valeur) {
// 	Fonction appelÃ©e par le bouton "Stocker une information"
// 	Le nom de l'information est prÃ©cÃ©dÃ©e de "_" pour ne pas interfÃ©rer avec les noms utilisÃ©s par le site.
//			SetCookie("_"+nom,valeur,date_exp,myDomain);
	writeCookie(nom, valeur, 36500);
}

function get(nom) {
// Fonction appelÃ©e par le bouton "RÃ©cupÃ©rer une information"
// Le nom de l'information est prÃ©cÃ©dÃ©e de "_" pour ne pas interfÃ©rer avec les noms utilisÃ©s par le site.
//		var valeur=GetCookie("_"+nom);
//		if (valeur!=null) {return valeur;}
//		else {return "";}

	return readCookie(nom);
}


// Fonction pour changer une image par une autre de maniÃ¨re interactive
function switchImage(imgName, imgSrc) {
  if (document.images) {
    if (imgSrc != "none") document.images[imgName].src = imgSrc;
  }
}

function display_tooltip(text){
	tooltip_div = document.getElementById("shortcut_tooltip").innerHTML = text;
}

function show(div_id) {
  window.document.getElementById(div_id).style.display="block";
}

function hide(div_id) {
  window.document.getElementById(div_id).style.display="none";
}

function show_chapter_item(chapter){
  show('div_'+chapter+'_0');
  //for(i=1;i<chapter;i++) hide('plus_icon_'+i+'_0');
  show('minus_icon_'+(chapter-1)+'_0');
  hide('plus_icon_'+(chapter-1)+'_0');
}

function show_bib_item(i){
  show('div_'+i);
  show('minus_icon_'+(i-1));
  hide('plus_icon_'+(i-1));
}

function hide_bib_item(i){
    window.document.getElementById('title_'+i).value = '';
    window.document.getElementById('authors_'+i).value = '';
    window.document.getElementById('publisher_'+i).value = '';
    window.document.getElementById('year_'+i).value = '';
    hide('div_'+i);
}

function hide_netbib_item(i){
    window.document.getElementById('url_'+i).value = '';
    window.document.getElementById('description_'+i).value = '';
    hide('div_'+i);

}


function hide_chapter_item(chapter){
    window.document.getElementById('item_'+chapter+'_0').value = '';
    hide('div_'+chapter+'_0');
    c = 1;
    for(i=1;i<=20;i++){
        window.document.getElementById('item_'+chapter+'_'+i).value = '';
        hide('div_'+chapter+'_'+i);

        if(window.document.getElementById('div_'+i+'_0').style.display=='block' || window.document.getElementById('plus_icon_'+i+'_0').style.display=='block'){
            window.document.getElementById('num_item_'+i+'_0').innerHTML = c+".&nbsp;";
            c++;
        }
    }

}

function hide_section_item(chapter,section){
    window.document.getElementById('item_'+chapter+'_'+section).value = '';
    hide('div_'+chapter+'_'+section);
    c = 1;
    for(i=1;i<=20;i++){
        if(window.document.getElementById('div_'+chapter+'_'+i).style.display=='block' || window.document.getElementById('plus_icon_'+chapter+'_'+i).style.display=='block'){
            //alert("iui");
            window.document.getElementById('num_item_'+chapter+'_'+i).innerHTML = chapter+". "+c+".&nbsp;";
            c++;
        }
    }
    
}



function show_sub_chapter_item(chapter){
  show('div_'+chapter+'_1');
  show('minus_icon_'+chapter+'_1');
  hide('plus_icon_'+chapter+'_1');

  show('div_'+chapter+'_2');
  hide('arrow_icon_'+chapter+'_0');

}

function show_section_item(chapter,section){
  show('div_'+chapter+'_'+section);
  show('minus_icon_'+chapter+'_'+(section-1));
  hide('plus_icon_'+chapter+'_'+(section-1));
}

// Fonction pour sÃ©lectionner une image associÃ©e Ã  un utilisateur
function select_icon(img_id, input_name){
	var input = window.document.getElementById(input_name);
	var img_div = window.document.getElementById('div_'+img_id);
	
	// DÃ©sÃ©lectionner toutes les images
	var images_set = document.getElementById('icondiv_'+input_name).getElementsByTagName("img");
 	var n = images_set.length;
	for (i=0 ; i< n ; i++)	{
		other_img_id = images_set[i].id;
		//alert(other_img_id);
		other_img_div = window.document.getElementById('div_'+other_img_id);
		other_img_div.className = 'one_icon';
	}
	img_div.className = 'one_icon_selected';
	input.value = img_id;
}


function PrintContent(titre, div_id)
{
var DocumentContainer = document.getElementById(div_id);
var WindowObject = window.open("", "PrintWindow", "height=500, width=600, toolbar=0, menubar=0, scrollbars=1, resizable=1, status=0, location=0, left=10, top=10");

zi_doc = "<html>\n";
zi_doc = zi_doc + "<head>\n";
zi_doc = zi_doc + "<link rel='stylesheet' type='text/css' href='css/style_fsegn_1.css' />\n";
zi_doc = zi_doc + "<link rel='stylesheet' type='text/css' href='css/files_style.css' />\n";
zi_doc = zi_doc + "<title>"+titre+"</title>\n";
zi_doc = zi_doc + "</head>\n";
zi_doc = zi_doc + "<body style='margin:10px;'>\n";
zi_doc = zi_doc + "<div id='textbox'>\n";

WindowObject.document.writeln(zi_doc);
WindowObject.document.writeln(DocumentContainer.innerHTML);
WindowObject.document.writeln("\n</div>\n</body>\n</html>");

WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
WindowObject.close();
}



// Afficher/Masquer le div child correspondant au div parent dans le navigateur interactif des dossier
function Show_Hide_Div(id) {
	var child_div = window.document.getElementById('child'+id);
	//var img_div = window.document.getElementById('img'+id);
	//alert(child_div.style.display);
        if (child_div.style.display=="none") {
		child_div.style.display="block";
		switchImage_f('folder'+id, 'folder-open.png');
		switchImage_f('img'+id, 'minus_f.png');
	} else {
		child_div.style.display="none";
		switchImage_f('folder'+id, 'folder.png');
		switchImage_f('img'+id, 'plus_f.png');
	}
}


// Fonction pour changer atrtribuer une source imgSrc à une balise image imgName
function switchImage_f(imgName, imgSrc) {
  if (document.images) {
    if (imgSrc != "none") document.images[imgName].src = 'images/'+imgSrc;
  }
}


function check_selection(form_name) {
	var select_submit = window.document.getElementById('submit');
	var nb = 0;
	var cases = document.forms[form_name];
	for (i=0;i<cases.length;i++) {
            if(cases[i].type=='checkbox' && cases[i].checked) nb++;
        }
        /*
        if(nb>0) {select_submit.style.visibility = "visible";}
        else     {select_submit.style.visibility = "hidden";}
        */
        if(nb>0) {select_submit.disabled = false;}
        else     {select_submit.disabled = true;}

}




// fonction pour vérifier si un champs text est de type numérique
function check_input_numeric(input_name){
	var input = window.document.getElementById(input_name);
	var input_label = window.document.getElementById(input_name+'_label');
	var submit_button = window.document.getElementById('submit');
        if(input.value!=''){
		if(isNaN(input.value)){
			input.className="input_error";
			input_label.className="input_label_error";
                        submit_button.disabled = true;
			return false;
		} else {
			input.className="";
			input_label.className="input_label";
                        submit_button.disabled = false;
			return true;
		}
	} else {
		input.className="";
		input_label.className="input_label";
                submit_button.disabled = false;
		return true;
	}
}


// Cette fonction javascript teste qu'une adresse mail est correctement écrite dans la zone de texte input_name
function check_email(input_name) {
		var input = document.getElementById(input_name);
		var input_label = window.document.getElementById(input_name+'_label');
		//var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]­{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
                var submit_button = window.document.getElementById('submit');
		mailteste = input.value;
		if(!echeck(mailteste) && mailteste!=''){
			input.className="input_error";
			input_label.className="input_label_error";
                        submit_button.disabled = true;
			return false;
		} else {
			input.className="";
			input_label.className="input_label";
                        submit_button.disabled = false;
			return true;
		}
}



function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)	return false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)	return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)	return false;
	if (str.indexOf(at,(lat+1))!=-1)	return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)	return false;
	if (str.indexOf(dot,(lat+2))==-1)	return false;
	if (str.indexOf(" ")!=-1)	return false;
	return true
}


function DecodeMel(adrc, dec) {
  adrd="";
  for (i=0; i<adrc.length; i++) adrd+=String.fromCharCode(adrc.charCodeAt(i)+dec);
  document.write("<br /><a href='mailto:"+adrd+"'>"+adrd+"</a>");
}

function DecodeMel2(adrc, msgc,  msgd, dec) {
  adrd="";
  for (i=0; i<adrc.length; i++) adrd+=String.fromCharCode(adrc.charCodeAt(i)+dec);
  document.write("<a href='mailto:"+adrd+"'>"+msgc+" "+msgd+"</a>");
}

/*

// Fonction pour compter le nombre de ligne dans un textarea
function countLines(input_name)
{
  var area = document.getElementById(input_name);
  // trim trailing return char if exists
  var text = area.value.replace(/\s+$/g,"");
  var split = text.split("\n");
  return split.length;
}
*/
/*

// Fonction pour compter le nombre de ligne dans un textarea
function countLines(input_name){
var theArea = document.getElementById(input_name);
var theLines = theArea.value.replace((new RegExp(".{"+theArea.cols+"}","g")),"\n").split("\n");
if(theLines[theLines.length-1]=="") theLines.length--;
//theArea.form.lineCount.value = theLines.length;

return theLines.length;
}
*/

/*
function countLines(input_name){

    var taID = document.getElementById(input_name);

    // the value 3.4 below is an estimate of the number of words per line
    nChar = taID.value.match(/\S/g).length;
    tmpCalc = (taID.cols/3.4).toFixed(2);
    tmpCalc = taID.cols-tmpCalc;
    nLines = parseInt(nChar/tmpCalc);
    //alert(nLines);
    return nLines;
}
*/

/*
// Fonction pour agrandir la zone de texte en fonction du nombre de ligne 
function enlarge_text_area(input_name,min_rows){
    var area = document.getElementById(input_name);
    if(countLines(input_name)>min_rows) area.rows = countLines(input_name);
    else area.rows = min_rows;
    //area.rows = countLines(input_name);
    //alert('oui');
}
*/



function can_i_submit_form(input_id){
/*
  if($('#'+input_id).attr('optional')=="false" && $('#'+input_id).val()!=''){
    $('#'+input_id).removeClass('missing_input');
  }
*/
  var form_id = $('#'+input_id).parents('form').attr('id');

  var resp = true;
  // Pour les inputs text
  $('#'+form_id+' input').each(function(){
    if($(this).attr('optional')=="false"){
      resp = resp & ($(this).attr('value')!='') & ($(this).attr('value')!='Entrez une date');
    }
  });


  // Pour les textarea
  $('#'+form_id+' textarea').each(function(){
    if($(this).attr('optional')=="false"){
      resp = resp & ($(this).attr('value')!='');
    }
  });
  // Pour les select
  $('#'+form_id+' select').each(function(){
    if($(this).attr('optional')=="false"){
      resp = resp & ($(this).val()!='')
    }
  });
  // Pour les checkbox
  $('#'+form_id+' input:checkbox').each(function(){
    if($(this).attr('optional')=="false"){
      resp = resp & ($(this).attr('checked'));
    }
  });

  // Pour les radios
  $('#'+form_id+' input:radio').each(function(){
    var radio_name = $(this).attr('name');
    if($(this).attr('optional')=="false"){
      var radio_value = $('input[name='+radio_name+']:checked').attr('value');
      //alert(radio_name+' --> '+radio_value);
      resp = resp & (radio_value!=null);
    }
  });


  // A compléter avec les bouton radios
  //alert(resp);
  return resp;
}

function manage_form_submission(input_id){
//alert('tt');
//$('#profile_form').submit();
document.forms["profile_form"].submit();


  var form_id = $('#'+input_id).parents('form').attr('id');
  if(can_i_submit_form(input_id)) { $('#'+form_id).submit(); }
  else {


    $('#msg_err').fadeIn('fast');

    // Pour les inputs
    $('#'+form_id+' input').each(function(){
      var check_id = $(this).attr('id');
      if($(this).attr('optional')=="false" && ($(this).attr('value')=='' || $(this).attr('value')=='Entrez une date')){
        $('#'+check_id+'_label').addClass('input_label_error');
        //$('#'+check_id).addClass('input_error');
      } else {
        $('#'+check_id+'_label').removeClass('input_label_error');
        //$('#'+check_id).removeClass('input_error');
      }
    });

    // Pour les textarea
    $('#'+form_id+' textarea').each(function(){
      var check_id = $(this).attr('id');
      if($(this).attr('optional')=="false" && $(this).attr('value')==''){
        $('#'+check_id+'_label').addClass('input_label_error');
        //$('#'+check_id).addClass('input_error');
      } else {
        $('#'+check_id+'_label').removeClass('input_label_error');
        //$('#'+check_id).removeClass('input_error');
      }
    });

    // Pour les select
    $('#'+form_id+' select').each(function(){
      var check_id = $(this).attr('id');
      if($(this).attr('optional')=="false" && $(this).val()==''){
        $('#'+check_id+'_label').addClass('input_label_error');
        //$('#'+check_id).addClass('input_error');
      } else {
        $('#'+check_id+'_label').removeClass('input_label_error');
        //$('#'+check_id).removeClass('input_error');
      }
    });

    // Pour les checkbox
    $('#'+form_id+' input:checkbox').each(function(){
      var check_id = $(this).attr('id');
      if($(this).attr('optional')=="false" && !$(this).attr('checked')){
        $('#'+check_id+'_label').addClass('input_label_error');
      } else {
        $('#'+check_id+'_label').removeClass('input_label_error');
      }
    });

    // Pour les radios
    $('#'+form_id+' input:radio').each(function(){
      var radio_name = $(this).attr('name');
      //alert(radio_name);
      var radio_value = $('input[name='+radio_name+']:checked').attr('value');
      if($(this).attr('optional')=="false" && radio_value==null){
        $('#'+check_id+'_label').addClass('input_label_error');
      } else {
        $('#'+check_id+'_label').removeClass('input_label_error');
      }
    });


  }
}

