//
//  Validation Specifics for HTML
//
//  Author: Micah Dubinko
//  Date:   4/20/2001
//
//  Requires: CS3_Library_*
//


CSExt.KeystrokeFilter = function( fld, evt ) {
  var chrcod = 0;
  document.htmlform.DFS__ttfld = fld;
  document.htmlform.DFS__tttimer = setTimeout( "cs_tt_hack();", 0);
  if (evt.charCode) {
    chrcod = evt.charCode;
    cstrace( "charCode is " + chrcod + " (" + String.fromCharCode( chrcod ) + ")");
  } else { // IE5 still uses keycode for everything
    //chrcod = evt.keyCode;
    //window.status = "keyCode is " + chrcod + " (" + String.fromCharCode( chrcod ) + ")";
    // probably should NOT do the below check for broken versions of IE (so far, Win versions up to 5.5) ...
    //if (chrcod==8 || chrcod==13 || chrcod==37 || chrcod==38 || chrcod==39 || chrcod==40 || chrcod==46) return true; // delete, bs, enter, etc.
  }
  //if (chrcod!=0) {
  //  var chr = String.fromCharCode( chrcod );
  //  //alert( "You just pressed: " + chr );
  //  if (fld.fmttxt && fld.fmttxt.ac) {
  //    alert( "return CS_CharFilter" );
  //    return CS_CharFilter( chr, fld.fmttxt.ac );
  //  }
  //}
  //alert( "return true" );
  return true;
}

function cs_tt_hack() {
  var fld = document.htmlform.DFS__ttfld;
  var show = true;
  if (fld.value == "") show = false;
  fld.uval = fld.value;
  if (CSFmt.validate( fld ) == CSErr.ERR_SUCCESS) show = false;
  cs_setVisibleTTdiv( show );
  clearTimeout(document.htmlform.DFS__tttimer);
}

CSExt.OnChange_bEP.CSValidate = function( fld ) { // before OnChange Entry Point
  // need to do any on-the-spot validations and formatting...
  var res = false;
  if (CSExt.validate( fld, "loose" )) {
    CSExt.format( fld );
    res = true;
  }
  
  return res;
}


CSExt.OnFocus_aEP.ToolTip = function( fld ) {
  cstrace( "OnFocus_aEP " + typeof fld);
  cstrace( "OnFocus_aEP " + fld.name );
  var msg = "";
  if (fld.fmtdt) msg = fld.fmtdt.tmpl;
  if (fld.fmttmpl) msg = fld.fmttmpl.tmpl;
  if (msg != "") {
    cs_createTTdiv( fld, msg.replace(  /[\\!]/g, "") );
    fld.uval = fld.value;
    cs_setVisibleTTdiv( (fld.value.length != "") && (CSFmt.validate( fld ) != CSErr.ERR_SUCCESS) );
  }
  return true;
}

CSExt.OnBlur_aEP.ToolTip = function( fld ) {
  cstrace( "OnFocus_aEP " + fld.name );
  cs_destroyTTdiv();
  return true;
}

function cs_createTTdiv( fld, msg ) {
  // create a new tooltip
  var dt = document.createElement("div");
  dt.setAttribute("id", "DFS__tipdiv");

  // move the tooltip <div> to the correct location
  dt.style.position = "absolute";
  dt.style.top = "100%";
  dt.style.left = "0%";
  dt.style.width = "auto";
  dt.style.visibility = "hidden";
  dt.style.border = "1px solid gray";
  dt.style.font = "90% sans-serif";
  dt.style.backgroundColor = "#ffffcc";

  // set the content and insert
  dt.appendChild(document.createTextNode( msg ));
  var wrapdiv = fld.parentNode;
  wrapdiv.appendChild(dt);
}

function cs_destroyTTdiv() {
  // remove the tipdiv from the page
  var dt = document.getElementById( "DFS__tipdiv" );
  if (dt != null) {
    var parent = dt.parentNode;
    parent.removeChild(dt);
  }
}

function cs_setVisibleTTdiv( visible ) {
  var visstr = "hidden";
  if (visible) visstr = "visible";
  var dt = document.getElementById( "DFS__tipdiv" );
  if (dt != null) {
    dt.style.visibility = visstr;
  }
}


CSExt.validate = function( fld, strictness ) {
  // strictness should be "strict" or "loose"
  cstrace("entering validate() for " + fld.name );
  var isValid = true;
  var message = "No message";
  var field = CSForm.getField( fld.name );
  if (field == null) {
    field = CSForm.getField( fld[0].name );
  }

  do {
    if (field == null) {
      break;
    }
    if ((field.isRequired() == true) && strictness!="loose") {
      //  CR LO-84278-EntryRequired-for-Lists
      if ((fld.initialVal != null) && (fld.initialVal.length > 0)) {
        if (field.getValue() == fld.initialVal) {
          isValid = false;
          message = CS_ERR_ENTRY_REQUIRED_STR;
          break;
        }
      } else {
        if (field.getValue().length == 0) {
          isValid = false;
          message = CS_ERR_ENTRY_REQUIRED_STR;
          break;
        }
      }
    }
    if (fld.fmt == null) {
      break;
    }
    fld.uval = fld.value;
    if (CSFmt.validate( fld ) != CSErr.ERR_SUCCESS) {
      isValid = false;
      message = CSFmt.getErrStr( fld );
      break;
    }
    if (CSFmt.charsValid( fld ) != CSErr.ERR_SUCCESS) {
      isValid = false;
      message = CSFmt.getErrStr( fld );
      break;
    }
  } while(false);
  if (!isValid) {
    if (confirm( message + "\n(" + field.getName() + ")" )) {
      cstrace( "scheduling focus() and select() for " + fld.name );
      if (fld.type != "hidden") {
        document.htmlform.DFS__SetFocusTo.value = fld.name;
        document.htmlform.DFS__focustimer = setTimeout( "cs_focus_hack();", 0);
      }
    }
    fld.value = "";
    if (fld.oldValue != null) {
      fld.value = fld.oldValue;
    }
    if (fld.fmt && fld.fmt != "txt") {
      //<debug>
      //alert(fld.fmt);
      //</debug>
      if (fld.rsvd) {
        fld.rsvd.ival = 0;
      }
    }
  } else {
    fld.oldValue = fld.value;
    cstrace("field validated: " + fld.name);
  }
  return isValid;
}

function cs_focus_hack() {
  try {
    var hft = document.htmlform[document.htmlform.DFS__SetFocusTo.value];
    if (hft[0]) {
      cstrace( "performing focus() and select() for " + hft[0].name );
      if (hft[0].focus) hft[0].focus();
      if (hft[0].select) hft[0].select();
    } else {
      cstrace( "performing focus() and select() for " + hft.name );
      if (hft.focus) hft.focus();
      if (hft.select) hft.select();
    }
  } catch( e ) {}
  clearTimeout(document.htmlform.DFS__focustimer);
}

CSExt.format = function( fld ) {
  if (fld.fmt != null) {
    cstrace("entering format() for " + fld.name );
    if (fld.rsvd != null) {
      fld.rsvd.ival = 0;
    }
    fld.uval = fld.value;
    CSFmt.format( fld );
    fld.value = fld.dval;
    fld.oldValue = fld.value;
  }
}


function trace() {}
function cstrace() {}