function isLetter (c) {
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) ) }

function isDigit (c) {
	return ((c >= "0") && (c <= "9")) }

function isDate(d) {
	var amin = 1980; // année mini
	var separateur = "/";
	var pos1 = d.indexOf(separateur, 0);
	var pos2 = d.indexOf(separateur, pos1 + 1);
	var j = (d.substring(0, pos1));
	var m = (d.substring(pos1 + 1, pos2));
	var a = (d.substring(pos2 + 1));
	var ok = true;
	if ( ((isNaN(j))||(j < 1)||(j > 31))) ok = false;
	if ( ((isNaN(m))||(m < 1)||(m > 12))) ok = false;
	if ( ((isNaN(a))||(a < amin))) ok = false;
	if (ok) {
		var d2 = new Date(a, m - 1, j);
		j2 = d2.getDate();
		m2 = d2.getMonth() + 1;
		a2 = d2.getYear();
		if (a2 <= 100)
			a2 = 1900 + a2;
		else
			if (a2 < 2000)
				a2 = 2000 + (a2 - 100);
		if ( (j != j2)||(m != m2)||(a != a2) ) ok = false;
	}
	return ok;
}

function toDate(strDate) {
	var separateur = "/";
	var pos1 = strDate.indexOf(separateur, 0);
	var pos2 = strDate.indexOf(separateur, pos1 + 1);
	var j = (strDate.substring(0, pos1));
	var m = (strDate.substring(pos1 + 1, pos2));
	var a = (strDate.substring(pos2 + 1));
	return new Date(a, m - 1, j);
}

function isHeure(str) {
	var separateur = ":";
	var pos = str.indexOf(":");
	var ok = true;
	if (pos == 2) {
		var h = (str.substring(0,2));
		var m = (str.substring(3,5));
	} else {
		if (pos == 1) {
			var h = (str.substring(0,1));
			var m = (str.substring(2,5));
		} else
			ok = false;
	}
	if ( ((isNaN(h))||(h < 0)||(h > 23)) || ((isNaN(m))||(m < 0)||(m > 59)) )
		ok = false;
	if ( (str.substring(pos, pos + 1) != separateur) && (ok) )
		ok = false;
	return ok;
}

function isEMail(emailStr) {
	var validDomain = false;
	var AtPos = emailStr.lastIndexOf("@");
	var FirstAtPos = emailStr.indexOf("@");
	var DotPos = emailStr.lastIndexOf(".");
	var SpacePos = emailStr.lastIndexOf(" ");
	if(((AtPos > 0) && (DotPos > 0) && (AtPos < DotPos) && (AtPos==FirstAtPos) && (SpacePos<0))) {
		var domainExtList = 'ac.ad.ae.af.ag.ai.al.am.an.ao.aq.ar.as.at.au.aw.az.ba.bb.bd.be.bf.bg.bh.bi.bj.bm.bn.bo.br.bs.bt.bv.bw.by.bz.ca.cc.cd.cf.cg.ch.ci.ck.cl.cm.cn.co.cr.cs.cu.cv.cx.cy.cz.de.dj.dk.dm.do.dz.ec.ee.eg.eh.er.es.et.eu.fi.fj.fk.fm.fo.fr.fx.ga.gb.gd.ge.gf.gg.gh.gi.gl.gm.gn.gp.gq.gr.gs.gt.gu.gw.gy.hk.hm.hn.hr.ht.hu.id.ie.il.i?????????Au???????m.in.io.iq.ir.is.it.je.jm.jo.jp.ke.kg.kh.ki.km.kn.kp.kr.kw.ky.kz.la.lb.lc.li.lk.lr.ls.lt.lu.lv.ly.ma.mc.md.mg.mh.mk.ml.mm.mn.mo.mp.mq.mr.ms.mt.mu.mv.mw.mx.my.mz.na.nc.ne.nf.ng.ni.nl.no.np.nr.nt.nu.nz.om.pa.pe.pf.pg.ph.pk.pl.pm.pn.pr.ps.pt.pw.py.qa.re.ro.ru.rw.sa.sb.sc.sd.se.sg.sh.si.sj.sk.sl.sm.sn.so.sr.st.su.sv.sy.sz.tc.td.tf.tg.th.tj.tk.tm.tn.to.tp.tr.tt.tv.tw.tz.ua.ug.uk.um.us.uy.uz.va.vc.ve.vg.vi.vn.vu.wf.ws.ye.yt.yu.za.zm.zr.zw.com.edu.gov.int.mil.net.org.biz.pro.info.aero.name.coop.arpa.nato.museum.EoF';
		var domainExt = domainExtList.split(".");
		var emailExt = emailStr.substr(DotPos + 1);
		emailExt = emailExt.toLowerCase();
		for(i = 0; domainExt.length; i++) {
			if (domainExt[i] == 'EoF') break; //infinite loop
			else if (emailExt == domainExt[i]) {validDomain = true; break;}
		}
	    validDomain = true;
	}
	return(validDomain);
}

