/*
Plausi.js -> 050102->050908 -> vb
formular-plausicheck per php (& ggf. javascript)

$Log: Plausi.js,v $
Revision 1.12  2005/09/07 23:19:01  vb
plausisyserror nicht defaultmaessig (online ausgeben)

Revision 1.11  2005/06/05 23:25:14  vb
label highlightung

Revision 1.10  2005/05/31 00:51:30  vb
bin testen...
*/

/* man uebergebe ein radio-box-form-element & bekomme true zurueck, wenn etwas angewaehlt ist: */
function anybodyChecked( radio ) {
  if (radio.length > 1) {
    for (i = 0; i < radio.length; i++) {
      if (radio[ i ].checked) {
        return true;
      }
    }
  } else {
    /* du hast nur ein element, was ist denn das fuer ne (generierte) radiobox? */
    return (radio.checked);
  }
  return false;
}  // function anybodyChecked

function isTextfield( fieldType ) {
  return (
    (fieldType == 'text') ||
    (fieldType == 'textarea') ||
    (fieldType == 'password') ||
    (fieldType == 'select-one')
  );
}  // function isTextfield

function plausiSysError( errorStr ) {
  //alert( "Plausi.js: ERROR: " + errorStr );
  document.title = "Plausi.js: ERROR: " + errorStr;
  window.status = "Plausi.js: ERROR: " + errorStr;
}  // function plausiSysError

