//how to call the function

// onkeyup="verif(this.form,'2','NN/NN/NNNN')" in any input field (2 mean the field order in the form)

/*
<SCRIPT>
<!-- VERSION 1.2 du 07/08/2000 VIENT GERARD FRANCE -->
<!-- Le paramètre onKeyUp entre en action à chaque caractère saisie. -->
<!-- Paramètre à passer à la fonction verif. -->
<!-- Le premier est l'identification de la form (toujours this.form). -->
<!-- Le second est le numero de champ dans le formulaire(le premier commençant à zéro). -->
<!-- Le troisième est le mask de saisie : -->
<!--  - Mettre autant de symbole que de caractères à saisir. -->
<!--  - N => numérique de 0 à 9 -->
<!--  - C => numérique de 0 à 9 et les symboles ,.+- -->
<!--  - A => alpha de A à Z (majucule) -->
<!--  - a => apha de a à z (minuscule) -->
<!--  - X => apha de A à z (majuscule et minuscule) -->
<!--  - Z => alpha de A à z et chiffres de 0 à 9 -->
<!--  - ? => mask spécial de format ?NNNxxxxxxxxxxx -->
<!--         NNN    => longeur maxi saisie (exemple 015) -->
<!--         xxx... => caractères autorisés -->
<!--  - $ => caractère quelconque --> 

*/
function verifyMaskedInput(txtInputControl, mask)
{
// array of allowed characters
 tableC = "0123456789.,+-";
 
 // the value of the input control
 InputControlValue = txtInputControl.value;
 resultValue = "";
 // length of characters
 mLength = InputControlValue.length;
 // get first character from the mask.
 info = mask.substring(0,1);
 if (info == "?")
 {
  linfo = eval(mask.substring(1,4));
  minfo = mask.substring(4,mask.length);
 }
 // parse the input value
 for (g=0;g<=mLength;g++)
 {
  chr1 = InputControlValue.substring(g,g+1);
  chr2 = mask.substring(g,g+1);
  if (info == "?")
  {
   chr2 = "?";
  }
  switch (chr2)
  {
   case "$":
   {
     resultValue = resultValue + chr1;
     break;
   }
   case "N":
   {
    if (chr1 >= "0" && chr1 <= "9" )
    {
     resultValue = resultValue + chr1;
    }
     break;
   }
   case "a":
   {
    if (chr1 >= "a" && chr1 <= "z" )
    {
     resultValue = resultValue + chr1;
    }
    break;
   }
   case "A":
   {
    if (chr1 >= "A" && chr1 <= "Z" )
    {
     resultValue = resultValue + chr1;
    }
    break;
   }
   case "C":
   {
    if (tableC.indexOf(chr1,0) > -1 )
    {
     resultValue = resultValue + chr1;
    }
    break;
   }
   case "X":
   {
    if ((chr1 >= "a" && chr1 <= "z") || (chr1 >= "A" && chr1 <= "Z") )
    {
     resultValue = resultValue + chr1;
    }
    break;
   }
   case "Z":
   {
    if ((chr1 >= "a" && chr1 <= "z") || (chr1 >= "A" && chr1 <= "Z") || (chr1 >= "0" && chr1 <= "9"))
    {
     resultValue = resultValue + chr1;
    }
    break;
   }
   case "?":
   {
    if (minfo.indexOf(chr1,0) > -1 && resultValue.length < linfo )
    {
     resultValue = resultValue + chr1;
    }
    break;
   }
   default :
   {
    if (chr1 == chr2 )
    {
     resultValue = resultValue + chr1;
    }
    break;
   }
  }
 } 
 if (resultValue != InputControlValue ) 
 {
  txtInputControl.value = resultValue;
 }
}
/*-----------------------------------------------*/
// mask : dd/MM/yyyy
function verifyDate(txtInputControl, mask)
{
// alert(event.keyCode);

// array of allowed characters
 tableC = "0123456789.,+-";
 
 // the value of the input control
 InputControlValue = txtInputControl.value;
 resultValue = "";
 // length of characters
 mLength = InputControlValue.length;

 // parse the input value
 for (g=0;g<=mLength;g++)
 {
  chr1 = InputControlValue.substring(g,g+1);
  chr2 = mask.substring(g,g+1);

  switch (chr2)
  {
   // Days 
   case "d":
   {
	if(g == 0) // first digit
	{
		if (chr1 >= "0" && chr1 <= "3" )
		{
			resultValue = resultValue + chr1;
		}
	}
	else // second digit
	{
		// check the previous day, if it is "0" , do not allow another "0"
		// if it is "3", do not allow more than "1"
		// first 10 days
		if(resultValue.charAt(0) == "0")
		{
			if (chr1 >= "1" && chr1 <= "9" )
			{
			 resultValue = resultValue + chr1;
			 resultValue = resultValue + "/";
			}
			
		}
		// last days
		else if(resultValue.charAt(0) == "3")
		{
			if (chr1 >= "0" && chr1 <= "1" )
			{
			 resultValue = resultValue + chr1;
			 resultValue = resultValue + "/";
			}		
		}
		else  // normal case
		{
			if (chr1 >= "0" && chr1 <= "9" )
			{
			 resultValue = resultValue + chr1;
			 resultValue = resultValue + "/";
			}
		}
    }
     break;
   }
   // Months
   case "M":
   {
		if(g == 3) // first month digit
		{
			if (chr1 >= "0" && chr1 <= "1" )
			{
				resultValue = resultValue + chr1;
			}
		}
		else  // second month digit
		{
			if(resultValue.charAt(3) == "1")
			{
				if (chr1 >= "0" && chr1 <= "2" )
				{
				 resultValue = resultValue + chr1;
				 resultValue = resultValue + "/";
				}
			}
			else if(resultValue.charAt(3) == "0")
			{
				if (chr1 >= "1" && chr1 <= "9" )
				{
				 resultValue = resultValue + chr1;
				 resultValue = resultValue + "/";
				}					
			}
	    }    
    break;
   }
   // Year
   case "y":
   {
		if(g > 5)
		{
			if (chr1 >= "0" && chr1 <= "9" )
			{
				resultValue = resultValue + chr1;
			}
		}
		else
		{
			if (chr1 >= "0" && chr1 <= "9" )
			{
			 resultValue = resultValue + chr1;
			}
	    }    
    break;
   }
  }
 } 
	// the user pressed 'Backspace'
	if (event.keyCode == 8)
	{
	// delete two digits if on one of the "/"
		if( (InputControlValue.length == 2) )
		{
			resultValue = resultValue.substring(0, (resultValue.length - 2)) ;
		}
		else if( (InputControlValue.length == 5) )
		{
			resultValue = resultValue.substring(0, (resultValue.length - 2)) ;
		}
	}
 if (resultValue != InputControlValue ) 
 {
  txtInputControl.value = resultValue;
 }
 
}


