// beta 6.5, a commenter
// a completer comparaison avec une date et mettre les commentaires
// avec une comparaison avec la date du jour, deux champs dates complets

var ERR_EMPTY			= "ERR_EMPTY";
var ERR_NATURE			= "ERR_NATURE";
var ERR_VILLE			= "ERR_VILLE";
var ERR_VILLEPAYS		= "ERR_VILLEPAYS";
var ERR_Regle1			= "ERR_Regle1";
var ERR_FORMAT_DATE		= "ERR_FORMAT_DATE";
var ERR_INVALID_DATE	= "ERR_INVALID_DATE";
var ERR_NO_SELECTION	= "ERR_NO_SELECTION";
var ERR_NOT_DECIMAL		= "ERR_NOT_DECIMAL";
var ERR_NOT_INTEGER		= "ERR_NOT_INTEGER";
var ERR_NOT_LENGTH		= "ERR_NOT_LENGTH";
var ERR_NOT_ENOUGTH		= "ERR_NOT_ENOUGTH";
var ERR_TOO_BIG			= "ERR_TOO_BIG";
var ERR_DATE_VALUE_SUP   	= "ERR_DATE_VALUE_SUP"; 
var ERR_DATE_VALUE_INF   	= "ERR_DATE_VALUE_INF";
var ERR_DATE_JOUR_VALUE_SUP   	= "ERR_DATE_JOUR_VALUE_SUP"; 
var ERR_DATE_JOUR_VALUE_INF   	= "ERR_DATE_JOUR_VALUE_INF";
var ERR_NO_VALID_MAIL		= "ERR_NO_VALID_MAIL";
var ERR_UNKNOWN			= "ERR_UNKNOWN";

//*******************************************************************
// function de travail sur les expressions regu
function testExp(reg, strChaine) {
  //alert(new RegExp(reg));
  return ((new RegExp(reg)).test(String(strChaine)));
} 
//********************************************************************
function isEmpty(strChaine) {
  return this.testExp("^\\s*$",strChaine);
}
//********************************************************************
// verifie si la chaine est de bonne longueur
function isGoodLength(strChaine,theLength) { 
  if (theLength < 1) return false; 
  return ((String(strChaine).length==theLength) || (String(strChaine).length==0));
}
//********************************************************************
// verifie si la chaine est bien un entier de occu chiffre
function isInteger(strChaine,typeInteger) {
  typeInteger = (this.testExp("^\\d*$",typeInteger)? "^\\-?\\d{"+typeInteger+"}$":"^\\-?\\d*$");
  return this.testExp(typeInteger,strChaine);
}
//********************************************************************
// permet de connaitre la date d'origine suivant le navigateur
function dateOrigine() {
  return ((new Date).getYear() < 1900)?1900:0; 
}
//********************************************************************
// teste si un float
function isFloat(strChaine,regOccu) {
  var posiSeparator = String(regOccu).search(this.sepaGeneFloat);
  if ((posiSeparator != -1) && this.testExp("^[\\d*\*]"+this.sepaGeneFloat+"[\\d*\*]$",regOccu)) {
  	var decoupage = new Array();
  	decoupage=String(regOccu).split(String(regOccu).charAt(posiSeparator));
	var beforeVig = (this.testExp("^\\d*$",decoupage[0]))? "\\d{"+decoupage[0]+"}":"\\d*";
	var afterVig = (this.testExp("^\\d*$",decoupage[1]))? "\\d{"+decoupage[1]+"}":"\\d*";
	return (this.testExp("^\\-?"+beforeVig+String(regOccu).charAt(posiSeparator)+afterVig+"$",strChaine) || this.isEmpty(strChaine));
  }
  return (this.testExp("^-?\\d*"+this.sepaGeneFloat+"?\\d*$",strChaine));
}
//********************************************************************
// verif s'il y a un champs Selected dans une combo
// !!!!!! Attention, ceci prend un objet
function isSelected(objetBox) {
  return (objetBox.selectedIndex >0);
}
//********************************************************************
// renvoie le champs select dans une combo
// !!!!!! Attention, ceci prend un objet
function strSelected(objetBox) {
  return (objetBox[objetBox.selectedIndex].value);
}

