function rSubmitThisForm ( ) {

    //return rfIsNetscape() ? document.layers["BaseForm"].document.aForm : document.aForm;
    return rfIsNetscapeUnder5() ? document.layers["BaseForm"].document.aForm : document.aForm;
    //return rfIsNetscape() ? this.aForm : document.aForm;
}

//---------------------------------------------------------------------------

function rSetImg ( ButtonName, GIFName ) {

    ButtonName.src = GIFName;
}

//---------------------------------------------------------------------------

function fIsOKEMailAddress ( EMailAddr ) {

    if ( EMailAddr.length < 5 || EMailAddr.indexOf("@") < 0 || EMailAddr.indexOf(".") < 0) {

        alert("Please enter a valid e-mail address.");
        return false;
    }
    return true;
}

var gMissingFields = "";
var aForm; // set this value before calling any function below
var gExclChars = "";
// requires hidden  field 'setfocus'

function rfTestPhoneNumber (WhichValue, WhichName) {
    var TheValue;
    var Cmd;
      
    Cmd = "TheValue = aForm." + WhichValue + ".value;";
    eval(Cmd);
    
    
    TheValue = TheValue.replace(".","")
    TheValue = TheValue.replace("-","")
    TheValue = TheValue.replace(" ","")
    TheValue = TheValue.replace("(","")
    TheValue = TheValue.replace(")","")
    
    if (!IsNumeric(TheValue))
    {
         AddMissingFieldsText(WhichValue, WhichName,'(xxx)xxx-xxxx');
         return false;
    }
    
    rDeleteTextNodeDiv('div' + WhichValue); 
    return true;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   //var ValidChars = "0123456789.";
   
   var Char;
 
   for (i = 0; i < sText.length; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) < 0){
       // alert("IsNumeric not numeric char:" + Char);
        return false;
      }  
   }
   
  // alert("IsNumeric true");
   return true;
   
}

//-------------------------------------------------------------------
function rfTestTextMinMax ( WhichValue, WhichName, MinLength, MaxLength ) {

    var TheValue;
    var Cmd;
      
    Cmd = "TheValue = aForm." + WhichValue + ".value;";
    eval(Cmd);
    
    if ( TheValue.length < MinLength || TheValue.length > MaxLength ) {
    
         AddMissingFieldsText(WhichValue, WhichName,'<' + MinLength + ' - ' + MaxLength + ' chr>');
         return false;
    }
    
    if (rfHasExcludeChar(WhichValue, WhichName)) return;
    
    rDeleteTextNodeDiv('div' + WhichValue); 
    return true;
}

function AddMissingFields (WhichValue,WhichName){
  
   if (aForm.setfocus.value.length < 2) aForm.setfocus.value = WhichValue;
         
   if (gMissingFields == '')
     gMissingFields = WhichName;
   else
     gMissingFields = gMissingFields + ', ' +WhichName;
     
   rCreateTextNodeDiv('div' + WhichValue,'*');
}


function AddMissingFieldsText (WhichValue,WhichName,TextStr){
  
   if (aForm.setfocus.value.length < 2) aForm.setfocus.value = WhichValue;
         
   if (gMissingFields == '')
     gMissingFields = WhichName;
   else
     gMissingFields = gMissingFields + ', ' +WhichName;
     
   rCreateTextNodeDiv('div' + WhichValue,TextStr);
}

function AddMissingFieldsOld (WhichValue,WhichName){
  
    if (aForm.setfocus.value.length < 2 )  aForm.setfocus.value = WhichValue;
         
    gMissingFields += "\t" + WhichName + "\n";
}

    
//---------------- rfTestPWFileds ("PW","PW2","Password", 3,50) --------------
function rfTestPWFileds(WhichValue1, WhichValue2,WhichName, MinLength, MaxLength ) {
    var TheValue1;
    var TheValue2;
    var Cmd;
    //var aForm = rSubmitThisForm();
    
    Cmd = "TheValue1 = aForm." + WhichValue1 + ".value;";
    eval(Cmd);
    
    if ( TheValue1.length < MinLength || TheValue1.length > MaxLength ) {
    
        AddMissingFieldsText(WhichValue1, WhichName,'<' + MinLength + ' - ' + MaxLength + ' chr>');
        return false;
    }
    
    rDeleteTextNodeDiv('div' + WhichValue1); 
    
    Cmd = "TheValue2 = aForm." + WhichValue2 + ".value;";
    eval(Cmd);
        
    if ( TheValue2.length < MinLength || TheValue2.length > MaxLength ) {
    
        AddMissingFieldsText(WhichValue2, WhichName,'<' + MinLength + ' - ' + MaxLength + ' chr>');
        return false;
    }
    // compair two passwords
    
    if (TheValue1 != TheValue2) {
	    //AddMissingFields(WhichValue1, WhichName);
	    AddMissingFieldsText(WhichValue2, WhichName,"<Passwords don't match>");
        return false;
    }
    
    if (rfHasExcludeChar(WhichValue1, WhichName)) return;
    
    
    rDeleteTextNodeDiv('div' + WhichValue2); 
    return true;
}

