//------------------------------------------------------------------------------
// FONCTIONS UTILISEES POUR LES EVENEMENTS UTILISATEURS
//------------------------------------------------------------------------------

// passage de la valeur du bouton cliqué dans le champ caché wcAction

function aide(cAction) {
	document.wcAction.value = cAction;
}


function controleChampObligatoire() {
tableau1 = new Array();
tableau2 = new Array();
testObli = true;
var listeErreur="";


  tableau1=obli.split(",");
  tableau2=obliLabel.split(",");

  for (i=0;i<tableau1.length;i++) {


    if (eval("document.forms[0]." + tableau1[i] + ".value") == "" ) {
       if (listeErreur == "") listeErreur = MSG16(tableau2[i]); 
       else listeErreur = listeErreur + "\n" + MSG16(tableau2[i]); 
    }
  }
  if (listeErreur != "") { alert(listeErreur); testObli = false; }  
}

function controleChamp(obj,format) {

AlphaChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
NumChars = "-0123456789";
HexaChars = "ABCDEFabcdef";

 //   if (CheckField(obj,format) == false) obj.focus();  

}

// appelée si changement de valeur dans une zone de saisie obligatoire 
function modificationChamp() {
	document.forms[0].modifChamp.value = "O";
}

function checkdate(ocdate,soumettre,format) {

  	var err=0;
	var psj=0;
	var a="";
	cdate = ocdate.value;
	a=cdate;
      ocdate.className="formu";
	if (cdate.length==0) 
	{
	return;
	}
	
	if (a.length ==6 ) {
	    if (format=="E") {
		b=a.substring(0, 2)  // day
		d=a.substring(2, 4)  // month
            }
	    else {
		b=a.substring(2, 4)
		d=a.substring(0, 2)
            }
		f=a.substring(4, 6) // year

		// basic error checking
		if (b<1 || b>31) err=1
		if (d<1 || d>12) err=1
		if (f<0 || f>99) err=1
	    if (f<25) {f=20+f}
	    else {if (f<100) f=19+f;}		
	    
	}
	if (a.length ==8 ) {
	    if (a.indexOf("/") != -1) {
	      if (format=="E") {
		  b=a.substring(0, 2)  // day
		  d=a.substring(3, 5)  // month
              }
	      else {
		  b=a.substring(3, 5)  // day
		  d=a.substring(0, 2)  // month
	      }
		f=a.substring(6, 9) // year		
	    }
	    else { 
	      if (format=="E") {
		  b=a.substring(0, 2)  // day
		  d=a.substring(2, 4)  // month
              }
	      else {
		  b=a.substring(2, 4)  // day
		  d=a.substring(0, 2)  // month
	      }
		f=a.substring(4, 9) // year
	    }
		// basic error checking
		if (b<1 || b>31) err=1
		if (d<1 || d>12) err=1
		if (f<0 || f>9999) err=1
		if (d.substring(0,1) == '/') err=1 
	    if (f<25) {f=20+f}
	    else {if (f<100) f=19+f;}	
	}
	if (a.length ==10 ) {
	    if (format=="E") {
		b=a.substring(0, 2)  // day
		d=a.substring(3, 5)  // month
	    }
	    else {
		b=a.substring(3, 5)  // day
		d=a.substring(0, 2)  // month
            }
            c=a.substring(2, 3)  // '/'	
	    e=a.substring(5, 6)  // '/'	
	    f=a.substring(6, 10) // year

		// basic error checking
		if (b<1 || b>31)   err=1
		if (c != '/')      err=1
		if (d<1 || d>12)   err=1
		if (e != '/')      err=1
		if (f<0 || f>2100) err=1
		}
	if (a.length !=0) {
		if (a.length !=6 && a.length !=8 && a.length !=10) err=1
		else {
			// advanced error checking

			// month with 30 days
			if (d==4 || d==6 || d==9 || d==11)
				{if (b==31) err=1}

			// february, leap year
			if (d==2)
				{// feb
				var g=parseInt(f/4)
				if (isNaN(g))	{err=1}
				if (b>29) err=1
				if (b==29 && ((f/4)!=parseInt(f/4))) err=1
				}
		     }

		if (err==1)
			{
                        //alert('Format incorrect ! \nExemples de formats possibles: \n 010101 \n 01/01/01 \n 01/01/2001')	
				document.getElementById("MsgErreur").innerHTML="&nbsp;&nbsp;&nbsp;<IMG SRC='/ImagesGICFR/alert.gif'>&nbsp;&nbsp;Le format de la date saisie est incorrect <br><br>";
				ocdate.className="formuerreur";
				//alert(MSG5('monParam'));
			      // ocdate.focus();	
			}
				
		else {
			document.getElementById("MsgErreur").innerHTML="";
			ocdate.className="formu";
			if (soumettre=="soumettre") {ocdate.value= b + "/" + d + "/" + f;}
			
	     	     }
	}
}


