///////////////////////////////////////   vislit.js ///////////////////////////////////////////////////
// To be used with vis_lit project
//////////////////////////////////////////////////////////////////////////////////////////////////////
   function check_user(f,LDAP,pk)
   {
//alert("LDAP IS " + LDAP);

      if (LDAP != 0)
      {
        var x=window.alert('Authorization Denied. Please retry your netid and password.');
           document.login.flag.value='';
           document.login.action='login.pl';
           document.login.submit();
      }

      if (LDAP == 0) {
          if (pk == '') {
               var x=window.alert('Authorization Denied. \n\nYou are either not enrolled in the class or have been inactivated. \nIf this is an error, please contact your instructor.');
               document.login.flag.value='';
               document.login.action='login.pl';
               document.login.submit();
          } else {
                   document.login.pk.value=pk;
                   document.login.action='/cgi-bin/vis_lit/submit_form.pl';
                   document.login.submit();
                 }
       }
    }
///////////////////////////////////////////////////////////////////////////////////////////////////
   function check_image(pk,id) 
   {
      var bad=0;
      var a=document.submit.assg;
      var cnt=document.submit.catCnt.value;

     for(var i=0;i<cnt;i++) {
       bad=1;
       if (document.submit.cat[i].selectedIndex != -1) 
       {
         bad=0;
         break;
       }
     }
     if (bad == 1) {
       alert('You must select at least one item from categories');
     }
 
      if (bad == 0) 
      {
        if (a.selectedIndex==0) 
        {
           alert('You must select an assignment');
           a.focus();
           bad=1;  
      }
      } 

      if (bad == 0) {
        var x=document.submit.iname;
        if(x.value=='') 
        {
          alert('Please select an image');
          x.focus();
          bad=1;
        }
      } 

      if (bad==0) {
        var t=document.submit.tname;
        if(t.value=='') 
        {
          alert('Please select a thumbnail');
          t.focus();
          bad=1;
         }
       }

      if (bad == 0) {
         var ending=x.value.substring(x.value.length - 4);
         if (ending == '.jpg' || ending == '.gif') 
         {
            bad=0;
         }
         else
         {
            alert('Image must be .jpg or .gif format');
            x.focus();
            bad=1;
         }

         var ending=t.value.substring(t.value.length - 4);
         if (ending == '.jpg' || ending == '.gif') 
         {
            bad=0;
         }
         else
         {
            alert('Thumbnail must be .jpg or .gif format');
            t.focus();
            bad=1;
         }
      }
      if (bad == 0) {
        document.submit.pk.value=pk;
        document.submit.user_pk.value=pk;
        document.submit.userid.value=id;
        document.submit.code.value='39tnqt803nt9';
        document.submit.action='submit.pl';
        document.submit.submit();
      } 
   }
///////////////////////////////////////////////////////////////////////////////////////////////////
   function updateForm(id,pk,where) {
      var bad=0;
      var cnt=document.submit.catCnt.value;

     for(var i=0;i<cnt;i++) {
       bad=1;
       if (document.submit.cat[i].selectedIndex != -1)
       {
         bad=0;
         break;
       }
     }
     if (bad == 1) {
       alert('You must select at least one item from categories');
     }

      if (bad == 0)
      {
        var a=document.submit.assg;
        if (a.selectedIndex==0)
        {
           alert('You must select an assignment');
           a.focus();
           bad=1;
        }
      }

      var x=document.submit.iname;
      var t=document.submit.tname;

      if (bad == 0 && x.value!='') {
         var ending=x.value.substring(x.value.length - 4);
         if (ending == '.jpg' || ending == '.gif')
         {
            bad=0;
         }
         else
         {
            alert('Image must be .jpg or .gif format');
            x.focus();
            bad=1;
         }
      }

     if (bad == 0 && t.value!='') {
         var ending=t.value.substring(t.value.length - 4);
         if (ending == '.jpg' || ending == '.gif')
         {
            bad=0;
         }
         else
         {
            alert('Thumbnail must be .jpg or .gif format');
            t.focus();
            bad=1;
         }
     }

     if (bad == 0)  // means good, not bad
     {
       document.submit.flag.value='u';
       document.submit.update.value=pk;
       document.submit.user_pk.value=id;
       document.submit.image_pk.value=pk;
       document.submit.code.value='859dbtr038';

       if (where == 'A') {  // coming from admin form
          document.submit.action='admin_update.pl'; 
          var L=document.submit.stat.length;
          var ret="";
          for (var i = 0 ; i< L ; i++) {
            if(document.submit.stat[i].checked) {
              ret=document.submit.stat[i].value;
              break;
            }
          }
          if (ret=='delete') {
             if (confirm("Are you sure you want to delete this record\nand all associated images?")) { document.submit.submit(); }
          } else { document.submit.submit(); }
       } else {  // not from admin form
          document.submit.action='update_form.pl';
          document.submit.submit();
       } // coming from update form

     }  //if  good, not bad
   }  
////////////////////////////////////////////////////////////////////////////////////////////////////
   function pick() {
       document.search.flag.value="T";
       document.search.action="search.pl";
       document.search.submit();
   }
///////////////////////////////////////////////////////////////////////////////////////////////////
   function adminpick() {
       document.search.flag.value="T";
       document.search.action="admin_search.pl";
       document.search.submit();
   }
///////////////////////////////////////////////////////////////////////////////////////////////////
   function recStat() {
     document.update.action="admin_display.pl";
     option = -1;
     var el=document.update.elements;
     for (var i=0; i< el.length; ++i) {

       if(el[i].type == "radio") {
          for (var j=0; j<document.update.stat.length; j++) {
            if (document.update.stat[j].checked) {
              option=j;
              break;
            } //if update checked
          } //inside for loop
       } //if radio btn
     }
     if (option == 2) {
       if (confirm("Are you sure you want to delete this record?") ) return true;
          else {
                  document.update.reset();
                  return false;
               }
     }
     if (option == 0 || option == 1) return true;

   }