//---------------------------------------------------------------------------
    // test e-mail 
    function rfTestTextMinMaxTestChar (WhichValue, WhichName, MinLength, MaxLength, TestChar) {
    
        var TheValue;
        var Cmd;
        var xchar;
        var NotValid;
        
        
        if (aForm == null){
          alert("aForm is null");
          return;
        }
        
        Cmd = "TheValue = aForm." + WhichValue + ".value;";
        eval(Cmd);
        
        if (TheValue == null){
          alert('TheValue is null');
          NotValid = true;
        }
        
        if ( NotValid || TheValue.length < MinLength || TheValue.length > MaxLength){
          AddMissingFieldsText(WhichValue, WhichName,'<' + MinLength + ' - ' + MaxLength + ' chr>');
          return false;
        }
        
        for(i=0;i < TestChar.length;i++){
         xchar = TestChar.substring(i,i+1)
         if (TheValue.indexOf(xchar,0) == -1){
           NotValid = true;
           AddMissingFieldsText(WhichValue, WhichName,"< Character: '" + xchar + "' is missing>");
           return false;
         }
        }
        
        if (rfHasExcludeChar(WhichValue, WhichName)) return;
        
      
        rDeleteTextNodeDiv('div' + WhichValue); 
        return true;
    }
    
    function rfHasExcludeChar(WhichValue, WhichName) {
    
        var TheValue;
        var Cmd;
        var xchar;
        
        if (aForm == null){
          alert("aForm is null");
          return;
        }
        
        Cmd = "TheValue = aForm." + WhichValue + ".value;";
        eval(Cmd);
        
        if (gExclChars.length > 0){
          for(i=0;i < gExclChars.length;i++){
          
             xchar = gExclChars.substring(i,i+1)
             if (TheValue.indexOf(xchar,0) > -1){
               AddMissingFieldsText(WhichValue, WhichName,"< Character: '" + xchar + "' not allowed>");
              // alert("< Character: '" + xchar + "' not allowed>");
               return true;       
             }
          }
        }
        return false;
    }
    
    
//---------------------------------------------------------------------------
    function rfTestSelectBadValue ( WhichValue, WhichName, BadValue) {
    // validate option control
    
        var Cmd;
        var ctl;
        var inx;
        var opt;
            
       Cmd = "ctl = aForm." + WhichValue + ";";
       //Cmd = "ctl = rSubmitThisForm()." + WhichValue + ";";
       eval(Cmd);
       
       inx = ctl.selectedIndex;
	   if ((inx != -1) && (ctl.options[inx].value != BadValue) ){
		  opt = ctl.options[inx].value;
		  rDeleteTextNodeDiv('div' + WhichValue); 
		  return true;
	   }
	   else {
	    //gMissingFields += "\t" + WhichName + "\n";
	    AddMissingFields(WhichValue, WhichName);
         return false;
	   }
      
    }
    
    
    //----------- test integer -------------
    function rfTestTextInt ( WhichValue, WhichName, ExcludeChars, NumberOfDigits ) {
    
        var TheValue;
        var Cmd;
        var xchar;
        var NotValid;
        var xasc
        //var  aForm = rSubmitThisForm();
        
      
        Cmd = "TheValue = aForm." + WhichValue;
        eval(Cmd);
        
        if (!TheValue){
         // controll does not exist
         return true;
        }
        
        TheValue = TheValue.value;
        if(TheValue.length != NumberOfDigits){
          NotValid = true;
        } 
        else
        {     
			NotValid = true;
        
			for(i=0;i < TheValue.length;i++){
				NotValid = false;
				xchar = TheValue.substring(i,i+1);
				xasc = xchar.charCodeAt(0);
				if (!(
					(xasc >= 48) && 
					(xasc <= 57) && 
					(ExcludeChars.indexOf(xchar,0) == -1)
					)){
				  NotValid = true;
				  break;
				}
			}  
        }
        if (NotValid){
           
           AddMissingFieldsText(WhichValue, WhichName,'< numeric only >');
           
          //  aForm.WhichValue = ""
            return false;
        }
        rDeleteTextNodeDiv('div' + WhichValue); 
        return true;
    }
//---------------------------------------------------------------------------
    function rfTestRadioArray ( WhichValue, WhichName, BadValue) {
    // validate radio array control
    
        var Cmd;
        var ctl;
        var checkedButton;
        
        checkedButton = "";    
       Cmd = "ctl = aForm." + WhichValue + ";";
       //Cmd = "ctl = rSubmitThisForm()." + WhichValue + ";";
       eval(Cmd);
       
       //for (var i in ctl) {
       for (var i=0; i < ctl.length;i++) {
         if (ctl[i].checked) {
            checkedButton=ctl[i].value
         }
       }
   
       if ((checkedButton != "") && (checkedButton != BadValue) ){
          rDeleteTextNodeDiv('div' + WhichValue); 
		  return true;
	   }
	   else {
	    
	     AddMissingFieldsText(WhichValue, WhichName,'< selection is not allowed >');
         return false;
	   }
      
    }
