/**
 ' *************************************************************************
 ' Program:  validation.js              Author:  Larry Hord
 '           (INCLUDE FILE)            Created:  April, 2002
 ' -------------------------------------------------------------------------
 '   This program is used to validate form selection criteria and pass the 
 ' selected data to the display grid program.  
 ' *************************************************************************
 ' Modifications:  
 '
 '
 ' *************************************************************************
 *
 **/

// ***************************************************
function formValidator(theForm)
// *************************************************** 
{
   var formValid = true;
   
      //  *****  Validate Date of Birth  *****   
    dobMonth = new Array(12);
    dobMonth[0] = "Jan";
	dobMonth[1] = "Feb";
	dobMonth[2] = "Mar";
	dobMonth[3] = "Apr";
	dobMonth[4] = "May";
	dobMonth[5] = "Jun";
	dobMonth[6] = "Jul";
	dobMonth[7] = "Aug";
	dobMonth[8] = "Sep";
	dobMonth[9] = "Oct";
	dobMonth[10] = "Nov";
	dobMonth[11] = "Dec";

   var dobMO = "";
   var checkM = theForm.voterDOBM.value;
   var checkD = theForm.voterDOBD.value;
   var checkY = theForm.voterDOBY.value;
   var checkOk = "0123456789";
   var checkStr = "";
   var allValid = true;
      
   if (checkM == "" || checkD == "" || checkY == "")
   {     
     alert("Date of Birth is required for a name search, you must enter date.");
     theForm.voterDOBM.focus();
     formValid = false;
     return formValid;
   }
   
   for (i = 0; i < checkM.length; i++)
   {
     ch = checkM.charAt(i);
     for (m = 0; m < checkOk.length; m++)
     {
       if (ch == checkOk.charAt(m))
       {   break;   }
     }  
     if (m == checkOk.length)
     {
         allValid = false;
         break;
     }
   }
   
   if (checkM < 1 || checkM > 12)
   {
         allValid = false;
   }
   
   if (allValid)
   {
       theForm.voterDOBM.value = checkM;
       for (x = 0; x < dobMonth.length; x++)
            {   
                 if (x == (checkM - 1))
                 {
                     dobMO = dobMonth[x];
                     break;
                 }
            }
   }
   else
   {
       alert("Please enter a valid \"Month\" between 1 and 12.");
       theForm.voterDOBM.focus();
       formValid = false;
       return formValid;
   }
   
   allValid = true;
   
   for (i = 0; i < checkD.length; i++)
   {
     ch = checkD.charAt(i);
     for (d = 0; d < checkOk.length; d++)
     {
       if (ch == checkOk.charAt(d))
       {   break;   }
     }  
     if (d == checkOk.length)
     {
         allValid = false;
         break;
     }
   }
      
   if (checkD < 1 || checkD > 31)
   {
         allValid = false;
   }
   
   if (allValid)
   {
       theForm.voterDOBD.value = checkD;
   }
   else
   {
       alert("Please enter a valid \"Day\" between 1 and 31.");
       theForm.voterDOBD.focus();
       formValid = false;
       return formValid;
   }
   
   allValid = true;
   
   for (i = 0; i < checkY.length; i++)
   {
     ch = checkY.charAt(i);
     for (d = 0; d < checkOk.length; d++)
     {
       if (ch == checkOk.charAt(d))
       {   break;   }
     }  
     if (d == checkOk.length)
     {
         allValid = false;
         break;
     }
   }
   
   if (i != 4) allValid = false;
   
   if (allValid)
   {
       theForm.voterDOBY.value = checkY;
   }
   else
   {
       alert("Please enter a valid \"Year\" consisting of 4 numbers (ccyy).");
       theForm.voterDOBY.focus();
       formValid = false;
       return formValid;
   }
   
   theForm.voterDate.value = checkD + "-" + dobMO + "-" + checkY;
   
      //  *****  Validate Voter Name  *****
       
   var lname = theForm.voterLName.value;
   var fname = theForm.voterFName.value;
      
   if (lname == ""  &&  fname == "")
   {     
     alert("If you are doing a name search, you must enter both last and first names.");
     theForm.voterLName.focus();
     formValid = false;
     return formValid;
   }
      
   if (lname != ""  &&  fname == "")
   {     
     alert("You must enter a first name with a last name.");
     theForm.voterFName.focus();
     formValid = false;
     return formValid;
   }
      
   if (lname == ""  &&  fname != "")
   {     
     alert("You must enter a last name with a first name.");
     theForm.voterLName.focus();
     formValid = false;
     return formValid;
   }
   
   lname = lname.toUpperCase();
   fname = fname.toUpperCase();
   theForm.voterLName.value = lname;
   theForm.voterFName.value = fname;

   return formValid;
}

// ***************************************************
function formValidNumber(theForm)
// *************************************************** 
{
   var formValid = true;
   
      //  *****  Validate Certificate Number  *****
       
   var checkOk = "0123456789";
   var checkStr = theForm.voterCNumber.value;
   var allValid = true;
      
   if (checkStr == "")
   {     
     alert("If you are doing a certificate search, you must enter certificate number.");
     theForm.voterCNumber.focus();
     formValid = false;
     return formValid;
   }
   
   for (i = 0; i < checkStr.length; i++)
   {
     ch = checkStr.charAt(i);
     for (j = 0; j < checkOk.length; j++)
     {
       if (ch == checkOk.charAt(j))
       {   break;   }
     }  
     if (j == checkOk.length)
     {
         allValid = false;
         break;
     }
   }
   
   if (allValid)
   {
       theForm.voterCNumber.value = checkStr;
   }
   else
   {
       alert("Please enter numbers ONLY for the \"Certificate Number\".");
       theForm.voterCNumber.focus();
       formValid = false;
       return formValid;
   }
     
   return formValid;
}

// ***************************************************
function formValidName(theForm)
// *************************************************** 
{
   var formValid = true;
   
      //  *****  Validate Voter Name  *****
       
   var lname = theForm.formLastName.value;
   var fname = theForm.formFirstName.value;
      
   if (lname == ""  &&  fname == "")
   {     
     alert("If you are doing a simple name search only, you must enter a last and first name.");
     theForm.formLastName.focus();
     formValid = false;
     return formValid;
   }
      
   if (lname == ""  &&  fname != "")
   {     
     alert("You must enter a last name with a first name.");
     theForm.formLastName.focus();
     formValid = false;
     return formValid;
   }
   
   if (lname != ""  &&  fname == "")
   {     
     alert("You must enter a first name with a last name.");
     theForm.formFirstName.focus();
     formValid = false;
     return formValid;
   }

   lname = lname.toUpperCase();
   fname = fname.toUpperCase();
   theForm.formLastName.value = lname;
   theForm.formFirstName.value = fname;
   
   return formValid;
}
