// ------------------------------------------------
// TELEform HTML+Forms
// JScript Interactive Client-Side Validation 
// Cardiff Software,Inc. (http://www.Cardiff.com)
// Copyright(c)1991-2000, All Rights Reserved.
// ------------------------------------------------
// Instant Client-Side Validation
var sICSVAL="Microsoft Internet Explorer";
var bMSIE=false;

var cNumeric =1;
var cAlpha   =2;
var cSpecial =4;
var cStrict  =8;
var CUpper   =256;

//
// BOOL scriptEnabled() 
// checks if the browser can handle scripts (Mozilla/3 or MSIE 3)
//
function scriptEnabled() {
 var checkOK="0123456789-";
 var agent=navigator.userAgent;
 var i=-1;
 var j=0;
 var ch="x";
 var v=3;
 if (navigator.appName==sICSVAL) bMSIE=true;
 while(true){
  if (is_nav5up) return(true);
  if (is_ie4up) return(true);
  if (is_ie3) return(true);
  if (is_nav3) return(true);
  if (is_nav4) return(true);
  //i=agent.indexOf('M',i+1);
  //if(i<0)break;
  //if((i+5)>=agent.length)break;
  //ch=agent.charAt(i+5);
  //for(j=0;j<checkOK.length;j++)
  // if(ch==checkOK.charAt(j))break;
  //if((j>=v)&&(j<10)){return(true);} // using MSIE 3 or later
  //if((i+8)>=agent.length)break;
  //ch=agent.charAt(i+8);
  //for(j=0;j<checkOK.length;j++)
  // if(ch==checkOK.charAt(j))break;
  //if((j>=v)&&(j<10)){return(true);} // using Mozilla 3 or later
  continue;
 }
 return(false);
}

//
// -- define object fieldAttr --
//
  function fieldAttr(sName,sType,nSet,bReq,bRng,nSize,sTmpl,rFrom,rTo,sNice,sEntryChars){
    if (bReq>1) bReq=0; // range: 0..1
    if (bRng>1) bRng=0;	// range: 0..1
    if ((nSet & CUpper) == CUpper) {
      this.bUpper =true;
      nSet &= ~CUpper;
    }
    if (nSet>9) nSet=0; // range: 0..9
    this.sName=sName;   // "un-mangled" name of this field
    this.sType=sType;	  // field type
    this.nSet =nSet;	  // allowed char set
    this.bReq =bReq;    // input required?
    this.bRng =bRng;    // input range specified?
    this.nSize=nSize;	  // max num of char.
    this.sTmpl=sTmpl; // template
    this.rFrom=rFrom;   // range from
    this.rTo  =rTo;     // range to
    this.sNice=sNice;	  // filed name to appear in error msgs
    this.sEntryChars=sEntryChars 
  } 

//
// -- define global variables --
//
  var objs     =new fieldContainer();
  var sFocus   ="XXX"; // name of that gets the focus 
  var bFocus   =false; // manually altered focus	
  var bError   =false; // error detected
  var bCheckAll=false; // final checking mode afer submit was pressed
  var bScript  =scriptEnabled(); // browser supports client scripting
  var sAlphaSet="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒšœŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ";
  var sNumSet  ="0123456789";
  var sNuSpSet ="-.,";// these are Numeric and Special Characters
  var sAllSet  =" \r\n";  // these are Num., Alpha, and Special Chars 


// VOID clrWndStatus()
// clears the statusbar
//
  function clrWndStatus(){
  	window.status="";
  	return;
  }

// FLOAT getFloat(theField)
// check for a valid float entry
// set the global bError variable
// return floating point number
//
  function getFloat(theField){
    var checkOK  =sNumSet+sNuSpSet;
    var checkStr =theField.value;
    var decPoints=0;
    var allNum   ="";
    bError       =false;
    for (var i=0;i<checkStr.length;i++){
      var ch= checkStr.charAt(i);
      for (var j=0;j<checkOK.length;j++)
        if(ch==checkOK.charAt(j))break;
      if(j==checkOK.length){bError= true;break;}
      if(ch=="."){
        allNum+=".";
        decPoints++;
      } else 
        if(ch!=",")
          allNum+=ch;
    } // end for
    if(decPoints>1)bError=true;
    if(bError){return(0)};
    var chkVal=allNum;
    var prsVal=parseFloat(allNum);
    return(prsVal);
  }

