//////////////////////////////////////////////////////////////////////////////////////////////

function EnterKey(event) {
   if (isNav) {if (event.which == 13) {return true;}}
   if (isIE) {if (event.keyCode == 13) {return true;}}
   return false;
}

function checkSearch()
{
   var doc = window.document.forms["utk_seek"];

       with (window.document.forms["utk_seek"])
       {
           if (doc.qtype.options[doc.qtype.selectedIndex].value == "cis")
           {
                 doc.qp.value="+site:www.cis.utk.edu";
                 window.location.href = "/cgi-bin/search.pl?qp=" + doc.qp.value + "&qt=" + doc.qt.value;
           }
            else if(doc.qtype.options[doc.qtype.selectedIndex].value == "epa")
                 {
                    doc.qp.value="+site:epamact.utk.edu";
                  window.location.href = "/cgi-bin/epamact/search.pl?qp=" + doc.qp.value + "&qt=" + doc.qt.value;
                  }

           else {
                      doc.submit();
                }
      }
}


function oldcheckSearch() {
   var doc = window.document.forms["utk_seek"];

       with (window.document.forms["utk_seek"]) {
           if (doc.qtype.options[doc.qtype.selectedIndex].value == "cis") {
                 doc.qp.value="+site:www.cis.utk.edu";
                 window.location.href = "/cgi-bin/search.pl?qp=" + doc.qp.value + "&qt=" + doc.qt.value;
           }  
           else {
                      doc.submit();
           }
    }
}


function cisSearch() {
   var doc = window.document.forms["seek"];

       with (window.document.forms["seek"]) {
           if (doc.qtype.value == "cis") {
                 doc.qp.value="+site:www.cis.utk.edu";
                 window.location.href = "/cgi-bin/search.pl?qp=" + doc.qp.value + "&qt=" + doc.qt.value;
           }  else {
                      doc.submit();
           }
    }
}


/////////////////////////////////////////////////

// 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 //
//alert('lstr > 0 is '+lstr+' proceeding with check')
    if (str.indexOf(at)==-1){
//alert('Error on 1')
       return 0
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
//alert('Error on 2')
       return 0
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
//alert('Error on 3')
       return 0
    }
    if (str.indexOf(at,(lat+1))!=-1){
//alert('Error on 4')
      return 0
    }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
//alert('Error on 5')
      return 0
    }
    if (str.indexOf(dot,(lat+2))==-1){
//alert('Error on 6')
      return 0
    }
//    if (str.indexOf(" ")!=-1){
//alert('Error on 7 index is'+str.indexOf("x"))
//      return 0
//    }
//alert('No Error found condition 1')
//alert('Indicated NO Error on 7 index is '+str.indexOf(" "))
    return 1
  }else{
    // all ok
//alert('No Error found condition 2')
    return 1
  }
} // end echeck //

