
/********************************
Project            : 
Creator            : Cédric - Sébastien 
Creation date      : 14/05/200
Modifier           : 
Modification date  : 
Version            : 
Comment : Javascript functions about form object and check of fields

********************************/

function editChampAvecEktron(champCache, idDivAffichage,parametres_ektron){
	fen = window.open('ektron_edit.php?contenu='+champCache.name+'&affichage='+idDivAffichage+'&parametres_ektron='+parametres_ektron,'fiche'+idDivAffichage,'screenX=20,screenY=20,height=500,width=760,scrollbars=yes');
	setTimeout('fen.focus();',200);
} /// editChampAvecEktron

  function voirTexteEktron(nomChamp,styleSheet){
    fen = window.open('','previsualisation', 'screenX=15,screenY=15,height=500,width=760,scrollbars=yes,resizable=yes');
    fen.document.writeln('<html><head>');
	fen.document.writeln('<link rel="stylesheet" href="../css/ektron.css">');
	if (styleSheet){
		fen.document.writeln('<link rel="stylesheet" href="'+styleSheet+'">');
	}    
    fen.document.writeln('<title>Pr&eacute;visualisation</title><head><body style="background-color:white";><div style="margin:30px;padding:10px;border:1px solid #CCCCCC;">');
    if (styleSheet){
    	fen.document.writeln('<div id="ektron">');
    }
    	fen.document.writeln(nomChamp.value);
    if (styleSheet){
    	fen.document.writeln('</div>');
    }
    fen.document.writeln('</div></body></html>');
  } /// voirTexteEktron

// ----------------------------------------------------------------------------
// RADIO OBJECT ---------------------------------------------------------------
// ----------------------------------------------------------------------------

function init_radio(a_name, a_value) {
  // alert('init_radio name=' + a_name + ' value=' + a_value);
  var i;
  for (i=0; i < a_name.length; i++){
    if (a_name[i].value == a_value) {
      a_name[i].checked = true;
    }
  }
} /// function init_radio

function get_radio_value(obj_radio) {
  // alert('--> get_radio_value');
  var i;
  var res = false;

  for (i=0; i < obj_radio.length; i++){
    if (obj_radio[i].checked) {
      res = obj_radio[i].value;
    }
  }
  
  // alert('<-- get_radio_value(' + obj_radio + ') => ' + res);
  return res;
}

// ----------------------------------------------------------------------------
// SELECT OBJECT --------------------------------------------------------------
// ----------------------------------------------------------------------------

function init_select(name, value) {
  // alert('name=' + name + ' value=' + value);
  var i;
  for (i=0; i < name.options.length; i++){
    if (name.options[i].value == value) {
      name.selectedIndex = i;
    }
  }
} // function

function get_select_value(name) {
  return name.options[name.selectedIndex].value;
}


// ----------------------------------------------------------------------------
// CHECK OF DATE --------------------------------------------------------------
// ----------------------------------------------------------------------------


function check_date_select(select_name, select_day, select_month, select_year) {
  var day   = 1 * get_select_value(select_day   );
  var month = 1 * get_select_value(select_month );
  var year  = 1 * get_select_value(select_year  );
  
  // alert('date: ' + day + '/' + month + '/' + year);
  
  nb_day_month = 0;
  switch ( month) {
  case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 : nb_day_month = 31; break;
  case 4: case 6 : case 9 : case 11 : nb_day_month = 30; break;
  case 2 : 
    if (year % 4 == 0) 
      nb_day_month = 29; 
    else 
      nb_day_month = 28;
    break;
  } // switch
  
  if (nb_day_month < day) {
    alert('Ce jour du mois n\'existe pas dans le champ ' + select_name);
    // alert('month=' + month + ' => nb_day_month < day : ' + nb_day_month + ' < '+ day);
    init_select(select_day, nb_day_month);
    return false;
  }
}

function check_date_input_text(input_obj, field_name) {
  // alert('check_date : ' + input_obj + ',' + field_name);
  var tmp = input_obj.value.split('/');
  if (tmp.length != 3) {
    alert('Le format attendu pour la date est DD/MM/YYYY');
    return false;
  } 

  var day   = parseInt( tmp[0] );
  var month = parseInt( tmp[1] );
  var year  = parseInt( tmp[2] );

  if (false) alert('check_date ' + input_obj.value + '==> tmp.length=' + tmp.length + ' :  '  + 
		   tmp[0] + "->" + day + " " + 
		   tmp[1] + "->" + month + " " + 
		   tmp[2] + "->" + year );

  if (isNaN(day)) { 
    alert('Le jour ne peut pas être ' + tmp[0] + ' dans le champ ' + field_name);
    return false;
  }

  if (isNaN(month)) { 
    alert('Le mois ne peut pas être ' + tmp[1] + ' dans le champ ' + field_name);
    return false;
  }

  if (isNaN(year)) { 
    alert('L\'année ne peut pas être ' + tmp[2] + ' dans le champ ' + field_name);
    return false;
  }
  
  
  nb_day_month = 0;
  switch ( month) {
  case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 : nb_day_month = 31; break;
  case 4: case 6 : case 9 : case 11 : nb_day_month = 30; break;
  case 2 : 
    if (year % 4 == 0) 
      nb_day_month = 29; 
    else 
      nb_day_month = 28;
    break;
  } // switch
  
  if (nb_day_month < day) {
    alert('Ce jour du mois n\'existe pas ' + '\n' + 'Champ '+ ' ' + field_name);
    // alert('month=' + month + ' => nb_day_month < day : ' + nb_day_month + ' < '+ day);
    return false;
  }

  return true;
}