//
// VOID tellRequired(theField)
// assemble a message and tell in the statusbar that an input for this field is required 
//
  function tellRequired(theField){
    window.status= sCmtRequired + objs[theField.name].sNice + sCmtField;
    return;
  }

//
// VOID tellRange(theField)
// assemble a message and tell in the statusbar the allowed inputrange
//
  function tellRange(theField){
    var r0=objs[theField.name].rFrom;
    var r1=objs[theField.name].rTo;
    window.status= sCmtRange + r0 + sCmtAnd + r1 + sCmtInThe + objs[theField.name].sNice + sCmtField;
    return;
  }

//
// BOOL validateRequired(theField)
// tell in a msgbox that an input for this field is required
// return true if condition was met
//
  function validateRequired(theField){
    if (theField.value==""){
      //clrWndStatus();
      if (bCheckAll){
        alert(sCmtRequired + objs[theField.name].sNice + sCmtField);
        return(false);
      }
      if (confirm(sCmtRequired + objs[theField.name].sNice + sCmtField))
        return(false);
    }
    return(true);
  }

//
// BOOL validateRadio(theField)
// tell in a msgbox that an input for those Radiobuttons is required
// return true if condition was met
//
  function validateRadio(theField){
    for(var i=0;i<theField.length;i++){
      if (theField[i].checked)
        return(true);
    }
    return(false);
  }