//////////////////////////////////////////////////////////////////////////////////////////////
// search form for all required fields to be filled in.
function verify_fields() {
  var MissingItem = 0; // Total list of empty required fields
  var CountryIndx;     // All SELECT country list items
  var CountryItem;     // Item chosen from SELECT country list
  var FindIndx;        // All SELECT find about list items
  var FindItem;        // Item chosen from SELECT find about list
  var StateIndx;       // All SELECT state list items
  var StateItem;       // Item chosen from SELECT state list
  var RegionIndx;      // All SELECT region list items
  var RegionItem;      // Item chosen from SELECT region list
  CountryIndx = document.contact_info.country
  CountryItem = CountryIndx.options[CountryIndx.selectedIndex].text
  FindIndx = document.contact_info.find
  FindItem = FindIndx.options[FindIndx.selectedIndex].text
  StateIndx = document.contact_info.state
  StateItem = StateIndx.options[StateIndx.selectedIndex].text
  RegionIndx = document.contact_info.region
  RegionItem = RegionIndx.options[RegionIndx.selectedIndex].text
  for  (i = 11; i < document.contact_info.elements.length ; i++) {
    // parse form looking at required entry fields
    if (i == 12 || i == 15 || i == 17 || i == 23 || i == 24 || i == 25 || i == 26 || i == 28) {
      // Ignore these non-required fields
    }else{                                          // These fields are required
      if (document.contact_info.elements[i].type == "text" && document.contact_info.elements[i].value == "") {
        MissingItem = 1;                            // A required field was not filled in
      }
      if (document.contact_info.elements[i].type == "select-one" && (FindItem == "--")) {
        MissingItem = 1;                            // A required field was not filled in
      }
      if (document.contact_info.elements[i].type == "select-one" && (FindItem == "Other" && document.contact_info.other.value =="")) {
        MissingItem = 1;                            // A required field was not filled in
      }
      if (document.contact_info.elements[i].type == "select-one" && (StateItem == "--" && CountryItem == "United States")) {
        MissingItem = 1;                            // A required field was not filled in
      }
      if (document.contact_info.elements[i].type == "select-one" && (StateItem == "Tennessee" && RegionItem == "--")) {
        MissingItem = 1;                            // A required field was not filled in
      }
      if (document.contact_info.elements[i].type == "select-one" && (StateItem == "--" && RegionItem == "--" && CountryItem == "--")) {
        MissingItem = 1;                            // A required field was not filled in
      }
      if (document.contact_info.elements[i].type == "select-one" && (StateItem != "--" && CountryItem == "--")) {
        MissingItem = 1;                            // A required field was not filled in
      }
    }
    //for testing
    //alert(i + " = " + document.contact_info.elements[i].value)
  }

  //if condition added per conversation with Buggeln 11/21/2003 - carnduff
  if (document.contact_info.email_entry.value == "" && document.contact_info.fax.value=="") {
     alert('You must enter either a valid email address OR fax number');
     MissingItem = 3;
     document.contact_info.email_entry.focus();
  }

  if (document.contact_info.phone.value.length < 7) {
     alert('Invalid Phone Number');
     document.contact_info.phone.focus();
     MissingItem=3;
  }

  if (document.contact_info.fax.value !="") {
    if (document.contact_info.fax.value.length < 7) {
        alert('Please enter a valid FAX Number');
        document.contact_info.fax.focus();
        MissingItem=3;
     }
  }
  //end of conditions added by carnduff on 11/21/2003



  if (document.contact_info.email_entry.value != "") {
    // execute if user entered an email address
 //  alert('an email was entered and will be checked for validity');
    if (echeck(document.contact_info.email_entry.value)==0) {
      // check to see if email entry is valid format
      MissingItem = 2;                     // invalid e-email format entered
    }
  }

  if (MissingItem != 0) {                              // print error msg to user
    if (MissingItem == 1) {
      alert('A required item is missing.\nPlease fill in all fields marked with an asterisk (*)')
    }else{
           if (MissingItem==2) {
               alert('The email address you provided is invalid.\nPlease correct your entry.')
           }
    }
    document.contact_info.go_num.value = '0'
  }else{
    if (document.contact_info.go_num.value == '1') {  // select proper cgi script to execute
      document.contact_info.action="/cgi-bin/TME/verifyInfo2.pl"
      document.contact_info.submit(this.form)
    }
    if (document.contact_info.go_num.value == '2') {  // select proper cgi script to execute
      if (document.contact_info.btn.value == 'C') {   // user selected 'Contact CIS' option
        document.contact_info.action="/cgi-bin/TME/mail_list.pl?C"
      } else {
       // document.contact_info.skip_verify.value == '1'
       // save the above line for using with skipping email verify for W and A flags
        document.contact_info.action="/cgi-bin/TME/matSearch2.pl"
      }
      document.contact_info.submit(this.form)
    }
  }
} // end verify_fields //

//////////////////////////////////////////////////////////////////////////////////////////////
function validate_user(){
  var doc=document.verify_user;
  if (doc.fname.value== "") {
    alert("Please Type in Your First Name")
    doc.fname.focus()
    return false
  }  else if (doc.lname.value == "") {
          alert("Please Type in Your Last Name")
          doc.lname.focus()
          return false
       } else if (doc.zip.value == "") {
       } else if (doc.zip.value == "") {
             alert("Please Type in Your Zip Code")
             doc.zip.focus()
             return false
           }  else {
               document.verify_user.action="/cgi-bin/TME/matSearch2.pl"
                   }
                     // new code here
                        window.onerror = null;

                        var lastname=doc.lname.value;
                        var firstname=doc.fname.value;
                        var zipsend=doc.zip.value;
                        var url=("/cgi-bin/TME/followUp.pl?newuser=0&lname=" + lastname + "&fname=" + firstname + "&zip=" + zipsend);

                     // function helpWindow(url) {
                        if (helpWin == null) {
                          helpWin = window.open(url, "Help","scrollbars,resizable,height=580,width=750,top=50,left=60,alwaysRaised")
                        } else {
                           if (helpWin.closed) {
                             helpWin = window.open(url, "Help","scrollbars,resizable,height=580,width=750,top=50,left=60,alwaysRaised")
                           } else {
                                    helpWin.location.href = url
                                    helpWin.focus()
                                  }
                       }

                     // Window variable
                        var helpWin = null
                    // new code above

} // end validate_user //
//////////////////////////////////////////////////////////////////////////////////////////////
// get current system date for display in HTML field
function get_day() {
  var d=new Date()
  curYear=d.getFullYear() 
  curMonth=d.getMonth() + 1
  curDay=d.getDate()
  if (curDay<10) { 
    curDay = "0" + curDay
  }
  if (curMonth<10) { 
    curMonth = "0" + curMonth
  }
  result = curYear + "/" + curMonth + "/" + curDay
} // end get_day //