//********************************************************************
// teste si la valeur est un nombre
function isCompareMinMax(minOrMax,compar,valueChaine) {
  if (!isNaN(parseInt(minOrMax)) || isFloat(parseInt(minOrMax))) {
    if (!eval("parseFloat(valueChaine) "+compar+" minOrMax")) return false;
	}
  return true;
}
//********************************************************************
// sert a retourner les champs
function valChamps(strChamps,nomForm) {
  return (eval("nomForm."+strChamps+".value"));
}
//********************************************************************
// sert a creer les dates
function tabCreateDate(strDateSaisie) {
  var decoupage = String(strDateSaisie).split("/");
  return (new Date(decoupage[2],decoupage[1]-1,decoupage[0]));
}     
//********************************************************************
// renvoie un boolean pour le type d'argument
function typeArgument(strArgu) {
  return (String(strArgu).toUpperCase() == "TRUE")
}
//*************************************************************************
function createDate(strDateValue,nomForm) {
  var strDate="";
  var decoupage = new Array();
  decoupage = String(strDateValue).split("/");
  for (i=0;i<3;i++) {
    strDate = strDate +((i>0)? "/":"")+this.valChamps(decoupage[i],nomForm)	
  } 
  return ((String(strDateValue).length==2)? "":strDate);
}
//********************************************************************
function isTypeEMail(strChaine) {
  return this.testExp("^..*@..*$",strChaine);
}
//*************************************************************************
//********************* OBJET QUI REGROUPE LES OUTILS DE BASE ***********
//*************************************************************************
function primFunction() {
//  this.sepaGeneFloat="[,.]";
  this.sepaGeneFloat="[.]";
  // this.sepaGeneDate="/";
  this.testExp=testExp;
  this.isEmpty=isEmpty;
  this.isInteger=isInteger;
  this.isFloat=isFloat;  
  this.dateOrigine=dateOrigine;
  this.isSelected=isSelected;
  this.strSelected=strSelected;
  this.isGoodLength=isGoodLength;
  this.isCompareMinMax=isCompareMinMax;
  this.valChamps=valChamps;
  this.createDate=createDate;
  this.tabCreateDate=tabCreateDate;
  this.typeArgument=typeArgument;
  this.isTypeEMail=isTypeEMail;
} 
//*************************************************************************
//***********  			   DEBUT DES CHECK             ********************
//*************************************************************************
function checkDate(field) {
	 var strDateSaisie= (this.typeArgument(field.checkDate)? field.value : this.createDate(field.checkDate,this.form));
	 
         if (strDateSaisie == "01/01/1970") return true; 
	
	 var decoupage = new Array();
	 if (String(strDateSaisie).length==0 || String(strDateSaisie)=="//") return true;
	 if (!this.testExp("^\\d\\d?\\/\\d\\d?\\/\\d{4}$", strDateSaisie)) return ERR_FORMAT_DATE;
	 strDateConvertie=this.tabCreateDate(strDateSaisie);
	 if (!Date.parse(strDateConvertie)) return ERR_FORMAT_DATE;
	 decoupage = String(strDateSaisie).split("/");

         
 	
	
        var annee= strDateConvertie.getYear()+this.dateOrigine();
	
        if (!(annee > 99 )) annee = annee +1900 ; 
       
	 if (strDateConvertie.getDate() != decoupage[0]	|| strDateConvertie.getMonth() != decoupage[1]-1 || (annee) != decoupage[2]) return ERR_INVALID_DATE;
	 return true;
}
//********************************************************************
function checkCompDate(field) {
  
  var decoupage  = new Array();
  var decoupage1 = new Array();
  var decoupage2 = new Array();
  decoupage  = String(field.checkCompDate).split("|");
  decoupage2 = String(decoupage[1]).split("/");
  decoupage1 = String(decoupage[0]).split("/");
  decoupage3 = decoupage[2];

  var  dateBasse = new Date(decoupage1[2],decoupage1[1]-1, decoupage1[0]);
  var  dateHaute = new Date(decoupage2[2],decoupage2[1]-1, decoupage2[0]);

  // var dateBasse = this.tabCreateDate(this.createDate(decoupage[0],this.form));
  // if ((String(field.checkCompDate).split("/")).length==2)
  // var dateHaute=this.tabCreateDate(this.createDate(decoupage[1],this.form));


  if (dateBasse > dateHaute)
    {
    if (decoupage3 == ">") return ERR_DATE_VALUE_SUP;
    if (decoupage3 == "<") return ERR_DATE_VALUE_INF;
    }

  return true;
}
//********************************************************************
function checkCompDateJour(field) {
  
  var decoupage = new Array();
  var decoupage1 = new Array();
  var decoupage2 = new Array();
  decoupage = String(field.checkCompDateJour).split("|");
  decoupage2 = String(decoupage[1]).split("/");
  decoupage1 = String(decoupage[0]).split("/");
  decoupage3=decoupage[2];

  var  dateBasse = new Date(decoupage1[2],decoupage1[1]-1, decoupage1[0]);
  var  dateHaute = new Date(decoupage2[2],decoupage2[1]-1, decoupage2[0]);

  //var dateBasse = this.tabCreateDate(this.createDate(decoupage[0],this.form));
  //if ((String(field.checkCompDate).split("/")).length==2)
 // var dateHaute=this.tabCreateDate(this.createDate(decoupage[1],this.form));


  if (dateBasse > dateHaute)
    {
//    if (decoupage3 == ">") return ERR_DATE_JOUR_VALUE_SUP;
    if (decoupage3 == "<") return ERR_DATE_JOUR_VALUE_INF;
    }
  else if (dateBasse < dateHaute)
    {
    if (decoupage3 == ">") return ERR_DATE_JOUR_VALUE_SUP;
//    if (decoupage3 == "<") return ERR_DATE_JOUR_VALUE_INF;
    }
  return true;
}
//********************************************************************
function checkMandatory(field) {
	if (this.isEmpty(field.value)) return ERR_EMPTY;
	return true;
}

