module Panel::Calendar::BackEnd::CalsHelper

	def monthStartDay(month,year)
		dt = Date.new(year,month,1)
		startday = dt.wday + 1
		return startday
	end

	def getDateSet(month,year,prepost)
		dateset = Array.new
		startday = monthStartDay(month,year)
		monthsdays = getMonthDays(year)

		cur_month_days = monthsdays[month]
		next_month_days = monthsdays[month+1]
		prev_month_days = monthsdays[month-1]

		prev_month_days_to_add = startday - 1

		i = prev_month_days_to_add
		while i > 0 do
			i -= 1
			if prepost
				dateset << prev_month_days - i
			else
				dateset << "NaN"
			end
		end

		i = 0
		while i < cur_month_days do
			dateset << i + 1
			i += 1
		end 

		total_days = 42
		if !prepost
			if (prev_month_days_to_add + cur_month_days) < 35
				total_days = 35
			end
		end
		next_month_days_to_add = total_days - (prev_month_days_to_add + cur_month_days)

		i = 0 
		while i < next_month_days_to_add do
			if prepost
				dateset << i + 1
			else
				dateset << "NaN"
			end	
			i += 1
		end

		return dateset
	end

	def getMonthDays(year)
		monthsdays = Array.new
		monthsdays << 0
		monthsdays << 31
		if Date.new(year).leap?
			monthsdays << 29
		else
			monthsdays << 28
		end
		monthsdays << 31
		monthsdays << 30
		monthsdays << 31
		monthsdays << 30
		monthsdays << 31
		monthsdays << 31
		monthsdays << 30
		monthsdays << 31
		monthsdays << 30
		monthsdays << 31
		return monthsdays
	end

	def getDayName(date,month,year)
		date = date.to_i
		month = month.to_i
		year = year.to_i
		dt = Date.new(year,month,date)
		name = Date::ABBR_DAYNAMES[dt.wday]
		return name
	end

	def week_dates(week_num,year)
		year = year.to_i
		week_num = week_num.to_i
	    dates = Array.new
		
		if week_num == 1
			weekstartday = monthStartDay(1,year)
			d = 31
			x = 0
			for i in d - (weekstartday - 2)..d
				dates << Date::ABBR_DAYNAMES[x] + " 12/" + i.to_s
				x = x + 1
			end
			for	i in 1..8 - weekstartday
				dates << Date::ABBR_DAYNAMES[x] + " 1/" + i.to_s
				x = x + 1
			end
		else
	    	week_start = Date.commercial(year, week_num-1, 7)
	    	month = week_start.strftime("%m")
		    month_days = getMonthDays(year)
		    date = week_start.strftime("%d")
		    x = date.to_i 
		    for	i in 0..6
		    	d = x + i
		    	if d > month_days[month.to_i]
		    		d = d - month_days[month.to_i]  
		    	end
		    	dates << Date::ABBR_DAYNAMES[i] + " " + month + "/" + d.to_s
		    end
	    end
	    dates
  	end

  	def week_range(week_num,year)
		year = year.to_i
		week_num = week_num.to_i
		if week_num == 1
			weekstartday = monthStartDay(1,year)
			start_date = 31 - (weekstartday - 2)
			start_year = year - 1
			end_date = 8 - weekstartday
			range = "Dec " + start_date.to_s + ", " + start_year.to_s + " - " + "Jan " + end_date.to_s + ", " + year.to_s  
		else
		    week_start = Date.commercial(year, week_num-1, 7)
		    week_end = Date.commercial(year, week_num, 7)
		    week_end = week_end - 1
		    start_date =  week_start.strftime("%d")
		    end_date = week_end.strftime("%d")
		    start_month = week_start.strftime("%m")
		    end_month = week_end.strftime("%m")

		    if end_month == start_month
		   		range = Date::ABBR_MONTHNAMES[start_month.to_i] + " " + start_date.to_s + " - " + end_date.to_s + ", " + week_start.strftime("%Y").to_s
		   	else
		   		range = Date::ABBR_MONTHNAMES[start_month.to_i] + " " + start_date.to_s + " - " + Date::ABBR_MONTHNAMES[end_month.to_i] + " " + end_date.to_s + ", " + week_start.strftime("%Y").to_s
		    end
		end
	    range
  	end

  	def getWeekDataSet(week_num,year)
  		year = year.to_i
		week_num = week_num.to_i
	    dates = Array.new
		
		if week_num == 1
			weekstartday = monthStartDay(1,year)
			d = 31
			x = 0
			for i in d - (weekstartday - 2)..d
				dates << i.to_s
				x = x + 1
			end
			for	i in 1..8 - weekstartday
				dates << i.to_s
				x = x + 1
			end
		else
	    	week_start = Date.commercial(year, week_num-1, 7)
	    	month = week_start.strftime("%m")
		    month_days = getMonthDays(year)
		    date = week_start.strftime("%d")
		    x = date.to_i 
		    for	i in 0..6
		    	d = x + i
		    	if d > month_days[month.to_i]
		    		d = d - month_days[month.to_i]  
		    	end
		    	dates << d.to_s
		    end
	    end
	    dates
  	end

  	def getHours(format)
  		hours = Array.new
  		if format == 12
  			hours << 12
  			for i in 1..11
  				hours << i
  			end
  		elsif format == 24
  			for i in 0..23
  				hours << i
  			end
  		end
  		hours
  	end

  	def getHoursForForm
  		hours = Array.new
  		for i in 1..12
  			hours << {"val"=>i,"title"=>i.to_s+":00"}
  			hours << {"val"=>i+0.5,"title"=>i.to_s+":30"}
  		end
  		hours
  	end
end





