function el(id) { return document.getElementById(id); }

// An implementation of the debug log. 

//var logging__ = true;
var logging__ = false;

function Log() {};

Log.lines = [];

Log.write = function(s) {
  if (logging__) {
    this.lines.push(xmlEscapeText(s));
    this.show();
  }
};

// Writes the given XML with every tag on a new line.
Log.writeXML = function(xml) {
  if (logging__) {
    var s0 = xml.replace(/</g, '\n<');
    var s1 = xmlEscapeText(s0);
    var s2 = s1.replace(/\s*\n(\s|\n)*/g, '<br/>');
    this.lines.push(s2);
    this.show();
  }
}

// Writes without any escaping
Log.writeRaw = function(s) {
  if (logging__) {
    this.lines.push(s);
    this.show();
  }
}

Log.clear = function() {
  if (logging__) {
    var l = this.div();
    l.innerHTML = '';
    this.lines = [];
  }
}

Log.show = function() {
  var l = this.div();
  l.innerHTML += this.lines.join('<br/>') + '<br/>';
  this.lines = [];
  l.scrollTop = l.scrollHeight;
}

Log.div = function() {
  var l = document.getElementById('log');
  if (!l) {
    l = document.createElement('div');
    l.id = 'log';
    l.style.position = 'absolute';
    l.style.width = '300px';
    l.style.height = '500px';
    l.style.top = '0px';
    l.style.left = '0px';
    l.style.overflow = 'auto';
    l.style.backgroundColor = '#f0f0f0';
    l.style.border = '1px solid gray';
    
    l.style.fontSize = '10px';
    l.style.padding = '5px';
    /*
    l.style.border = '0px';
    l.style.padding = '0px';
    l.style.height = '0px';
    
		l.style.width = '0px';
    */
    document.body.appendChild(l);
  
  }
  return l;
}

// Escape XML special markup chracters: tag delimiter < > and entity
// reference start delimiter &. The escaped string can be used in XML
// text portions (i.e. between tags).
function xmlEscapeText(s) {
  if (s && s.replace)
    return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
  return false;
}

// Escape XML special markup characters: tag delimiter < > entity
// reference start delimiter & and quotes ". The escaped string can be
// used in double quoted XML attribute value portions (i.e. in
// attributes within start tags).
function xmlEscapeAttr(s) {
  return xmlEscapeText(s).replace(/\"/g, '&quot;');
}

// Escape markup in XML text, but don't touch entity references. The
// escaped string can be used as XML text (i.e. between tags).
function xmlEscapeTags(s) {
  return s.replace(/</g, '&lt;').replace(/>/g, '&gt;');
}


function Timer() {}
Timer.start = function() {}
Timer.end = function() {}


/*---------------------------------------------
BROWSER DETECTION
---------------------------------------------*/
function BrowserCheck() {
 var b = navigator.appName
 if (b=="Netscape") this.b = "ns"
 else if (b=="Microsoft Internet Explorer") this.b = "ie"
 else this.b = b
 this.version = navigator.appVersion
 this.v = parseInt(this.version)
 this.dom=document.getElementById?1:0
 this.ns = (this.b=="ns" && this.v>=4)
 this.ns4 = (this.b=="ns" && this.v==4)
 this.ns5 = (this.b=="ns" && this.v==5)
 this.ie = (this.b=="ie" && this.v>=4)
 this.ie4 = (this.version.indexOf('MSIE 4')>0)
 this.ie5 = (this.version.indexOf('MSIE 5')>0)
 this.ie6 = (this.version.indexOf('MSIE 6')>0)
 this.min = (this.ns||this.ie)
}
is = new BrowserCheck()

function lib_bwcheck(){ //Browsercheck (needed)
 this.ver=navigator.appVersion; this.agent=navigator.userAgent
 this.dom=document.getElementById?1:0
 this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
 this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;
 this.ie4=(document.all && !this.dom)?1:0;
 this.ie=this.ie4||this.ie5||this.ie6
 this.mac=this.agent.indexOf("Mac")>-1
 this.opera5=this.agent.indexOf("Opera 5")>-1
 this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
 this.ns4=(document.layers && !this.dom)?1:0;
 this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5 || this.dom)
 return this
}
var bw=new lib_bwcheck() //Making browsercheck object