//********************plage clase************************************************
function checkPlage(field) {
	 if ((String(field.value) < "A") || (String(field.value) > "H")) return ERR_UNKNOWN;
	return true;
}
//********************************************************************




function checkPaysVille(field) {

  var decoupage = new Array();
  decoupage =String(field.checkPaysVille).split("|");
  decoupage1 = String(decoupage[1]).split("-");
  if (decoupage[0]=="504" && decoupage1[0] =="000")  return ERR_VILLEPAYS;
  if (decoupage[0]!="504" && decoupage1[0] !="000")  return ERR_VILLEPAYS;
return true;
}

//********************************************************************
function checkNature(field) {

  var decoupage = new Array();
  decoupage =String(field.checkNature).split("|");

  if ((decoupage[1 ] == "504") && (decoupage[0] != "C"))  return ERR_NATURE;
 if ((decoupage[1 ] != "504") && (decoupage[0]  == "C"))  return ERR_NATURE;

return true;
}
//********************************************************************
function checkLibVille(field) {

  var decoupage = new Array();
  decoupage =String(field.checkLibVille).split("|");

  if (decoupage[0]=="000" && decoupage[1] =="") return ERR_VILLE;

    
return true;

}
//********************************************************************
function checkregle1(field) {

  var decoupage = new Array();
	var year1 =1 ;
	var year2;
	var date1;
	var date2;

  decoupage =String(field.checkregle1).split("\|");
  decoupage1=String(decoupage[1]).split("/");
  date1= new Date(decoupage1[2],decoupage1[1]-1,decoupage1[0]);

  date2  = new Date();

  year1 = date2.getYear();

  year1  =year1 - 18;

  date2.setYear(year1); 


  if (date1 > date2 ) 
     {
	if ((decoupage[0]=="01") || (decoupage[0]=="06"))    return ERR_Regle1;
     }
    if (date1 < date2 ) 
     {

	if ((decoupage[0]=="02"))     return ERR_Regle1;
     }
return true;
}
//********************************************************************
function checkEMail(field) {
	if (!this.isEmpty(field.value)){
	   if (!this.isTypeEMail(field.value)) return ERR_NO_VALID_MAIL;
	}
	return true;
}
//********************************************************************
function checkIndexSelected(field) {
	if (!this.isSelected(field)) return ERR_NO_SELECTION;
	return true;
}
//***********************************************************************
function checkInteger(field) { 
	if (!this.isInteger(field.value,"*")) return ERR_NOT_INTEGER;
	if (!this.typeArgument(field.checkInteger))
	   if (!this.isGoodLength(field.value,field.checkInteger)) return ERR_NOT_LENGTH;
	return true;
}
//***********************************************************************
function checkDecimal(field) {
	if (!this.isFloat(field.value,"*")) return ERR_NOT_DECIMAL;
	if (String(field.checkDecimal).toUpperCase() != "TRUE")
	   if (!this.isFloat(field.value,field.checkDecimal)) return ERR_NOT_LENGTH;		
	return true;
}
//***********************************************************************
function checkLength(field) {
	if (!(this.isGoodLength(field.value,field.checkLength)) && (String(field.value).length != 0))
	   return ERR_NOT_LENGTH;
	return true;
}
//*************************************************************************
function checkValueMinMax(field) {
  var decoupage = new Array();
  decoupage=String(field.checkValueMinMax).split("\|");
  if (parseFloat(decoupage[1])>parseFloat(decoupage[0])) {
    if (this.isCompareMinMax(decoupage[0],"<",field.value)) return ERR_NOT_ENOUGTH;
    if (this.isCompareMinMax(decoupage[1],">",field.value)) return ERR_TOO_BIG;
  }
  return true;
}
//*************************************************************************
function showError(field, code) {
	var msg = this.message(field, code);
	field.focus();
//	field.select();
	alert(msg);
	return false;
}
//********************************************************************
function checkField(field) {
	var noError=false;
	if (field.checkMandatory) {
		noError = this.checkMandatory(field);
		if (noError != true) return noError;
	}
	if (field.checkPlage) {
		noError = this.checkPlage(field);
		if (noError != true) return noError;
	}
	
	if (field.checkEMail) {
		noError = this.checkEMail(field);
		if (noError != true) return noError;		
	}
	if (field.checkDate) {
		noError = this.checkDate(field);
		if (noError != true) return noError;
	}
	if (field.checkCompDate) {
		noError = this.checkCompDate(field);
		if (noError != true) return noError;
	}
	if (field.checkCompDateJour) {
		noError = this.checkCompDateJour(field);
		if (noError != true) return noError;
	}
	if (field.checkIndexSelected) {
		noError = this.checkIndexSelected(field);
		if (noError != true) return noError;
	}		
	if (field.checkInteger) {
		noError = this.checkInteger(field);
		if (noError != true) return noError;
	}
	if (field.checkDecimal) {
		noError = this.checkDecimal(field);
		if (noError != true) return noError;
	}
	if (field.checkLength) {
	   noError = this.checkLength(field);
	   if (noError != true) return noError;
	}
	if (field.checkValueMinMax) {
	   noError = this.checkValueMinMax(field);
	   if (noError != true) return noError;
	}
//-----------------------------------------------------------
	if (field.checkregle1) {
		noError = this.checkregle1(field);
		if (noError != true) return noError;
	}
	if (field.checkLibVille) {
		noError = this.checkLibVille(field);
		if (noError != true) return noError;
	}

	if (field.checkPaysVille) {
		noError = this.checkPaysVille(field);
		if (noError != true) return noError;
	}
	if (field.checkNature) {
		noError = this.checkNature(field);
		if (noError != true) return noError;
	}

	return true;
}
//*************************************************************************
function checking() {
	for (j=0; j<this.form.length; j++) {
		var noError = this.checkField(this.form[j]);
		if (noError != true) {
		   return this.showError(this.form[j], noError);
		}
	}
	return true;
}
//********************* OBJET QUI REGROUPE LES FONCTIONS DE CHECK *********
function objCheck(nameForm) {
   //le check
  this.form=nameForm;
  this.checking=checking;
  this.checkField=checkField;
  this.checkDate=checkDate;
  this.checkMandatory=checkMandatory;
  this.checkPlage=checkPlage;
  this.checkIndexSelected=checkIndexSelected;
  this.checkregle1=checkregle1;
  this.checkLibVille=checkLibVille;
  this.checkPaysVille=checkPaysVille;
  this.checkNature=checkNature;
  this.checkInteger=checkInteger;
  this.checkDecimal=checkDecimal;
  this.checkValueMinMax=checkValueMinMax;
  this.checkLength=checkLength;
  this.showError=showError;
  this.checkCompDate=checkCompDate;
  this.checkCompDateJour=checkCompDateJour;
  this.checkEMail=checkEMail;
  // heritage par masquage
  this.temp = primFunction;
  this.temp();
  delete this.temp;

}
objCheck.prototype = new displayError;
//*************************************************************************
//************************ Function de depart sans arguments ***************
function checkForm(nameForm) {
	
	verification = new objCheck(nameForm);

	return verification.checking();
}

