// formDateValidate.js

// newFunction
function getLastSunday(){
   startDate = new Date();
   var stringDate = null;   
   
   if(startDate.getDay() == 0){
      // It's Friday today, just subtract one week
	   startDate.setDate(startDate.getDate() - 7);
	   stringDate = startDate.getMonth()+1+'/'+startDate.getDate()+'/'+startDate.getFullYear();
   }
   else {
      // Walk backwards a day at a time until we get to Friday
      while(startDate.getDay() != 0){
            startDate.setDate(startDate.getDate() - 1);		
      }
	  stringDate = startDate.getMonth()+1+'/'+startDate.getDate()+'/'+startDate.getFullYear(); 
   }
   return stringDate;
}

//Extending the Date Object 
function getDateString(){
    var dateStr;
    dateStr = "" + this.getFullYear();
    if (this.getMonth() < 9)
        dateStr += "0";
    dateStr +=  (this.getMonth() + 1);
    if (this.getDate() < 10)
        dateStr += "0";
    dateStr += this.getDate();
    return dateStr;
}

function compareDatesTwoEnd(field, minDate, maxDate, fieldLabel){
    var datefield = field.value;
    Date.prototype.getDateString = getDateString;
	
    //startDay = new Date("01/01/2005");
    startDay = new Date(minDate);
    validDay = new Date(datefield);
	endDay = new Date(maxDate);
	
    if((validDay.getDateString() < startDay.getDateString()) || (validDay.getDateString() > endDay.getDateString())){
		alert("Date field '"+fieldLabel+"' must be between "+minDate+" and "+maxDate+".");			   
		field.focus();
		field.select();
		return false;
    }
	return true;	
}

function getCompareTwoDates(field1, field2){
   var dateField1 = field1.value;
   var dateField2 = field2.value;
   
   validDay1 = new Date(dateField1);
   validDay2 = new Date(dateField2);
   
   if(validDay1.getDateString() > validDay2.getDateString()){
      alert("TO DATE '"+dateField2+"' must be great than FROM DATE '"+dateField1+"'");
	  return false;
   }
   return true; 
}

//Beginning Valid date
function validateDate(strDate) { 
     var datePat = /^(\d{2})(\/)(\d{2})\2(\d{4})$/;
     var matchArray = strDate.match(datePat);
	 if (matchArray == null) return false; //The field value is not null
    //if (matchArray == null && !isEmpty(strDate)) return false;  //The field value is null
	/*if(matchArray == null){//The field value is null
	     if(!isEmpty(strDate)){
		      return false;  
		 }else{
		      return true;
		 }
     }*/
     // matchArray[0] will be the original entire string, for example 4/12/2002
     var month = matchArray[1];     // (\d{1,2}) - 1st parenthesis set - 4
     var day = matchArray[3];         // (\d{1,2}) - 3rd parenthesis set - 12
     var year = matchArray[4];        // (\d{4}) - 5th parenthesis set - 2002
     if (month < 1 || month > 12) return false;
     if (day < 1 || day > 31) return false;
     if ((month == 4 || month == 6 || month==9 || month == 11) && day == 31) return false;
     if (month == 2) {
          var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
          if (day > 29 || (day == 29 && !isleap)) return false;
     }
     return true;
}

function isValidDate(field, fieldLabel){
  var datefield = field.value;
  if(!validateDate(datefield)){
      alert('The format of "'+fieldLabel+'" the date must be: (mm/dd/yyyy)\n\n where mm=Month, dd=Day, yyyy=Year \n\nExample: 02/07/2004 or 2/7/2004');
	  field.focus();
	  field.select();
	  return false;
  }
  return true;  	
}
// End Valid date


function radioButtonChecker(field)
{
    // set var radio_choice to false
    var radio_choice = false;

   // Loop from zero to the one minus the number of radio button selections
   for (counter = 0; counter < field.length; counter++)
   {
   // If a radio button has been selected it will return true
   // (If not it will return false)
       if (field[counter].checked)
           radio_choice = true; 
   }

   if (!radio_choice)
   {
   // If there were no selections made display an alert box 
      alert("Please select a radio button.")
      return (false);
   }
   return (true);
}
//Select an element in the list
function validateSelection(field, fieldLabel){
    var selectionField = field.selectedIndex;
    if(selectionField == 0){
		alert("\nYou must make a selection from the drop-down menu ("+fieldLabel+").");
		field.focus();
		return false;
	}
	return true;
}

function daysBetweenTwoDates(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY)
}

function daysBetweenTwoDatesIsLessThan365(field1, field2){

     var dateField1 = field1.value;
     var dateField2 = field2.value;
   
     // Store the first and second date and time
     first_date = new Date(dateField1); 
     second_date = new Date(dateField2);
     
     // Call the daysBetweenTwoDates function
     var numberOfDay = daysBetweenTwoDates(first_date, second_date);
     
     if(numberOfDay > 365){
        alert(" The number of days ('"+numberOfDay+"') between FROM DATE '"+dateField1+"' and TO DATE '"+dateField2+"' must be less than equal to 365. ");
        return false;
     }
     return true;
}




