// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function disFields() {
   var theForm = document.forms[0];
   theForm.date1.disabled = true;
   theForm.date2.disabled = true;
   theForm.date3.disabled = true;
   theForm.date4.disabled = true;
   theForm.date5.disabled = true;
   theForm.date6.disabled = true;
   theForm.date7.disabled = true;
   theForm.date8.disabled = true;
   theForm.date9.disabled = true;
}

function calcDate() {
   // calculate all the dates
   var theForm = document.forms[0];
   var weddingDate = dateSetter(theForm.wedding_date.value);
   var myDate=new Date();
   if ( myDate >= weddingDate ) {
      alert("Please enter a date in the future");
   } else {
      // calculate all the dates
      var date1 = new Date(dateCalc(weddingDate, -210));
      var date2 = new Date(dateCalc(weddingDate, -390));
      var date3 = new Date(dateCalc(weddingDate, -84));
      var date4 = new Date(dateCalc(date3, -14));
      var date5 = new Date(dateCalc(date4, -45));
      var date6 = new Date(dateCalc(weddingDate, -30));
      var date7 = new Date(dateCalc(weddingDate, -120));
      var date8 = new Date(dateCalc(weddingDate, -30));
      var date9 = new Date(dateCalc(weddingDate, -30));

      // put them in the form
      theForm.date1.value = dateFormat(date1);
      theForm.date2.value = dateFormat(date2);
      theForm.date3.value = dateFormat(date3);
      theForm.date4.value = dateFormat(date4);
      theForm.date5.value = dateFormat(date5);
      theForm.date6.value = dateFormat(date6);
      theForm.date7.value = dateFormat(date7);
      theForm.date8.value = dateFormat(date8);
      theForm.date9.value = dateFormat(date9);
   }
}

function dateCalc(startDate, delta) {
  var startMilli = Date.parse(startDate);
	var oneDayLength = Date.parse("1/2/1970 00:00:00 GMT");
	var numberOfMilli = delta * oneDayLength;
	var sum = startMilli + numberOfMilli;
	var dateOut = new Date();
	dateOut.setTime(sum);
	return dateOut;
}

function dateFormat(theDate) {
   var m = theDate.getMonth();
   var d = theDate.getDate();
   var y = theDate.getFullYear();
   m++;
   if ( m < 10 ) { m = "0" + m }
   if ( d < 10 ) { d = "0" + d }
   var dateOut = m + "/" + d + "/" + y;
   return dateOut;
}

//----------------------------------------------------------
// set the date for the chkDates function
//----------------------------------------------------------
function dateSetter(strDate) {
   var arrDate = strDate.split("/");
   var dt = new Date();
   dt.setMonth(arrDate[0]-1);
   dt.setDate(arrDate[1]);
   dt.setYear(arrDate[2]);
   return dt;
}
