/* Request functions
 * These functions support the devotional cal request entry window
 * Author: Bill Roberts
 * Date: July, 2007
 */
 
var buttonClicked = false; /* initialize */

/* check for that travesty of a browser, IE 6 */
function isIE6( ) {
   var isAwfulBrowser = false;

   var appVer = navigator.appVersion.toLowerCase();
   var iePos = appVer.indexOf('msie');
   if (iePos !=-1) {
      var is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)));
      var is_major = parseInt(is_minor);
   }

   if (navigator.appName.substring(0,9) == "Microsoft") { // Check if IE version is 6 or older
      isAwfulBrowser = (is_major <= 6)
   }

   return isAwfulBrowser;
}


function preventMultiClick( ) {
/* prevents multiple clicks of a button */
   if ( buttonClicked ) {
      return false;
   }
   else {
      buttonClicked = true;
   }
   return true;
}

var win; /* thie variable holds the popup window for ajax lookups */
var inLookup = false;
var nextField;

/* Function called to lookup spin numbers, and the recipient's name */
function getMatchingPerson( pNextField ) {
   /* if in a lookup, just return */
   if ( inLookup ) {
      return;
   }
   
   requestForm = document.getElementById( "request_form" );

   id = requestForm.recipient_id.value;
      
   if ( id > 0 ) { /* don't match if we already have a match */
      return;
   }
   
   /* if the next field is facility id, and the spin field is blank, ignore */
   if ( pNextField == "recipient_facility_id" && requestForm.recipient_spin.value == "" ) { 
      return;
   }
      
   /* set the next field global so it will know what field to go to next */
   nextField = pNextField;
         
   spin = requestForm.recipient_spin.value;
   firstName = requestForm.recipient_first_name.value;
   lastName = requestForm.recipient_last_name.value;
   
   if ( spin > "" || (firstName > "" && lastName > "" ) ) {
      http = createRequestObject();
      http.open('get', 'ajax/spin_lookup.php?spin=' + encodeURI( spin ) + '&id=' + encodeURI( id ) + 
                       '&first_name=' + encodeURI( firstName ) + '&last_name=' + encodeURI( lastName ) );

      /* Define a function to call once a response has been received. This will be our 
         handleSpinLookup function that we define below. */
      http.onreadystatechange = handleSpinLookup;
    
      /* Send the data. We use something other than null when we are sending using the POST method. */
      http.send( null );

   }
}

/* handles the results of the spin or name lookup */
function handleSpinLookup( ) {
   /* Make sure that the transaction has finished. The XMLHttpRequest object 
      has a property called readyState with several states:
      0: Uninitialized
      1: Loading
      2: Loaded
      3: Interactive
      4: Finished */
      
   if ( http.readyState == 4 ) { /* Finished loading the response */
      /* We have got the response from the server-side script,
         let's see just what it was. using the responseText property of 
         the XMLHttpRequest object. */
      var response = http.responseText;
      
      /* And now we want to popup a window containing this list */
      if ( response > "" ) {
         win = new PopupWindow( 'spindiv' );
         inLookup = true; /* set a flag so that we won't do multiple lookups at once */
         
         contents = "<div style='border: solid 1px #000000; padding: 10px;'>" +
                    "<strong>Matching ID #(s) or Name(s) found:</strong><br />" +
                    "<i>Please select match or press \"don't select\" if this is not a duplicate</i><br /><ul>";

                    
         var spinAr = response.split( '~' );
         
         for ( i = 0; i < spinAr.length; i = i + 20 ) {

            contents = contents + "<li>" +
                                  "<a href='' onClick='setSpinFields( \"" + escape( spinAr[ i ] ) + "\", " +
                                                                     "\"" + escape( spinAr[ i+1 ] ) + "\", " +
                                                                     "\"" + escape( spinAr[ i+2 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+3 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+4 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+5 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+6 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+7 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+8 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+9 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+10 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+11 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+12 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+13 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+14 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+15 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+16 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+17 ] ) + "\", " + 
                                                                     "\"" + escape( spinAr[ i+19 ] ) + "\" " + 
                                                                    "); return false;'>" + 
                                  spinAr[ i+14 ] + " - " +
                                  spinAr[ i+1 ] + " " +
                                  spinAr[ i+2 ] + " " +                                  
                                  spinAr[ i+3 ] + " " +                                  
                                  spinAr[ i+15 ] + ", " +                                  
                                  spinAr[ i+7 ] + " " +                                  
                                  spinAr[ i+8 ] + ", " +
                                  spinAr[ i+9 ] + ", " +
                                  spinAr[ i+10 ] + " " +
                                  spinAr[ i+11 ] + ", last request: " + spinAr[ i+18 ] + "</a></li>";                                                                                                     
                                  
         }
         
         contents = contents + "</ul><a href='' onClick='clearLookupWindow( ); setFocus( \"" + nextField + "\" ); return false;'>don't select</a></td></tr></div>";
         win.populate( contents ); 
         win.offsetY = 10;

         /* hide the facilities and state drop-downs in IE */
         if ( isIE6( ) ) {
            requestForm = document.getElementById( "request_form" );
            requestForm.recipient_facility_id.style.visibility = 'hidden';
            requestForm.state.style.visibility = 'hidden';
         }

         win.showPopup( 'spinanchor' ); 
       }   
   }
}

