//-------------------------------------------------------------------------------------------
// Copyright (c) 2004 - 2006, Inmagic, Inc., Woburn, MA, USA. All rights reserved.
// This code is proprietary to Inmagic, Inc.
// 
// Product:   Inmagic Genie
// Version:   3.0
//
// File:      genie.js
// Function:  functions called by scripts in Genie pages
//-------------------------------------------------------------------------------------------
/*
Used in checkout page to ensure a borrower has been selected.
Called from a CustomValidation control.
*/
function validate_borrowerselection(source, args)
{
   if (document.getElementById("ShowBorrower").innerHTML != "")
   {
      args.IsValid = true;
   }
   else
   {
      args.IsValid = false;
   }
}

/*
Used in webforms to select other forms from a dropdown.
Option values contain actual form names, so option text is flexible.
*/
function dbtw_form_change_onvalue(select_object, action, params)
{
   //alert("enter");
   var new_form = escape( select_object.options[select_object.selectedIndex].value );
   var i = new_form.indexOf( "+" );
   while ( i >= 0 )
   {
      new_form = new_form.substring( 0, i ) +
                  "%2B" +
                  new_form.substring( i+1, new_form.length );
      i = new_form.indexOf( "+" );
   }

   var form_key = "RF=";
   var n1 = params.indexOf( form_key );
   var n2 = n1 + form_key.length;
   var n3 = params.indexOf( "&", n2 );
   var n4 = params.length;
   var form_change_url = action + "?AC=CHANGE_REPORT" +
                           params.substring( 0, n2 ) +
                           new_form +
                           params.substring( n3, n4 );
   window.location.href = form_change_url;
}
   
// For loans forms - shows copymgmt entry matching barcode on item loaned
function ShowCopy(CopyManagement, Barcode)
{
   var splitchar = "|split|"; //Put this in CopyManagement separator text
   var arrCopies = new Array();
   var i;
   var matchCopy = "";

   // split CopyManagement string if splitchar is found
   arrCopies = CopyManagement.split(splitchar);

   // loop through the array, snipping filenames
   for (i=0; i<arrCopies.length; i++)
   {
      if (arrCopies[i].indexOf(Barcode) != -1)
      {
         matchCopy = arrCopies[i];
         break;
      }
   }
   return matchCopy; 
}  

function DoSubmit()
{
   Window.event.returnvalue = true; 
}

function OverdueToggle(strLoanDateDue,strLoanDateReturned)
{
if (document.getElementById('QueryScreenControl1_OverdueLoanCheckBox').checked==true)
   {
      document.getElementById('QueryScreenControl1_QI'+strLoanDateReturned).value='=*';
      document.getElementById('QueryScreenControl1_QB'+strLoanDateReturned).value ='NOT';
      document.getElementById('QueryScreenControl1_QI'+strLoanDateDue).value='<@Date';
      document.getElementById('QueryScreenControl1_QB'+strLoanDateDue).value ='AND';
      if(  document.getElementById('QueryScreenControl1_PermanentLoanCheckBox')!=null)
      document.getElementById('QueryScreenControl1_PermanentLoanCheckBox').checked= false;
      document.getElementById('QueryScreenControl1_RF').value = "GenieOverdue";
   }  

  else
   {
      document.getElementById('QueryScreenControl1_RF').value = "GenieByTitle";
      document.getElementById('QueryScreenControl1_QI'+strLoanDateDue).value='';
      document.getElementById('QueryScreenControl1_QB'+strLoanDateDue).value ='AND';
       if (document.getElementById('QueryScreenControl1_ActiveLoanCheckBox')!=null)
         {
            if (document.getElementById('QueryScreenControl1_ActiveLoanCheckBox').checked == false)
            {
                  document.getElementById('QueryScreenControl1_QI'+strLoanDateReturned).value='';
                  document.getElementById('QueryScreenControl1_QB'+strLoanDateReturned).value ='AND';
            }
         }
   }
}


function PermanentToggle(strLoanDateDue,permID)
{
 if (document.getElementById('QueryScreenControl1_PermanentLoancheckBox').checked==true)
   {
      if( document.getElementById('QueryScreenControl1_OverdueLoanCheckBox')!=null)
         document.getElementById('QueryScreenControl1_OverdueLoanCheckBox').checked=false;
      document.getElementById('QueryScreenControl1_QI'+strLoanDateDue).value='';
      document.getElementById('QueryScreenControl1_QB'+strLoanDateDue).value ='AND';
      document.getElementById('QueryScreenControl1_QI'+permID).value='=Yes';
      
   }  
}
   
function ActiveToggle(strLoanDateDue,strLoanDateReturned)
{
  
   if (document.getElementById('QueryScreenControl1_ActiveLoanCheckBox').checked==true)
   {
      
      document.getElementById('QueryScreenControl1_QI'+strLoanDateReturned).value='=*';
      document.getElementById('QueryScreenControl1_QB'+strLoanDateReturned).value ='NOT';
   }
   else
   {
 
      document.getElementById('QueryScreenControl1_QI'+strLoanDateDue).value='';
  
      document.getElementById('QueryScreenControl1_QB'+strLoanDateDue).value ='AND';
  
      document.getElementById('QueryScreenControl1_QI'+strLoanDateReturned).value='';
 
      document.getElementById('QueryScreenControl1_QB'+strLoanDateReturned).value='AND';
    
      if(document.getElementById('QueryScreenControl1_OverdueLoanCheckBox')!=null)
         document.getElementById('QueryScreenControl1_OverdueLoanCheckBox').checked=false;
   }  
}


