var lang="fr";
function input_setvalue(theinput,thevalue) {
  if(document.getElementById) {
    obj = document.getElementById(theinput);
    obj.value = thevalue;
  }
  else if(document.all) {
    obj = document.all[theinput];
    obj.value = thevalue;
  }
  else if(document.layers) {
    obj = eval("document."+theinput);
    obj.value = thevalue;
  }
}
function div_display(thediv,thedisplay) {
  if(document.getElementById) {
    obj = document.getElementById(thediv);
    if (obj)
	    obj.style.display = thedisplay;
  }
  else if(document.all) {
    obj = document.all[thediv];
	if (obj)
	    obj.style.display = thedisplay;
  }
  else if(document.layers) {
    obj = eval("document."+thediv);
	if (obj)
	    obj.style.display = thedisplay;
  }
}
function SetCSSValue(where,what,to){
   var r='cssRules';
   if(document.all && navigator.appName.indexOf('Opera')==-1)
      r='rules';
   var i;
   for(i=0;i<document.styleSheets.length;++i) {
      var cssrules=document.styleSheets[i][r];
      for(j=0;j<cssrules.length;++j)
         if(cssrules[j].selectorText.toUpperCase()==where.toUpperCase()) {
            cssrules[j].style[what]=to;
            return false;
         }
   }
   return false;
}
function codefin_off() {
	div_display('codefin','none');
}
function codefin_on() {
	div_display('codefin','block');
	setTimeout('codefin_off()',4000);
}

function trim(val) {
  var retour = ""
  for(i=0;i < val.length;i++)
    if(val.charAt(i) != " ") {
      retour += val.charAt(i)
    }
  return retour
} 

function isNotEmpty(valeur,texte) {
   var re = /^\S+$/;
   valeur = trim(valeur);
   if(!re.test(valeur)) {
        if (lang=="en")
          alert("Data required : “"+texte+"“ !");
		else
          alert("Il faut remplir la zone “"+texte+"“ !");
        return false;
   }
   return true;
}

function isInteger(valeur,texte){
   var re = /^(-|\+)?\d+$/;
   
    if(!re.test(valeur)){
        if (lang=="en")
          alert("Incorrect number in “"+texte+"“ !");
		else
          alert("Vous n'avez pas saisie un nombre correct pour “"+texte+"“ !");
        return false;
   }
   return true;
}

function isReal(valeur,texte){
   var reI = /^(-|\+)?\d+$/;
   var reR = /^(-|\+)?\d+([\.])?\d+$/;
   if(reI.test(valeur) || reR.test(valeur))
	   return true;
   if (lang=="en")
     alert("Incorrect number in “"+texte+"“ (ex : 1.5) !");
   else
     alert("Vous n'avez pas saisie un nombre correct pour “"+texte+"“ (ex 1.5) !");
   return false;
}

function isDateFR(valeur) { 
   var re = /^\d{2}[\/]\d{2}[\/]\d{4}$/;
    if (!re.test(valeur)) { 
        if (lang=="en")
          alert("Incorrect date (dd/mm/yyyy) !");
		else
          alert("Vous n'avez pas saisie une date correcte !");
        return false; 
    } 
    return true; 
} 

function isCP(valeur){
   //var re = /^\d{1}\d{1}\d{1}\d{1}\d{0,1}$/;
   var re = /^\d{4,5}$/;

   if(!re.test(valeur)){
        if (lang=="en")
          alert("Incorrect zip code !");
		else
          alert("Vous n'avez pas saisie un code postal correct !");
        return false;
   }
   return true;
}

function isMailOk(valeur) {
  var mail = /^([a-zA-Z0-9_\.\-])+@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;

  reponse=mail.test(valeur)
  if (reponse) {
    return true;
  } else {
    if (lang=="en")
      alert("Incorrect mail address !");
    else
      alert("Adresse mail INVALIDE !");
    return false;
  }
}