function setFocus( focusField ) {
   requestForm = document.getElementById( "request_form" );
   switch( focusField ) {
      case "recipient_spin" :
         requestForm.recipient_spin.focus( );      
         break;
      case "recipient_facility_id" :
         requestForm.recipient_facility_id.focus( );      
         break;
      case "recipient_dorm" :
         requestForm.recipient_dorm.focus( );      
         break;
      case "request_in_touch_flag" :
         requestForm.request_in_touch_flag.focus( );      
         break;         
   }
}

function setSpinFields( recipientId, firstName, middleInitial, lastName, facilityId, useFacilityAddressFlag, 
                        addressId, address01, address02, city, state, zipCode, phone, dorm, spin, suffix,
                        addressEffectiveFromDate, recipientEffectiveFromDate, devotionalSendDisallowedFlag ) {

   requestForm = document.getElementById( "request_form" );
   requestForm.recipient_id.value = unescape( recipientId );
   requestForm.recipient_first_name.value = unescape( firstName );
   requestForm.recipient_middle_initial.value = unescape( middleInitial );   
   requestForm.recipient_last_name.value = unescape( lastName );
   requestForm.recipient_suffix.value = unescape( suffix );
   requestForm.recipient_facility_id.value = unescape( facilityId );
   if ( unescape( useFacilityAddressFlag ) == "Y" ) {
      requestForm.recipient_use_facility_address_flag.checked = true;
      disableRecipientAddress( );
   }
   else {
      requestForm.recipient_use_facility_address_flag.checked = false;
      enableRecipientAddress( );
   }
   requestForm.address_id.value = unescape( addressId );
   requestForm.address_01.value = unescape( address01 );
   requestForm.address_02.value = unescape( address02 );
   requestForm.city.value = unescape( city );
   requestForm.state.value = unescape( state );
   requestForm.zip_code.value = unescape( zipCode );
   requestForm.recipient_dorm.value = unescape( dorm );
   requestForm.recipient_spin.value = unescape( spin );
   requestForm.address_effective_from_date.value = unescape( addressEffectiveFromDate );
   requestForm.recipient_effective_from_date.value = unescape( recipientEffectiveFromDate );
   requestForm.devotional_send_disallowed_flag.value = unescape( devotionalSendDisallowedFlag );

   setNoSpiralCheckBox( );

   setNoDevotionalCheckBox( );

   setFieldClasses( );
   
   clearLookupWindow( );

   setFocus( "request_in_touch_flag" );   
}