//////////////////////////////////////////////////////////////////////////////////////////////
// add, remove or update user information for CIS mailing list
function mail_list(go_num) {
 var good=1;
 if (go_num == 1) { 
   // Update user information in database
   document.showinfo.action="/cgi-bin/TME/verifyInfo2.pl"
 }
 if (go_num == 2) {
   // Add to mailing list
    if (checking_boxes(document.showinfo)) {
         document.showinfo.action="/cgi-bin/TME/mail_list.pl?S"
    } else { 
            good=0;
           }
 }
 if (go_num == 3) {
  // Remove from mailing list after confirmation 
  input_box=confirm("Are you sure you would like to be removed from the TME mailing list?")
  if(input_box == true) { // user has selected removal from mailing list
    document.showinfo.action="/cgi-bin/TME/mail_list.pl?U"
  }
 }
   if(good == 1) { document.showinfo.submit(); }
} // end mail_list //
//////////////////////////////////////////////////////////////////////////////////////////////
// direct user to appropriate page for materials wanted, available, and listed and mailing list
function material_list(go_btn) {

  switch(go_btn) {
    case "L":
              // List Materials first time through
              document.showinfo.action="/cgi-bin/TME/matList.pl";
              break;
    case "G":
             // List Materials second time through for error checking
             // form validation added by DAC on 19 Feb 03
             if (checking_matstat() &&
                 checking_class() &&
                 checking_sn() &&
                 checking_phys() &&
                 checking_pack() &&
                 checking_other() ) {
                   document.showinfo.action="/cgi-bin/TME/matUpload.pl";
                   document.showinfo.submit()
                 }
              break;
    case "W":
              document.showinfo.action="/cgi-bin/TME/submitResponse2.pl";
              break;
    case "A":
              document.showinfo.action="/cgi-bin/TME/submitResponse2.pl";
              break;
    case "M":
              document.showinfo.action="/cgi-bin/TME/mail_list1.pl";
              break;
  } // end switch //

   function checking_matstat(){
   // use var to define iTmp as local variable
    var iTmp = 0  
    for (var i=0; i<document.showinfo.materials.length; i++) if (document.showinfo.materials[i].checked) iTmp++
         if (iTmp==0) { 
          alert('Please check material wanted or available.')
          return false}
      else return true
   }
   function checking_sn(){
      if (document.showinfo.sn.value ==""){
          alert('Please type in materials to be listed')
          document.showinfo.sn.focus()
          return false}
      else return true
   }
   function checking_class() {
      if (document.showinfo.kindmat.selectedIndex==0) {
          alert('Please select a category.')
          document.showinfo.kindmat.focus()
          return false}
      else return true
   }
   function checking_phys(){
    var iTmp = 0  
    for (var i=0; i<document.showinfo.phys.length; i++) if (document.showinfo.phys[i].checked) iTmp++
         if (iTmp==0) { 
          alert('Please select physical state.')
          return false}
      else return true
   }
   function checking_pack(){
    var iTmp = 0
    for (var i=0; i<document.showinfo.pack.length; i++) if (document.showinfo.pack[i].checked) iTmp++
        if (iTmp==0) {
           alert('Please select packaging.')
           return false}
        else return true
   }
   function checking_other(){
      if (document.showinfo.pack[4].checked && document.showinfo.po.value==''){
          alert('Please specify other packaging.')
          document.showinfo.po.focus()
          return false}
      else return true
   }

} // end material_list //

/////////////////////////

      function pick_sel()
      {
        var cnt=0;
        if(!document.srch.choice.length)
        {
           document.srch.choice.checked=true;
        }
        else 
        {
          for(var i=0;i<document.srch.choice.length;i++)
          {
             if (document.srch.choice[i].checked)
              cnt++;
          }

          if (cnt>0) 
          { 
             return true; 
          } 
          else 
          { 
                alert("No materials have been selected.  To select, click on the appropriate checkbox(es).")
                return false; 
          }
        } 
      }

