// form.js
//
// BASIC DATA VALIDATION FUNCTIONS:
//
// isWhitespace (s)                     Check whether string s is empty or whitespace.
// isDigit (c)                          Check whether character c is a digit 
// isInteger (s [,eok])                 True if all characters in string s are numbers.
// isFRZIPCode (s [,eok])               True if string s is a valid FRENCH ZIP code.
// isBEZIPCode (s [,eok])               True if string s is a valid BELGUIM ZIP code.
// isEmail (s [,eok])                   True if string s is a valid email address.
// CompteClic(formulaire)				Compte le nombre de clic sur un bouton.	


// VARIABLE DECLARATIONS

var digits="0123456789";

var defaultEmptyOK=false;

// whitespace characters
var whitespace=" \t\n\r";

// FRENCH ZIP codes have 5 digits.
// It is  formatted as 94250.
var digitsInFRZIPCode=5;

// BELGUIM ZIP codes have 4 digits.
// It is  formatted as 1000.
var digitsInBEZIPCode=4;


// Check whether string s is empty.

function isEmpty(s)
{   
	return ((s==null) || (s.length==0));
}



// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)
{   var i;

    // Is s empty?
    if(isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i=0; i<s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c=s.charAt(i);

        if(whitespace.indexOf(c)==-1) return false;
    }

    // All characters are whitespace.
    return true;
}


// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   
	return ((c>="0") && (c<="9"));
}

function isInteger (s)
{   var i;

    if(isEmpty(s)) 
       if(isInteger.arguments.length==1) return defaultEmptyOK;
       else return (isInteger.arguments[1]==true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i=0; i<s.length; i++)
    {   
        // Check that current character is number.
        var c=s.charAt(i);

        if(!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{   if(isEmpty(s)) 
       if(isEmail.arguments.length==1) return defaultEmptyOK;
       else return (isEmail.arguments[1]==true);
   
    // is s whitespace?
    if(isWhitespace(s)) return false;
    
    // there must be>=1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i=1;
    var sLength=s.length;

    // look for @
    while((i<sLength) && (s.charAt(i)!="@"))
    { i++;
    }

    if((i>=sLength) || (s.charAt(i)!="@")) return false;
    else i+=2;

    // look for .
    while((i<sLength) && (s.charAt(i)!="."))
    { i++;
    }

    // there must be at least one character after the .
    if((i>=sLength - 1) || (s.charAt(i)!=".")) return false;
    else return true;
}


function isIntegerInRange (s, a, b)
{   if(isEmpty(s)) 
       if(isIntegerInRange.arguments.length==1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1]==true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if(!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num=parseInt (s);
    return ((num>=a) && (num<=b));
}


// isFRZIPCode (STRING s [, BOOLEAN emptyOK])
// 
// isFRZIPCode returns true if string s is a valid 
// FRENCH ZIP code.  Must be 5 digits only.
//
// NOTE: Strip out any delimiters (spaces, hyphens, etc.)
// from string s before calling this function.  
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isFRZIPCode (s)
{  if(isEmpty(s)) 
       if(isFRZIPCode.arguments.length==1) return defaultEmptyOK;
       else return (isFRZIPCode.arguments[1]==true);
   return (isInteger(s) && 
            (s.length==digitsInFRZIPCode) && isIntegerInRange (s, "1","98000"));
}

// isBEZIPCode (STRING s [, BOOLEAN emptyOK])
// 
// isBEZIPCode returns true if string s is a valid 
// BELGUIM ZIP code.  Must be 4 digits only.
//
// NOTE: Strip out any delimiters (spaces, hyphens, etc.)
// from string s before calling this function.  
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isBEZIPCode (s)
{  if(isEmpty(s)) 
       if(isBEZIPCode.arguments.length==1) return defaultEmptyOK;
       else return (isBEZIPCode.arguments[1]==true);
   return (isInteger(s) && 
            (s.length==digitsInBEZIPCode) && isIntegerInRange (s, "1","9999"));
}

function CompteClic(formulaire) { // Fonction appelée par le bouton

	nbclic++; // nbclic+1
	if (nbclic>1) { // Plus de 1 clic
		return;
	}
	else { // 1 seul clic
         	document.form.submit();
	}
}