/*-----------------------------------------------*/
// Verify an integer of max value, default 4 digits
function verifyInteger(txtInput, maxValue, minValue )
{
	var initialVal = txtInput.value;
	maxValue = (maxValue == null) ? 9999 : maxValue;
	minValue = (minValue == null) ?    0 : minValue;
	var val = parseInt( txtInput.value, 10 );
	val = ( isNaN( val ) ) ? minValue : Math.abs(val);
	if( val <  minValue ) val = minVal;
	if( val >  maxValue ) 
	{ 
		var valString = new String();
		valString = val + "";
		valString = valString.substring(0, maxValue.length);
	}
	if( initialVal != val ) txtInput.value = val;
}
/*-----------------------------------------------*/
// Verify an number of max value, default 4 digits with decimal
function verifyNumer(txtInput, maxValue, minValue )
{
	var initialVal = txtInput.value;
	maxValue = (maxValue == null) ? 9999 : maxValue;
	minValue = (minValue == null) ?    0 : minValue;
	var val = parseFloat( txtInput.value );
	val = ( isNaN( val ) ) ? minValue : Math.abs(val);
	if( val <  minValue ) val = minVal;
	if( val >  maxValue ) 
	{ 
		var valString = new String();
		valString = val + "";
		valString = valString.substring(0, maxValue.length);
	}
	if( initialVal != val ) txtInput.value = val;
}
