// *****************************************
// js_check.js
// *****************************************
//   Listing:
//     * GENERAL                    
//       - isAlphabeticChar           - isDigit
//       - isNumberChar               - isNumeric
//       - isAlphaNumChar             - isNumericInterv
//       - isChar                     - isFloat
//       - isEmpty                    - isHour
//       - isAlpha                    - 
//       - isDate                     - 
//       -                            - 
//       -                            - 
//       -                            - 
//     * SPECIFIC
//       - isTexte                    - isEmail
//       - isComment                  - isBrowser
//       - isTexte                    - isCodePostal
//       - isName                     - isPhoneNumber
//       - isCountry                  - 
//       -                            - 
// ***************************************** */

var E_NO_ERROR    = 0;
var E_FIELD_EMPTY = 1;
var E_FIELD_INV_FORMAT = 2;
var E_FIELD_ALPHA      = 3;
var E_FIELD_EMAIL      = 4;
var E_FIELD_NOT_NUM    = 5;
var E_NOT_IN_INTERV    = 6;
var E_NOT_TIME         = 7;

function isAlphabeticChar (InString)  {
        if(InString.length!=1) 
	  {
	    return (false);
	  }
        InString=InString.toLowerCase();
        RefString="abcdefghijklmnopqrstuvwxyz";
        if (RefString.indexOf (InString.toLowerCase(), 0)==-1) 
	  {
	    return (false);
	  }
        return (true);
}

function isNumberChar (InString)  {
        if(InString.length!=1) 
                return (false);
        RefString="1234567890";
        if (RefString.indexOf (InString, 0)==-1) 
	  {
	    return (false);
	  }
        return (true);
}

function isAlphaNumChar (InString)  {
        if(InString.length!=1) 
                return (false);
        InString=InString.toLowerCase();
        RefString="1234567890abcdefghijklmnopqrstuvwxyz";
        if (RefString.indexOf (InString, 0)==-1)  
	  {
	    return (false);
	  }
        return (true);
}

/* --------------------------------------
   * isEmpty : Check if a field is EMPTY
   *
   * Usage: isEmpty(
   *                sFieldName, 
   *                sValue, 
   *                bDisplay
   *               )
   -------------------------------------- */
function isEmpty(sValue, bMandatory) 
{
  // Return false if number field is blank.
  
  // Supprime les espaces devant et derrière la chaine
  sValue = DeleteSpaces(sValue);
  if (sValue == "")
    {
      if (bMandatory)
	return E_FIELD_EMPTY;
      else
	return E_NO_ERROR;
    }
  return E_NO_ERROR;
}

/* --------------------------------------
   * isChar : Check if a value is a char
   *
   * Usage: isChar(
   *               cCh 
   *               )
   -------------------------------------- */
function isChar(cCh)
{
  if (((cCh < "a") || ("z" < cCh)) && ((cCh < "A") || ("Z" < cCh)))
    return false;
  else
    return true;
}

/* ---------------------------------------
   * isAlpha: Check if a field is a string
   *
   * Usage: isAlpha(
   *                sFieldName, 
   *                Element, 
   *                bDisplay
   *               )
   --------------------------------------- */
function isAlpha(sFieldName, ElemAlpha, bMandatory) 
{
  ErrNum = E_NO_ERROR;

  // Return false if name field is blank.
  ErrNum = isEmpty(ElemAlpha.value, bMandatory);
  
  if (ErrNum != E_NO_ERROR)
    putFocus(ElemAlpha);
  else
    {
      for (var idx = 0; idx < ElemAlpha.value.length; idx++) 
	{
	  var ch = ElemAlpha.value.substring(idx, idx + 1);
	  
	  if ((!isChar(ch) && ch != ' ' && ch != 'é' && ch != 'è' && ch != 'à'))
	    {
	      ErrNum = E_FIELD_ALPHA;
	      putFocus(ElemAlpha);
	    }
	}
    }
   return ErrNum;
}

/* ----------------------------------------------
   * isDigit : Check if a field is a digit (0..9)
   *
   * Usage: isDigit(
   *                cCh
   *               )
   ---------------------------------------------- */
function isDigit(cCh)
   {
     // Return false if field is not a digit.
     if (cCh < "0" || "9" < cCh)
       {
	 return false;
       }
     else
       {
	 return true;
       }
   }