//
// BOOL validateChrSet(theField)
// tell in a msgbox the allowed character set
// 0-none    1-num       2-alpha        3-alphaNum 4-spcl
// 5-numSpcl 6-alphaSpcl 7-alphaNumSpcl 8-strict   9-strictNum
// 10-EntryField
// return true if condition was met
//  
  function validateChrSet(theField){
    var checkStr=theField.value;
    var nSet=objs[theField.name].nSet;
    var bValid=true;
    if((nSet<cNumeric)||(nSet>=(cNumeric+cAlpha+cSpecial+cStrict))){ 
      return(bValid);
    }
    for(var i=0;i<checkStr.length;i++){ //check entered characters 
      var ch=checkStr.charAt(i);
      var itemOf=0;
      var j=0;
      // check to which char.set ch belongs
      // handle multiset characters first
      if(itemOf==0){ // ch itemOf AllSet?
        for(j=0;j<sAllSet.length;j++){
          if(ch==sAllSet.charAt(j)){
            itemOf=cNumeric+cAlpha+cSpecial;
            break;
          }
        }//end for(j)
	  }
      if(itemOf==0){ // ch itemOf NuSpSet?
        for(j=0;j<sNuSpSet.length;j++){
          if(ch==sNuSpSet.charAt(j)){
            itemOf=cNumeric+cSpecial;
            break;
          }
        }//end for(j)
	  }
      if(itemOf==0){ // ch itemOf NumSet ?
        for(j=0;j<sNumSet.length;j++){
          if(ch==sNumSet.charAt(j)){
            itemOf=cNumeric;
            break;
          }
        }//end for(j)
      }
      if(itemOf==0){ // ch intemOf AlphaSet ?
        for(j=0;j<sAlphaSet.length;j++){
          if(ch==sAlphaSet.charAt(j)){
            itemOf=cAlpha;
            break;
          }
        }//end for(j)
      }
      if((itemOf==0)&&(checkStr.length>0)){
        itemOf=cSpecial;
      }
      if(!(itemOf & nSet)){
        bValid=false;
        break;
      }
      if(!(itemOf & ~(cAlpha | cNumeric)) && objs[theField.name].sEntryChars.length > 0) {
        if (objs[theField.name].sEntryChars.indexOf(ch) == -1) {

          bValid=false;
          nSet+=10;
          break;
	}
      }
    } //end for(i)
    if(!bValid){
      var sUsing = "";
      if (nSet > 10) {
        // Entry field. Validate specific characters.
        var sTmplChars = "";
	if (objs[theField.name].sTmpl.length > 2 && objs[theField.name].sTmpl.charAt(0) == "i") {
          for(j=0;j<sNuSpSet.length;j++){
            if(objs[theField.name].sTmpl.indexOf(sNuSpSet.charAt(j)) != -1) {
              sTmplChars += sNuSpSet.charAt(j);
            }
          }
        }
        sUsing = " " + "Use characters" + " (" + objs[theField.name].sEntryChars + sTmplChars + ") ";
        nSet -= 10;
        if (nSet == 5 || nSet == 6) {
          nSet = 7;
        }
      }
      if(bCheckAll){
        if(nSet==1)   
          alert(sCmtChrSet1 + objs[theField.name].sNice + sCmtField + sUsing);
        else if(nSet==2)   
          alert(sCmtChrSet2 + objs[theField.name].sNice + sCmtField + sUsing);
        else if(nSet==3)   
          alert(sCmtChrSet3 + objs[theField.name].sNice + sCmtField + sUsing);
        else if(nSet==4)   
          alert(sCmtChrSet4 + objs[theField.name].sNice + sCmtField + sUsing);
        else if(nSet==5)   
          alert(sCmtChrSet5 + objs[theField.name].sNice + sCmtField + sUsing);
        else if(nSet==6)
          alert(sCmtChrSet6 + objs[theField.name].sNice + sCmtField + sUsing);
        else if(nSet==7)
          alert(sCmtChrSet7 + objs[theField.name].sNice + sCmtField + sUsing);
        else if(nSet>=8) {
          alert(sCmtNotValid+ objs[theField.name].sNice + sCmtField + sUsing);
        }
      }else{
        if(nSet==1)   
          bValid=!confirm(sCmtChrSet1 + objs[theField.name].sNice + sCmtField + sUsing);
        else if(nSet==2)   
          bValid=!confirm(sCmtChrSet2 + objs[theField.name].sNice + sCmtField + sUsing);
        else if(nSet==3)   
          bValid=!confirm(sCmtChrSet3 + objs[theField.name].sNice + sCmtField + sUsing);
        else if(nSet==4)   
          bValid=!confirm(sCmtChrSet4 + objs[theField.name].sNice + sCmtField + sUsing);
        else if(nSet==5)   
          bValid=!confirm(sCmtChrSet5 + objs[theField.name].sNice + sCmtField + sUsing);
        else if(nSet==6)
          bValid=!confirm(sCmtChrSet6 + objs[theField.name].sNice + sCmtField + sUsing);
        else if(nSet==7)
          bValid=!confirm(sCmtChrSet7 + objs[theField.name].sNice + sCmtField + sUsing);
        else if(nSet>=8) {
          bValid=!confirm(sCmtNotValid+ objs[theField.name].sNice + sCmtField + sUsing);
        }
      }
    } else {
      if(cStrict & nSet){
        var dummy=getFloat(theField);
        if(bError){
          bError=false; // reset bError because user gets notified
          bValid=!confirm(sCmtNotValid + objs[theField.name].sNice + sCmtField);
        }
      }
    }

    return(bValid);
  }

//
// BOOL validateRange(theField) called by handleBlur()
// assemble a msg. and tell in a msgbox the allowed inputrange
//
  function validateRange(theField){
    if (theField.value!=""){
      var r0=objs[theField.name].rFrom;
      var r1=objs[theField.name].rTo;
      var v =getFloat(theField); // set bError 
      if (!bError){
        if ((v<r0)||(v>r1)) 
          bError=true;
      }
      if(bError){
        //clrWndStatus();
        bError=false;  // reset bError because user gets notified
        if(bCheckAll){
          alert(sCmtRange + r0 + sCmtAnd + r1 + sCmtInThe + objs[theField.name].sNice + sCmtField);
          return(false);
        }
        if(confirm(sCmtRange + r0 + sCmtAnd + r1 + sCmtInThe + objs[theField.name].sNice + sCmtField))
          return(false);
      }
    }
    return(true);
  }

//
// VOID EVENT-Handler for onFocus()event
// tell something in the statusbar or clear it
//
  function handleFocus(theField){
    if(bFocus) {
      return;
    }
    if(objs[theField.name].bRng==1)
      tellRange(theField);
    else 
      if(objs[theField.name].bReq==1)
      	tellRequired(theField);
    theField.nSize="N";
    return;
  }