/*---------------------------------------------
function doPopUp(xPopup, xUrl,xWidth,xHeight,xScroll)
---------------------------------------------*/
function doPopUp(xPopup, xUrl,xWidth,xHeight,xScroll) {
  var xParameters = 'SCROLLBARS='+ xScroll +',WIDTH='+ xWidth +',HEIGHT='+ xHeight +',TOOLBAR=NO,LOCATION=NO,DIRECTORIES=NO,STATUS=NO,MENUBAR=NO,RESIZABLE=NO,COPYHISTORY=YES';
  var WinPop = window.open(xUrl, xPopup, xParameters);
  WinPop.focus();
}


/*---------------------------------------------
function doPopUp(xUrl,xWidth,xHeight,xScroll)
---------------------------------------------*/
function goToUrl(xUrl) {
  document.location.href = xUrl;
}

/*---------------------------------------------
function isValidEmail(strEmail)
---------------------------------------------*/
function isValidEmail(strEmail) {
	if (!/^[\.\w-_]+@[\.\w-_]+\.\w{2,4}$/g.test(strEmail))
		return true;
	else
		return false;
}

var tempo = new Array();
var max_mois = new Array();
for (n=1;n<100;n++) {
	if (n==4 || n==6 || n==9 || n==11) max_mois[n] = 30;
	else max_mois[n] = 31;
}

function CheckDate(quoi,p_jour,p_mois,p_an) {
    tmp = quoi.split(' ');
    quoi = tmp[0];
	quoi = quoi.replace(/-/g,'/');
	tempo = quoi.split('/');
	if (tempo.length < 3) {
	   return false;
	}
	jour = eval(tempo[(p_jour-1)]);
	mois = eval(tempo[(p_mois-1)]);
	an = eval(tempo[(p_an-1)]);
	if (bisx(an))
	   max_mois[2] = 29;
	else
	   max_mois[2] = 28;
	t_tempo = (jour<=max_mois[mois] && mois<=12 && mois>=1);
	return t_tempo;
}

function bisx(quoi) {
	if ((quoi % 100 == 0) && (quoi % 400 == 0)) return true;
	else if ((quoi % 4) == 0) return true;
	return false;
}


/*---------------------------------------------
function dateTest(formName)
Create 3 dropdown with names : sDay/sMonth/sYear
---------------------------------------------*/
function dateTest(formName) {
  theDay = formName.sDay.options[formName.sDay.options.selectedIndex].value;
  theMonth = formName.sMonth.options[formName.sMonth.options.selectedIndex].value;
  theYear = formName.sYear.options[formName.sYear.options.selectedIndex].value;

  var DayPerMonth = new MonthDetails(31,28,31,30,31,30,31,31,30,31,30,31);

  // bissextile
  if (((theYear % 4 == 0) && (theYear % 100 != 0)) || (theYear % 400 == 0)) { DayPerMonth[1] = 29; }

  if (theDay > DayPerMonth[theMonth-1]) {
    alert("This is not a correct date.");
    return false;
  }

  // right date
  return true;
}

/*---------------------------------------------
function MonthDetails (m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11)
Used with dateTest function
---------------------------------------------*/
function MonthDetails (m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11) {
    this[0] = m0;this[1] = m1;this[2] = m2;this[3] = m3;this[4] = m4;this[5] = m5;this[6] = m6;this[7] = m7;this[8] = m8;this[9] = m9;this[10] = m10;this[11] = m11;
}

