/*********************************************************************************/
// 			ALL OF THIS Foction are used to controle client side forms
/*********************************************************************************/
 function batchvalidate(){
  nparm=Math.floor(batchvalidate.arguments.length/3);
  count=1;
  ptr=0;
  result=true;
  while ((count<=nparm) && (result==true)) {
   field=batchvalidate.arguments[ptr];
   tpe=batchvalidate.arguments[ptr+1];
   cmt=batchvalidate.arguments[ptr+2];
   result=validate (field, tpe, cmt);
   count=count+1;
   ptr=ptr+3;
  } // wend
  return(result);
 } // batchvalidate 

function checkstring(mystring, validchars) {
// This function checks in a string : must only contain validchars
// else returns false
   var temp;
   var erflag=true;
   for (var i=0; i<mystring.length; i++) {
    temp = "" + mystring.substring(i, i+1);
    if (validchars.indexOf(temp) == "-1") {
     erflag=false;
    } // invalid chars ?  
   } // next
   return(erflag);
} // CheckString

function validate(fld,type, cmt) {
// This functions checks the content
// of a given field in a form.
// depending of the type of check
// 0 (128)=nonzero 
// 1 (129)=text 
// 2 (130)=numeric 
// 3 (131)=email 
// 4 (132)=date
// 5 (133)=time
// 6 (134)=time w/o limit to hour, ex. 37:00
// 7 (135)=vat number (belgian checkdigit system compliant)
// 8 (136)=bank account number (belgian  checkdigit system compliant)
//
// By adding 128 to that value, the validation script will also accept empty field.
//
// Example : type=131 (3+128) will accept an empty field or a valid email address.

 result=true;  // Checks ok by default
 emptyok=0  // if type>128 -> we set emptyok to 1 and check at the end of the function
 
 if (type>=128) {
  emptyok=1;
  type-=128;
  } //(type>=128)

 if (type==0) {
   // nonzero : for drop menu 
   if (fld.value==0) {
    result=false;
  } //fld.value==0 
 } // Type=0
 
  if (type==1) {
   // text checks for an emptied field only
   if (fld.value=="") {
    result=false
   } // fld.value=""  
 } // Type=1
 
  if (type==2) {
   // Only digits are accepted
   result=checkstring(fld.value,"0123456789-+,");
   if ((fld.value=="") && (emptyok==0)) {result=false;}
 } // Type=2
 
  if (type==3) {	
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	result=filter.test(fld.value);  
 } // Type=3
 
  if (type==4) {
 // VALID DATE format : accepts dd/mm or dd/mm/yy or dd/mm/yyyy then
 // check for valid date
  result=true;
  var yearvalue, monthvalue, dayvalue
  var cdate= new Date;
  
  result=checkstring(fld.value,"0123456789/-.")
  fld.value=fld.value.replace("-","/")
  fld.value=fld.value.replace(".","/")

  if (result==true) {  
    var dateArray = fld.value.split('/');  
    if (dateArray.length==2) {
     dayvalue=dateArray[0];
     monthvalue=dateArray[1];
     yearvalue=cdate.getFullYear();
     if ((dayvalue>31) || (dayvalue<=0)) {result=false;}
     if ((monthvalue>12) || (monthvalue<=0)) {result=false;}
     if ((yearvalue>2020) || (yearvalue<=0)) {result=false;}
     //if (result) {fld.value=fld.value+"/2005";}
	 if (result) {fld.value=fld.value+"/"+cdate.getFullYear();}       
    }//(dateArray.length==2)
    
    if (dateArray.length==3) {
     dayvalue=dateArray[0];
     monthvalue=dateArray[1];
     yearvalue=dateArray[2];
     if ((dayvalue>31) || (dayvalue<=0)) {result=false;}
     if ((monthvalue>12) || (monthvalue<=0)) {result=false;}
     if ((yearvalue>2020) || (yearvalue<=0)) {result=false;}
	 if (result) {
	 //recompose the date if <20 -- example 05 becomes 2005
	  if (yearvalue<20) {fld.value=dayvalue+"/"+monthvalue+"/"+"20"+yearvalue;}
	 } //result   
    }//(dateArray.length==3)
    
    if ((dateArray.length<2) || (dateArray.length>3)) {result=false;} 
  }//(result==true)
 } // Type=4
 
  if (type==5) {
  // Time
  //firt ensure that we have only valid chars
  result=checkstring(fld.value,"0123456789:")
  if (result==true) {
   // we must convert separators to /
   // and btw count and store the position of separators
   var nsep=0 // number of separators
   mysep=new Array(2); 
   nv=new String("");
   tmp=new String("");
   for (var i=0; i<fld.value.length; i++) {
    tmp=fld.value.substring(i, i+1);
	if (tmp==":") {
	// found a separator, whatever
	nsep=nsep+1; // increment the sep. counter
	if (nsep<=1) {
	 mysep[nsep]=i; // stores the pointer
	} // nsep<=1
	 tmp="/";
	} // (tmp==":")
	nv=nv+tmp;
   } // next i
   if (nsep!=1) {
    // Wrong number of separators...
	result=false;
   } // (nsep!=1)
   else {
   // Ok, we got 1 separator
   // We must now extract each part of the string
   hr=fld.value.substring(0, mysep[1]);
   mi=fld.value.substring(mysep[1]+1, fld.value.length);
   if (hr>23) {
    result=false;
   } // (hr>23)
   if (mi>59) {
    result=false;
   } // (mi>59)       
   } // nsep=1
  } // result=true  
 } // Type=5
 
 if (type==6) {
  // Time w/o limit for hour, example 27:00
  //firt ensure that we have only valid chars
  result=checkstring(fld.value,"0123456789:")
  if (result==true) {
   // we must convert separators to /
   // and btw count and store the position of separators
   var nsep=0 // number of separators
   mysep=new Array(2); 
   nv=new String("");
   tmp=new String("");
   for (var i=0; i<fld.value.length; i++) {
    tmp=fld.value.substring(i, i+1);
	if (tmp==":") {
	// found a separator, whatever
	nsep=nsep+1; // increment the sep. counter
	if (nsep<=1) {
	 mysep[nsep]=i; // stores the pointer
	} // nsep<=1
	 tmp="/";
	} // (tmp==":")
	nv=nv+tmp;
   } // next i
   if (nsep!=1) {
    // Wrong number of separators...
	result=false;
   } // (nsep!=1)
   else {
   // Ok, we got 1 separator
   // We must now extract each part of the string
   hr=fld.value.substring(0, mysep[1]);
   mi=fld.value.substring(mysep[1]+1, fld.value.length);
   if (mi>59) {
    result=false;
   } // (mi>59)       
   } // nsep=1
  } // result=true  
 } // Type=6
 
 if (type==7) {
 // VAT number (belgian checkdigit system compliant)
 // also compatible with the new Enterprise Number procedure (10 digits)
 // If the value to be check has a length of 9, a leading 0 is inserted before the string.
 //Checks that a vat field is compliant to the BE form
 //which carries a checksum (2 digits) as following
 //example : what is the checksum for the TVA code 1234567 ?
 //Let's divide 1234567 and take the modulo 97, so we get 48
 //The checksum is the same as for the bank accounts excepted that it is given
 //in its complement form to 97 : so in our case 97-48=49
 //so the full tva number would be 1234567 49
 //
 // 1. the string must be 9 chars long
  result=true;
  if (fld.value.length==9) { fld.value="0"+fld.value }  // leading "0"
  if (fld.value.length==10) {
   // 2. must ensure that the field has only 0-9 as chars.
   result=checkstring(fld.value,"0123456789")     
    if (result==true) {
     // 3. we must now extract the checksum, recalculate it and compare
 	p1=fld.value.substring(0,8)
 	p2=fld.value.substring(8,10)
 	p3=97 - (p1 % 97);
 	if (p2 != p3) {result=false} //erroneous checkdigit
     } //chars ok
  } //fld.length=10
  else {
   result=false;
  } // fld.length<>10
 } // Type=7
 
  if (type==8) {
 // ACCOUNT NUMBER (Belgian system compliant)
 // The last 2 digits is the modulo 97 of the 7 first digits
 //
 // 1. the string must be 9 chars long
  result=true;
  if (fld.value.length==9) {
   // 2. must ensure that the field has only 0-9 as chars.
   result=checkstring(fld.value,"0123456789")     
    if (result==true) {
     // 3. we must now extract the checksum, recalculate it and compare
 	p1=fld.value.substring(0,7)
 	p2=fld.value.substring(7,9)
 	p3=(p1 % 97);
 	if (p2 != p3) {result=false} //erroneous checkdigit
     } //chars ok
  } //fld.length=9
  else {
   result=false;
  } // fld.length<>9
 } // Type=8
 
 
if ((emptyok==1) && (fld.value.length==0)) {result=true}
  
 	//if (result==false) {
  	//alert(cmt);
  	//if (fld.type!="select-one") {fld.select();}
  	//fld.focus();
  	//}
  
  return(result);
}   

 		function setClass(str_id , str_class){
			document.getElementById(str_id).className=str_class ;
		}
		
		 function validateFormLoggin(form){
		  var errMsg="" ;
		  
		  
		   if(validate(form.loggin,1, "Veuillez entrer votre loggin") ){
					setClass('loggin' , '');
			}else{	
					errMsg = errMsg + "Veuillez entrer votre loggin  !!! \n" ;
					setClass('loggin' , 'error');
			}
			
			if(validate(form.pw,1, "Veuillez entrer votre mot de passe") ){
					setClass('pw' , '');
			}else{	
					errMsg = errMsg + "Veuillez entrer votre mot de passe  !!! \n" ;
					setClass('pw' , 'error');
			}
		  
		  
		  if(validate(form.code_loggin,1, "Recopier le code correctement SVP")&& (form.code_loggin.value.length==4) ){
					setClass('code_loggin' , '');
				}else{	
					errMsg = errMsg + "Veuillez recopier le code  !!! \n" ;
					setClass('code_loggin' , 'error');
				}
		  
		  
		   if (errMsg==""){
					//return confirm("Je confirme envoi du mail");
				 }else{
				 	alert(errMsg);
					return false ;
				 }	
		}		 			
 