function formatdate (cdate) {

  var checkOK = "0123456789/";
  var allValid = true;
  var sep = 0;

  for (i = 0;  i < cdate.length;  i++)
  {
    ch = cdate.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
     if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
     break;
    }
    if (ch == "/")
    {
      sep++;
    }
  }
// alert (sep + "v" + allValid);

  switch (cdate.length){
	case 8:
  		switch (sep){
		case 0:
			if (!CheckField(cdate,'NUM')) {allValid = false;}
  			break;
		case 2:
  			if ((!CheckField(cdate.substring(0,2),'NUM')) || (!CheckField(cdate.substring(3,5),'NUM')))
			{allValid = false;}
			break;
		default:
			allValid = false;
		
		}
		break;
	case 6:
		if (!CheckField(cdate,'NUM')) {allValid = false;}
		break;
  	default:
  		if (sep!=2) {allValid = false;}
  		if ((!CheckField(cdate.substring(0,2),'NUM')) || (!CheckField(cdate.substring(3,5),'NUM')))
  			{allValid = false;}	
  }
  return allValid;

}

function CheckField(obj,Format) {
       Data = obj.value;
	var exact = false;
      obj.className="formuNum";
	if (Data.length == 0) exact = true;
	if (Data.length > 0) {
		if (Format == "NUM")
			exact = IsNumericData(Data);
		if (Format == "ALNUM")
			exact = IsAlphaNumeric(Data);
		if (Format == "DEC")
			exact = IsDecimal(obj);
		if (Format == "HEXA")
			exact = IsHexa(Data);
		if (Format == "ALPHA")
			exact = IsAlpha(Data);
	}
	if (exact == false) {obj.className="formuerreur";}
	return exact;
}

function IsNumeric(fillin) {

  //  var It = true;
  //  var c;
  //  for (var x = 0; x < fillin.value.length; x++)
  //  	if (It) {
  //  		c = fillin.value.charAt(x);
  //  		It = ( NumChars.indexOf(c) != -1);
  //  	}
  //      if (It == false) { 
  //                        alert('erreur') ;
  //                        fillin.focus();
  //                        fillin.value = "0"
  //                       }         
  //  return It;

  if (window.event.keyCode <48 || window.event.keyCode>57) 
	if (window.event.keyCode !=46) {
		window.event.keyCode ="";
		return;
	}

}

function IsNumericData(data) {

	var It = true;
	var c;
	for (var x = 0; x < data.length; x++)
		if (It) {
			c = data.charAt(x);
			It = ( NumChars.indexOf(c) != -1);
		}
        if (It == false) { 
                          //alert('Format entier invalide') ;
				  document.getElementById("MsgErreur").innerHTML="<font color='#FF0033'>&nbsp;&nbsp;&nbsp;<IMG SRC='/ImagesGICPRO/alert.jpg'>&nbsp;&nbsp;<b>Le format de la valeur entière est incorrect</b></font>";
				  }         
	return It;
}


function IsDecimal(obj) {

	
	obj.className="formuNum";
      Data = obj.value;
	Data = Data .replace("," , ".");
 	obj.value = Data ;
  	//var checkOK = "0123456789-.,";
      var checkOK = "0123456789.";
	var It = true;
	var virgule = false;
	var c;
	c = Data.charAt(0);	
	It = ( (NumChars.indexOf(c) != -1) || (c == "+") || (c=="-") || (c==".") || (c==",") );
	if ((c ==".") || (c==",")){virgule = true}
	for (var x = 1; x < Data.length; x++)
		if (It) {
			c = Data.charAt(x);
    			for (j = 0;  j < checkOK.length;  j++){
      				if (c == checkOK.charAt(j))
        			break;
			}
			if ( (c==".") || (c == ",") ) {
				if (virgule) 
					It = false;
				else {
					It = true;
					virgule = true;
				}
			}
			else {
				It = (NumChars.indexOf(c) != -1);	
			}		
    			if (j == checkOK.length)
   			{
				It = false;
      				break;
    			}
		}
	//if (It == false) alert("Format décimal invalide");



	if (It == false) {
		document.getElementById("MsgErreur").innerHTML="<font color='#FF0033'>&nbsp;&nbsp;&nbsp;<IMG SRC='/ImagesGICPRO/alert.jpg'>&nbsp;&nbsp;<b>Le format de la valeur décimale saisie est incorrect</b></font>";
		obj.className="formuerreur";
	}
	else {
		//document.getElementById("MsgErreur").innerHTML="";
		obj.className="formuNum";
	}


	if (verDec(obj) == false) {It = false}
	return It;
}

function IsAlphaNumeric(Data) {
	var It = true;
	var c;
	for (var x = 0; x < Data.length; x++)
		if (It) {
			c = Data.charAt(x);
			It = (  (AlphaChars.indexOf(c) != -1) || (NumChars.indexOf(c) != -1) );
		}
	return It;
}

function IsHexa(Data) {
	var It = true;
	var c;
	for (var x = 0; x < Data.length; x++)
		if (It) {
			c = Data.charAt(x);
			It = (  (HexaChars.indexOf(c) != -1) || (NumChars.indexOf(c) != -1) );
		}
	return It;
}