///////////////////////////////////////////////////////////////////////////////////////////////////
   function pickCat(pk,id) {
       var d=document.submit;
       document.submit.flag.value="T";
       document.submit.pk.value=pk;
       document.submit.userid.value=id;
       document.submit.action="submit_form.pl";
       document.submit.submit();
   }
///////////////////////////////////////////////////////////////////////////////////////////////////
   function updateCat(id,code,pk,del,where) {
       document.submit.flag.value="T";
       document.submit.user_pk.value=id;
       document.submit.code.value=code;
       document.submit.chkbx.value=del;
       document.submit.update.value=pk;
       if (where == 'A') {
          document.submit.action="admin_display.pl";
       } else {
          document.submit.action="update_form.pl";
       }
       document.submit.submit();
   }
/////////////////////////////////////////////////////////////////////////////////////////////////////
   function updateUser() {
       var d=document.update;
       var opt='';

       //get choice value
       j=d.choice.length;
       for (i=0; i<j; i++){
          if(d.choice[i].checked) opt = d.choice[i].value
       }
       
       if (opt=='') {
           alert('Please select an update option.')
           return false;
       } else {
          if (opt=='D') {
             if (confirm("Are you sure you want to delete this netid\nand all associated images?") ) {
                return true;
             } else {
                       return false;
             }
          } else {
              if (d.userID.selectedIndex==0) {
                 alert('Please select a netID.')
                 d.userID.focus()
                 return false;
              }
          }
    //     return true;
       }
   }
/////////////////////////////////////////////////////////////////////////////////////////////////////
   function myStuff(pk,netid)
   {
       var f=document.mystuff;
       f.user_pk.value = pk;
       f.userid.value=netid;
       f.code.value='n39tn9ej5n';
       f.action='list.pl';
       f.submit();
   }
/////////////////////////////////////////////////////////////////////////////////////////////////////
   function viewStuff(pk,netid)
   {
       var f=document.viewstuff;
       f.user_pk.value = pk;
       f.userid.value=netid;
       f.code.value='n39tn9ej5n';
       f.action='list.pl';
       f.submit();
   }
/////////////////////////////////////////////////////////////////////////////////////////////////////
   function gotoIntent(pk,image,assg)
   {
       var f=document.gotoI;
       f.userpk.value = pk;
       f.imagepk.value = image;
       f.assgpk.value = assg;
       f.action='intent.pl';
       f.submit();
   }
////////////////////////////////////////////////////////////////////////////////////////////////////
   function hiddenOne() {
      document.write("<input type='hidden' name='user_pk'>");
      document.write("<input type='hidden' name='code'>");
      document.write("<input type='hidden' name='userid'>");
   }
////////////////////////////////////////////////////////////////////////////////////////////////////
   function hiddenTwo() {
      document.write("<input type='hidden' name='pk'>");
      document.write("<input type='hidden' name='flag'>");
   }
////////////////////////////////////////////////////////////////////////////////////////////////////
   function hiddenThree() {
      document.write("<input type='hidden' name='userpk'>");
      document.write("<input type='hidden' name='imagepk'>");
      document.write("<input type='hidden' name='assgpk'>");
      document.write("<input type='hidden' name='doWhat'>");
   }
///////////////////////////////////////////////////////////////////////////////////////////////////
   function intentForm(pk,ipk,assg) {
       var i=document.intent;
       i.userpk.value=pk;
       i.doWhat.value='U';
       i.imagepk.value=ipk;
       i.assgpk.value=assg;
       // if first three blank alert, else submit
       if (i.intent.value == '' && i.match.value == '' && i.revised.value == '') {
//       alert('You must complete first three fields*')
         alert('You must fill in one of the three fields*')
       } 
       else 
       {
          i.action='intent.pl';
          i.submit();
       }
   }
////////////////////////////////////////////////////////////////////////////////////////////////////
   function myStuff2(u) {
       var f=document.submit;
       f.userid.value=u;
       f.user_pk.value=u;
       f.code.value='n39tn9ej5n';
       f.action='list.pl';
       f.submit();
   }
////////////////////////////////////////////////////////////////////////////////////////////////////
   function goDisplay(image,user,c) {
       var f=document.disp;
       f.g_userid.value=user;
       f.g_pk.value=image;
       f.g_cond.value=c;
       f.action='display.pl';
       f.submit();
   }
////////////////////////////////////////////////////////////////////////////////////////////////////
   function check_action(u,opt,i)
   {
       var el=document.list;

       document.list.user_pk.value=u;
       document.list.option.value=opt;
       document.list.image_pk.value=i;
       document.list.code.value="859dbtr038";
       document.list.action="update_form.pl";

       if(opt == 1) {   //update
            el.submit();
       }

       if(opt == 2) {  //delete
            var s=(confirm('You are requesting this record to be deleted.\n Continue?'));
            if (s) {
                     el.submit();
                   }  
       }

   }
///////////////////////////////////////////////////////////////////////////////////////////////////
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 check_assgn()
   {
      var bad=0;
      var a=document.assign;

      if(a.descript.value=='')
      {
        alert('Please enter a short description');
        a.descript.focus();
        bad=1;
      }
      
      if (bad==0) {
        if(a.ldesc.value=='')
        {
          alert('Please enter a long description');
          a.ldesc.focus();
          bad=1;
        }
      }

      if (bad==0) {
        if (a.crs.selectedIndex==0)
        {
           alert('You must select a course');
           a.crs.focus();
           bad=1;
        }
      }

      if (bad==0) {
        a.submit();
      }
   }
//////////////////////