// arg0 = emptyok
// arg1 = bqe
// arg2 = gui
// arg3 = cpt
// arg4 = cle
function isRIBvalid(emptyok) {
if (isRIBvalid.arguments.length>=5) {
	var emptyok = isRIBvalid.arguments[0];
    var bqe=isRIBvalid.arguments[1];
    var gui=isRIBvalid.arguments[2];
    var cpt=isRIBvalid.arguments[3].toUpperCase();

    if (bqe+gui+cpt != "") {
      // Conversion du numero de compte en chiffres
      var tab= "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
      var tab1="123456789123456789234567890123456789".split("");
    
      while (cpt.match(/\D/) != null)
        cpt=cpt.replace(/\D/, tab1[tab.indexOf(cpt.match(/\D/))]);
      var cp=parseInt  (cpt, 10);

      //Calcul clé RIB
      a=bqe%97;
      a=a*100000+parseInt(gui, 10);
      a=a%97;
      a=a*Math.pow(10, 11) + cp;
      a=a%97
      a=a*100;
      a=a%97
      a=97-a;
      if (isRIBvalid.arguments[4]==a)
        return true;
      else {
        alert("Incorrect !");
        return false;
      }
    } else {
      if (emptyok) {
        return true;
	  } else {
		if (lang=="en")
			alert("Code needed !");
		else
			alert("Entrez un code !");
        return false;
	  }
	}
  } else {
    alert("Incomplet !");
    return false;
  }
} 
function LZ(x) {return(x<0||x>9?"":"0")+x}
function isDate(val,format) { var date=getDateFromFormat(val,format); if (date==0) { return false;} return true;}
function compareDates(date1,dateformat1,date2,dateformat2) { var d1=getDateFromFormat(date1,dateformat1); var d2=getDateFromFormat(date2,dateformat2); if (d1==0 || d2==0) { return -1;} else if (d1 > d2) { return 1;} return 0;}
function formatDate(date,format) {
 format=format+""; var result=""; var i_format=0; var c=""; var token=""; var y=date.getYear()+""; var M=date.getMonth()+1; var d=date.getDate(); var E=date.getDay(); var H=date.getHours(); var m=date.getMinutes(); var s=date.getSeconds(); var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k; var value=new Object(); if (y.length < 4) {y=""+(y-0+1900);} value["y"]=""+y; value["yyyy"]=y; value["yy"]=y.substring(2,4); value["M"]=M; value["MM"]=LZ(M); value["MMM"]=MONTH_NAMES[M-1]; value["NNN"]=MONTH_NAMES[M+11]; value["d"]=d; value["dd"]=LZ(d); value["E"]=DAY_NAMES[E+7]; value["EE"]=DAY_NAMES[E]; value["H"]=H; value["HH"]=LZ(H); if (H==0){value["h"]=12;} else if (H>12){value["h"]=H-12;} else {value["h"]=H;} value["hh"]=LZ(value["h"]); if (H>11){value["K"]=H-12;} else {value["K"]=H;} value["k"]=H+1; value["KK"]=LZ(value["K"]); value["kk"]=LZ(value["k"]); if (H > 11) { value["a"]="PM";} else { value["a"]="AM";} value["m"]=m; value["mm"]=LZ(m); value["s"]=s; value["ss"]=LZ(s); 
 while (i_format < format.length) {
 c=format.charAt(i_format); token="";
 while ((format.charAt(i_format)==c) && (i_format < format.length)) { token += format.charAt(i_format++);}
 if (value[token] != null) { result=result + value[token];} else { result=result + token;}
}
 return result;
}
function _isInteger(val) { var digits="1234567890"; for (var i=0; i < val.length; i++) { if (digits.indexOf(val.charAt(i))==-1) { return false;} } return true;}
function _getInt(str,i,minlength,maxlength) { for (var x=maxlength; x>=minlength; x--) { var token=str.substring(i,i+x); if (token.length < minlength) { return null;} if (_isInteger(token)) { return token;} } return null; }
function getDateFromFormat(val,format) {
 val=val+""; format=format+""; var i_val=0; var i_format=0; var c=""; var token=""; var token2=""; var x,y; var now=new Date(); var year=now.getYear(); var month=now.getMonth()+1; var date=1; var hh=now.getHours(); var mm=now.getMinutes(); var ss=now.getSeconds(); var ampm="";
 while (i_format < format.length) {
 c=format.charAt(i_format);
 token="";
 while ((format.charAt(i_format)==c) && (i_format < format.length)) {
 token += format.charAt(i_format++);
 }
 if (token=="yyyy" || token=="yy" || token=="y") {
 if (token=="yyyy") { x=4;y=4;}
 if (token=="yy") { x=2;y=2;}
 if (token=="y") { x=2;y=4;}
 year=_getInt(val,i_val,x,y);
 if (year==null) { return 0;}
 i_val += year.length;
 if (year.length==2) {
 if (year > 70) { year=1900+(year-0);}
 else { year=2000+(year-0);}
 } }
 else if (token=="MMM"||token=="NNN"){
 month=0;
 for (var i=0; i<MONTH_NAMES.length; i++) {
 var month_name=MONTH_NAMES[i];
 if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) {
 if (token=="MMM"||(token=="NNN"&&i>11)) {
 month=i+1;
 if (month>12) { month -= 12;}
 i_val += month_name.length;
 break;
 } } }
 if ((month < 1)||(month>12)){return 0;}
 }
 else if (token=="EE"||token=="E"){
 for (var i=0; i<DAY_NAMES.length; i++) {
 var day_name=DAY_NAMES[i];
 if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) {
 i_val += day_name.length;
 break;
 } } }
 else if (token=="MM"||token=="M") {
 month=_getInt(val,i_val,token.length,2);
 if(month==null||(month<1)||(month>12)){return 0;}
 i_val+=month.length;}
 else if (token=="dd"||token=="d") {
 date=_getInt(val,i_val,token.length,2);
 if(date==null||(date<1)||(date>31)){return 0;}
 i_val+=date.length;}
 else if (token=="hh"||token=="h") {
 hh=_getInt(val,i_val,token.length,2);
 if(hh==null||(hh<1)||(hh>12)){return 0;}
 i_val+=hh.length;}
 else if (token=="HH"||token=="H") {
 hh=_getInt(val,i_val,token.length,2);
 if(hh==null||(hh<0)||(hh>23)){return 0;}
 i_val+=hh.length;}
 else if (token=="KK"||token=="K") {
 hh=_getInt(val,i_val,token.length,2);
 if(hh==null||(hh<0)||(hh>11)){return 0;}
 i_val+=hh.length;}
 else if (token=="kk"||token=="k") {
 hh=_getInt(val,i_val,token.length,2);
 if(hh==null||(hh<1)||(hh>24)){return 0;}
 i_val+=hh.length;hh--;}
 else if (token=="mm"||token=="m") {
 mm=_getInt(val,i_val,token.length,2);
 if(mm==null||(mm<0)||(mm>59)){return 0;}
 i_val+=mm.length;}
 else if (token=="ss"||token=="s") {
 ss=_getInt(val,i_val,token.length,2);
 if(ss==null||(ss<0)||(ss>59)){return 0;}
 i_val+=ss.length;}
 else if (token=="a") {
 if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="AM";}
 else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="PM";}
 else {return 0;}
 i_val+=2;}
 else {
 if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
 else {i_val+=token.length;}
 }
 }
 if (i_val != val.length) { return 0;}
 if (month==2) {
 if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
 if (date > 29){ return 0;}
 }
 else { if (date > 28) { return 0;}}
 }
 if ((month==4)||(month==6)||(month==9)||(month==11)) {
 if (date > 30) { return 0;}
 }
 if (hh<12 && ampm=="PM") { hh=hh-0+12;}
 else if (hh>11 && ampm=="AM") { hh-=12;}
 var newdate=new Date(year,month-1,date,hh,mm,ss);
 return newdate.getTime();
}
function parseDate(val) {
 var preferEuro=(arguments.length==2)?arguments[1]:false; generalFormats=new Array('y-M-d','MMM d, y','MMM d,y','y-MMM-d','d-MMM-y','MMM d'); monthFirst=new Array('M/d/y','M-d-y','M.d.y','MMM-d','M/d','M-d'); dateFirst =new Array('d/M/y','d-M-y','d.M.y','d-MMM','d/M','d-M'); var checkList=new Array('generalFormats',preferEuro?'dateFirst':'monthFirst',preferEuro?'monthFirst':'dateFirst'); var d=null;
 for (var i=0; i<checkList.length; i++) {
 var l=window[checkList[i]];
 for (var j=0; j<l.length; j++) {
 d=getDateFromFormat(val,l[j]);
 if (d!=0) { return new Date(d);}
 }
}
 return null;
}