//---------- Contrôle de chaîne contenant uniquement un nombre ----------//

function isNumberString(NumStr) {
	NumStr = trim(NumStr);
	NumStr = NumStr.replace( ",", "." );
	NumStr = NumStr.replace( /[^0-9\.]/, "" );
	var ok = true;
	if (NumStr.length > 0) {
		if (isNaN(NumStr)) ok = false;
	} else
		ok = false;
	return ok;
}

//---------- "Nettoie" une chaîne contenant un nombre ----------//

function cleanNumberString(NumStr) {
	NumStr = trim(NumStr);
	/*re = /,/gi;
	NumStr = NumStr.replace(re, ".");
	re = / /gi;
	NumStr = NumStr.replace(re, "");*/
	NumStr = NumStr.replace( ",", "." );
	NumStr = NumStr.replace( /[^0-9\.]/, "" );
	return NumStr;
}

//---------- Contrôle de chaîne contenant uniquement des chiffres ----------//

function isNumString(NumStr) {
	var ok = true;
	var i = 0;
	while (ok && i < NumStr.length) {
		var c = NumStr.charAt(i);
		if (!isDigit(c)) ok = false;
		i++;
	}
	return ok;
}

//---------- Transformation de chaîne : ne garde que les chiffres ----------//

function toNumString(str) {
	str2 = "";
	var i;
	for (i = 0; i < str.length; i++) {
		var c = str.charAt(i);
		if (isDigit(c)) str2 += c;
	}
	return str2;
}

//---------- Contrôle de chaîne alphanumérique ----------//

function isAlphaNumString(NumStr) {
	var i;
	var ok = true;
	for (i = 0; i < NumStr.length; i++) {
		var c = NumStr.charAt(i);
		if (!(isLetter(c) || isDigit(c))) ok = false;
	}
	return ok;
}

//---------- Contrôle de chaîne alpha ----------//

function isAlphaString(NumStr) {
	var i;
	var ok = true;
	for (i = 0; i < NumStr.length; i++) {
		var c = NumStr.charAt(i);
		if (!(isLetter(c))) ok = false;
	}
	return ok;
}

//---------- Contrôle de chaîne vide ----------//

function trim(str) {
	str = trimLeft(str);
	str = trimRight(str);
	return str;
}

function trimLeft(str) {
	var regTrimleft = /^\s+/g;
	str = str.replace(regTrimleft,"");
	return str;
}

function trimRight(str) {
	var regTrimright = /\s+$/g;
	str = str.replace(regTrimright,"");
	return str;
}

function isNotEmpty(str) {
	str = trim(str);
	if (str.length == 0) return false; else return true;
}

function isEmpty(str) {
	str = trim(str);
	if (str.length==0) return true; else return false;
}

function isCodePostal(str) {
	str = trim(str);
	ok = false;
	if (isNotEmpty(str))
		if (isNumberString(str))
			if (str.length >= 4 && str.length <= 6)
				ok = true;
	return ok;
}

//---------- Popups ----------//

function openImageView(url_image,width,height){
	pop_width = width + 50;
	pop_height = height + 50;
	if( screen ){
		// Centre la popup si l'objet screen est disponible
		pop_left = (screen.width - pop_width) / 2;
		pop_top = (screen.height - pop_height) / 2;
	}else{
		// Position par défaut
		pop_top = pop_left = 300;
	}
	var win = window.open("","imageview","width=" + pop_width + ",height=" + pop_height + ",top=" + pop_top + ",left=" + pop_left );
	win.document.write( '<table width="100%" height="100%"><tr><td align="center" valign="middle">' );
	win.document.write( '<img src="' + url_image + '">' );
	win.document.write( '</td></tr></table>' );
	win.focus();
}

function getRandomNum(lbound, ubound) {
	return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}
function getRandomChar(number, lower, upper, other, extra) {
	var numberChars = "0123456789";
	var lowerChars = "abcdefghijklmnopqrstuvwxyz";
	var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var otherChars = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";
	var charSet = extra;
	if (number == true)
		charSet += numberChars;
	if (lower == true)
		charSet += lowerChars;
	if (upper == true)
		charSet += upperChars;
	if (other == true)
		charSet += otherChars;
	return charSet.charAt(getRandomNum(0, charSet.length));
}
function getPassword(length, extraChars, firstNumber, firstLower, firstUpper, firstOther, latterNumber, latterLower, latterUpper, latterOther) {
	var rc = "";
	if (length > 0)
		rc = rc + getRandomChar(firstNumber, firstLower, firstUpper, firstOther, extraChars);
	for (var idx = 1; idx < length; ++idx) {
		rc = rc + getRandomChar(latterNumber, latterLower, latterUpper, latterOther, extraChars);
	}
	return rc;
}