// *****************************************************************
// *  Name                : check.js
// *  Description         : form javascript header
// *  Return value        : none
// *  Parameters in       : none
// *  Parameters out      : none
// *  Global data used    : none
// *  Global data changed : none
// *  Author              : S.Lafon, VULCAIN
// *  Creation            : 26/03/2002
// *  History             : <date> <author> <description>
// *****************************************************************



// *****************************************************************
// *  Name                : is_null
// *  Description         : check if form field is null/empty
// *  Return value        : true if empty, other false
// *  Parameters in       : form field object (text,password,select)
// *  Parameters out      : none
// *  Global data used    : none
// *  Global data changed : none
// *  Author              : S.Lafon, VULCAIN
// *  Creation            : 31/10/2000
// *  History             : <date> <author> <description>
// *****************************************************************
function is_null(obj) {
    switch (obj.type) {
        case "text" :
            if (obj.value.length > 0) {
                return false;
            } else {
                return true;
            }
            break;
        case "password" :
            if (obj.value.length > 0) {
                return false;
            } else {
                return true;
            }
            break;
        case "select-one" :
            if (obj.options[obj.selectedIndex].value.length > 0) {
                return false;
            } else {
                return true;
            }
            break;
    }
    return false;
}


// *****************************************************************
// *  Name                : is_number
// *  Description         : check if form field is a number
// *  Return value        : true if empty, other false
// *  Parameters in       : form field object (text)
// *  Parameters out      : none
// *  Global data used    : none
// *  Global data changed : none
// *  Author              : S.Lafon, VULCAIN
// *  Creation            : 31/10/2000
// *  History             : <date> <author> <description>
// *****************************************************************
function is_number(obj) {
    if (obj.value.length == 0) return true;
    if (isNaN(parseFloat(obj.value)))
    {
        return false;
    }
    return true;
}


// *****************************************************************
// *  Name                : is_date
// *  Description         : check if form field is a date DD/MM/YYYY
// *  Return value        : true if empty, other false
// *  Parameters in       : form field object (text)
// *  Parameters out      : none
// *  Global data used    : none
// *  Global data changed : none
// *  Author              : S.Lafon, VULCAIN
// *  Creation            : 31/10/2000
// *  History             : <date> <author> <description>
// *****************************************************************
function is_date(obj) {
    if (obj.value.length == 0) return true;

    if ((obj.value.length != 10) || (obj.value.charAt(2)!="/") || (obj.value.charAt(5)!="/"))
    {
       alert("La date n'est pas au format JJ/MM/AAAA");
       return false;
    }

    var dd =obj.value.substr(0,2);
    var mm =obj.value.substr(3,2);
    var yy =obj.value.substr(6,2);
    if ((isNaN(dd))||(dd<1)||(dd>31)) {
      alert("Le jour n'est pas correct.");
      return false;
    } else {
      if ((((mm==4)||(mm==6)||(mm==9)||(mm==11))&&(dd>30)) ||
         ((mm==2)&&(dd>29)) || ((yy%4==0 && yy%100!=0 || yy%400==0) & (dd==29))) {
        alert("Cette date n'existe pas !");
        return false;
      }
    }
    if ((isNaN(mm))||(mm<1)||(mm>12)) {
      alert("Le mois n'est pas correct.");
      return false;
    }
    if (isNaN(yy)) {
      alert("L'année n'est pas correcte.");
      return false;
    }
             
    return true;
}


// *****************************************************************
// *  Name                : is_doc
// *  Description         : check if form field is a doc file
// *  Return value        : true if empty, other false
// *  Parameters in       : form field object (text)
// *  Parameters out      : none
// *  Global data used    : none
// *  Global data changed : none
// *  Author              : L.Borg, VULCAIN
// *  Creation            : 29/04/2002
// *  History             : <date> <author> <description>
// *****************************************************************
function is_doc(obj) {
    var exten=obj.value.substr(obj.value.length-4,4);
    if ((exten != ".doc") && (exten!= ".DOC")) {
      return false;
    }
    return true;
}


// *****************************************************************
// *  Name                : is_pdf
// *  Description         : check if form field is a pdf file
// *  Return value        : true if empty, other false
// *  Parameters in       : form field object (text)
// *  Parameters out      : none
// *  Global data used    : none
// *  Global data changed : none
// *  Author              : L.Borg, VULCAIN
// *  Creation            : 29/04/2002
// *  History             : <date> <author> <description>
// *****************************************************************
function is_pdf(obj) {
    var exten=obj.value.substr(obj.value.length-4,4);

    if ((exten != ".pdf") && (exten!= ".PDF")) {
      return false;
    }
    return true;
}

// *****************************************************************
// *  Name                : is_hour
// *  Description         : check if form field is a hour HH:MI
// *  Return value        : true if empty, other false
// *  Parameters in       : form field object (text)
// *  Parameters out      : none
// *  Global data used    : none
// *  Global data changed : none
// *  Author              : S.Lafon, VULCAIN
// *  Creation            : 31/10/2000
// *  History             : <date> <author> <description>
// *****************************************************************
function is_hour(obj) {
    if (obj.value.length == 0) return true;

    if ((obj.value.length != 5) || (obj.value.charAt(2)!=":"))
    {
        return false;
    }

    return true;
}


// *****************************************************************
// *  Name                : is_duplicate
// *  Description         : check if select field contains a code
// *  Return value        : true if exist, other false
// *  Parameters in       : code to check
// *                        select object
// *  Parameters out      : none
// *  Global data used    : none
// *  Global data changed : none
// *  Author              : S.Lafon, VULCAIN
// *  Creation            : 31/10/2000
// *  History             : <date> <author> <description>
// *****************************************************************
function is_duplicate(obj,sel) {
    for (var i=0; i<sel.length;i++) {
        if (sel.options[i].value == obj.value) {
            return true;
        }
    }

    return false;
}