// plausi.check-class aehnlich der Plausi.inc.php
function plausiCheck() {
  var checkung = true;
  if (document.getElementById( 'errorMsg' )) {
    if (plausiCheck.arguments.length == 0) {
      document.getElementById( 'errorMsg' ).innerHTML = PLAUSIDEFAULT_CHECKING + '<br />';
      document.getElementById( 'errorMsg' ).className = 'success';
      document.getElementById( 'errorMsg' ).style.display = 'block';
      window.scroll( 0, 0 );
      for (i in this.formArr) {
        thisCheckung = this.check( i );
        if (checkung) checkung = thisCheckung;
      }  // for formArr
      if (!checkung) {
        //document.getElementById( 'errorMsg' ).style.display = 'block';
        document.getElementById( 'errorMsg' ).className = 'error';
        window.scrollTo( 0, 0 );
      } else {
        //if (document.getElementById( 'errorMsg' ).innerHTML == '')
        document.getElementById( 'errorMsg' ).innerHTML = PLAUSIDEFAULT_LOADING + '<br />';
        //document.getElementById( 'errorMsg' ).className = 'success';
      }  // if checkung
    } else {
      fieldName = plausiCheck.arguments[ 0 ];
      fieldExpr = this.formArr[ fieldName ][ 0 ];
      fieldElem = document.forms[ this.formName ].elements[ fieldName ];
      //alert( fieldName + ' ' + typeof( fieldElem ) );
      if (typeof( fieldElem ) != 'undefined') {
        fieldType = fieldElem.type;
        if (typeof( fieldType ) == 'undefined' && fieldElem.length > 0) {
          fieldType = 'radio';  // ihr macht mich ganz wuschig, ihr typenlosen
        }  // if fieldElem
        if (fieldType == 'checkbox') {
          if ((typeof( fieldExpr ) != 'undefined') && (fieldExpr != ''))
            checkung = fieldElem.checked;
        } else {
          if (fieldType == 'radio') {
            if ((typeof( fieldExpr ) != 'undefined') && (fieldExpr != '')) {
              checkung = anybodyChecked( fieldElem );
              if (fieldElem.length > 1) {
                // for rot-anmalung:
                for (var i = 0; i < fieldElem.length; i++) {
                  if (document.getElementById( this.formName + '_' + fieldName + fieldElem[ i ].value + '_label' )) {
                    if (checkung) {
                      document.getElementById( this.formName + '_' + fieldName + fieldElem[ i ].value + '_label' ).className = 'normal';
                    } else {
                      document.getElementById( this.formName + '_' + fieldName + fieldElem[ i ].value + '_label' ).className = 'error';
                      if (document.getElementById( 'errorMsg' ).innerHTML.indexOf( PLAUSIDEFAULT_CHECKING ) != -1) {
                        document.getElementById( 'errorMsg' ).innerHTML = '';
                      }
                      if (document.getElementById( 'errorMsg' ).innerHTML.indexOf( this.formArr[ fieldName ][ 1 ] ) == -1) {
                        document.getElementById( 'errorMsg' ).className = 'error';
                        document.getElementById( 'errorMsg' ).innerHTML += this.formArr[ fieldName ][ 1 ] + '<br />';
                      }  // if thisErrDone
                    }  // if checkung
                  } else {
                    //plausiSysError( "no label for " + fieldName + fieldElem[ i ].value );
                    //plausiSysError( this.formName + '_' + fieldName + fieldElem[ i ].value + '_label' );
                    checkung = true;  // ohne label zum anmalen kein fehler
                  }  // if label
                }  // for i
              }  // if fieldElem.length > 1
            }  // if checkable
          }  // if radio
          if (isTextfield( fieldType )) {
            // im ie is die expr n object, sonst ne function, sonst check ich nix:
            if ((typeof( fieldExpr ) == 'object') || (typeof( fieldExpr ) == 'function')) {
              checkung = fieldExpr.test( fieldElem.value );
              //alert( 'checking ' + fieldName + ' containing ' + fieldElem.value + ' using ' + fieldExpr + ' is ' + checkung );
            }  // if regexp
          }  // radio-box-problem
        }  // if isTextfield
        thisLabel = document.getElementById( this.formName + '_' + fieldName + '_label' );
        // vb: 050413: fixme: radioboxen werden daher derzeit nur serverseitig geprueft:
        //if (thisLabel || !isTextfield( fieldType )) {
        if (thisLabel) {
          if (checkung) {
            thisLabel.className = 'normal';
            if (isTextfield( fieldType )) {
              thisClass = document.getElementById( this.formName + '_' + fieldName ).className.replace( /error/, 'normal' );
              document.getElementById( this.formName + '_' + fieldName ).className = thisClass;
            }  // if isTextfield
          } else {
            if (typeof( this.formArr[ fieldName ][ 1 ] ) == 'string') {
              if (document.getElementById( 'errorMsg' ).innerHTML.indexOf( PLAUSIDEFAULT_CHECKING ) != -1) {
                document.getElementById( 'errorMsg' ).innerHTML = '';
              }
              if (document.getElementById( 'errorMsg' ).innerHTML.indexOf( this.formArr[ fieldName ][ 1 ] ) == -1) {
                document.getElementById( 'errorMsg' ).className = 'error';
                document.getElementById( 'errorMsg' ).innerHTML += this.formArr[ fieldName ][ 1 ] + '<br />';
              }
              thisLabel.className = 'error';
              if (isTextfield( fieldType )) {
                thisClass = document.getElementById( this.formName + '_' + fieldName ).className.replace( /normal/, 'error' );
                document.getElementById( this.formName + '_' + fieldName ).className = thisClass;
              }  // if isTextfield
            } else {
              plausiSysError( "no errormessage for " + fieldName );
              checkung = true;  // ohne fehlermeldung kein fehler
            }  // if error == string
          }  // if checkung
        } else {
          //plausiSysError( "no label for " + fieldName );
          //plausiSysError( this.formName + '_' + fieldName + '_label' );
          checkung = true;  // ohne label zum anmalen kein fehler
        }  // if thisLabel
      } else {
        plausiSysError( "can't check: *" + fieldName + "*" );
        checkung = true;  // ohne element keine checkung kein fehler
      }  // if fieldElem
    }  // if argument
  } else {
    plausiSysError( 'no <div id="errorMsg">' );
  }  // if errorMsg
  return checkung;
}  // function plausiCheck

function Plausi( formName, formArr ) {
  this.formName = formName;
  this.formArr = formArr;
  // methoden:
  this.check = plausiCheck;
  //isTextfield?
}  // konstruktor Plausi

