/* -----------------------------------------------------------
   Checks to see if the values on the form are valid.
   1. All values are required.
   2. The Insured Date of Birth must be in the past.
--------------------------------------------------------------*/
function validateForm(form){
   var retVal = false;
   var sFuneralHomeID = form.txtFuneralHomeID.value;
   var sPolicyID = form.txtPolicyID.value;
   var sDate = form.txtBirthdate.value;
   
   retVal = isValidFuneralHomeID(sFuneralHomeID);
   if (retVal) {retVal = isValidPolicyID(sPolicyID)};
   if (retVal) {retVal = isValidDate(sDate)};
   form.txtFormattedBirthdate.value = formatDate(sDate);

   if (retVal) {
      // USE IF WANT TO PASS IN URL
      //form.action = "SNIC_PolicyResult.asp?FuneralHomeID=" + sFuneralHomeID + "&PolicyID=" + sPolicyID + "&Birthdate=" + sDate;
      
      // USE IF WANT TO PASS IN FORM
      form.action = "SNIC_PolicyResult.asp";
   }
   return retVal;
}

/* -----------------------------------------------------------
   Checks to see if the funeral home id is valid. 
--------------------------------------------------------------*/

function isValidFuneralHomeID(sID) {
   var retVal = false;
   
   if (sID=="" || sID.length > 10) {
      alert ("Please review the Funeral Home ID before submitting. The Funeral Home ID must contain no more than 10 characters and numbers.");
   } else {
      retVal = true;
   }
   return retVal;
}


/* -----------------------------------------------------------
   Checks to see if the policy id is valid. 
--------------------------------------------------------------*/
function isValidPolicyID(sID) {
   var retVal = false;
  
   if (sID=="" || sID.length != 10) {
      alert ("Please enter the Policy ID before submitting. The PolicyID must be 10 digits.");
   } else {
      retVal = true;
   }
   return retVal;
}

/* -----------------------------------------------------------
   Checks to see if the string is a valid date.  A valid
   date is defined as any of the following:

      MM/DD/YY, MM/DD/YYYY, M/D/YY, M/D/YYYY,
      MM-DD-YY, MM-DD-YYYY, M-D-YY, M-D-YYYY
--------------------------------------------------------------*/
function isValidDate(sDate) {
   var retVal = true;
   var today = new Date();
   var aMaxDay = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
   
   var iDay, iMonth, iYear;
   var arrValues;	
    
   // Determine the date seperator and split into an array
   // Only - and / are allowed as date seperator
   if (sDate.indexOf("/") > 0) {
      arrValues = sDate.split("/");
   } else {
      arrValues = sDate.split("-");
   }
   iMonth = arrValues[0];
   iDay = arrValues[1];
   iYear = arrValues[2];

   // Make sure something was entered for month and year;
   if ((iMonth == null) || (iDay == null) || (iYear == null)) { retVal = false; }
   if ((iDay > 31) || (iMonth > 12) || (iYear < 1900 || iYear > today.getFullYear())) { retVal = false;}

   // Make sure the values entered are numbers
   if (isNaN(iMonth)) {retVal = false;}
   if (isNaN(iDay)) {retVal = false;}
   if (isNaN(iYear)) {retVal = false;}
   
   // Make sure the value entered for day is within the max days for the month entered
	if ((iDay < 1)  || (iDay > aMaxDay[iMonth-1])){ retVal = false;}
	
   // Leap Year Check; only executed if Feburuary 29 is entered
   if(iMonth == 2 && iDay == 29) {
	   var bLeap = false;
	   if (iYear%4==0) {bLeap = true;}
	   if (iYear%100==0) {bLeap = false;}
	   if (iYear%400==0) {bLeap = true;}
	   // If 29 was entered for the day but this is not a leap year. It is an invalid date
	   if (!bLeap){ retVal = false;}
	}	

   // Make sure that the date entered is in the past
   var dummyDate = new Date(iYear, iMonth - 1, iDay);
   if (dummyDate > today) { retVal = false;}

   if (!(retVal)) { alert ("Please enter a valid date before submitting. The Insured Date of Birth must be in the past.")};

   return retVal;
}

/* -----------------------------------------------------------
   Formats the date with leading zeroes if necessary 
--------------------------------------------------------------*/
function formatDate(sDate) {
   var retVal;

   if (sDate.indexOf("/") > 0) {
      arrValues = sDate.split("/");
   } else {
      arrValues = sDate.split("-");
   }
      
   if (arrValues[0].length == 1) { iMonth = "0" + arrValues[0]; } else { iMonth = arrValues[0];};
   if (arrValues[1].length == 1) { iDay = "0" + arrValues[1]; } else { iDay = arrValues[1];};
   iYear = arrValues[2];
      
   retVal = iMonth + "/" + iDay + "/" + iYear;
   return retVal;
}