var today = new Date();

// returns day of month, from 1 to 31
var date = today.getDate();

// returns day of week from 0 (Sunday) to 6 (Saturday)
var day = today.getDay();

// returns month, from 0-11
var month = today.getMonth();

var lastSun = date - day;

var nextSun;

// Statement 1 handles Sundays
if(day == 0) {
   if(date <= 7) { 
      if(today.getHours() < 13) { nextSun = 1; }
      else { nextSun = 2 }
	  }
   else if(date <= 14) { 
      if(today.getHours() < 13) { nextSun = 2; }
      else { nextSun = 3; }
	  }   
   else if(date <= 21) { 
      if(today.getHours() < 13) { nextSun = 3; }
      else { nextSun = 4; }
	  }  
   else if(date <= 28) { 
      if(today.getHours() < 13) { nextSun = 4; }
      else { nextSun = 5; }
	  }   	     
   else { 
      if(today.getHours() < 13) { nextSun = 5; }
      else { nextSun = 1; }
	  }
   } // end Statement 1   

else { // Statement 2 handles non-Sundays
   if(lastSun < 0) { nextSun = 1; }
   else if(lastSun <= 7) { nextSun = 2; }
   else if(lastSun <= 14) { nextSun = 3; }
   else if (lastSun <= 21) { nextSun = 4; }
   
   // Statement 3 handles the fifth week
   else {
      // statement for 31-day months
	  if((month == 0) || (month == 2) || (month == 4) || 
	     (month == 6) || (month == 7) || (month == 9) || 
		 (month == 11)) {
         if(lastSun < 25) { nextSun = 5; }
	     else { nextSun = 1; } 
	  } // end of statement for 31-day months 
	  
	  // statement for 30-day months 
      else if((month == 3) || (month == 5) || (month == 8) || 
	          (month == 10)) { 
         if(lastSun < 24) { nextSun = 5; }
		 else { nextSun = 1; }  
	     } // end of statement for 30-day months  	  
	  
	  // statement for February, doesn't deal with leap years
	  else if(month == 1) { 
	     if(lastSun > 21) { nextSun = 1; }
		 else { nextSun = 4; }
		 } // end of statement for February

      } // end of Statement 3
   
   } // end Statement 2