//
// BOOL EVENT handler for onBlur()
// show a msgbox in case validation fails
//
  function handleBlur(theField){
    if(!bScript)return(true);
 
    if(bFocus && (sFocus!=theField.name) && bMSIE) return(true);  	
    // if focus event was manually set then
    // ignore all blur event triggered by other fields
    // until the blur event appears for the field that manually received the focus  
    bFocus=false;
    
    //if(!(bCheckAll||bMSIE)) return(true);

    var bOK=true;	
    
    if (objs[theField.name].bUpper) {
      theField.value = theField.value.toUpperCase( );
    }
    if(objs[theField.name].nSet>0) // show err.msg
      bOK=validateChrSet(theField);
    if(bOK)
      if(objs[theField.name].bRng==1) // show err.msg
        bOK=validateRange(theField);
    if(bOK)
      if(objs[theField.name].bReq==1) // show err.msg
        bOK= validateRequired(theField);
    if (!bOK){
      if (objs[theField.name].sType=="Text"
          || objs[theField.name].sType=="Date"
          || objs[theField.name].sType=="TDate"){
        bFocus=true;          // manually alter the focus
        sFocus=theField.name; // field to receives the focus
        if (bMSIE || bCheckAll)
	      theField.focus();
      }
      return(false);
    }
    bError=false;
    clrWndStatus();
    return(true);
  }

//  Stript template characters when needed and add padding.
//  Currently only implemented for Date & TDate types
//
  function stripTempl(theField){
    if (objs[theField.name].sType=="Date"
          || objs[theField.name].sType=="TDate"){
      var checkStr="";
      var j=0;
      var fieldStr = "";
      for(var i=0;i<theField.value.length;i++){ //check entered characters 
        var ch=theField.value.charAt(i);
        if (ch>='0' && ch<='9') {
          fieldStr += ch;
        } else {
          if (fieldStr.length == 1 ) {
            fieldStr = "0" + fieldStr; // pad out the field
          }
          if (objs[theField.name].sType=="TDate"){
            fieldStr += ch; // put back template character
          }
          checkStr += fieldStr;
          fieldStr = "";
        }
      }
      if (fieldStr.length == 1 ) {
        fieldStr = "0" + fieldStr;
      }
      checkStr += fieldStr;    
      theField.value=checkStr;
    }
  }


//
// BOOL EVENT handler for onSubmit()
// check that all required fields are filled
//
  function checkAll(theForm){
    clrWndStatus();
    if(!bScript)return(true)
    
    var nFocus   ="xxx"; // field that currently has the focus

    bCheckAll=true;
    bError=false;
    for(var i in objs){
      if((objs[i].nSet>0)||(objs[i].bReq==1)||(objs[i].bRng==1)){ 
        nFocus= objs[i].sName;  
        if(objs[i].sType=="Text"
            || objs[i].sType=="Date"
            || objs[i].sType=="TDate"){
          if(handleBlur(theForm[nFocus])){
            if(objs[i].sType=="Date" || objs[i].sType=="TDate") {
              stripTempl(theForm[nFocus]);
              continue;
            } else {
              continue;
            }
          }else{
            bCheckAll=false;
            break;
          }
        } 
        if(objs[i].sType=="Radio"){
          if(validateRadio((theForm[nFocus]))){
            continue;
          }else{
            alert(sCmtChoose + objs[i].sNice + sCmtField);
            bCheckAll=false;
            // radio fields dont support focus();
            break;
          }
	}
        if(objs[i].sType=="Entry"){
          if(validateRadio((theForm[nFocus]))){
            continue;
          }else{
            alert(sCmtRequired + objs[i].sNice + sCmtField);
            bCheckAll=false;
            // entry fields dont support focus();
            break;
          }
	}
    	if(objs[i].sType=="Select"){
          var nSelIndex=theForm[nFocus].selectedIndex;
          if(nSelIndex<0){
            alert(sCmtSelect + objs[i].sNice + sCmtField);
	    bCheckAll=false;
            theForm[nFocus].focus();
            break;
          }
          if(theForm[nFocus].value == ""){
            alert(sCmtOption + objs[i].sNice + sCmtField);
            bCheckAll=false;
            theForm[nFocus].focus();
            break;
          }// endif nSelIndex 
        }// endif Type Select 	
      } // endif bReq&&bRng 
    } //endfor
    return(bCheckAll);
  }