function setAddressFacilityFields( addressId, address01, address02, city, state, zipCode, facilityId, facilityName, addressEffectiveFromDate, devotionalSendDisallowedFlag ) {
   requestForm = document.getElementById( "request_form" );
   requestForm.address_id.value = unescape( addressId );
   requestForm.address_01.value = unescape( address01 );
   requestForm.address_02.value = unescape( address02 );
   requestForm.city.value = unescape( city );
   requestForm.state.value = unescape( state );
   requestForm.zip_code.value = unescape( zipCode );
   requestForm.recipient_facility_id.value = unescape( facilityId );
   requestForm.address_effective_from_date.value = unescape( addressEffectiveFromDate );
   requestForm.devotional_send_disallowed_flag.value = unescape( devotionalSendDisallowedFlag );
   
   if ( facilityId > 0 ) {
      requestForm.recipient_use_facility_address_flag.checked = true;
      disableRecipientAddress( );
      requestForm.request_no_spiral_flag.checked = true;
   }
   else {
      requestForm.recipient_use_facility_address_flag.checked = false;
      enableRecipientAddress( );
      requestForm.request_no_spiral_flag.checked = false;
   }

   setNoDevotionalCheckBox( );
   
   clearLookupWindow( );
   setFocus( "recipient_dorm" );    
}

function clearLookupWindow( ) {
   win.hidePopup( );

   /* turn selects back on if they've been hidden */
   if ( isIE6( ) ) {
      requestForm = document.getElementById( "request_form" );
      if ( requestForm.recipient_facility_id != null ) {
         if ( requestForm.recipient_facility_id.style.visibility == "hidden" ) {
            requestForm.recipient_facility_id.style.visibility = 'visible';
         }
      }
      if ( requestForm.state != null ) {
         if ( requestForm.state.style.visibility == "hidden" ) {
            requestForm.state.style.visibility = 'visible';
         }
      }
   }

   inLookup = false;
}

/* functions to enable/disable/clear recipient address */
function enableRecipientAddress( ) {
   document.getElementById( "request_form" ).address_01.readOnly = false;      
   document.getElementById( "request_form" ).address_02.readOnly = false;    
   document.getElementById( "request_form" ).city.readOnly = false;    
   document.getElementById( "request_form" ).state.readOnly = false; 
   document.getElementById( "request_form" ).zip_code.readOnly = false;

   setFieldClasses( );
      
   /* because recipient address is being enabled, need to zero the address id field, so we don't overwrite an existing address */
   document.getElementById( "request_form" ).address_id.value = 0;           
   
}

function disableRecipientAddress( ) {
   document.getElementById( "request_form" ).address_01.readOnly = true;
   document.getElementById( "request_form" ).address_01.className = "disabled";
   document.getElementById( "request_form" ).address_01.readOnly = true;
   document.getElementById( "request_form" ).address_02.className = "disabled";
   document.getElementById( "request_form" ).address_02.readOnly = true;    
   document.getElementById( "request_form" ).city.readOnly = true;    
   document.getElementById( "request_form" ).city.className = "disabled";   
   document.getElementById( "request_form" ).state.readOnly = true;   
   document.getElementById( "request_form" ).state.className = "disabled";    
   document.getElementById( "request_form" ).zip_code.readOnly = true;  
   document.getElementById( "request_form" ).zip_code.className = "disabled";     
}

function clearRecipientAddress( ) {
   document.getElementById( "request_form" ).address_01.value = "";      
   document.getElementById( "request_form" ).address_02.value = "";    
   document.getElementById( "request_form" ).city.value = "";    
   document.getElementById( "request_form" ).state.value = ""; 
   document.getElementById( "request_form" ).zip_code.value = "";
}

/* this function dynamically adds a facility to the facility dropdown */
function addFacilityToDropdown( name, id ) {
   /* locate in facilities list where this one should be inserted */
   len = document.getElementById( "request_form" ).recipient_facility_id.options.length;
   
   for ( i = 1; i < len; i++ ) {
      if ( document.getElementById( "request_form" ).recipient_facility_id.options[i].text > name ) {
         break;
      }
   }

   /* create a space for the new facility */
   for ( j = len; j >= i; j-- ) {
      shiftName = document.getElementById( "request_form" ).recipient_facility_id.options[j-1].text;
      shiftId = document.getElementById( "request_form" ).recipient_facility_id.options[j-1].value;
      document.getElementById( "request_form" ).recipient_facility_id.options[j] = new Option( shiftName, shiftId );
   }
   
   document.getElementById( "request_form" ).recipient_facility_id.options[i] = new Option( name, id );
   document.getElementById( "request_form" ).recipient_facility_id.selectedIndex = i;
   
   /* set the "use facility address flag" checkbox to Checked and do an address lookup */
   document.getElementById( "request_form" ).recipient_use_facility_address_flag.checked = false; 
   document.getElementById( "request_form" ).recipient_facility_id.value = id;
   document.getElementById('request_form').recipient_facility_id.onchange( );
}

