// ------------------------------------------------------------------------------------------------
//
// File:                AppHome.js
//
// Description :        Javascript for the AppHome page.
//
// Author(s):           Michael G. Laris
//
// ------------------------------------------------------------------------------------------------
// Copyright (c) 2009-2011, Saint Bernard Rescue Foundation, Inc. 
// ------------------------------------------------------------------------------------------------
// This file is part of RescueDB.
//
// RescueDB is free software: you can redistribute it and/or modify it under the terms of the
// GNU General Public License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// RescueDB is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
// even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with RescueDB.
// If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------------------------------

var dob       = [];
var adopted   = [];
var start     = 0;
var stop      = 4;
var nextVet   = 2;
var nextChild = 4;

// ------------------------------------------------------------------------------------------------
//
// Function Name:       initDisplay
//
// Description:         Initialize the display of visible entries on load.
//
// Parameters:          None.
//
// Returns:             None.
//
// Comments:            None.
//
// ------------------------------------------------------------------------------------------------

function initDisplay (hasSpouse, hasPets, hasVet)
{

   if (hasSpouse == 'Y') document.getElementById("spouseInfo").style.display = "";

   if (hasPets == 'Y') {
      displayPets();
      document.getElementById("petInfo").style.display = "";
      if (hasVet == 'Y') {
         document.getElementById("vetInfo").style.display = "";
      } else {
         document.getElementById("noVetInfo").style.display = "";
      }
   }

};

// ------------------------------------------------------------------------------------------------
//
// Function Name:       displayPets
//
// Description:         Initialize the display of visible pet entries,
//
// Parameters:          None.
//
// Returns:             None.
//
// Comments:            None.
//
// ------------------------------------------------------------------------------------------------

function displayPets ()
{
   for (var i=start; i<stop; i++) {
      if (i >= 4) {
         var rowName = 'pet' + (i+1);
         document.getElementById(rowName).style.display = "";
      }
   }

   start = stop;
   if (stop <= 20) stop++;
   if (stop == 21) document.getElementById("morePets").style.display = "none";

};

// ------------------------------------------------------------------------------------------------
//
// Function Name:       displayVetInfo
//
// Description:         Display either the Vet info or the noVetInfo section
//
// Parameters:          None.
//
// Returns:             None.
//
// Comments:            None.
//
// ------------------------------------------------------------------------------------------------

function displayVetInfo ()
{
   var appForm      = document.getElementById("AppForm");
   var vetInfoGroup = appForm.elements['hasVet'];

   if (vetInfoGroup[0].checked) {
      document.getElementById("vetInfo")  .style.display = "";
      document.getElementById("noVetInfo").style.display = "none";
   } else {
      document.getElementById("vetInfo")  .style.display = "none";
      document.getElementById("noVetInfo").style.display = "";
   }

};

// ------------------------------------------------------------------------------------------------
//
// Function Name:       displayNextVet
//
// Description:         Display an additional vet dialog, up to a maximum of 5.
//
// Parameters:          None.
//
// Returns:             None.
//
// Comments:            None.
//
// ------------------------------------------------------------------------------------------------

function displayNextVet ()
{
   var rowName = 'vet' + (nextVet);
   document.getElementById(rowName).style.display = "";

   if (nextVet <= 5) nextVet++;
   if (nextVet == 6) document.getElementById("moreVets").style.display = "none";
};

// ------------------------------------------------------------------------------------------------
//
// Function Name:       displayNextChild
//
// Description:         Display an additional child dialog, up to a maximum of 12.
//
// Parameters:          None.
//
// Returns:             None.
//
// Comments:            None.
//
// ------------------------------------------------------------------------------------------------

function displayNextChild ()
{
   var rowName = 'child' + (nextChild);
   document.getElementById(rowName).style.display = "";

   if (nextChild <= 12) nextChild++;
   if (nextChild == 13) document.getElementById("moreChildren").style.display = "none";
};