function GetBaseUrl(returntype)
{
   // alert("begin");
   // alert(dbtw_params);
   
   var bu;
   
   bu = dbtw_params.substring(dbtw_params.indexOf("BU"), dbtw_params.length+1);
   bu = bu.substring(0, bu.indexOf("&"));
   
   if (returntype == "full")
   {
      return bu;
   }
   else if (returntype == "justbase")
   {
      bu = bu.substring(bu.indexOf("BU=")+3, bu.lastIndexOf("%2F"));
      return bu;
   }
   
   // alert(bu);
   
}
   
/*
CalculateDueDate is used in loans_checkout - this is the older version
- assumes a dropdownlist is being used for LoanTerm
*/
function CalculateDueDate(CurrentDateTextboxID, LoanTermSelectID, DueDateTextboxID)
{
   var LoanTerm = document.getElementById(LoanTermSelectID).options[document.getElementById(LoanTermSelectID).selectedIndex].value;
   var CurrentDate = new Date();
   var DueDate = new Date();
   var DayInMilliseconds = 86400000;
   var MonthArray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                              "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

   if (LoanTerm != -1 && LoanTerm != 0)
   {
      LoanTerm *= DayInMilliseconds;
      CurrentDate.setTime(Date.parse(document.getElementById(CurrentDateTextboxID).value));
      DueDate.setTime(CurrentDate.getTime() + LoanTerm);
      //  document.getElementById(DueDateTextboxID).value = MonthArray[DueDate.getMonth()] + " " + DueDate.getDate() + ", " + DueDate.getFullYear();
      document.getElementById(DueDateTextboxID).value = DueDate.getDate() + " " +
                                                        MonthArray[DueDate.getMonth()] + " " + 
                                                        DueDate.getFullYear();
   }
   else if (LoanTerm == -1)
   {
      document.getElementById(DueDateTextboxID).value = "permanent loan";
   }
   else if (LoanTerm == 0)
   {
      document.getElementById(DueDateTextboxID).value = "";
   }
  
   // alert("loan term = " + LoanTerm + "\n" + "today = " + CurrentDate + "\ndue date = " + DueDate);
}

/*
Newer version of CalculateDueDate - for use with loans_checkout_new
- major difference is textbox for Loan Term instead of dropdownlist so permanent loans
must be handled with a "p" instead of "-1"
*/
function CalculateDueDate_new(CurrentDateTextboxID, LoanTermTextboxID, DueDateTextboxID)
{

   if (document.getElementById(LoanTermTextboxID).value != "" && document.getElementById(CurrentDateTextboxID).value != "")
   {
      var LoanTerm = document.getElementById(LoanTermTextboxID).value;
      var CurrentDate = new Date();
      var DueDate = new Date();
      var DayInMilliseconds = 86400000;
      var MonthArray = new Array();
      MonthArray[0] = "January";
      MonthArray[1] = "February";
      MonthArray[2] = "March";
      MonthArray[3] = "April";
      MonthArray[4] = "May";
      MonthArray[5] = "June";
      MonthArray[6] = "July";
      MonthArray[7] = "August";
      MonthArray[8] = "September";
      MonthArray[9] = "October";
      MonthArray[10] = "November";
      MonthArray[11] = "December";

      if (isNaN(LoanTerm) == false && LoanTerm > 0)
      {
         LoanTerm *= DayInMilliseconds;
         CurrentDate.setTime(Date.parse(document.getElementById(CurrentDateTextboxID).value));
         DueDate.setTime(CurrentDate.getTime() + LoanTerm);
         //document.getElementById(DueDateTextboxID).value = MonthArray[DueDate.getMonth()] + " " + DueDate.getDate() + ", " + DueDate.getFullYear();
         document.getElementById(DueDateTextboxID).value = DueDate.getFullYear() + "/" + (DueDate.getMonth()+1) + "/" + DueDate.getDate();
      }
      else if (LoanTerm == "p" || LoanTerm == "P")
      {
         document.getElementById(DueDateTextboxID).value = "";
      }
      else
      {
         document.getElementById(DueDateTextboxID).value = "";
      }
  
      //alert("loan term = " + LoanTerm + "\n" + "today = " + CurrentDate + "\ndue date = " + DueDate);
   }
   else if (document.getElementById(LoanTermTextboxID).value == "" || document.getElementById(CurrentDateTextboxID).value == "")
   {
      document.getElementById(DueDateTextboxID).value = "";
   }
}
function openWindow(url) 
{ 
  var newWin = window.open(url, '_blank'); 
} 

function OpenHelpWindow(helpId,htp)
{
    
    w=window.open('usercontrols/PopHelpForIE.aspx?hid=' + helpId+'&htp='+htp, '', "left=650,top=650,width=1,height=1,toolbar=0,status=0,menubar=0");
	w.blur();
	setTimeout("w.close()", 3000);
}

function resetAppVars()
{
    if (window.event.clientX < 0 && window.event.clientY < 0)
    {
       if(!((window.opener)&&(window.opener != window.self)))
       {
        window.location="logout.aspx";
       }
    }
}    