function SendForm(aform,ascript,adivstatus) {
	var fieldsdata = Form.serialize(aform,true);
	if ($(adivstatus)) {
		$(adivstatus).innerHTML = "<img src='i/loader.gif'>";
		$(adivstatus).style.display = 'block';
	}
	new Ajax.Updater('formajaxdiv',ascript,{
		method: 'post',
		evalScripts: true,
		asynchronous: true,
		contentType: 'application/x-www-form-urlencoded',
		encoding: 'UTF-8',
		parameters: fieldsdata,
		onSuccess: function(transport){
			$(adivstatus).innerHTML = "OK";
			$(adivstatus).style.display = 'block';
		},
		onFailure: function(transport){
			$(adivstatus).innerHTML = "<font color=#CC0000>"+transport.statusText+"</font>";
			$(adivstatus).style.display = 'block';
		}
	});
}
function CloseForm() {
	$('formajaxdiv').innerHTML = "";
	$('formajaxdiv').style.visibility = 'hidden';
}

function setRowColor(theRow, theRowNum, theAction, theDefaultColor)
{
    var theCells = null;

    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }
    currentColor = theCells[0].className;
    var newColor = null;

    if (theAction == 'over') {
		newColor = 'sel'+theDefaultColor.toLowerCase();
	}

    if (theAction == 'out') {
		newColor = theDefaultColor.toLowerCase();
	}

    var c = null;
    for (c = 0; c < theCells.length; c++) {
       theCells[c].className = newColor;
    }

    return true;
}