/* Handles the results of the facility lookup */
function handleFacilityLookup( ) {
   /* Make sure that the transaction has finished. The XMLHttpRequest object 
      has a property called readyState with several states:
      0: Uninitialized
      1: Loading
      2: Loaded
      3: Interactive
      4: Finished */
      
   if ( http.readyState == 4 ) { /* Finished loading the response */
   
      /* We have got the response from the server-side script,
         let's see just what it was. using the responseText property of 
         the XMLHttpRequest object. */
      var response = http.responseText;
      
      /* parse the response text and store it in the fields */
      var addressAr = response.split( '~' );
      requestForm = document.getElementById( "request_form" );
      requestForm.address_id.value = addressAr[ 0 ];       
      requestForm.address_01.value = addressAr[ 1 ];      
      requestForm.address_02.value = addressAr[ 2 ];      
      requestForm.city.value = addressAr[ 3 ];      
      requestForm.state.value = addressAr[ 4 ];      
      requestForm.zip_code.value = addressAr[ 5 ];   
      requestForm.address_effective_from_date.value = addressAr[ 6 ];   
      requestForm.devotional_send_disallowed_flag.value = addressAr[ 7 ];   
      disableRecipientAddress( );
      setNoDevotionalCheckBox( );
   }
}

/* Function called to lookup facility addresses */
function getFacilityAddress( ) {
   /* if in a lookup, just return */
   if ( inLookup ) {
      return;
   }

   requestFormId = document.getElementById( "request_form" ); 

   /* if the facility is 0, make sure the use facility address flag is unchecked */
   if ( requestFormId.recipient_facility_id.value == 0 ) {
      requestFormId.recipient_use_facility_address_flag.checked = false;
      checkFacilityAddressFlagChanged( ); /* will clear out the address id */
   }

   /* first - check to see if the check box has been selected. If not, ignore */
   if ( requestFormId.recipient_use_facility_address_flag.checked && 
        requestFormId.recipient_facility_id.value > 0 ) { 

      http = createRequestObject();

      http.open('get', 'ajax/facility_lookup.php?facility_id=' + requestFormId.recipient_facility_id.value, true );

      http.onreadystatechange = handleFacilityLookup;
    
      http.send( null );
   }
   else {
   
      /* enable all address fields */
      enableRecipientAddress( );
   }
}

/* sets the no spiral checkbox to true if a facility is chosen */
function setNoSpiralCheckBox( ) {
   if ( document.getElementById( "request_form" ).recipient_facility_id.value > 0 ) { 
      document.getElementById( "request_form" ).request_no_spiral_flag.checked = true;
   }
   else {
      document.getElementById( "request_form" ).request_no_spiral_flag.checked = false;
   }   
}

/* sets the no devotional checkbox to true if a facility has devotionals disallowed */
function setNoDevotionalCheckBox( ) {
   if ( document.getElementById( "request_form" ).devotional_send_disallowed_flag.value == "Y" ) { 
      document.getElementById( "request_form" ).request_no_devotional_flag.checked = true;
   }
   else {
      document.getElementById( "request_form" ).request_no_devotional_flag.checked = false;
   }   
}

/* displays a warning if the No Devotional checkbox is set to false when the 
   devotional_send_disallowed_flag is set to Y */
function checkNoDevotionalValue( ) {
   if ( document.getElementById( "request_form" ).devotional_send_disallowed_flag.value == "Y" 
     && !document.getElementById( "request_form" ).request_no_devotional_flag.checked  ) { 
      alert( "Warning: the current facility has requested that no devotionals be sent to it. " +
             "You cannot uncheck the No Devotional checkbox." );
      document.getElementById( "request_form" ).request_no_devotional_flag.checked = true;
   }
}

