/////////////////////////////////////////////////////
// WARNING : UTF-8 encoded !!                      //
// This file is the common javascript lib          //
/////////////////////////////////////////////////////

function onLoadBody() {
  if (typeof startKupu == 'function')
    kupu = startKupu();
  if (typeof liveSearchInit == 'function')
    liveSearchInit();
  if (typeof onLoadFunction == 'function')
    onLoadFunction();
};

function empty(input) {
	input.value = '';
}

function fill(input, text) {
	input.value = text;
}


function invisible(id) {
  var id_off = document.getElementById(id);
  if (id_off) id_off.style.display = 'none';
}

function count(id) {
  try {
    span1 = document.getElementById(id + '_from');
    span2 = document.getElementById(id);
    span2.innerHTML = '(' + span1.innerHTML + ')';
  }
  catch (ex) {
    span2.innerHTML = '(0)';
  }
}

function check_selection(form_name) {
  // verifie qu'il existe au moins une checkbox de selectionnée
  var form = document.forms[form_name];
  var return_value = false;
  for (var i=0; i<form.elements.length; i++) {
	if (form.elements[i].type == 'checkbox' || form.elements[i].type == 'radio') {
	  if (form.elements[i].checked == true) {
	    return_value = true;
		break;
      }
	}
  }
  if (return_value == false)
    alert('Merci de selectionner au moins un élément.');

  return return_value;
}

function selectAll(formname) {
  var form = document.forms[formname];
  for (var i=0; i < form.length; i++) {
    el = form.elements[i];
    if (el.type == 'checkbox') {
      el.checked = 'checked';
    }
  }
  return 1;
}

function unselectAll(formname) {
  var form = document.forms[formname];
  for (var i=0; i < form.length; i++) {
    el = form.elements[i];
    if (el.type == 'checkbox') {
      el.checked = '';
    }
  }
  return 1;
}

function reverseSelectAll(formname) {
  var form = document.forms[formname];
  for (var i=0; i < form.length; i++) {
    el = form.elements[i];
    if (el.type == 'checkbox') {
      if (el.checked == '')
        el.checked = 'checked';
      else
        el.checked = '';
    }
  }
  return 1;
}

/* for lists */
function colorListSelection() {
	if ( ! list_action_uid ) { ; }
	else {
		var tr = document.getElementById(list_action_uid);
		if (tr) tr.style.backgroundColor = '#8CACBB';
	}
	if (! last_action_uid ) { ; }
	else {
		var tr = document.getElementById(last_action_uid);
		if (tr) tr.style.backgroundColor = '';
	}
}
function updateActionUid(uid) {
	if (typeof list_action_uid != 'undefined' && typeof last_action_uid != 'undefined') {
		last_action_uid = list_action_uid;
		list_action_uid = uid;
	}
}

function formatString2(el) {
	var txt = (100 + el) + '';
	txt = txt.substr(1, 2);
	return txt;
}

function getTime() {
  var d = new Date();
  var ret = formatString2(d.getDate()) + '/';
  ret += formatString2(d.getMonth()+1) + '/' + (d.getYear()+1900);

  ret += ' ' + formatString2(d.getHours()) + ':' ;
  ret += formatString2(d.getMinutes());
  return ret;
}
function getDate() {
  var d = new Date();
  var ret = formatString2(d.getDate()) + '/';
  ret += formatString2(d.getMonth()+1) + '/' + (d.getYear()+1900);	
  return ret;
}
function getHour() {
  var d = new Date();
  var ret = formatString2(d.getHours()) + ':' ;
  ret += formatString2(d.getMinutes());
  return ret;
}

/* list tab */

content_global = 'expend';

function reduct_content() {
    content_global = 'reduct';
}

function expend_content() {
	content_global = 'expend';
	return true;
}

function execute_tab_content(action) {
	return;
	if (arguments.length > 0) {
		content_global = action;
	}
	var el = document.getElementById('tab-list-folder');
	if (el) {
		if (content_global == 'reduct') {
			el.style.border='1px dashed black';
			el.style.overflow = 'auto';
			el.style.maxHeight = '120px';
		} else {
			el.style.border='0px solid black';
			el.style.overflow = 'visible';
			el.style.maxHeight = 'none';
		}
	}
	return true;
}
function invers_tab_content() {
	if (content_global == 'reduct') content_global = 'expend';
	else content_global = 'reduct';
	execute_tab_content();
}

/* edit action */

function set_end(select) {
	switch (select.value) {
		case "4":
		case "5":
		case "6":
			var fin1 = document.getElementById('action_input_end');
			if (fin1.value) return;
			fin1.style.backgroundColor = '#ffccca';
			fin1.value = getDate();

			var fin2 = document.getElementById('action_heure_end');
			fin2.style.backgroundColor = '#ffccca';
			fin2.value = getHour();
			break;
	}
}

function displayEndDate(id) {
	var endSpan = document.getElementById(id + '_off');
	if (endSpan) {
		autoDisplay(id, 'inline');
	}
}

global_action_date_input = null;
global_action_time_input = null;

function empty2(id, data_type) {
	var input = document.getElementById(id);
	if (input && input.type == 'text') {
		if (data_type == 'date')
			global_action_date_input = input.value;
		else if (data_type == 'time')
			global_action_time_input = input.value;
		input.value = '';
	}
}

function empty_date (id) {
	empty2(id + '_input_begin', 'date');
	empty2(id + '_heure_begin', 'time');

	var span = document.getElementById(id + '-debut-on');
	span.style.display = 'none';
	span = document.getElementById(id + '-debut-off');
	span.style.display = 'inline';
}

function display_date(id) {
	var span = document.getElementById(id + '-debut-off');
	span.style.display = 'none';
	span = document.getElementById(id + '-debut-on');
	span.style.display = 'inline' ;
	var input = document.getElementById(id + '_input_begin');
	if (global_action_date_input) { 
		input.value = global_action_date_input;
		global_action_date_input = null;
	} else input.value = getDate();

	var input = document.getElementById(id + '_heure_begin');
	if (global_action_time_input) { 
		input.value = global_action_time_input;
		global_action_time_input = null;
	}
}

$(document).ready(function() {
    $.datepicker.setDefaults($.extend({showMonthAfterYear: false}, $.datepicker.regional['fr']));
    $(".calendrier").datepicker();
});
