  /*
   * Pinpoint javascripts
   *
   * These are javascripts used by pinpoint for behavior and display.
   *
   */

  // TODO Document all of these

  function saveAndGo(destination) {
      if (document.forms['SourcesForm']) {
          //View sources page
          return saveSourcesAndGo(destination);
      }
      else if (document.forms['SearchForm']) {
          //Search page
          return saveSearchAndGo(destination);
      }
      else {
          return false;
      }
  }

  function doBrowse(field)
      {
          if (field == null || field == "") {
              field = "searchTerm1";
          }
          document.forms['SearchForm'].browseField.value = field;
          document.forms['SearchForm'].goSearch.value = "Browse";
          document.forms['SearchForm'].submit();
          showDiv('searchingmsg');
          return true;
      }

  function saveSearchAndGo(destination)
          {
              if (document.forms['SearchForm']) {
                  document.forms['SearchForm'].destinationPage.value = destination;
                document.forms['SearchForm'].goSearch.value = "";
                document.forms['SearchForm'].submit();
                  return true;
              }
              else {
                  return false;
              }
          }
  function saveSourcesAndGo(destination)
          {
              if (document.forms['SourcesForm']) {
                  document.forms['SourcesForm'].destinationPage.value = destination;
                document.forms['SourcesForm'].submit();
                  return true;
              }
              else {
                  return false;
              }
          }


  function clearLibraryAdvancedForm()
          {
              document.SearchForm.searchTerm1.value = "";
              document.SearchForm.searchTerm2.value = "";
              document.SearchForm.searchTerm3.value = "";
              document.SearchForm.searchLimit1.options[3].selected = true;
              document.SearchForm.searchLimit2.options[3].selected = true;
              document.SearchForm.searchLimit3.options[3].selected = true;
              document.SearchForm.searchOperator1[0].checked = "1";
              document.SearchForm.searchOperator2[0].checked = "1";
              document.SearchForm.searchTerm1.focus();
              var i;
              for (i = 0; i < document.SearchForm.copyType.length; i++){
                document.SearchForm.copyType.options[i].selected = false;
              }//end for loop
              document.SearchForm.copyType.options[0].selected = true;

              for (i = 0; i < document.SearchForm.location.length; i++){
                document.SearchForm.location.options[i].selected = false;
              }//end for loop
              document.SearchForm.location.options[0].selected = true;
              



          }

  function clearOtherLibrarySearchForm()
          {
              document.SearchForm.searchTerm1.value = "";
              document.SearchForm.searchTerm2.value = "";
              document.SearchForm.searchTerm3.value = "";
              document.SearchForm.searchLimit1.options[3].selected = true;
              document.SearchForm.searchLimit2.options[3].selected = true;
              document.SearchForm.searchLimit3.options[3].selected = true;
              document.SearchForm.searchOperator1[0].checked = "1";
              document.SearchForm.searchOperator2[0].checked = "1";
              //clear all checkboxes
              // check if they are defined
              if (document.SearchForm.selectedSites != null && document.SearchForm.selectedSites != "undefined"){
                for (i = 0; i < document.SearchForm.selectedSites.length; i++)
                	document.SearchForm.selectedSites[i].checked = false ;
              }

               if (document.SearchForm.selectedGroups != null && document.SearchForm.selectedGroups != "undefined"){
                for (i = 0; i < document.SearchForm.selectedGroups.length; i++)
                	document.SearchForm.selectedGroups[i].checked = false ;
              }
              document.SearchForm.searchTerm1.focus();
           }

  function clearAllInfoAdvancedForm(infoType, level)
          {
              document.SearchForm.searchTerm1.value = "";
              document.SearchForm.searchTerm2.value = "";
              document.SearchForm.searchTerm3.value = "";
              document.SearchForm.searchOperator1[0].checked = "1";
              document.SearchForm.searchOperator2[0].checked = "1";
              document.SearchForm.searchTerm1.focus();
          }

  function clearForm()
      {
        document.SearchForm.reset();
        document.SearchForm.searchTerm1.focus();
      }

  function clearQuickForm()
      {
        document.SearchForm.searchTerm1.value = "";
        document.SearchForm.searchTerm1.focus();
        var i;
        for (i = 0; i < document.SearchForm.searchLimit1.length; i++){
            if(document.SearchForm.searchLimit1[i].value == document.SearchForm.defaultLimit.value){
                document.SearchForm.searchLimit1[i].checked = true;
            }
        }

      }

  function clearReadingForm()
      {
        document.ReadingProgramSearchForm.reset();
        document.ReadingProgramSearchForm.readingProgLevel.value = "";
        document.ReadingProgramSearchForm.readingProgPoints.value = "";
        document.ReadingProgramSearchForm.searchTerm.value = "";

        document.ReadingProgramSearchForm.readingProgLevel.focus();
      }

  function resetAllInfoSourcesForm()
  {
    document.ViewAllInfoSourcesForm.status.value="RESET";
    document.ViewAllInfoSourcesForm.submit();
  }

  function openPinpointWindow (URL) {
              pinpointWindow = window.open(URL,"pinpointWindow","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=800,height=600");
             if (pinpointWindow.focus) { pinpointWindow.focus(); }
         }

  function showDiv(layerName)
   {
    if (navigator.appName == "Netscape") {
          if(document.getElementById) {
              document.getElementById(layerName).style.display="block";
              }
          else if(document.layers) {
                  document.layers[layerName].visibility="visible";
                    }
         }
         else{
        if(document.getElementById) {
              document.getElementById(layerName).style.display="block";
              }
       }
       if(document.forms['SearchForm']){
          document.forms[0].searchButton.disabled=true;
       }


    }  //end show div

  function hideDiv(layerName)
   {
    if (navigator.appName == "Netscape") {
          if(document.getElementById) {
              document.getElementById(layerName).style.display="none";
              }
          else if(document.layers) {
                  document.layers[layerName].visibility="hidden";
                    }
         }
         else{
        if(document.getElementById) {
              document.getElementById(layerName).style.display="none";
              }
       }

    }  //end hide div

    function showHelp(jspfile,admin)
          {
           //find help file for current jsp file
           var helppath="";
            var helpfile="";
           if(admin == 1){
              //assign admin path
              helppath="../ppadminhelp/";
           }else{
              helppath="help/";
              }

           var helpmap =  "allinfosearchform=ab_q_sch_pg,"+
                          "librarysearchform=ab_q_sch_pg_lib,"+
                          "allinfoadvancedsearchform=ab_a_sch_pg,"+
                          "libraryadvancedsearchform=ab_a_sch_pg_lib,"+
                          "libraryfulldisplay=ab_full_disp_pg,"+
                          "mybibliography=ab_mybib_pg,"+
                          "mylibraryinfo=ab_mylibinfo_pg,"+
                          "readingprogramsearchform=ab_r_sch_pg,"+
                          "searchresults=ab_sch_res_pg,"+
                          "viewallinfosources=ab_view_sch_sc,"+
                          "authenticateuser=lib_info_view,"+
                          "viewlibrarysources=ab_view_lib_sc,"+
                          "changeegemssettings=ab_ch_clip_pg,"+
                          "changemasterpassword=ab_ch_m_pwd_pg,"+
                          "patronaccessoptions=ab_ch_p_opts_pg,"+
                          "maintaindistrictsources=ab_d_sc_pg,"+
                          "editlibrarysources=ab_ed_lib_sc_pg,"+
                          "editinternetsources=ab_ed_int_sc_pg,"+
                          "editz39source=ab_ed_z_sc_pg,"+
                          "maintainsites=ab_maint_site_pg,"+
                          "editsites=ab_site_info_pg,"+
                          "districtsystemname=ab_dist_sys_name_pg,"+
                          "deletesite=site_delete,"+
                          "changeproxysettings=ab_proxy_pg,"+
                          "maintaingroups=ab_maint_grps_pg,"+
                          "changeemailsettings=ab_adm_email_pg,"+
                          "citationplugin=clip_plugin,"+
                          "libraryvisualsearchform=ab_v_sch_pg,"+
                          "browseresults=ab_brws_keywds_pg,"+
                          "geminimylibraryinfo=ab_mylibinfo_pg,"+
                          "geminiotherlibrarysearchform=ab_view_sch_sc,"+
                          "geminifulldisplay=ab_full_disp_pg,"+
                          "geminimarcdisplay=ab_full_disp_pg,"+
                          "geminicopydisplay=ab_full_disp_pg,"+
                          "libraryreadinglistsform=ab_rl," +
                          "moreinfodisplay=ab_moreinfo_pg," +
                          "statestandards=ab_state_std_pg," +
                          "advancedbookings=abm_ab_bookings_win,"+
                          "newbookings=ab_new_bookings,"+
                          "webpath=ab_wb_sch_pg,"
                          ;

           fileindex=helpmap.indexOf(jspfile.toLowerCase());
           if(fileindex > -1)
              {
              temp=helpmap.substring(fileindex,helpmap.indexOf(",",fileindex));
              helpfile=temp.split("=")[1];
              }
            if(helpfile==""){
                  helpfile="index.htm";
                  }else{
                  helpfile="index.htm#"+helpfile+".htm";

                  }

            eval("window.open('"+helppath+helpfile+"', 'InfoCentreHelp','width=772,height=450,resizable=yes')");
           }

  function sortSearchResults(Id) {
      location.href = "SearchResults.do?sortId=" + Id;
  }

  function sortBibResults(Id) {
          location.href = "MyList.do?sortId=" + Id;
  }

  function sortBookingResults(id) {
      location.href = "NewBookings.do?sortId=" + id;
  }

  function moreInfo(id){
      var obj;
      // get elements
      obj = document.getElementById(id);
      if(obj != undefined) {
          if(obj.style.display == 'none'){
              // display div
              obj.style.display = 'block';
          } else {
              obj.style.display = 'none';
          }
      }
  }

  function addToBib(id,section) {
      var theSrc;
      var currentTime = new Date();

      //to make buttons work after coming back to the search results page using browser back button
      //not sure why this is nacessary but it seems to do the trick
      //theSrc = "assets/img/spacer.gif?time="+currentTime.getTime();
      //alert(theSrc);
      document.code_image.src = "AddToBib.do?time="+currentTime.getTime()+"&id=" + id + "&section=" + section;
      if (document.getElementById('noMessage') != null) {
          hideDiv('noMessage');
      }
      showDiv('bibMessage');
      //document.code_image.src = theSrc;
  }

  function addAllToList(){
     //var theSrc;
     var currentTime = new Date()
     //theSrc = "assets/img/spacer.gif?time="+currentTime.getTime();
     //alert(theSrc);
     document.code_image.src = "AddToBib.do?time="+currentTime.getTime();
      if (document.getElementById('noMessage') != null) {
          hideDiv('noMessage');
      }
      showDiv('bibMessage');


      var addButtons = document.searchResults.addButton;
      if(addButtons.length){
            for(addCounter = 0; addCounter < addButtons.length; addCounter++){
                addButtons[addCounter].disabled=true;
            }//for
        }
  }

  function addToBooking(id,source,copyOid) {

      document.code_image.src = "AddToBookings.do?copyOid="+copyOid+"&id=" + id + "&source=" + source;
      if (document.getElementById('noMessage') != null) {
          hideDiv('noMessage');
      }
      showDiv('bookingMessage');
  }

  function deleteFromBib(id,section) {
      location.href="RemoveFromBib.do?id=" + id + "&section=" + section;
  }

  function deleteAllFromBib() {
      window.location.href="RemoveFromBib.do";
  }

  function deleteFromBookingsList(id,section) {
      location.href="RemoveFromBookings.do?id="+id+"&section="+section;
  }

  function deleteAllFromBookings() {
      window.location.href="RemoveFromBookings.do";
  }

  function exportToFile() {
      document.code_image.src = "SaveBibFile.do";
  }

  function calendarClick(element, calendarId, formFieldName, doSubmit){

  for (var i = 2; i < 8; i++)
    {
    for (var k = 0; k < element.parentNode.parentNode.rows[i].cells.length; k++)
    {

    if(element.parentNode.parentNode.rows[i].cells[k].className == 'calTdOn')
        {
        element.parentNode.parentNode.rows[i].cells[k].className = 'calTdOff';
        }

    }



    }

    element.className = 'calTdOn';
    eval(formFieldName + '.value = element.id;');
    window.document.forms[0].clickedCalendar.value = calendarId;


    if(doSubmit){
        window.document.forms[0].submit();
    }else{
        if(calendarId == 'calId1'){
            window.document.forms[0].startTime.value = openTimes[element.cellIndex];

        }
        if(calendarId == 'calId2'){
            window.document.forms[0].endTime.value = closeTimes[element.cellIndex];
            
        }
    }




  }




   function calendarNext(calendarId){
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    window.location.href=sPage + '?next=1&cal='+ calendarId;

    }

    function calendarPrev(calendarId){
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    window.location.href=sPage + '?prev=1&cal='+ calendarId;

    }


    function setFocus(elementId){
    if (document.getElementById(elementId) != null) {
        document.getElementById(elementId).focus();

    }

    }

    function checkCheck(checkboxName){
        var ret = false;

       alert(eval(checkboxName.length));

        if (checkboxName != null){
                if(checkboxName.length == 'undefined'){
                   return checkboxName.checked;

                }else{
                    for (i = 0; i < checkboxName.length; i++){
                        if(checkboxName[i].checked == true){
                            return true;
                        }

                    }//for
                }//if else

        }//if not null

        return ret;



    }

    function checkAndConfirm(elementId, errorMsg, confirmMsg){
        if(checkCheck(elementId) == true){
            return confirmSubmit(confirmMsg);

        }else{
            alert(errorMsg);
            return false;

        }


    }


    function confirmSubmit(msg)
    {

    var agree=confirm(msg);
    if (agree)
    	return true ;
    else
    	return false ;
    }

    var holdWindow = null;
    var newBookingWindow = null;
    var bookCoverWindow = null;
    var stateStandardsSearchLimit = null;
    var printWindow = null;
    var printOptionsWindow = null;

    function openHoldWindow(link){
        holdWindow = window.open(link ,'PlaceHold','toolbar=no,statusbar=no,width=420,height=300,scrollbars=yes');
    }

   function openBookingWindow(link, params){
        if(!params)
            params = 'toolbar=no,statusbar=no,scrollbars=yes,width=520,height=530';
        newBookingWindow = window.open(link,'NewBooking',params);
   }

  function openPreviewWindow(i) {
      if(i == 0) {
          newWindow = window.open("PreviewEbook.do");
      } else {
          newWindow = window.open("PreviewEbook.do",null,"resizable=yes,toolbar=no,width=420,height=300,scrollbars=yes");
      }
  }

  function openEbookCheckoutWindow(copyOid) {
      newWindow = window.open("CheckoutEbook.do?copyOid="+copyOid,null,"resizable=yes,toolbar=no,width=420,height=300,scrollbars=yes");      

  }

 function closeApp(){
    if(holdWindow && holdWindow.open && !holdWindow.closed) holdWindow.close();
    if(newBookingWindow && newBookingWindow.open && !newBookingWindow.closed) newBookingWindow.close();
    if(bookCoverWindow && bookCoverWindow.open && !bookCoverWindow.closed) bookCoverWindow.close();
    if(stateStandardsSearchLimit && stateStandardsSearchLimit.open && !stateStandardsSearchLimit.closed) stateStandardsSearchLimit.close();
    if(printOptionsWindow && printOptionsWindow.open && !printOptionsWindow.closed) printOptionsWindow.close();
    if(printWindow && printWindow.open && !printWindow.closed) printWindow.close();
   }

 function openPrintOptionsWindow(link){
        var params;
        params = 'toolbar=no,statusbar=yes,scrollbars=yes,width=450,height=440';
        printOptionsWindow = window.open(link,'PrintList',params);
 }

 function openPrintWindow(link){
         printWindow = window.open(link,'PrintList');
         return true;
  }




 function openCoverWindow(link){
        bookCoverWindow = window.open(link,'BookCover','toolbar=no,statusbar=no,scrollbars=yes,width=500,height=500,resizable=yes');
   }


 function showBookCoverError(error, image){
    var divRef = document.getElementById(error);
    var imgRef = document.getElementById(image);
    divRef.style.visibility='visible';
    imgRef.style.visibility='hidden';

  }



 function anySelected(elementName){
   var groupCounter;
   var groupCheckBoxes = document.forms[0].elements[elementName];
   if(groupCheckBoxes.length){
         for(groupCounter = 0; groupCounter < groupCheckBoxes.length; groupCounter++){
            if(groupCheckBoxes[groupCounter].checked == true){ return true;}//if
            }//for
   }else{
        if(groupCheckBoxes.checked == true){ return true; }

   }


   return false;
 }


   function openMultiCopyBookingWindow (sourceElementName, destElementName, params){

    var groupCounter;
    var groupCheckBoxesSource = document.forms[0].elements[sourceElementName];
    var groupCheckBoxesDest = document.forms[0].elements[destElementName];
    var queryParams = "edit&items=";

    if(groupCheckBoxesSource.length){
         for(groupCounter = 0; groupCounter < groupCheckBoxesSource.length; groupCounter++){
            if(groupCheckBoxesSource[groupCounter].checked == true){queryParams=queryParams+groupCheckBoxesDest[groupCounter].value+"|"+groupCheckBoxesSource[groupCounter].value; queryParams=queryParams+","; }//if
            }//for
    }else{
        if(groupCheckBoxesSource.checked == true){ queryParams=queryParams + groupCheckBoxesDest.value+"|"+groupCheckBoxesSource.value; queryParams=queryParams+",";  }

    }

    var bookingLink = "BookItem.do?" + queryParams;
    openBookingWindow(bookingLink, params);




  }




 function openLimitWindow(link){
        stateStandardsSearchLimit = window.open(link,'StateStandardsSearchLimit','toolbar=no,statusbar=no,scrollbars=yes,width=380,height=190');
   }