/* Function called to lookup addresses */
function getAddress( ) {
   /* if in a lookup, just return */
   if ( inLookup ) {
      return;
   }

   /* only do the lookup if address 1 or a zip is ready, and if we haven't already figured out the address or facility */
   if ( ( document.getElementById( "request_form" ).address_01.value > "" || 
          document.getElementById( "request_form" ).zip_code.value > "" )
        &&
        ( document.getElementById( "request_form" ).recipient_facility_id.value == 0 &&
          document.getElementById( "request_form" ).address_id.value == 0 ) ) { 

      http = createRequestObject();

      address01 = encodeURI(document.getElementById( "request_form" ).address_01.value);
      zipCode = encodeURI(document.getElementById( "request_form" ).zip_code.value);
      http.open('get', 'ajax/address_lookup.php?address_01=' + address01 + '&zip_code=' + zipCode , true );

      http.onreadystatechange = handleAddressLookup;
    
      http.send( null );
   }
}

/* handles the results of the address lookup */
function handleAddressLookup( ) {

   /* Make sure that the transaction has finished. The XMLHttpRequest object 
      has a property called readyState with several states:
      0: Uninitialized
      1: Loading
      2: Loaded
      3: Interactive
      4: Finished */
      
   if ( http.readyState == 4 ) { /* Finished loading the response */
      /* We have got the response from the server-side script,
         let's see just what it was. using the responseText property of 
         the XMLHttpRequest object. */
      var response = http.responseText;

      /* And now we want to popup a window containing this list */
      if ( response > "" ) {
         win = new PopupWindow( 'spindiv' );
         inLookup = true; /* set a flag so that we won't do multiple lookups at once */
         
         contents = "<div style='border: solid 1px #000000; padding: 10px;'>" +
                    "<strong>Possible Facility Address Match(es) found:</strong><br />" +
                    "<i>Please select match or press \"don't select\" if this is not a match</i><br /><ul>";
          
         var addrAr = response.split( '~' );
         
         for ( i = 0; i < addrAr.length; i = i + 10 ) {

            contents = contents + "<li>" +
                                  "<a href='' onClick='setAddressFacilityFields( \"" + escape( addrAr[ i ] ) + "\", " +
                                                                     "\"" + escape( addrAr[ i+1 ] ) + "\", " +
                                                                     "\"" + escape( addrAr[ i+2 ] ) + "\", " + 
                                                                     "\"" + escape( addrAr[ i+3 ] ) + "\", " + 
                                                                     "\"" + escape( addrAr[ i+4 ] ) + "\", " + 
                                                                     "\"" + escape( addrAr[ i+5 ] ) + "\", " + 
                                                                     "\"" + escape( addrAr[ i+6 ] ) + "\", " + 
                                                                     "\"" + escape( addrAr[ i+7 ] ) + "\", " + 
                                                                     "\"" + escape( addrAr[ i+8 ] ) + "\", " +  
                                                                     "\"" + escape( addrAr[ i+9 ] ) + "\" " +                                                                                                                                             
                                                                    "); return false;'>" +
                                  addrAr[ i+7 ] + " - " +
                                  addrAr[ i+1 ] + " " +
                                  addrAr[ i+2 ] + " " +                                  
                                  addrAr[ i+3 ] + ", " +                                  
                                  addrAr[ i+4 ] + " " +                                  
                                  addrAr[ i+5 ] +
                                  "</a></li>";                                                                                                     
                                  
         }
         
         contents = contents + "</ul><a href='' onClick='clearLookupWindow( ); return false;'>don't select</a></td></tr></div>";
         win.populate( contents ); 
         win.offsetY = 10; 

         /* hide the state drop-down in IE */
         if ( isIE6( ) ) {
            requestForm = document.getElementById( "request_form" );
            requestForm.state.style.visibility = 'hidden';
         }
         win.showPopup( 'addranchor' ); 
       }   
   }
}