/*---------------------------------------------
function IsValidNumber(frmField)
TEST if string is a valid number
---------------------------------------------*/
function IsValidNumber(frmField)
{
	str=eval('document.formulaire.'+monchamp+'.value');
	if (isNaN(frmField.value)) {
		frmField.focus();
		return false;
	}
	else {
		return true
	}
}

/*---------------------------------------------
function IsStrLong(frmField,intlong)
TEST if string is not too long
---------------------------------------------*/
function IsStrLong(frmField,intlong) {
	if (frmField.value.length > intlong) {
		var msg =	"Field '" + frmField.name + "' should be less than " + intlong + " characters long."
		var msg =	msg + "\n\n" + "Its current length is " + frmField.value.length + " characters."
//		window.alert(msg);
		frmField.focus();
	  return false;
	}
	else {
		return true;
	}
}

/*---------------------------------------------
function confirmDelete(id,msg,filename)
---------------------------------------------*/
function confirmDelete(id,msg,filename) {
	if (confirm(msg))
		document.location.href=filename+'?action=delete&id='+id;
}

/*---------------------------------------------
function comparePasswords(pass1,pass2)
---------------------------------------------*/
function comparePasswords(pass1,pass2) {
  if (pass1.value != pass2.value) {
    var msg = 'Password and confirm password should match';
    window.alert(msg);
    return false;	}
	else
	{	return true; }
}

/*---------------------------------------------
function isMandatory(frmField)
---------------------------------------------*/
function isMandatory(frmField) {
  if (frmField.value == "") {
    window.alert("Please supply '" + frmField.name+"'.");
    frmField.focus();
    return false;	}
  else { return true;	}
}

/*---------------------------------------------
function trim(string)
---------------------------------------------*/

function trim(string)
{
return string.replace(/(^\s*)|(\s*$)/g,'');
}

/*---------------------------------------------
function refreshSelect(formInput,currentpage,paramName,param)
---------------------------------------------*/
function refreshSelect(formInput,currentpage,paramName,param) {
  var value = formInput.options[formInput.selectedIndex].value;
  var redirect = currentpage+'?'+paramName+'='+value+'&'+param;
  document.location.href = redirect;
}

function showHide(id) {
  var elem = document.getElementById(id);
  if (elem.style.display == 'none')
    elem.style.display = 'block';
  else
    elem.style.display = 'none';
}

/*-----------------------------------------------
function toggleDiv(divID)
function showDiv(divID)
function hideDiv(divID)
--------------------------------------------------*/

function toggleDiv(id) { // Toggle a DIV
	if (document.all[id].style.visibility == "visible")
  	document.all[id].style.visibility = "hidden";
  else
  	document.all[id].style.visibility = "visible";
}

function showDiv(id) { // Show a DIV
	document.all[id].style.visibility = "visible";
}

function hideDiv(id) {// Hide a DIV
	document.all[id].style.visibility = "hidden";
}

function moveDiv(id, x, y) {// Hide a DIV
	document.all[id].style.left = x;
	document.all[id].style.top = y;
}

var mousePosX, mousePosY;
var isMousePositionInit = false;

function initMousePosition() {
  document.onmousemove = getMousePosition;
}

function getMousePosition() {
  if ( ! isMousePositionInit )
    initMousePosition();
  mousePosX = event.clientX + document.body.scrollLeft;
  mousePosY = event.clientY + document.body.scrollTop;
}

function dspProperties(nom_objet)
{
	for (prop in window){
    document.write("Propriété : " + prop + " ---------> " + window[prop] + "<br>");
  }
}

function confirmQuitPreinscription()
{
	if (confirm("Vous allez perdre les informations saisies, êtes-vous sûr(e) ?")) {
    document.location.href='recapitulatif.php';
	}
}

function htmlCollectionToArray(htmlCol) {  
	a = new Array();
	for (i = 0; i < htmlCol.length; i++)
		a[a.length] = htmlCol[i];
	return a;
}
 
function ouvrirPopup(page,nom,option) {
  window.open(page,nom,option);
}
