﻿function formatDate(d, langue) {
  if(langue == 'FR') {
      return getWeekDay(d)+' '+padLeft(String(d.getDate()),2,'0')+'/' +padLeft(String(d.getMonth()+1), 2, '0')+'/'+d.getFullYear();
  } else {
      return getWeekDay(d)+' '+d.getFullYear()+'-' +padLeft(String(d.getMonth()+1), 2, '0')+'-'+padLeft(String(d.getDate()),2,'0');
  }
}

function customFormatDate(d, format) {

	if(d == null) return "";
	if(typeof d == "undefined") { return "";}
	
	var result = format;
	result = result.replace("{D}", padLeft(String(d.getDate()),2,'0'));
	result = result.replace("{M}", padLeft(String(d.getMonth()+1), 2, '0'));
	result = result.replace("{Y}", d.getFullYear());
	result = result.replace("{w}", getWeekDay(d).toLowerCase());
	result = result.replace("{W}", getWeekDay(d));
	result = result.replace("{H}", padLeft(String(d.getHours()),2, '0'));
	result = result.replace("{h}", padLeft(String(getHour12(d)),2, '0'));
	result = result.replace("{N}", padLeft(String(d.getMinutes()),2, '0'));
	result = result.replace("{a}", getAmPm(d));
	
	return result;
}

function formatDateHour(d, langue) {
	var result = formatDate(d,langue);
	
	if(langue == 'FR') {
		result = result + ' à ' + formatHour(d,langue);
	} else {
		result = result + ' ' + formatHour(d,langue);
	}
	return result;
}

function formatHour(d, langue) {
	if(d == null) return "";
	if(typeof d == "undefined") { return "";}
	
	if(langue == 'FR') {
		return padLeft(String(d.getHours()),2, '0') + 'h' + padLeft(String(d.getMinutes()),2, '0');
	} else {	
		return padLeft(String(getHour12(d)),2, '0') + ':' + padLeft(String(d.getMinutes()),2, '0')+getAmPm(d);
	}
}

function getHour12(d) {
	var hours = d.getHours();
	
	if(hours > 12) {
		hours = hours - 12;
	}
	return hours;
}

function getAmPm(d) {
		var ampm = "am";
		
		if(d.getHours() > 12) {
			ampm = "pm";
		}
		return ampm;
}

function getWeekDay(d) {
  switch(d.getDay()) {
      case 0 : return METEO_LIB_DIMANCHE;
      case 1 : return METEO_LIB_LUNDI;
      case 2 : return METEO_LIB_MARDI;
      case 3 : return METEO_LIB_MERCREDI;
      case 4 : return METEO_LIB_JEUDI;
      case 5 : return METEO_LIB_VENDREDI;
      case 6 : return METEO_LIB_SAMEDI;
  }
  return "";
}

/* Renvoie une date à partir d'une chaine/entier au format YYYYMMJJ */
function getDate(intDate) {
  var strDate = String(intDate).replace(/-/g,'');
  var d = null;
  
  if(strDate.length >= 17) {
		d = new Date(strDate.substr(0,4), strDate.substr(4,2)-1, strDate.substr(6,2), strDate.substr(9,2), strDate.substr(12,2), strDate.substr(15,2));
	} else {
		d = new Date(strDate.substr(0,4), strDate.substr(4,2)-1, strDate.substr(6,2));
	}

  return d;
}
/* Renvoie une date 01/01/2000 à l'heure fournie au format HH:MM */
function getHour(strHours) {
	if ((strHours == "---") || (strHours == ""))
		return null;
		
	try
	{
		return new Date(2000, 1, 1, strHours.substr(0,2), strHours.substr(3,2), 0);
	}
	catch(e)
	{
		return null;
	}
}

function padLeft(strToPad, finalLength, carac) {
	var result = strToPad;
	for(i=result.length; i<finalLength; i++) {
		result = carac + result;
	}
	return result;
}