function setFieldClasses( ) {
   /* sets the css class on fields based on field values */
   requestForm = document.getElementById( "request_form" ); 

   recipientId = requestForm.recipient_id.value;
   if ( recipientId > 0 ) {
      requestForm.recipient_first_name.className = "existing";
      requestForm.recipient_middle_initial.className = "existing";
      requestForm.recipient_last_name.className = "existing";
      requestForm.recipient_suffix.className = "existing";
      requestForm.recipient_spin.className = "existing";
      requestForm.recipient_facility_id.className = "existing";
      requestForm.recipient_dorm.className = "existing";
      requestForm.recipient_suffix.className = "existing"
   }
   else {
      requestForm.recipient_first_name.className = "new";
      requestForm.recipient_middle_initial.className = "new";
      requestForm.recipient_last_name.className = "new"; 
      requestForm.recipient_suffix.className = "new"; 
      requestForm.recipient_spin.className = "new";
      requestForm.recipient_facility_id.className = "new";
      requestForm.recipient_dorm.className = "new";          
      requestForm.recipient_suffix.className = "new";          
   }
   
   addressId = requestForm.address_id.value;
   facilityId = requestForm.recipient_facility_id.value;
   useFacilityAddressFlag = requestForm.recipient_use_facility_address_flag.checked;
   if ( addressId > 0 && ( facilityId == 0 || !useFacilityAddressFlag ) ) {
      requestForm.address_01.className = "existing";
      requestForm.address_02.className = "existing";
      requestForm.city.className = "existing";
      requestForm.state.className = "existing";
 	  requestForm.zip_code.className = "existing";
   }
   else {
      if ( facilityId > 0 && useFacilityAddressFlag ) {
         disableRecipientAddress( );
      }
      else {
         requestForm.address_01.className = "new";
         requestForm.address_02.className = "new";
         requestForm.city.className = "new";
         requestForm.state.className = "new";
         requestForm.zip_code.className = "new";
      }
   }
}

/* simple function - just checks to see if the recipient_id is already set. If 
   it is, just display a quick alert notifying the user. Only do this once */

/* NOTE: TURNED THIS OFF BR 9/20/2007 - set to false below to get the alert to show up at least
   once. By setting it to true I've made sure it never pops up */
var recipientChangeWarned = true;

function checkRecipientChange( ) {
   if ( recipientChangeWarned ) {
      return;
   }
   
   requestForm = document.getElementById( "request_form" );    
   recipientId = requestForm.recipient_id.value;

   /* note: if this is a "single" request - i.e., one being edited within the 
      Search tab, just return */
   if ( requestForm.single ) {
      return;
   }
   
   if ( recipientId > 0 ) {
      alert( "Note: you are changing data for a recipient that already exists and has other requests. " +
             "If this isn't wnat you want, please cancel your changes. Otherwise, please continue." );
      recipientChangeWarned = true;
   }
}

function tabPastAddressIfChecked( ) {
   /* if the use facility address flag just got checked, tab past the address field */
   requestForm = document.getElementById( "request_form" );    
   useFacilityAddressFlag = requestForm.recipient_use_facility_address_flag.checked;
   recipientId = requestForm.recipient_id.value;

   if ( useFacilityAddressFlag && recipientId == 0 ) { /* recipient not setup yet, go to dorm */
      setFocus( "recipient_dorm" );
   }
   else {
      setFocus( "request_in_touch_flag" ); /* otherwise, blow down to the in touch flag */
   }
}

/* if the facility flag changes to false, clear the address id out */
function checkFacilityAddressFlagChanged( ) {
   requestForm = document.getElementById( "request_form" );      
   if ( !requestForm.recipient_use_facility_address_flag.checked ) { /* was changed to not-checked */
      requestForm.address_id.value = 0;
   }
}

/* initializes the search form */
function initializeSearchForm( ) {
   searchForm = document.getElementById( "search_form" );

   searchForm.date_range_type[ 0 ].checked = true;
   searchForm.date_range_start.value = "";
   searchForm.date_range_end.value = "";
/* qualifier fields hidden now, per RAK, 9/5/2007 */
/*   searchForm.first_name_qualifier[ 0 ].checked = true; */
   searchForm.first_name_pattern.value = "";
/*   searchForm.last_name_qualifier[ 2 ].checked = true; */
   searchForm.last_name_pattern.value = "";
   searchForm.recipient_spin_value.value = "";
   searchForm.state.selectedIndex = 0;
   searchForm.facility_id.selectedIndex = 0;
   searchForm.mailing_type_id.selectedIndex = 0;
   searchForm.first_name_pattern.focus( );
}