/* --------------------------------------
   * isNumeric: Check a NUMBER field
   *
   * Usage: isNumeric(
   *                  sFieldName, 
   *                  Elem, 
   *                  bMandatory
   *                 )
   * 
   *    sFieldName: Nom du champ
   *    Elem:       Valeur du champ
   *    bMandatory: TRUE le champ doit avoir une valeur.
   -------------------------------------- */
function isNumeric(sFieldName, Elem, bMandatory) 
{
  ErrNum = E_NO_ERROR;

  // Return false if name field is blank.
  ErrNum = isEmpty(Elem.value, bMandatory);

  if (ErrNum != E_NO_ERROR)
    putFocus(Elem);
  else
    {
      // Return false if characters are not '0-9'
      for (var i = 0; i < Elem.value.length; i++) 
	{
	  var ch = Elem.value.substring(i, i + 1);
	  if (!isDigit(ch))
	    {
	      ErrNum = E_FIELD_NOT_NUM;
	    }
	}
    }
  return ErrNum;
}

function isValueNumeric(sFieldName, Value, bMandatory) 
{
  ErrNum = E_NO_ERROR;

  // Return false if name field is blank.
  ErrNum = isEmpty(Value, bMandatory);

  if (ErrNum != E_NO_ERROR)
    {
    }
  else
    {
      // Return false if characters are not '0-9'
      for (var i = 0; i < Elem.value.length; i++) 
	{
	  var ch = Elem.value.substring(i, i + 1);
	  if (!isDigit(ch))
	    {
	      ErrNum = E_FIELD_NOT_NUM;
	    }
	}
    }
  return ErrNum;
}

function isNumericInterv(sFieldName, Elem, ElemLow, ElemHigh, bMandatory) 
{
  ErrNum = E_NO_ERROR;

  ErrNum = isNumeric(sFieldName, Elem, bMandatory);
  if (ErrNum == E_NO_ERROR)
    {
      if ((Elem.value < ElemLow) || (Elem.value > ElemHigh))
	{
	  ErrNum = E_NOT_IN_INTERV;
	  putFocus(Elem);
	}
    }

  return ErrNum;
}

/* ----------------------------
 * isTime : check if the field
 * contains a correct TIME format
   ---------------------------- */
function isTime(sFieldName,Elem,bMandatory)
{

 ErrNum = E_NO_ERROR;

 iLen = Elem.value.length;
 acTime = String(eval("Elem.value")+":");

 debut = 0;

 idx   = acTime.indexOf(':',debut);
 if (idx < iLen)
   {
     iHour = parseInt(acTime.substr(debut,idx-debut),10);
     if ((Elem.value < ElemLow) || (Elem.value > ElemHigh))
     Errnum = isNumericInterv(sFieldName,iHour,0,23,bMandatory);
    
     if (ErrNum == E_NO_ERROR)
       {
	 debut = idx+1;
	 idx   = acTime.indexOf(':',debut);
	 iMin = parseInt(acTime.substr(debut,idx-debut),10);
	 Errnum = isNumericInterv(sFieldName,Elem,0,59,bMandatory);
	   
	 if ((ErrNum == E_NO_ERROR) &&
	     (idx < iLen))
	   {
	     debut = idx+1;
	     idx   = acTime.indexOf(':',debut);
	     iSec = parseInt(acTime.substr(debut,idx-debut),10);
	     Errnum = isNumericInterv(sFieldName,Elem,0,59,bMandatory);

	     if ((ErrNum == E_NO_ERROR) &&
		 (idx < iLen))
	       {
		   ErrNum = E_NOT_TIME;
	       } /* endif*/
	   } /* endif*/
       } /*endif*/

     if (ErrNum != E_NO_ERROR)
       {
	 ErrNum = E_NOT_TIME;
       } /* end if */
   } /*endif*/
 else 
   {
     ErrNum = E_NOT_TIME;
   }

 

 return ErrNum;
}

/* ----------------------------
   * isName: Check a NAME field
   *
   * Usage: isName(
   *               sFieldName, 
   *               Element, 
   *               bDisplay
   *              )
   ---------------------------- */
function isName(sFieldName, ElemName, bMandatory) 
{
  ErrNum = E_NO_ERROR;

  // Return false if name field is blank.
  ErrNum = isEmpty(ElemName.value, bMandatory);
  
     if (ErrNum != E_NO_ERROR)
       putFocus(ElemName);
     else
       {
	 // Return false if characters are not a-z, A-Z, or a space.
	 //return isAlpha(sFieldName, Element, bDisplay);
       }

   return ErrNum;
   }

