/* 
     Description : Objet de validation d'un formulaire.
     Auteur : Porati Sebastien
     Date creation : xx/xx/xxxx
     Editeur : PHPEdit 0.8
     
	 Version : 0.8.0 beta
	 Date : 20/06/2002
	 
     Derniere modification :
        - Date : 20/06/2002
        - Auteur : Porati Sebastien
        - Description : Ajout fonction CheckLoginPassword().
        - Editeur : Dreamweaver MX
      

    + CFormChecker        CFormChecker          (string _FormName, string _Lang)

    + bool                IsValidated           (void)
    + string              GetErrorMessage       (void)

    + void                CheckRequiredField    (string _FieldName, string _FieldComment)
	+ void                CheckRequiredSelect   (string _SelectName, string _FieldComment, mixed _DefaultValue)
    + void                CheckMaxLength        (string _FieldName, string _FieldComment, int _MaxLength)
    + void                CheckNumericOnly      (string _FieldName, string _FieldComment)
    + void                CheckFloat            (string _FieldName, string _FieldComment)
    + void                CheckRegExp           (string _FieldName, string _FieldComment, string _RexExp, string _ErrorMessage)
    + void                CheckMail             (string _FieldName, string _FieldComment)
	+ void                CheckDate             (string _FieldName, string _FieldComment)
	+ void                CheckLoginPassword    (string _FieldName, string _FieldComment)

    - void                AddErrorMessage       (string _Message)
    - bool                IsEmpty               (string _Value)
	- mixed               GetValue              (string _FieldName)

*/

/*
 *   + CFormChecker  CFormChecker (string _FormName, string _Lang)
 */
function CFormChecker(_FormName, _Lang)
{
    // Membres
    this.m_ErrorMessage = "" ;
    this.m_FormName = _FormName ;

    if (_Lang != null) {
        this.m_Lang = _Lang ;
    } else {
        this.m_Lang = "fr" ;
    }

    // Methodes privee
    this.AddErrorMessage = _AddErrorMessage ;
    this.IsEmpty         = _IsEmpty ;

    // Methodes public
    this.IsValidated     = _IsValidated ;
    this.GetErrorMessage = _GetErrorMessage ;
	this.GetValue        = _GetValue ;

    this.CheckRequiredField  = _CheckRequiredField ;
	this.CheckRequiredSelect = _CheckRequiredSelect ;
    this.CheckMaxLength      = _CheckMaxLength ;
    this.CheckNumericOnly    = _CheckNumericOnly ;
    this.CheckFloat          = _CheckFloat ;
    this.CheckRegExp         = _CheckRegExp ;
	this.CheckMail           = _CheckMail ;
	this.CheckDate           = _CheckDate ;
	this.CheckLoginPassword  = _CheckLoginPassword ;
} // CFormChecker()


/*
 *   - void  AddErrorMessage  (string _Message)
 */
function _AddErrorMessage(_Message) {
    if (this.m_ErrorMessage == '') {
        this.m_ErrorMessage = _Message ;
    } else {
        this.m_ErrorMessage += "\n" + _Message ;
    }
} // _AddErrorMessage()


/*
 *   - bool  IsEmpty  (string _Value)
 */
function _IsEmpty(_Value) {
    var MyRegExp = new RegExp("[^ ]", "g") ;

    if (! MyRegExp.test(_Value)) {
        return true ;
    } else {
        return false ;
    }
} // _IsEmpty()


/*
 *   - mixed  GetValue  (string _FieldName)
 */
function _GetValue(_FieldName) {
    var Value = document.forms[this.m_FormName].elements[_FieldName].value ;
	
	return Value ;
} // _GetValue()


/*
 *   + bool  IsValidated  (void)
 */
function _IsValidated() {
    if (this.m_ErrorMessage == '') {
        return true ;
    } else {
        return false
    }
} // _IsValidated()


/*
 *   + string  GetErrorMessage  (void)
 */
function _GetErrorMessage(_Message) {
    return this.m_ErrorMessage ;
} // _GetErrorMessage()