/* Function called to lookup matching text for facilities (matching alias, facility name, etc */
function getMatchingFacility( ) {
   requestForm = document.getElementById( "request_form" );

   matchString = requestForm.facility_match_string.value;
      
   if ( matchString > "" ) {
      http = createRequestObject();
      http.open('get', 'ajax/facility_match.php?match_string=' + encodeURI( matchString ) );

      /* Define a function to call once a response has been received. This will be our 
         handleSpinLookup function that we define below. */
      http.onreadystatechange = handleFacilityMatchLookup;
    
      /* Send the data. We use something other than null when we are sending using the POST method. */
      http.send( null );
   }
}

/* Handles the results of the facility match lookup */
function handleFacilityMatchLookup( ) {
   /* Make sure that the transaction has finished. The XMLHttpRequest object 
      has a property called readyState with several states:
      0: Uninitialized
      1: Loading
      2: Loaded
      3: Interactive
      4: Finished */
      
   if ( http.readyState == 4 ) { /* Finished loading the response */
   
      /* We have got the response from the server-side script,
         let's see just what it was. using the responseText property of 
         the XMLHttpRequest object. */
      var response = http.responseText;
      
      if ( response > "" ) {
         win = new PopupWindow( 'spindiv' );
         inLookup = true; /* set a flag so that we won't do multiple lookups at once */
         
         contents = "<div style='border: solid 1px #000000; padding: 10px;'>" +
                    "<strong>Possible Facility Match(es) found:</strong><br />" +
                    "<i>Please select match or press \"don't select\" if this is not a match</i><br /><ul>";
          
         var facAr = response.split( '~' );

         
         for ( i = 0; i < facAr.length; i = i + 10 ) {

            contents = contents + "<li>" +
                                  "<a href='' onClick='setFacility(  \"" + escape( facAr[ i ] ) + "\", " +
                                                                     "\"" + escape( facAr[ i+2 ] ) + "\", " + 
                                                                     "\"" + escape( facAr[ i+3 ] ) + "\", " + 
                                                                     "\"" + escape( facAr[ i+4 ] ) + "\", " + 
                                                                     "\"" + escape( facAr[ i+5 ] ) + "\", " + 
                                                                     "\"" + escape( facAr[ i+6 ] ) + "\", " + 
                                                                     "\"" + escape( facAr[ i+7 ] ) + "\", " + 
                                                                     "\"" + escape( facAr[ i+8 ] ) + "\", " + 
                                                                     "\"" + escape( facAr[ i+9 ] ) + "\" ); return false;'>" +
                                  facAr[ i+1 ] + ", " +
                                  facAr[ i+5 ] + ", " +                                  
                                  facAr[ i+6 ] + "  " +                                  
                                  facAr[ i+7 ] +                                  
                                  "</a></li>";                                                                                                     
         }
         
         contents = contents + "</ul><a href='' onClick='clearLookupWindow( ); return false;'>don't select</a></td></tr></div>";
         win.populate( contents ); 
         win.offsetY = 10;

         /* hide the state drop-down in IE */
         if ( isIE6( ) ) {
            requestForm = document.getElementById( "request_form" );
            requestForm.state.style.visibility = 'hidden';
         }

         win.showPopup( 'addranchor' ); 
       }   
   }
}

function setFacility( facilityId, addressId, address01, address02, city, state, zipCode
                     ,addressEffectiveFromDate, devotionalSendDisallowedFlag ) {

   requestForm = document.getElementById( "request_form" );
   requestForm.recipient_facility_id.value = unescape( facilityId );
   requestForm.recipient_use_facility_address_flag.checked = true;
   disableRecipientAddress( );
   requestForm.address_id.value = unescape( addressId );
   requestForm.address_01.value = unescape( address01 );
   requestForm.address_02.value = unescape( address02 );
   requestForm.city.value = unescape( city );
   requestForm.state.value = unescape( state );
   requestForm.zip_code.value = unescape( zipCode );
   requestForm.address_effective_from_date.value = unescape( addressEffectiveFromDate );
   requestForm.devotional_send_disallowed_flag.value = unescape( devotionalSendDisallowedFlag );
   setNoSpiralCheckBox( );
   setNoDevotionalCheckBox( );

   setFieldClasses( );
   
   clearLookupWindow( );
}