// ------------------------------------------------------------------------------------------------
//
// Function Name:       validateApp
//
// Description:         Master validate function that in turn calls the functions that validate
//                      sub-sections.
//
// Parameters:          None.
//
// Returns:             None.
//
// Comments:            None.
//
// ------------------------------------------------------------------------------------------------

function validateApp (appForm)
{
   var hasPets = appForm.elements['hasPets'];

   //
   // Make certain that the Application Type has been selected.
   //
   var appType = appForm.appType;
   var iIndex = -1;
   
   //
   // Search the array of radio buttons to find the one button that is selected.
   //
   for (var i=appType.length-1; i > -1; i--) {
       if (appType[i].checked) { 
    	   iIndex = i; 
    	   i = -1;
       }
   }
   
   //
   // If iIndex is still -1, then no selected radio button was found,meaning that the user did not select an
   // application type.
   //
   if (iIndex == -1) {
	   alert ("The Application Type is a required field and is empty.  " +
       "Please select one of the rado buttons and submit your application again.");
	   return (false);
   }
   
   //
   // Validate the data groups.
   //
   if (!validateContactInfo(appForm)) { return(false); }
   if (!validateSpouseInfo (appForm)) { return(false); }
   if (!validateReferences (appForm)) { return(false); }

   //
   // If the current value of the "pets" radio button is "N", then
   // we do not need to validate any fields in the PetInfo or vetInfo sections.
   //
   if (hasPets[0].checked) {
      if (!validatePetInfo(appForm)) { return(false); }
      if (!validateVetInfo(appForm)) { return(false); }
   }

   //
   // Last but not least, validate the custom questions.
   //
   if (!validateQuestions(appForm)) { return(false); }


};

// ------------------------------------------------------------------------------------------------
//
// Function Name:       validateContactInfo
//
// Description:         Validate the contact information ection of the application form.
//
// Parameters:          None.
//
// Returns:             None.
//
// Comments:            None.
//
// ------------------------------------------------------------------------------------------------

function validateContactInfo (appForm)
{
   var firstName  = appForm.firstName .value;
   var lastName   = appForm.lastName  .value;
   var address1   = appForm.address1  .value;
   var city       = appForm.city      .value;
   var zipCode    = appForm.zipCode   .value;
   var email      = appForm.email     .value;
   var phoneHome  = appForm.phoneHome .value;
   var occupation = appForm.occupation.value;
   var hasSpouse  = appForm.elements['hasSpouse'];
   var hasPets    = appForm.elements['hasPets'];
   var status     = true;
   var field      = "";

   if (firstName .length == 0) { status = false; field = "First Name"   ; } else
   if (lastName  .length == 0) { status = false; field = "Last Name"    ; } else
   if (address1  .length == 0) { status = false; field = "Address"      ; } else
   if (city      .length == 0) { status = false; field = "City"         ; } else
   if (zipCode   .length == 0) { status = false; field = "Zip Code"     ; } else
   if (email     .length == 0) { status = false; field = "Email Address"; } else
   if (phoneHome .length == 0) { status = false; field = "Home Phone"   ; } else
   if (occupation.length == 0) { status = false; field = "Occupation"   ; } else

   //
   // The radio buttons are NOT initialized, so we must make sure that a value has
   // been selected.
   //
   if (!hasSpouse[0].checked && !hasSpouse[1].checked) { status = false; field = "Spouse"       ; } else
   if (!hasPets[0]  .checked && !hasPets[1]  .checked) { status = false; field = "Pets"         ; }

   //
   // If a missing field was detected, display an alert box and do not allow the
   // form to be submitted.
   //
   if (status == false) {
      alert ("The " + field + " of the applicant is a required field and is empty.  " +
             "Please enter the correct information into the field provided and submit your application again.");
   }

   return (status);

};

// ------------------------------------------------------------------------------------------------
//
// Function Name:       validateSpouseInfo
//
// Description:         Validate the spouse information section of the application form.
//                      If no s[pose is specified in the "spouse" radio button, then do
//                      not validate any element of the section.
//
// Parameters:          None.
//
// Returns:             None.
//
// Comments:            None.
//
// ------------------------------------------------------------------------------------------------