/////////////////////////////

      function pickAll(v)
      {
        if(!document.srch.choice.length)
        {
            if (document.srch.selall.checked==true) {
             document.srch.choice.checked=true; 
             document.srch.deall.checked=false;
             document.srch.selall.checked=false; 
            }
            if (document.srch.deall.checked==true) {
             document.srch.choice.checked=false; 
             document.srch.selall.checked=false; 
             document.srch.deall.checked=false;
            }
        }
        else
        {
                 for(var i=0;i<document.srch.choice.length;i++)
                 {
                    document.srch.choice[i].checked=eval(v);
                 }
                 document.srch.selall.checked=false; 
                 document.srch.deall.checked=false;
        }
     }

/////////////////////////////

function helpWindow(url) {
  if (helpWin == null) {
    helpWin = window.open(url, "Help", "height=200,width=420,top=200,left=360,alwaysRaised")
  } else {
    if (helpWin.closed) {
      helpWin = window.open(url, "Help", "height=200,width=420,top=200,left=360,alwaysRaised")
    } else {
      helpWin.location.href = url
      helpWin.focus()
    }
  }
  return false
}

  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
    }

///////////////////////////////////////////////////////////////
// for contact form

function contact_form()
{
  if (checking_fname(document.cis.fname_entry) &&
      checking_lname(document.cis.lname_entry) &&
      checking_phone(document.cis.phone) &&
      checking_zip(document.cis.zip) ) {
      return true; }
      else  {return false; }
 }

   function checking_fname(str){
      if (isBlank(str.value)){
          alert('Please type in First Name')
          str.value=str.defaultValue
          str.focus()
          return false}
      else return true
   }
   function checking_lname(str){ 
      if (isBlank(str.value)){ 
          alert('Please type in Last Name') 
          str.value=str.defaultValue
          str.focus() 
          return false} 
      else return true  
   } 
   function checking_phone(str){ 
      if (isBlank(str.value)){ 
          alert('Please type in Phone Number')
          str.value=str.defaultValue
          str.focus() 
          return false} 
      else return true 
   } 
   function checking_zip(str){ 
      if (isBlank(str.value)){ 
          alert('Please type in Zip/Postal Code') 
          str.value=str.defaultValue
          str.focus() 
          return false}  
      else return true 
   } 

///////////////////////////////
//for subscribe.pl
function check_form() {
  if (checking_cname(document.subscribe.cname) &&
      checking_fname(document.subscribe.fname) &&
      checking_lname(document.subscribe.lname) &&
//    checking_title(document.subscribe.title) &&
      checking_addr(document.subscribe.addr) &&
      checking_city(document.subscribe.city) &&
      checking_state(document.subscribe.state) &&
      checking_ctry(document.subscribe.ctry) &&
      checking_zip(document.subscribe.zip)  &&
      checking_phone(document.subscribe.ph) &&
      checking_email(document.subscribe.eml)  &&
      checking_phone(document.subscribe.ph)  &&
      checking_boxes(document.subscribe)
                                               ) {
      return true; }
      else  { return false; }
}

   function checking_state(str) {
      if (str.selectedIndex==0) {
          alert('Please select a state.')
          str.focus()
          return false}
      else return true
   }

   function checking_ctry(str) {
      if (str.selectedIndex==0) {
          alert('Please select a country.')
          str.focus()
          return false}
      else return true
   }

   function checking_title(str){
      if (isBlank(str.value)){
          alert('Please type in Title')
          str.value=str.defaultValue
          str.focus()
          return false}
      else return true
   }
   function checking_addr(str){
      if (isBlank(str.value)){
          alert('Please type in Street Address')
          str.value=str.defaultValue
          str.focus()
          return false}
      else return true
   }
   function checking_city(str){
      if (isBlank(str.value)){
          alert('Please type in City Name')
          str.value=str.defaultValue
          str.focus()
          return false}
      else return true
   }
   function checking_cname(str){
      if (isBlank(str.value)){
          alert('Please type in Company Name')
          str.value=str.defaultValue
          str.focus()
          return false}
      else return true
   }
   function checking_email(eml) {
      if (eml.value != "") {
        if (echeck(eml.value)==0) {
          alert('Invalid email address');
          eml.focus();
          return false; }
        else { return true; }
      }
      return true;
   }
   function checking_boxes(str){
        if (str.focus.checked || str.WRAP.checked || str.solutions.checked || str.tme.checked) {
            return true; 
        } else {
          alert('Please select a box(es) below');
          return false;
        }
   }
