/* -----------------------------------------------------------
   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 sFuneralHome;
   var sPolicyID;
   var sInsuredName;
   var sIssuedState;
   var retVal;
   
   sFuneralHome = form.txtFuneralHome.value;
   sPolicyID = form.txtPolicyID.value;
   sInsuredName = form.txtInsuredName.value;
   sIssuedState = form.cmbIssueState.value;
   retVal = false; 
    // 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_TrusteeResult.asp";   
   if ((isValidFuneralHome(sFuneralHome)) || (isValidPolicyID(sPolicyID)) || (isValidInsuredName(sInsuredName))) {
       form.action = "SNIC_TrusteeResult.asp";
       retVal = true;
   } else if (isValidIssuedState) {
      form.action = "SNIC_StateResult.asp";
      retVal = true;
   } else {
      form.action = "SNIC_TrusteeSearch.asp?error=true";
      retVal = false;
   }
   return retVal;
}

/* -----------------------------------------------------------
   Checks to see if the funeral home is valid. 
--------------------------------------------------------------*/

function isValidFuneralHome(sFuneralHome) {
   var retVal = false;
   
   if (sFuneralHome.length > 0) {
      retVal = true;
   }
   return retVal;
}


/* -----------------------------------------------------------
   Checks to see if the policy id is valid. 
--------------------------------------------------------------*/
function isValidPolicyID(sPolicyID) {
   var retVal = false;
  
   if (sPolicyID.length == 10) {
      retVal = true;
   }
   return retVal;
}

/* -----------------------------------------------------------
   Checks to see if the insured name is valid. 
--------------------------------------------------------------*/

function isValidInsuredName(sInsuredName) {
   var retVal = false;
   
   if (sInsuredName.length > 0) {
      retVal = true;
   }
   return retVal;
}

/* -----------------------------------------------------------
   Checks to see if the state is valid. 
--------------------------------------------------------------*/

function isValidIssuedState(sIssuedState) {
   var retVal = false;
   
   if (sIssuedState.length > 0) {
      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;
}

function logout() {
    setCookie("ValidUser", "", -1);
    window.location = "SNIC_TrusteeLogin.asp";
}

function setCookie(c_name,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}