function IsAlpha(Data) {
	var It = true;
	var c;
	for (var x = 0; x < Data.length; x++)
		if (It) {
			c = Data.charAt(x);
			It = ( AlphaChars.indexOf(c) != -1);
		}
	return It;
}

function controleSaisieTph(longueur) {
/*
	if(!document.all){ 
		window.captureEvents(Event.KEYUP); 
	}
	else{ 
		document.onkeypress = keypressHandler; 
	} 
	function keypressHandler(e){ 
		if(document.all) { //it's IE 
			var e = window.event.keyCode; 
		}
		else{ 
			e = e.which; 
		} 

  		if ((e < 48 || e > 57) && (e != 0) && (e != 8) ) 
			if (e != 46) {return false}

  		if ((longueur.length > 20) && (e != 0) && (e != 8)  ) {return false}

	} 

	window.onkeypress = keypressHandler; 
*/

  if (window.event.keyCode <48 || window.event.keyCode>57) 
	if (window.event.keyCode !=46) {
		window.event.keyCode ="";
		return;
	}
  if (longueur.length > 20) {
	window.event.keyCode ="";
	return;  	
  }

}

function tph(a,longueur) {


var i;
var longueur2=longueur;
// copier - coller caracteres alphanumeriques
	for (i=0;i<longueur.length;i++) {
 		if (longueur.charAt(i)<"0" ||longueur.charAt(i)>"9") 
		longueur2 = longueur2.replace(longueur.charAt(i),"");
	}
	longueur=longueur2;

// suppression des caracteres "."
	do {longueur=longueur.replace("." , "")} 
      while (longueur.indexOf(".")!=-1);

// ajout du "." tous les 2 chiffres
	for (i=0;i<longueur.length;i++) {
 		if (((i-2)/3)==parseInt((i-2)/3)) 
		longueur = longueur.substring(0 , i) + "." +
                       longueur.substring(i);
	}
	if (a.value!=longueur)
	a.value=longueur;  

}

function mult(a , b) {
/* MULTIPLIE a PAR b */
amult = 0;
bmult = 0;
	xa = a.toString();
	xb = b.toString();
	xa = xa.split(".");
	xb = xb.split(".");
	if (xa != a) amult = xa[1].length ;
	xa = a * Math.pow(10 , amult);
	if (xb != b) bmult = xb[1].length ;
	xb = b * Math.pow(10 , bmult);
	return xa * xb / Math.pow(10 , amult + bmult)
}


function verDec (obj) {
       valeur = obj.value;
       var lerreur = true;
       
  	if (valeur.indexOf('.') != -1) {
		tableau = new Array()
		chaine = new String();
		tableau = obj.value.split(".");
		if (tableau[0].length > obj.maxLength - 3) {
			lerreur = false;
			//alert("Partie entière limitée à : " + (obj.maxLength - 3) + " caractères")
			document.getElementById("MsgErreur").innerHTML="<font color='#FF0033'>&nbsp;&nbsp;&nbsp;<IMG SRC='/ImagesGICPRO/alert.jpg'>&nbsp;&nbsp;<b>Partie entière limitée à : " + (obj.maxLength - 3) + " caractères </b></font>";
			obj.className="formuerreur";

		}
		else if (tableau[1].length > 2) {
			lerreur = false;
			document.getElementById("MsgErreur").innerHTML="<font color='#FF0033'>&nbsp;&nbsp;&nbsp;<IMG SRC='/ImagesGICPRO/alert.jpg'>&nbsp;&nbsp;<b>Partie décimale limitée à deux caractères !</b></font>";
			obj.className="formuerreur";

			// alert("Partie décimal trop grande !")
		}

	}
	else {
       		if (obj.value.length > obj.maxLength - 3 ) {
			lerreur = false;
			document.getElementById("MsgErreur").innerHTML="<font color='#FF0033'>&nbsp;&nbsp;&nbsp;<IMG SRC='/ImagesGICPRO/alert.jpg'>&nbsp;&nbsp;<b>Partie entière limitée à : " + (obj.maxLength - 3) + " caractères </b></font>";
			obj.className="formuerreur";
		 	//alert('Partie entière limitée à : ' + (obj.maxLength - 3) + ' caractères ');
		}	 
	}
	return lerreur;
}

function ctrlPeriode(a , b)
{

   if (a.value != "" && b.value != "") {
      da = new Date(a.value.substring(6,10),a.value.substring(3,5),a.value.substring(0,2));
      db = new Date(b.value.substring(6,10),b.value.substring(3,5),b.value.substring(0,2));
      if (da > db) {
         alert('La date de fin ne doit pas être antérieure à la date de début');
         return false;
      }
      else {
         return true;
      }
   }
   else {
      return true;
   }
}

function ctrlAnnee(a , b)
{
   if (parseInt(a.value) > parseInt(b.value)) {
      alert("L'année de fin ne doit pas être antérieure à l'année de début");
      return false;
   }
   else {
      return true;
   }
}