function validateSpouseInfo (appForm)
{
   var hasSpouse        = appForm.elements['hasSpouse'];
   var spouseFirstName  = appForm.spouseFirstName .value;
   var spouseLastName   = appForm.spouseLastName  .value;
   var spouseOccupation = appForm.spouseOccupation.value;
   var spouseEmail      = appForm.spouseEmail     .value;
   var status           = true;
   var field            = "";

   //
   // If the current value of the "spouse" radiobutton is "N", then
   // we do not need to validate any fields in this section.
   //
   if (hasSpouse[0].checked) {
      if (spouseFirstName .length == 0) { status = false; field = "Spouse's First Name"   ; } else
      if (spouseLastName  .length == 0) { status = false; field = "Spouse's Last Name"    ; } else
      if (spouseOccupation.length == 0) { status = false; field = "Spouse's Occupation"   ; } else
      if (spouseEmail     .length == 0) { status = false; field = "Spouse's Email"        ; }
   }

   //
   // If a missing field was detected, display an alert box and do not allow the
   // form to be submitted.
   //
   if (status == false) {
      alert ("The " + field + " of the application is a required field and is empty.  " +
             "Please enter the correct information into the field provided and submit your application again.");
   }

   return (status);

};

// ------------------------------------------------------------------------------------------------
//
// Function Name:       validatePetInfo
//
// Description:         Validate the pet information section of the application form.
//
// Parameters:          None.
//
// Returns:             None.
//
// Comments:            None.
//
// ------------------------------------------------------------------------------------------------

function validatePetInfo (appForm)
{
   var status           = true;
   var petCount         = 0;

   for (var i=0; i<stop; i++) {
      var petName = "petName_"  + (i+1);
      var gender  = "gender_"   + (i+1);
      var altered = "altered_"  + (i+1);

      //
      // If the petName field has a non-zero length, then both the gender and
      // the altered radio buttons must have a value selected.
      //
      var petNameElement = appForm.elements[petName];
      if (petNameElement.value.length != 0) {
         var genderGroup  = appForm.elements[gender];
         var alteredGroup = appForm.elements[altered];
         petCount++;
         if (!genderGroup[0]. checked && !genderGroup[1]. checked) { status = false; } else
         if (!alteredGroup[0].checked && !alteredGroup[1].checked) { status = false; }
      }
   }

   //
   // If no pets were listed, complain.
   //
   if (petCount == 0) {
      status = false;
      alert ("You have previously indicated that you currently have pets, or have had pets within the last 5 years.  " +
             "Please provide the requested information about your pets, as indicated by the fields marked by the red asterisk.");
   }

   //
   // If a missing field was detected, display an alert box and do not allow the
   // form to be submitted.
   //
   else if (status == false) {
      alert ("You must specify the gender and if the animal is spayed or neutered for all pets currently in your household.  " +
             "Please select the appropriate buttons and submit your application again.");
   }

   return (status);

};

// ------------------------------------------------------------------------------------------------
//
// Function Name:       validateVetInfo
//
// Description:         Validate the vet information section of the application form.
//
// Parameters:          None.
//
// Returns:             None.
//
// Comments:            Only validate the first vest specified.
//
// ------------------------------------------------------------------------------------------------