/* Function called to lookup a friend, based on match text entered for the friend */
function getMatchingFriend( pNextField ) {
   /* if in a lookup, just return */
   if ( inLookup ) {
      return;
   }
   
   requestForm = document.getElementById( "request_form" );

   id = requestForm.requesting_friend_id.value;
      
   /* set the next field global so it will know what field to go to next */
   nextField = pNextField;
         
   friendName = requestForm.requesting_friend_match_string.value;

   if ( friendName > "" ) {
      http = createRequestObject();
      http.open('get', 'ajax/friend_lookup.php?friendName=' + encodeURI( friendName ) );

      /* Define a function to call once a response has been received. This will be our 
         handleFriendLookup function that we define below. */
      http.onreadystatechange = handleFriendLookup;
    
      /* Send the data. We use something other than null when we are sending using the POST method. */
      http.send( null );

   }
}

/* handles the results of the friend lookup */
function handleFriendLookup( ) {
   /* Make sure that the transaction has finished. The XMLHttpRequest object 
      has a property called readyState with several states:
      0: Uninitialized
      1: Loading
      2: Loaded
      3: Interactive
      4: Finished */
      
   if ( http.readyState == 4 ) { /* Finished loading the response */
      /* We have got the response from the server-side script,
         let's see just what it was. using the responseText property of 
         the XMLHttpRequest object. */
      var response = http.responseText;
      
      /* And now we want to popup a window containing this list */
      if ( response > "" ) {
         win = new PopupWindow( 'spindiv' );
         inLookup = true; /* set a flag so that we won't do multiple lookups at once */
         
         contents = "<div style='border: solid 1px #000000; padding: 10px;'>" +
                    "<strong>Matching Name(s) found:</strong><br />" +
                    "<i>Please select match or press \"don't select\" if this is not a duplicate</i><br /><ul>";

                    
         var friendAr = response.split( '~' );
         
         for ( i = 0; i < friendAr.length; i = i + 7 ) {

            contents = contents + "<li>" +
                                  "<a href='' onClick='setFriendFields( \"" + escape( friendAr[ i ] ) + "\", " +
                                                                     "\"" + escape( friendAr[ i+1 ] ) + "\" " +
                                                                    "); return false;'>" + 
                                  friendAr[ i+1 ] + ", " +
                                  friendAr[ i+2 ] + " " +                                  
                                  friendAr[ i+3 ] + ", " +                                  
                                  friendAr[ i+4 ] + " " +                                  
                                  friendAr[ i+5 ] + ", last request: " + friendAr[ i+6 ] + "</a></li>";                                                                                                     
                                  
         }
         
         contents = contents + "</ul><a href='' onClick='clearLookupWindow( ); setFocus( \"" + nextField + "\" ); return false;'>don't select</a></td></tr></div>";
         win.populate( contents ); 
         win.offsetY = 10;
         win.showPopup( 'friendanchor' ); 
       }   
   }
}

function setFriendFields( recipientId, recipientName ) {

   requestForm = document.getElementById( "request_form" );
   requestForm.requesting_friend_id.value = unescape( recipientId );
   title = document.getElementById( "request_header" );
   type = title.innerHTML.substring( 0, 3 );
   if ( type == "Upd" ) {
      title.innerHTML = "Update Request";
   }
   else {
      title.innerHTML = "Add New Request";
   }

   title.innerHTML = title.innerHTML + " (Requested By Friend: " + unescape( recipientName ) + ") " +
                                       "<a href='' onclick='clearFriendFields( );" +
                                       " return false;' title='drop this friend link' alt='drop this friend link' >" +
                                       "<img src='images/b_drop.png' border=0></a>";
   
   if ( inLookup ) {
      clearLookupWindow( );
   }
}

function clearFriendFields( ) {
   requestForm = document.getElementById( "request_form" );
   requestForm.requesting_friend_id.value = "";
   title = document.getElementById( "request_header" );
   type = title.innerHTML.substring( 0, 3 );
   if ( type == "Upd" ) {
      title.innerHTML = "Update Request";
   }
   else {
      title.innerHTML = "Add New Request";
   }
}