/* ------------------------------------------
   * isCodePostal : Check a CODE POSTAL field
   *
   * Usage: isComment(
   *                  sFieldName, 
   *                  Element, 
   *                  bDisplay
   *                 )
   ------------------------------------------ */
function isCodePostal(sFieldName, ElemCP, bMandatory) 
{
  ErrNum = E_NO_ERROR;

  // Return false if name field is blank.
  ErrNum = isEmpty(ElemCP.value, bMandatory);
  
  if (ErrNum != E_NO_ERROR)
    putFocus(ElemCP);
  else
    {
      sAreaCodeFormat=get_AreaCodeFormat(CurrentCountry);
      if ((sAreaCodeFormat != "") &&
	  (!mask(ElemCP.value, sAreaCodeFormat)))
	{
	  putFocus(ElemCP);
	  ErrNum = E_FIELD_INV_FORMAT;
	}
    }
  return ErrNum;
}

/* -----------------------------------
   * isCountry : Check a COUNTRY field
   *
   * Usage: isCountry(
   *                  sFieldName, 
   *                  Element, 
   *                  bDisplay
   *                 )
   ----------------------------------- */
function isCountry(sFieldName, ElemCountry, bMandatory)
{
  ErrNum = E_NO_ERROR;

  ErrNum = isEmpty(ElemCountry.value, bMandatory);
  
  if (ErrNum != E_NO_ERROR)
    putFocus(ElemCountry);
  else
    {
      ErrNum = isAlpha(sFieldName, ElemCountry, bMandatory);
    }
  return ErrNum;
}

/* --------------------------------------------
   * isPhoneNumber : Check a PHONE NUMBER field
   *
   * Usage: isPhoneNumber(
   *                      sFieldName, 
   *                      ElemPhoneNum, 
   *                      ElemCountry,
   *                      bDisplay
   *                     )
   -------------------------------------------- */
function isPhoneNumber(sFieldName, ElemPhoneNum, bMandatory)
{
  var sFormat="";
  var sValue=ElemPhoneNum.value;
  var bEmpty=false;
  
  ErrNum = E_NO_ERROR;

  ErrNum = isEmpty(sFieldName, ElemPhoneNum.value, bMandatory);

  if (ErrNum == E_NO_ERROR)
    {
      if (ElemPhoneNum.value != "")
	{
	  sFormat=get_PhoneNumberFormat(CurrentCountry);
	  if (!mask(ElemPhoneNum.value, sFormat))
	    {
	      ErrNum = E_FIELD_INV_FORMAT;
	      ElemPhoneNum.focus();
	    }
	}
    }
  else
    putFocus(ElemPhoneNum);

  return ErrNum;   
}

/* ---------------------------------
   * isEmail : Check an E-MAIL field
   *
   * Usage: isEmail(
   *                sFieldName, 
   *                ElemMail, 
   *                bMandatory
   *               )
   --------------------------------- */
function isEmail(sFieldName, ElemMail, bMandatory)
{
  ErrNum = E_NO_ERROR;

  // Return false if name field is blank.
  ErrNum = isEmpty(sFieldName, ElemMail.value, bMandatory);
  
  if (ErrNum != E_NO_ERROR)
    putFocus(ElemMail);
  else
    {
      if (ElemMail.value.indexOf ('@',0) == -1 || 
	  ElemMail.value.indexOf ('.',0) == -1)
	{
	  ErrNum = E_FIELD_EMAIL;
	  putFocus(ElemMail);
	}
    }
  
  return ErrNum;
}

function CheckSelect(MySelect, MyElemTxt)
{
  var bOk;

//  MyElemTxt.value = MyElemTxt.value.toUpperCase();
  if (MySelect.selectedIndex != MySelect.options.length-1)
    {
      MyElemTxt.value=MySelect.options[MySelect.selectedIndex].value; 
    }
  else
    {
      for (var Count=0; Count<MySelect.options.length; Count++)
	{
	  if (MyElemTxt.value == MySelect.options[Count].value)
	    {
	      bOk = true;
	      MySelect.options[Count].selected = true;
	    }
	}

      if (!bOk)
	{
	  DisplayInfo('window', "Invalid data");
	  putFocus(MyElemTxt);
	}
    }
}


function GetDataSelect(Element, MySelect)
{
  Element.value=MySelect.options[MySelect.selectedIndex].value; 
}