function validateVetInfo (appForm)
{
   var hasVet     = appForm.elements['hasVet'];
   var status     = true;

   //
   // If the user indicated that they have a vet, then we must validate the first
   // vet in the list.
   //
   if (hasVet[0].checked) {
      var clinicName = appForm.clinicName_1.value;
      var phoneWork  = appForm.phoneWork_1 .value;
      var address1   = appForm.address1_1  .value;
      var city       = appForm.city_1      .value;
      var zipCode    = appForm.zipCode_1   .value;

      if (clinicName.length == 0) { status = false; } else
      if (phoneWork .length == 0) { status = false; } else
      if (address1  .length == 0) { status = false; } else
      if (city      .length == 0) { status = false; } else
      if (zipCode   .length == 0) { status = false; }

      //
      // If a missing field was detected, display an alert box and do not allow the
      // form to be submitted.
      //
      if (status == false) {
         alert ("You must specify the name and address of the Vetrinary clinic that treats the pets currently in your household.  " +
                "Please provide the requested information and submit your application again.");
      }
   }

   //
   // If the user indicates that they DO NOT have a vet, then they must offer an
   // explanation of they do not take their current pets to a Vet.
   //
   else if (hasVet[1].checked) {
      var noVetReason = appForm.noVetReason.value;
      if (noVetReason.length == 0) { status = false; }

      //
      // If a missing field was detected, display an alert box and do not allow the
      // form to be submitted.
      //
      if (status == false) {
         alert ("If you have pets but do not have a regular vet, you must explain the situation.  " +
                "Please provide the requested information and submit your application again.");
      }
   }

   //
   // If neither the Yes or No answer to the "Have you taken your pets to a veterinarian within the last 5 years?" question
   // then tell the user that the question must be answered.
   //
   else {
      status = false;
      alert ("You must provide the requested information regarding the vetrinary care that you have provided to your pets.  " +
             "Please answer the question \"Have you taken your pets to a veterinarian within the last 5 years?\" and the subsequent " +
             "questions concerning vetrinary care and submit your application again.");
   }

   return (status);

};

// ------------------------------------------------------------------------------------------------
//
// Function Name:       validateReferences
//
// Description:         Validate the Personal References section of the application form.
//
// Parameters:          None.
//
// Returns:             None.
//
// Comments:            Only validate the first two references specified.
//
// ------------------------------------------------------------------------------------------------

function validateReferences (appForm)
{
   var referenceName1 = appForm.referenceName_1.value;
   var address1       = appForm.address_1      .value;
   var phone1         = appForm.phone_1        .value;
   var relationship1  = appForm.relationship_1 .value;

   var referenceName2 = appForm.referenceName_2.value;
   var address2       = appForm.address_2      .value;
   var phone2         = appForm.phone_2        .value;
   var relationship2  = appForm.relationship_2 .value;

   var status     = true;

   if (referenceName1.length == 0) { status = false; } else
   if (address1      .length == 0) { status = false; } else
   if (phone1        .length == 0) { status = false; } else
   if (relationship1 .length == 0) { status = false; } else
   if (referenceName2.length == 0) { status = false; } else
   if (address2      .length == 0) { status = false; } else
   if (phone2        .length == 0) { status = false; } else
   if (relationship2 .length == 0) { status = false; }

   //
   // If a missing field was detected, display an alert box and do not allow the
   // form to be submitted.
   //
   if (status == false) {
      alert ("You must specify the name, address, phone number and relationship of at least two references.  " +
             "Please provide the requested information and submit your application again.");
   }

   return (status);

};

// ------------------------------------------------------------------------------------------------
//
// Function Name:       validateQuestions
//
// Description:         Validate the custom questions based on the data in  the "question"
//                      array.  This array is created by the JSP page at loadtime by using
//                      the answerBean passed into the page.
//
// Parameters:          None.
//
// Returns:             None.
//
// Comments:            None.
//
// ------------------------------------------------------------------------------------------------

function validateQuestions (appForm)
{
   var status     = true;

   for (var i = 0; i < questions.length-1; i++) {
      //
      // Validate an Answer that uses a text input field or a text area field.
      //
      if (questions[i].type == 'I' || questions[i].type == 'B') {
         var q = document.getElementById("q" + questions[i].seqno);
         if (q.value.length == 0) { status = false; }
      }

      //
      // Validate an Answer that uses a radio button for a Yes/No value.
      //
      else if (questions[i].type == 'Y') {
         var q = appForm.elements["q" + questions[i].seqno];
         if (!q[0].checked && !q[1].checked) { status = false; }
      }

      //
      // If status is false, then a required question is missing.  display an alert
      // and return a validation failure.
      //
      if (status == false) {
         alert ("You must provide an answer for all questions marked with a red asterisk.  " +
                 "Please answer the question \"" + questions[i].question + "\" and submit your application again.");
         return (status);
      }
   }

   return (status);

};