/*
 *   + void  CheckRequiredField  (string _FieldName, string _FieldComment)
 */
function _CheckRequiredField(_FieldName, _FieldComment) {
    var Ok = true ;

    var Value = this.GetValue(_FieldName) ;

    if (this.IsEmpty(Value)) {
        Ok = false ;
    }

    if (! Ok) {
        switch (this.m_Lang) {
          case "fr" :
            this.AddErrorMessage("- Le champ " + _FieldComment + " est obligatoire.") ;
            break ;

          case "en" :
            this.AddErrorMessage("- The field " + _FieldComment + " is required.") ;
            break ;
        }
    }
} // _CheckRequiredField()


/*
 *   + void  _CheckRequiredSelect  (string _FieldName, string _FieldComment, string _DefaultValue)
 */
function _CheckRequiredSelect (_SelectName, _FieldComment, _DefaultValue) {
    var Ok = true ;

    var Value = this.GetValue(_SelectName) ;

    if (Value == _DefaultValue) {
        Ok = false ;
    }

    if (! Ok) {
        switch (this.m_Lang) {
          case "fr" :
            this.AddErrorMessage("- Le champ " + _FieldComment + " est obligatoire.") ;
            break ;

          case "en" :
            this.AddErrorMessage("- The field " + _FieldComment + " is required.") ;
            break ;
        }
    }
} // _CheckRequiredSelect()


/*
 *   + void  CheckMaxLength  (string _FieldName, string _FieldComment, int _MaxLength)
 */
function _CheckMaxLength (_FieldName, _FieldComment, _MaxLength) {
    var Ok = true ;

    var Value = this.GetValue(_FieldName) ;

    if (Value.length > _MaxLength) {
        Ok = false ;
    }

    if (! Ok) {
        switch (this.m_Lang) {
          case "fr" :
            this.AddErrorMessage("- Le champ " + _FieldComment + " doit contenir " + _MaxLength + " caractères maximum (ici " + Value.length + ").") ;
            break ;

          case "en" :
            this.AddErrorMessage("- The field " + _FieldComment + " can contain " + _MaxLength + " characters maximum, (here " + Value.length + ").") ;
            break ;
        }
    }
} // _CheckMaxLength()


/*
 *   + void  CheckNumericOnly  (string _FieldName, string _FieldComment)
 */
function _CheckNumericOnly (_FieldName, _FieldComment) {
    var Ok = true ;

    var Value = this.GetValue(_FieldName) ;

    var MyRegExp = new RegExp("^[0-9]*$", "g") ;

    if (! this.IsEmpty(Value) && ! MyRegExp.test(Value)) {
        Ok = false ;
    }

    if (! Ok) {
        switch (this.m_Lang) {
          case "fr" :
            this.AddErrorMessage("- Le champ " + _FieldComment + " ne peut contenir qu'un nombre (sans virgule ni espace).") ;
            break ;

          case "en" :
            this.AddErrorMessage("- The field " + _FieldComment + " can contain only a numeric (without spaces, neither comma).") ;
            break ;
        }
    }
} // _CheckNumericOnly()


/*
 *   + void  CheckFloat  (string _FieldName, string _FieldComment)
 */
function _CheckFloat (_FieldName, _FieldComment) {
    var Ok = true ;

    var Value = this.GetValue(_FieldName) ;

    var MyRegExp = new RegExp("^[0-9]+(.[1-9]+)?$", "g") ;

    if (! this.IsEmpty(Value) && ! MyRegExp.test(Value)) {
        Ok = false ;
    }

    if (! Ok) {
        switch (this.m_Lang) {
          case "fr" :
            this.AddErrorMessage("- Le champ " + _FieldComment + " ne peut contenir qu'un nombre (avec ou sans virgule, sans espace).") ;
            break ;

          case "en" :
            this.AddErrorMessage("- The field " + _FieldComment + " can contain only a numeric (with or without commas, without comma).") ;
            break ;
        }
    }
} // _CheckFloat()


