// Calendar for delivdate on custom pages
var now = new Date();
var nowMS = now.valueOf();
var yesterday = new Date(nowMS-86400000);
var oneYearFromNow = new Date(nowMS+(365*86400000));
var oneYearFromNowForCalendar = new Date(nowMS+(366*86400000));
var cal = new CalendarPopup();
cal.offsetX=100;
cal.offsetY=100;
cal.setYearSelectStartOffset(1);
cal.addDisabledDates(null,formatDate(yesterday,"yyyy-MM-dd"));
cal.addDisabledDates(formatDate(oneYearFromNowForCalendar,"yyyy-MM-dd"),null);
cal.setReturnFunction("setDelivDate");
document.write(cal.getStyles());

function setRadioOnPageLoad(){
   // Get selected Month and year
   // - date object uses 0-11 to represent months
   intMonth = document.orderForm.delivmonth.value - 1;
   intDay   = document.orderForm.delivday.value;
   intYear  = document.orderForm.delivyear.value;

   // Create Date object based on month selection, no time sets to begining of day
   objDelivDate = new Date(intYear, intMonth, intDay);

   // Create Date object for today, set to beginning of day
   objToday = new Date();
   objToday.setHours(0);
   objToday.setMinutes(0);
   objToday.setSeconds(0);

   // Set date radio
   //if (objDelivDate > objToday){
   //   document.orderForm.senddate[1].checked = true; 
   //   return;
   //}
   //document.orderForm.senddate[0].checked = true; 
}

function getDateString(y_obj,m_obj,d_obj) {
    var y = y_obj.options[y_obj.selectedIndex].value;
    var m = m_obj.options[m_obj.selectedIndex].value;
    var d = d_obj.options[d_obj.selectedIndex].value;
    if (y=="" || m=="") { return null; }
    if (d=="") { d=1; }
    return str= y+'-'+m+'-'+d;
}

// Set reminders from delivdate
function setReminders(strMonth, strDate){ 
   if (document.orderForm.bday){
       objReminderMonthSelectObject = document.orderForm.bmonth;
       objReminderDateSelectObject  = document.orderForm.bday;
   }
   if (document.orderForm.aday){
      objReminderMonthSelectObject = document.orderForm.amonth;
      objReminderDateSelectObject  = document.orderForm.aday;
   }
  
   // set reminder month select
   for (i=0; i < objReminderMonthSelectObject.length; i++){
       strMonthSelectDate   = objReminderMonthSelectObject.options[i].value;
       if (strMonthSelectDate == (strMonth)){objReminderMonthSelectObject.options[i].selected = 1;break;}
   }

   // set reminder date select
   for (i=0; i < objReminderDateSelectObject.length; i++){
       strDaySelectDate   = objReminderDateSelectObject.options[i].value;
       if (strDaySelectDate == strDate){objReminderDateSelectObject.options[i].selected = 1;break;}
   }
}

//Function checkdate range checks to see if date
// falls within approved date range
// IF so set radio button to checked
function checkDateRange(){
   // Get selected Month and year
   // - date object uses 0-11 to represent months
   intMonth = document.orderForm.delivmonth.value - 1;
   intDay   = document.orderForm.delivday.value;
   intYear  = document.orderForm.delivyear.value;

   // Create Date object based on month selection
   objDelivDate = new Date(intYear, intMonth, intDay);

   // Verify date in proper range
   if ((objDelivDate.valueOf() < yesterday.valueOf()) || (objDelivDate.valueOf() > oneYearFromNow.valueOf())){
      alert("Date must be within " + now.toDateString() + " and " + oneYearFromNow.toDateString());
      setSelectedMonth(now.getMonth() + 1);
      setSelectedYear(now.getYear());
      fillDate(now.getDate());
      return;
   }

   // Set date to selected date .. ie refill days etc
   fillDate(objDelivDate.getDate())
   
   // Set future radio button to checked, assume since function is only called if date is changed
   //document.orderForm.senddate[1].checked = true; 

   // if reminders are used, set reminders 
   if ((document.orderForm.bday) || (document.orderForm.aday)){        
      setReminders(intMonth+1, intDay);
   }
}