function PopupCentrer(page,largeur,hauteur,options) {
  var top=(screen.height-hauteur)/2;
  var left=(screen.width-largeur)/2;
  window.open(page,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
  document.RechercheFrm.action=selObj.options[selObj.selectedIndex].value; 
 document.RechercheFrm.submit(); 
}
//********************************************
function disp(txt) {document.write(txt);}

var IB=new Object;

var nsx=0;nsy=0;

function AffBulle(texte) {

contenu="<TABLE border=0 cellspacing=0 cellpadding="+IB.NbPixel+"><TR bgcolor='"+IB.ColContour+"'><TD><TABLE border=0 cellpadding=2 cellspacing=0 bgcolor='"+IB.ColFond+"'><TR><TD><FONT size='-1' face='arial' color='"+IB.ColTexte+"'>"+texte+"</FONT></TD></TR></TABLE></TD></TR></TABLE>";

 

       if (document.layers) {

             document.layers["bulle"].document.write(contenu);

             document.layers["bulle"].document.close();

             document.layers["bulle"].top=nsy+10;

             document.layers["bulle"].left=nsx-10;

             document.layers["bulle"].visibility="show";}

       if (document.all) {

             var f=window.event; e=f;

             doc=document.body.scrollTop;

             bulle.innerHTML=contenu;

             document.all["bulle"].style.top=f.clientY+doc+10;

             document.all["bulle"].style.left=f.x-10;

             document.all["bulle"].style.visibility="visible";

 

       }

}

function ns(e) {

       nsx=e.x;nsy=e.y;

}

function HideBulle() {

       if (document.layers) {document.layers["bulle"].visibility="hide";}

       if (document.all) {document.all["bulle"].style.visibility="hidden";}

}