/*
 *   + void  CheckRegExp  (string _FieldName, string _FieldComment, string _RegExp, string _ErrorMessage)
 */
function _CheckRegExp (_FieldName, _FieldComment, _RegExp, _ErrorMessage) {
    var Ok = true ;

    var Value = this.GetValue(_FieldName) ;

    var MyRegExp = new RegExp(_RegExp, "g") ; alert(_RegExp) ;

    if (! MyRegExp.test(Value)) {
        Ok = false ;
    }

    if (! Ok) {
        switch (this.m_Lang) {
          case "fr" :
            this.AddErrorMessage(_ErrorMessage) ;
            break ;

          case "en" :
            this.AddErrorMessage(_ErrorMessage) ;
            break ;
        }
    }
} // _CheckRegExp()


/*
 *   + void  CheckMail (string _FieldName, string _FieldComment)
 */
function _CheckMail (_FieldName, _FieldComment) {
    var Ok = true ;

    var Value = this.GetValue(_FieldName) ;

    var MyRegExp = new RegExp("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)$", "g") ; 

    if (! this.IsEmpty(Value) && ! MyRegExp.test(Value)) {
        Ok = false ;
    }

    if (! Ok) {
        switch (this.m_Lang)
        {
          case "fr" :
            this.AddErrorMessage("- Le champ " + _FieldComment + " ne peut contenir qu'une adresse email valide.") ;
            break ;

          case "en" :
            this.AddErrorMessage("- The field " + _FieldComment + " can contain only a valid email adress.") ;
            break ;
        }
    }
} // _CheckMail()


/*
 *   + void  CheckDate (string _FieldName, string _FieldComment)
 */
function _CheckDate (_FieldName, _FieldComment) {
    var Ok = true ;

    var Value = this.GetValue(_FieldName) ;

    var MyRegExp = new RegExp("^[0-9]{2}/[0-9]{2}/[0-9]{4}$", "g") ; 

	if (! this.IsEmpty(Value) && ! MyRegExp.test(Value)) {
        Ok = false ;
    } else {
	    // Verification validite de la date
		var TabDate = Value.split('/') ;
		
		var Day   = eval(TabDate[0]) ;
		var Month = eval(TabDate[1]) ;
		var Year  = eval(TabDate[2]) ;
		
		var MyDate = new Date(Year, Month - 1, Day) ;
		
		var RefDay   = MyDate.getDate() ;
		var RefMonth = MyDate.getMonth() + 1 ;
		var RefYear  = MyDate.getFullYear() ;
	
		if ((Day != RefDay) || (Month != RefMonth) || (Year != RefYear)) {
		    Ok = false ;
		}
	}

    if (! Ok) {
        switch (this.m_Lang) {
          case "fr" :
            this.AddErrorMessage("- Le champ " + _FieldComment + " ne peut contenir qu'une date valide au format jj/mm/aaaa.") ;
            break ;

          case "en" :
            this.AddErrorMessage("- The field " + _FieldComment + " can contain only a valid date (format : dd/mm/yyyy).") ;
            break ;
        }
    }
} // _CheckDate()


/*
 *   + void  CheckLoginPassword (string _FieldName, string _FieldComment)
 */
function _CheckLoginPassword (_FieldName, _FieldComment) {
    var Ok = true ;

    var Value = this.GetValue(_FieldName) ;

    var MyRegExp = new RegExp("^[A-Za-z0-9]+$", "g") ; 

    if (! this.IsEmpty(Value) && ! MyRegExp.test(Value)) {
        Ok = false ;
    }

    if (! Ok) {
        switch (this.m_Lang)
        {
          case "fr" :
            this.AddErrorMessage("- Le champ " + _FieldComment + " ne peut contenir que des lettres ou des chiffres, pas de caractères spéciaux.") ;
            break ;

          case "en" :
            this.AddErrorMessage("- The field " + _FieldComment + " can contain only letters and numbers, no special characters.") ;
            break ;
        }
    }
} // _CheckLoginPassword()