//Function is called by calendar object to provide selected date values
// Set the deliv date selects from the calendar popup
// -- function does not have to check date range because 
//    calendar is already restricting date by striking out invalid dates.
function setDelivDate(strYear, strMonth, strDate){
  // if date is 1 char, append a 0. SimpleTime mask expects dd
  strDate = new String(strDate);
  if (strDate.length == 1){strDate = "0" + strDate;}

  // if month is 1 char, append a 0. SimpleTime mask expects mm
  strMonth = new String(strMonth);
  if (strMonth.length == 1){strMonth = "0" + strMonth;}
  
  // Set delivmonth to selected month
  setSelectedMonth(strMonth);

  // Call fillDate to create delivday select object     
  fillDate(strDate);
  
  // Set delivyear to selected Year     
  setSelectedYear(strYear);
   
  // Set future radio button to checked, assume since function is only called if date is changed
  //document.orderForm.senddate[1].checked = true; 
   
  // if reminders are used, set reminders 
  if ((document.orderForm.bday) || (document.orderForm.aday)){    
     setReminders(strMonth, strDate);
  }
}

//Function to set selected month for delivmonth select object off of a supplied month
function setSelectedMonth(strMonth)
{
  for (i=0; i < 13; i++){
      strMonthSelectDate   = document.orderForm.delivmonth.options[i].value;
      if (strMonthSelectDate == strMonth){document.orderForm.delivmonth.options[i].selected = 1;break;}
  }
}

//Function to set selected year for delivyear select object off of a supplied value
function setSelectedYear(strYear)
{
  for (i=0; i < document.orderForm.delivyear.length; i++){
      strYearSelectDate   = document.orderForm.delivyear.options[i].value;
      if (strYearSelectDate == strYear){document.orderForm.delivyear.options[i].selected = 1;break;}
  }
}

//Function for date select object fill and check. Called when Month select object is changed.
// - fills date field
// - verifys selected date is in proper range
function fillDays()
{
 // function is called only when month is changed. Therefore reset
 //  date field to first day of month
 fillDate("1");
 checkDateRange();
}

// Function to fill day field for delivery date selects
function fillDate(strSelectedDate)
{
    // initialize variables
    var aryDayDescription = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
    var intDayCnt = 1;
    
    // Get selected Month and year
    // - date object uses 0-11 to represent months
    intMonth = document.orderForm.delivmonth.value - 1;
    intYear  = document.orderForm.delivyear.value;
    
    // Create Date object based on month selection
    objDate = new Date(intYear, intMonth, 1);
     
    // Create Date object for todays date
    objTodaysDate = new Date();
    intTodaysDate = objTodaysDate.getDate();
    intTodaysMonth = objTodaysDate.getMonth() + 1;
    intTodaysYear = objTodaysDate.getFullYear();
    
    // Setting length of options to 0 clears out previously assigned options in list
    document.orderForm.delivday.options.length = 0;
    
    // For loop does the following:
    // - if date or month returned is 1 char, append a 0. SimpleTime mask expects dd
    // - create a new option object to assign to options array
    // - increment date counter and setDate to advance to next date
    // - test to see if month changes, if so break out of loop
    // - test to see if day being added is 364 days in future, if so break out of loop
    for (i=0; i < 31; i++){
         // if date is 1 char, append a 0. SimpleTime mask expects dd
         strDate = new String(objDate.getDate());
         if (strDate.length == 1){strDate = "0" + strDate;}
    
         // if month is 1 char, append a 0. SimpleTime mask expects mm
         strMonth = new String((objDate.getMonth() + 1));
         if (strMonth.length == 1){strMonth = "0" + strMonth;}
    
         // format dates for value and text properties of select options
         objOption = new Option((strDate), (strDate));
    
    	 // Ticket 47159: Opera compatibility
    	 // this does not work in Opera    	 
         // set the selected date
         //if (i == (strSelectedDate-1)){ 
         // objOption.selected = 1;         	        	
         //}
    
         // Add new option object to option element array   
         document.orderForm.delivday.options[i]  = objOption;
         
         // increment date counter and setDate to advance to next date
         intDayCnt = intDayCnt + 1;
         objDate.setDate(intDayCnt);
    
         //if month changes, break out of loop
         if ((objDate.getMonth() + 1) != (intMonth + 1)){break;}
    }
    // Ticket 47159: Opera compatibility
    document.orderForm.delivday.options[strSelectedDate-1].selected =1;    
}
