//////////////////////////////////////////////////////////////////////////////////////////////
// check email address for valid format, return 0 if check fails
  function echeck(str) {
    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (lstr > 0) {
    // user entered an email address, check it's format //
    if (str.indexOf(at)==-1){
       return 0
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       return 0
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
       return 0
    }
    if (str.indexOf(at,(lat+1))!=-1){
      return 0
    }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      return 0
    }
    if (str.indexOf(dot,(lat+2))==-1){
      return 0
    }

    return 1
    }else{
          // all ok
             return 1
    }
  } // end echeck //

//////////////////////////////////////////////////////////////////////////////////////////////
  function checking_email(fform) {
   if (fform.value != "") {
      if (echeck(fform.value)==0) {
        alert ('Please Enter a Valid Email Address')
        fform.focus()
        return 0  }
      else return 1
              
   }  else return 1
           
  }
//////////////////////////////////////////////////////////////////////////////////////////////
  function checking_form(fform,ccase,where) {
     if (verify_fields(fform,ccase,where)==1 &&
         checking_county(fform)==1 &&
         checking_zip(fform.servZip)==1 &&
         checking_zip(fform.mailZip)==1 &&
         checking_phone(fform.phone)==1 &&
         checking_email(fform.email)==1 ) {
           return true }
     else return false  
  }
/////////////////////////////////////////////////////////////////////////////////////////////
  function checking_phone(str) {
     var lstr=str.value.length
     var i
     var j=0
     for (i=0; i<lstr; i++) {
       if (str.value.charAt(i)>="0" && str.value.charAt(i)<="9") {
          j++
       }
     }
     if(j==10) {
       return 1 
     } else {
       alert ('Please enter valid phone number (xxx)xxx-xxxx')
       str.focus()
       return 0
     }
  }
/////////////////////////////////////////////////////////////////////////////////////////////
  function checking_zip(str) {
     var lstr=str.value.length
     var i
     var j=0
     for (i=0; i<lstr; i++) {
       if ((str.value.charAt(i)>="0" && str.value.charAt(i)<="9") || str.value.charAt(i)=="-") {
          j++
       }
     }
     if(j==lstr && (j==5 || j==10)) {
       return 1 
     } else {
       alert ('Please enter valid Zip code (xxxxx) or (xxxxx-xxxx)')
       str.focus()
       return 0
     }
  }
/////////////////////////////////////////////////////////////////////////////////////////////
  function checking_county(f1)
  {
     var stateItem=f1.servState.options[f1.servState.selectedIndex].text
     var ind=f1.ctyID.selectedIndex;

// alert('stateItem=' + stateItem + ' and index=' + ind);
     if (stateItem == 'Tennessee' && ind > 1) {
        return 1;  
     } else {
               if (stateItem != 'Tennessee' && ind == 1) {
                  return 1; 
               } else {
                      
                  alert('Invalid County/State Relation\n\nYou must specify a county if state=Tennessee,\n otherwise county must be Not Applicable');
                  f1.ctyID.focus();
                  return 0;  }
           }
  }
////////////////////////////////////////////////////////////////////////////////////////////
  function checking_status() {
     statusOption = -1
     var el=document.search.status
     for (i=0; i<el.length; i++) {
         if (el[i].checked) {
           statusOption = i
//alert('statusOption is ' + i)
         }
     }
  }
/////////////////////////////////////////////////////////////////////////////////////////////
  function checking_field(str) {
    if (str.value == "") {
      alert('Please enter customer ID')
      str.focus()
      return false
    } else return true
  }
/////////////////////////////////////////////////////////////////////////////////////////////
  function isBlank(s) {
    var len=s.length
    var i
    for (i=0;i<len;i=i+1) {
      if(s.charAt(i)!=" ")
        return false }
    return true
  }
///////////////////////////////////////////////////////////////////////////////////////////
  function verify_fields(fform,ccase,where) {
    switch(ccase) {
      case "C":   // company entry form
        var missingItem=1;      // for required fields
        var countyIndx;         // county list
        var countyItem;         // items selected from county list
        countyIndx=fform.ctyID
        countyItem=countyIndx.options[countyIndx.selectedIndex].text

        for (i=0; i<fform.elements.length; i++) {
// alert(i + "=" + fform.elements[i].value)
          if (i==6 || i==14 || i==15 || i==16 || i>=18) {
            //ignore these non-required fields
          } else {
             if (fform.elements[i].type == "text" && fform.elements[i].value =="") {
                alert ('A required item is missing.\nPlease fill in all fields marked with a red asterisk (*)')
                fform.elements[i].focus()
                missingItem=0;
                break; 
             } 
             if (fform.elements[i].type == "select-one" && countyItem == "--") {
               alert('A required item is missing.\nPlease fill in all fields marked with a red asterisk (*)')
               fform.elements[i+1].focus()
               missingItem=0;
               break;
             }
          }  // end of else required fields
        }  // end of for loop
      if (missingItem==1) {
          return 1 }
      else return 0
      break;
    } // end of switch
  }
//////////////////////////////////////////////////////////////////////////////////////////
     function sameBtn(fform) {
       fform.mailAddr.value=fform.servAddr.value
       fform.mailCity.value=fform.servCity.value
 //    fform.mailState.selectedIndex=fform.servState.selectedIndex
       fform.mailState.value=fform.servState.value
       fform.mailZip.value=fform.servZip.value
     }
///////////////////////////////////////////////////////////////////////////////////////////
     function adminmenu() 
     {
      document.write("<form>");
document.write('<input type="button" value="Search Database" class="iform" onClick="location.href=\'/cgi-bin/RMD/admin/a_searchRMD.pl\'" onMouseover="window.status=\'Search Database\'; return true;" onMouseout="window.status=\'\'; return true;">');
document.write("<br><br><br>");
document.write('<input type="button" value="Category Update" class="iform" onClick="location.href=\'/cgi-bin/RMD/admin/categoryUpdate.pl\'" onMouseover="window.status=\'Category Update\'; return true;" onMouseout="window.status=\'\'; return true;">');
  document.write("<br /><br /><br />");
document.write('<input type="button" value="EPA_RMD Listing" class="iform" onClick="location.href=\'/cgi-bin/RMD/admin/EPA_listing.pl\'" onMouseover="window.status=\'EPA_RMD Listing\'; return true;" onMouseout="window.status=\'\'; return true;">');
      document.write("<br /><br /><br />");

      document.write("</form>");
     }