/* Suggest OACI
*/
var curSuggest = null;
var oldSuggest = '';
var theResult = '';
function SuggestOnKey(e,keyword,col0,col1,adiv,genre,idex)
{
	e = (!e) ? window.event : e;
	var kw = $(keyword);
	if (!kw.form.onkeypress) {
		kw.suggestdiv = adiv;
		kw.form.onkeypress = function (e) {
			e = (!e) ? window.event : e;
			if (e.keyCode == 9) {
				curSuggest = null;
				oldSuggest = '';
				$(kw.suggestdiv).style.visibility = 'hidden';
			}
		};
	}
	var b=e.keyCode;
	if (b==27) { // Esc
		curSuggest = null;
		oldSuggest = '';
		$(kw.suggestdiv).style.visibility = 'hidden';
		return false;
	} else
	if (b==38) { // Up
		ScrollResult(-1);
		return false;
	} else
	if (b==40) { // Down
		ScrollResult(+1);
		return false;
	}
	if (b==13) { // Return
		if (!document.getElementsByTagName || !document.createTextNode) return;
		var rows = document.getElementById('tblsuggest').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
		if (curSuggest != null) {
			rows[curSuggest].onclick(e);
		}
		return false;
	} else {
		var K = $(keyword).value = $(keyword).value.toUpperCase();
		if ((K != '') && (K != oldSuggest)) {
			$(adiv).innerHTML = "<img src='i/loader.gif' border=0>";
			new Ajax.Request('priv.ajax.searchoaci.php',
			{
				method: 'post',
				parameters: {key: K, idex:idex, genre:genre},
				onSuccess: function(transport){
					if (transport.responseText.indexOf("error") >= 0)
					{
						alert(transport.responseText);
						return;
					}
					oldSuggest = K;
					DisplayResult(transport.responseXML.documentElement,keyword,col0,col1,adiv,genre);
				},
				onFailure: function(transport){
					alert('Error : '+transport.statusText)
				}
			});
		}
	}
}

function DisplayResult(xmlresponse,keyword,col0,col1,adiv,genre)
{
	var div = "<table id=\"tblsuggest\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tbody>"; 
	for(i=0;i<xmlresponse.childNodes.length;i++)
	{
		div += "<tr>" + 
		       "<td>" + xmlresponse.childNodes.item(i).firstChild.data + "</td>" +
		       "<td>" + xmlresponse.childNodes.item(i).attributes[0].nodeValue + "</td>" +
			   "</tr>";
	}
	div += "</tbody></table>";
	$(adiv).innerHTML = div;
	curSuggest = null;
    if (!document.getElementsByTagName || !document.createTextNode) return;
    var rows = document.getElementById('tblsuggest').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
    for (i = 0; i < rows.length; i++) {
        rows[i].onclick = function() {
			if (col0)
				$(col0).value = this.cells[0].firstChild.data;
			if (col1)
				$(col1).value = this.cells[1].firstChild.data;
			curSuggest = null;
			oldSuggest = '';
			$(adiv).style.visibility = 'hidden';
        }
        rows[i].onmouseover = function() {
            this.className = 'trsuggest';
        }
        rows[i].onmouseout = function() {
            this.className = '';
        }
    }
	if (rows.length > 0) {
		$(adiv).style.visibility = 'visible';
		$(adiv).style.left = Element.cumulativeOffset($(keyword)).left + 'px';
		$(adiv).style.top = Element.cumulativeOffset($(keyword)).top + $(keyword).offsetHeight + 'px';
		$(adiv).style.width = '300px';
		if ((rows.length == 1) && (rows[0].cells[1].firstChild.data != "")) {
			if (col0)
				$(col0).value = rows[0].cells[0].firstChild.data;
			if (col1)
				$(col1).value = rows[0].cells[1].firstChild.data;
		}
	}
}