// ---------------------
  function rGetSelectIndex (WhichValue) {
       var Cmd;
       var ctl;
       //var  aForm = rSubmitThisForm();
       
       Cmd = "ctl = aForm." + WhichValue + ";";
       eval(Cmd);
       	
       	//alert("\n\nIndex:\n\n" + ctl.selectedIndex );   
	    return ctl.selectedIndex;
  }	    
//-------------------------------------------------------------------------
   function rfTestASelect (WhichValue, WhichName, BadValue) {
       //var  aForm = rSubmitThisForm();
	    if (rGetSelectIndex(WhichValue) == BadValue) {
	     //AddMissingFields(WhichValue, WhichName);
	     AddMissingFieldsText(WhichValue, WhichName,'<make valid selection>');
         return false;
	    } 
	    
	    rDeleteTextNodeDiv('div' + WhichValue);  
	    return true;
	    
   }    
//---------------------------------------------------------------------------
    function GetSelectedSelect (ctl) {
       var opt;
       var inx;
       
       inx = ctl.selectedIndex;
	   if (inx != -1){
		  opt = ctl.options[inx].value;
		  return opt;
	   }
	   
	   return false;
    }
    
//---------------------------------------------------------------------------
    function GetSelectedRadio (ctl) {
       var i;
       var checkedButton;
       
       for (var i=0; i < ctl.length;i++) {
         if (ctl[i].checked) {
            checkedButton=ctl[i].value
            return checkedButton;
         }
       }
       return false
    }
    

//---------------------------------------------------------------------------
    // switch between select and radio array validation
    function rfTestAValue6 ( WhichValue, WhichName, BadValue) {
        var Cmd;
        var ctl;
        var res;
        var xtype;
        var xtag;
        
        res = "";
        
        Cmd = "ctl = aForm." + WhichValue + ";";
        //  Cmd = "ctl = rSubmitThisForm()." + WhichValue + ";";
        eval(Cmd);
        
        if (!(ctl && ctl[0])){
          return false;
        }
        
        xtag = ctl.tagName; // not defined in Netscape
        xtype = ctl[0].type;
        if (ctl.options){
          res = GetSelectedSelect (ctl); 
        }
        else {
          if (xtype && (xtype.toUpperCase() == "RADIO")){
             res = GetSelectedRadio (ctl);
          }
          else {
            window.alert(xtype + ' ' + xtag);
          }
        }  
        if (res && (res != BadValue) ){
          rDeleteTextNodeDiv('div' + WhichValue); 
		  return true;
	    }
	    else {
	     //AddMissingFields(WhichValue, WhichName);
	     AddMissingFieldsText(WhichValue, WhichName,'< selection is not allowed >');
         return false;
	    }  
    }
    
    //---------------------------------------------------------------------------
    // switch between "text" and "select" controll validation
    function rfTestAValue7 (WhichValue, WhichName, SelBadValue, MinLength, MaxLength ) {
        var Cmd;
        var ctl;
        var res;
        
        Cmd = "ctl = rSubmitThisForm()." + WhichValue + ";";
        eval(Cmd);
        
        if (!ctl){
          alert("\n\nControll " + WhichValue + " not found!");
          return false;
        }
        else {
          if (ctl.options){
            res = rfTestAValue4(WhichValue, WhichName, SelBadValue);
            return res;
          }
          else {
            res = rfTestAValue(WhichValue, WhichName, MinLength, MaxLength);
            return res;
          }
        }
    }
    

    function rGetSelectedOption (xcontrol){
      var ctl;
      var res;
      var xstr
      
      //xstr = "ctl=document.aForm." + xcontrol;
      xstr = "ctl=document." + xcontrol;
      eval(xstr);
      
      if (ctl && ctl.options){
          res = GetSelectedSelect (ctl); 
          return res;
       }
       return false;
    }  
    
    function rDisplayErrors(divId,Errtext){
    
       var errpar = "You must appropriately complete: " + Errtext;
       rCreateTextNodeDiv(divId,errpar);
    } 
    
    function rCreateTextNodeDiv(divId,divText) {
       var div = document.getElementById(divId);
               
	   if (div != null){

          var newNode=document.createTextNode(divText);
            
		    if (div.childNodes[0] != null){ 
				div.replaceChild(newNode,div.childNodes[0]);
			}
			else {
				div.appendChild(newNode);
			}
	   }		
    } 
    function rDeleteTextNodeDiv(divId) {
       var div = document.getElementById(divId);
               
	   if (div != null){

          var newNode=document.createTextNode('');
            
		    if (div.childNodes[0] != null){ 
				div.replaceChild(newNode,div.childNodes[0]);
			}
	   }		
    } 
    
//************************ rOnBodyLoad2 ***********
function rOnBodyLoad2() {
    var xtile
    var Cmd
    //var  aForm = rSubmitThisForm();    
    xtile = aForm.setfocus.value;
    
    if (!(xtile == "")){
        Cmd = "aForm." + xtile + ".focus();";
        //alert("Cmd: " + Cmd);
        eval(Cmd);
    }      
    aForm.setfocus.value = "";
}