function ScrollResult(sens)
{
    if (!document.getElementsByTagName || !document.createTextNode) return;
    var rows = document.getElementById('tblsuggest').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
	if (curSuggest != null)
		rows[curSuggest].className = '';
	if (sens > 0) {
		if (curSuggest == null)
			curSuggest = 0;
		else
			curSuggest++;
	} else {
		if (curSuggest == null)
			curSuggest = rows.length-1;
		else
			curSuggest--;
	}
	if (curSuggest < 0)
		curSuggest = rows.length-1;
	if (curSuggest >= rows.length)
		curSuggest = 0;
	rows[curSuggest].className = 'trsuggest';
}

function ScrollableTable (tableEl, tableHeight, tableWidth) {
	this.initIEengine = function () {
		this.containerEl.style.overflowY = 'auto';
		if (this.tableEl.parentElement.clientHeight - this.tableEl.offsetHeight < 0) {
			this.tableEl.style.width = this.newWidth - this.scrollWidth +'px';
		} else {
			this.containerEl.style.overflowY = 'hidden';
			this.tableEl.style.width = this.newWidth +'px';
		}
		if (this.thead) {
			var trs = this.thead.getElementsByTagName('tr');
			for (x=0; x<trs.length; x++) {
				trs[x].style.position ='relative';
				trs[x].style.setExpression("top",  "this.parentElement.parentElement.parentElement.scrollTop + 'px'");
			}
		}
		if (this.tfoot) {
			var trs = this.tfoot.getElementsByTagName('tr');
			for (x=0; x<trs.length; x++) {
				trs[x].style.position ='relative';
				trs[x].style.setExpression("bottom",  "(this.parentElement.parentElement.offsetHeight - this.parentElement.parentElement.parentElement.clientHeight - this.parentElement.parentElement.parentElement.scrollTop) + 'px'");
			}
		}
 
		eval("window.attachEvent('onresize', function () { document.getElementById('" + this.tableEl.id + "').style.visibility = 'hidden'; document.getElementById('" + this.tableEl.id + "').style.visibility = 'visible'; } )");
	};
	this.initFFengine = function () {
		this.containerEl.style.overflow = 'hidden';
		this.tableEl.style.width = this.newWidth + 'px';
 
		var headHeight = (this.thead) ? this.thead.clientHeight : 0;
		var footHeight = (this.tfoot) ? this.tfoot.clientHeight : 0;
		var bodyHeight = this.tbody.clientHeight;
		var trs = this.tbody.getElementsByTagName('tr');
		if (bodyHeight >= (this.newHeight - (headHeight + footHeight))) {
			this.tbody.style.overflow = '-moz-scrollbars-vertical';
			for (x=0; x<trs.length; x++) {
				var tds = trs[x].getElementsByTagName('td');
				tds[tds.length-1].style.paddingRight += this.scrollWidth + 'px';
			}
		} else {
			this.tbody.style.overflow = '-moz-scrollbars-none';
		}
 
		var cellSpacing = (this.tableEl.offsetHeight - (this.tbody.clientHeight + headHeight + footHeight)) / 4;
		this.tbody.style.height = (this.newHeight - (headHeight + cellSpacing * 2) - (footHeight + cellSpacing * 2)) + 'px';
 
	};
	this.tableEl = tableEl;
	this.scrollWidth = 16;
	this.originalHeight = this.tableEl.clientHeight;
	this.originalWidth = this.tableEl.clientWidth;
	this.newHeight = parseInt(tableHeight);
	this.newWidth = tableWidth ? parseInt(tableWidth) : this.originalWidth;
	this.tableEl.style.height = 'auto';
	this.tableEl.removeAttribute('height');
	this.containerEl = this.tableEl.parentNode.insertBefore(document.createElement('div'), this.tableEl);
	this.containerEl.appendChild(this.tableEl);
	this.containerEl.style.height = this.newHeight + 'px';
	this.containerEl.style.width = this.newWidth + 'px';
	var thead = this.tableEl.getElementsByTagName('thead');
	this.thead = (thead[0]) ? thead[0] : null;
	var tfoot = this.tableEl.getElementsByTagName('tfoot');
	this.tfoot = (tfoot[0]) ? tfoot[0] : null;
	var tbody = this.tableEl.getElementsByTagName('tbody');
	this.tbody = (tbody[0]) ? tbody[0] : null;
	if (!this.tbody) return;
	if (document.all && document.getElementById && !window.opera) this.initIEengine();
	if (!document.all && document.getElementById && !window.opera) this.initFFengine();
}