
var MONTH = new Array ();
var DAYS = new Array ();
// CSS Container IDConstants;

var MONTH_NAME_FIELD_ID = "monthname";
var DAY_INDEX_ID = "dayrow";
var DAY_INDEX_DAY_DIV_CLASS = "inner_date";
var HOUR_GRID_ID = "scrollbox";
var HOUR_GRID_DAY_DIV_CLASS = "daybox";
var CALENDAR_ID = "calendar";
var TIMEZONE = null;
var SITEPREFIX = "";
var GRDWIDTH = 92;
var PXSCALE = 1.2;
var HINDEXWIDTH = 50;
var CALBOTTOM = 1224;
var HBOXCLASSN = "inner_data"; 
var SCROLLTO9 = 450;
var FDROW = "fulldayrow";
var HOLIDAYROW = "holidayrow";
var disabledFields = ["location_details", "reminder", "pp", "email_r", "emailsms_r", "sms_r", "notes", "feid", "fuid", "id", "rid", "fb"]


/*
 * Relopia Calendar
 * Main Constructor for the Relopia Calendar (Weekview)
 * can obtain the values for startofweek and endofweek to get a 
 * display range used by Relopia_Calendar_Item.show()
 * if both values are not set, the values for today's week are used,
 * obtained by the Javascript function new Date().
 */

var Relopia_Calendar = new Class.create();
Relopia_Calendar.prototype = {
	initialize: function ( _startofweek, _endofweek ) {
	DAYS = $("lc_days").innerHTML.split(",");
	MONTH = $("lc_months").innerHTML.split(",");
   	this.startofweek = _startofweek; 
    this.endofweek = _endofweek; 
    this.monthname = null;
    this.weeknumber = null;
    this.calendar_events = new Array(); 
    this.calendar_day_column = new Array();
    this.dataSet = null;
    this.indexrow = null;
	this.today = new Date();
	this.edit = new TabWindow($("ewin"),this);
	this.datepicker_edit = new DatePicker("DatePicker","ewin");
	this.edit.smartform[0] = new SmartForm($("eventinput"), this);
	this.adresspicker = new AdressPicker(Calendar_AdressBook_Events.AddParticipant, $("invited_participants"),$("participants"));
	this.viewOnly = false;
	this.datepicker_week = new DatePicker("calendar_datepicker",null,this.datepicked,this)
	this.datepicker_week.datepicker.toggle();
	this.calendar = this;
	this.url = document.location.toString().split("/");
	
	this.tday = new Date();
	this.tday = [this.today.getFullYear(),(this.today.getMonth()+1).toPaddedString(2), this.today.getDate().toPaddedString(2)].join("-");
	
	this.getTimezone();
	
	// obtain todays date and set start and endofweek 
	// if no start/end are specified by the constructor
	
	if (this.url[5] && this.url[5].substr(0,7) == "#today=") {
		var tmp = this.url[5].substr(7,10).split("-");
		this.today = new Date(tmp[0],(Number(tmp[1])-1),tmp[2]);		
	}
	
	if (!this.startofweek && !this.endofweek) {
		this.setWeekforToday();
      }
	
    tt = new CalendarDayBox("index",-1,$(HOUR_GRID_ID)); 
	
    for (var i = 0; i < 7; i++) {
    	tt = new CalendarDayBox(i,this.get_date_code_for_day(i),$(HOUR_GRID_ID),this);
      	this.calendar_day_column.push(tt);
    	}
  	
    this.indexrow = new CalendarIndexRow(this);
    this.fetch_events();
	
	$("calendar_print").href = SITEPREFIX + "/calendar/" + USERID + "/print/" + this.get_date_code_for_day(0);
	$("prev").href = "#today=" + dateFormat(prevWeek(this.startofweek)[0]);
	$("next").href = "#today=" + dateFormat(nextWeek(this.startofweek)[0]);

	
	$(HOUR_GRID_ID).scrollTop = SCROLLTO9
	
	Event.observe( "new_event","click",Calendar_Hbox_Events.clicked.bindAsEventListener(this.calendar_day_column[0],null));
    Event.observe( "prev","click",Calendar_Nav_Events.clicked.bindAsEventListener(this,"prev"));
    Event.observe( "next","click",Calendar_Nav_Events.clicked.bindAsEventListener(this,"next"));
	
	$("loading_overlay").hide();
	},
	redraw: function () {
		$("monthname").update("<img src='/static/img/icons/wait.gif' style='margin: 0; padding: 0' align='absmiddle'> "+$("monthname").innerHTML);
		setTimeout(this.render.bind(this),200); // Ajax Latency Fix
		
		$("prev").href = "#today=" + dateFormat(prevWeek(this.startofweek)[0]);
		$("next").href = "#today=" + dateFormat(nextWeek(this.startofweek)[0]);
		
		$("calendar_print").href = SITEPREFIX + "/calendar/" + USERID + "/print/" + this.get_date_code_for_day(0);
  	},
	render: function () {
		for (var i = 0, l = this.calendar_events.length; i < l; i++) {
			this.calendar_events[i].purgeEvents();
			};
		this.calendar_events = new Array();
		this.indexrow.update();
		this.edit.close_win();
		this.fetch_events();
		document.location = "#today=" + dateFormat(this.startofweek);
	},
	navigate: function (direction) {	
  	switch(direction) {
    	case "next":
      		this.startofweek = new Date(this.startofweek.getFullYear(),this.startofweek.getMonth(),this.startofweek.getDate()+7);
    		this.endofweek = new Date(this.startofweek.getFullYear(),this.startofweek.getMonth(),this.startofweek.getDate()+7);
    		break;
    	case "prev":
      		this.startofweek = new Date(this.startofweek.getFullYear(),this.startofweek.getMonth(),this.startofweek.getDate()-7)
    		this.endofweek = new Date(this.startofweek.getFullYear(),this.startofweek.getMonth(),this.startofweek.getDate()+7)
    		break;
		case "today":
      		this.startofweek = new Date(this.today.getFullYear(),this.today.getMonth(),this.today.getDate()-this.today.getDay()+1);
 			this.endofweek = new Date(this.startofweek.getFullYear(),this.startofweek.getMonth(),this.startofweek.getDate()+7);
        	break;
    }
	
	$(HOUR_GRID_ID).scrollTop = SCROLLTO9;
	this.redraw();
  },
  fetch_events: function () {
	p = this;
	var requrl = "/" + this.url[3] + "/" + this.url[4]+"/weekview/";
	
	new Ajax.Request( requrl, {
		method: 'get',
		parameters: 'from=' + DateToNumbers(p.startofweek),
		onComplete: function(transport, json) {
			if (transport.responseText) {
				
				var myEvents = eval('('+transport.responseText + ')');
				var _Calendar_Data = new Calendar_Data(USERID, myEvents);
				p.parseResult(_Calendar_Data); 
			}
      		}
    	});
	},
  parseResult: function (_Calendar_Data) {
  	this.dataSet = _Calendar_Data;
	if (this.dataSet.Events) {

		if (this.dataSet.shareIndex && this.dataSet.shareIndex[0] != USERID ) {
			this.viewOnly = true;
			}

	  for (var key = 0, l = this.dataSet.Events[USERID].length; key < l; key++) {
			flag = true;
	    	tt = new Calendar_Item(this.dataSet.Events[USERID][key], $(HOUR_GRID_ID), this, flag);
	      	tt.show_();
    	}
		
	} 
	
	if ($(FDROW).descendants().length > 0) {
		$(FDROW).show();
		} else {
		$(FDROW).hide();
		}
			
  },
  getTimezone: function () {
  		if ($("location_timezone").value != "") {
	  		ohours = Number($("location_timezone").value.substr(5,2));
			ominutes = Number($("location_timezone").value.substr(7,2));
			oprefix = $("location_timezone").value.substr(4,1);
			offsetInMinutes = Number((oprefix + (ohours*60 + ominutes)));
			TIMEZONE = offsetInMinutes;
			} else {
				// console.log("Debug: Location not set | Falling Back to GMT+1");
				TIMEZONE = 60;
			}
  },
  get_date_code_for_day: function (day) {
  	b = new Date(this.startofweek.getFullYear(),this.startofweek.getMonth(),this.startofweek.getDate()+day)
    return b.getFullYear().toString()+"-"+((b.getMonth()+1) < 10 ? ("0" + (b.getMonth()+1)) : b.getMonth()+1)+"-"+ (b.getDate() < 10 ? ("0" + b.getDate()) : b.getDate());
  },
  get_date_string_for_day: function (day) {
  	b = new Date(this.startofweek.getFullYear(),this.startofweek.getMonth(),this.startofweek.getDate()+day);
	
	return DAYS[b.getDay()] + "<br>" + b.getDate() + "." + MONTH[b.getMonth()];
  },
  datepicked: function (event, param) {
  	newday = new Date(this.datepicker_week.year, this.datepicker_week.month, param)
	this.today = newday;
	this.setWeekforToday();
	this.redraw();
  },
  setWeekforToday: function () {
  	tday = getDayOfWeek(this.today.getDay());
  	this.startofweek = new Date(this.today.getFullYear(),this.today.getMonth(),this.today.getDate()-tday);
 	this.endofweek = new Date(this.startofweek.getFullYear(),this.startofweek.getMonth(),this.startofweek.getDate()+7);
  }
}

/*
 * Calendar_Item class 
 * Abstract Class holding the information for events that is important for
 * displaying.
 * 
 * Constructed by (object, container, calendar, child)
 * 		object is the raw event data in hash form
 * 		container is the html element the item is drawn in (hourgrid)
 * 		calendar is the associated calendar object
 * 		child is atm. unused.
 * 
 * Calendar_Item.element is an CalendarItemBox Object which is used to
 * render the Event on the hourgrid. 
 * Calendar_Item.siblings are an items siblings if its a multiday event
 * this might change when we switch the display type of multiday events
 * and merge it with the fullday row.
 */
var Calendar_Item = new Class.create();
Calendar_Item.prototype = {
	initialize: function ( eventitem, _container, _calendar, editflag) {
	this.uri = eventitem.id;
  	this.id = "calendaritem"+_calendar.calendar_events.length;
    this.title = eventitem.title;
	this.description = eventitem.description
	this.start = new Date(eventitem.start);
   	this.end = new Date(eventitem.end);
    this.container = _container;
    this.element = null;
    this.rep = eventitem.repeat;
	this.repeat_matrix = eventitem.rep_matrix;
	this.fullday = eventitem.fullday;
    this.siblings = new Array();
    this.calendar = _calendar;
	this.child = null;
	this.data = eventitem;
	this.canedit = editflag;
	this.foreign_event = false;
	this.has_participants = false;
	this.repeated_event = false;
	
	if (this.rep == "on" || this.child != null) {
		this.repeated_event = true;
	}
	
	if (this.fullday != "on") {
		timeConv(this.start);
		timeConv(this.end)
	} 
	
	if (this.data.fuid != "0" && this.data.feid != "0") {
		this.foreign_event = true;
	}
	
	if (this.data.participants != ";" && this.data.participants != "") {
		this.has_participants = true;
	} 
	
	if (this.data.type == "BDAY") {
		this.canedit = false;
		}
	
	// initialize with fulldayrow container
	this.container = $(FDROW);

    // constructor adds itself to the calendar_events array of the calling object.
	this.calendar.calendar_events.push(this);
  },
  /*
   * Long show logic for calendar events,
   * spawns clones if its a repeated events
   * using Calendar_Item_clone()
   */
  show_: function () { 
		var done = false;
		
		if ( !this.start  || !this.end ) {
			return;
		};
		
		// repeat logic
		if (this.rep == "on") {
			t = this.repeat_matrix.split(";");
			for (var i = 0; i < t.length; i++) {
				if (t[i].split(",").length == 2) {
					st = t[i].split(",")[0];
					ed = t[i].split(",")[1];
					this.repeats(st,ed,i);	
				} else {
					this.repeats(t[i],null,i);
				}			
				}
			done = true;
			return;
		}
		
		if (this.data.type == "PHOL") {
			this.container = $(HOLIDAYROW).getElementsByClassName("hday")[getDayOfWeek(this.start.getDay())]
			this.canedit = false;
			this.has_participants = false;
			this.foreign_event = true;
			new CalendarItemBox(0,0,0,"hol",this);
		/*	if ($(HOLIDAYROW).descendants().length > 9) {
				$(HOLIDAYROW).show();
			}*/
			return;
		}
		
		// is it a fullday event?
  		if (this.fullday == "on" && this.start.toDateString() == this.end.toDateString()) {
			if ( (this.calendar.endofweek <= this.end && 
			 this.start >= this.calendar.endofweek) ||
			 (this.calendar.startofweek >= this.end &&
			 this.calendar.startofweek >= this.start)  ) 
			 { return } else {
			tt = new CalendarItemBox(this.getX(this.start.getDay()),this.getX(this.end.getDay()),0,"fd",this);
			return;
			}
		}
		
		// is the start and end of the event outside the scope of the calendar view
		if ( (this.calendar.endofweek < this.end && 
			 this.start > this.calendar.endofweek) ||
			 (this.calendar.startofweek > this.end &&
			 this.calendar.startofweek > this.start)  ) 
			 { return }
		
		
  		
		// starts before startofweek and ends before endofweek 
		// (overlapping from the start)
    	if ( this.start < this.calendar.startofweek && this.end < this.calendar.endofweek && this.end > this.calendar.startofweek ) {
			tt = new CalendarItemBox(this.getX(this.calendar.startofweek.getDay()),this.getX(this.end.getDay()),0,"fd",this);
			return;
      	}
        
		// does it go through the whole week?
      	if (this.start < this.calendar.startofweek && this.end > this.calendar.endofweek ) {
      		tt = new CalendarItemBox(this.getX(1),this.getX(0),0,"fd",this);
			return;
      }
		
	  // does it go beyond the end of the week?		
      if ( this.start < this.calendar.endofweek && this.end > this.calendar.endofweek && done == false) {
	  	tt = new CalendarItemBox(this.getX(this.start.getDay()),607,0,"fd",this);
		return;     		
      }
    	
	  // multiday rendering (Start string date part != end string date part)	
      if (this.start.toDateString() != this.end.toDateString() && done == false) {
	  	tt = new CalendarItemBox(this.getX(this.start.getDay()),this.getX(this.end.getDay()),0,"fd",this);
		return;
      }
 			
    if (this.siblings.length == 0) {
		this.container = $(HOUR_GRID_ID);
    	this.element = new CalendarItemBox(this.getTop(),(this.getBottom()-this.getTop()),this.getX(this.start.getDay()), "si", this);
    }
	
  },
  // passes the event data to the form 
  edit: function () {
  	paste = $H(this.data)
	calendar_edit_form = this.calendar.edit.smartform[0].frm;
	obj = this;
	this.calendar.edit.show_win();
	
	$("event_edit").show();
	$("event_add").hide();
	
	paste.each ( function (pair) {
		try  { 
			if (pair.key == "start") {
				tmp = DateToNumbers(obj.start);
				set_date("s",tmp);
				$("s_hh").value = obj.start.toTimeString().split(" ")[0].split(":")[0]
				$("s_mn").value = obj.start.toTimeString().split(" ")[0].split(":")[1]
				return;
			} else if ( pair.key == "end" ) {
					tmp = DateToNumbers(obj.end);
					set_date("e",tmp);
					// calendar_edit_form.elements["e_date"].value = DateToNumbers(obj.end)
					$("e_hh").value = obj.end.toTimeString().split(" ")[0].split(":")[0]
					$("e_mn").value = obj.end.toTimeString().split(" ")[0].split(":")[1]
			} else if ( pair.key == "fullday" ) {
					calendar_edit_form.elements["fullday"].checked = (pair.value == "on" ? true : false )
			} else if ( pair.key == "repeat" ) {
					calendar_edit_form.elements["repeat"].checked = (pair.value == "on" ? true : false );
					
					if (obj.child.data.rep_date != "0000-00-00") {
						set_date("rep",obj.child.data.rep_date);
						}
						
			} else if ( pair.key == "remindway") {
					switch (pair.value) {
						case "1":
							$("emailsms_r").checked = true;
							break; 
						case "2":
							$("sms_r").checked = true;
							break;
						case "3":
							$("email_r").checked = true;
							break;
						default:
						break;
						}
			} else {
				calendar_edit_form.elements[pair.key].value = pair.value;
			}
		
		} catch (e) {
			// alert (e);
		}
	});
	this.calendar.adresspicker.refresh();
	calendar_edit_form.elements["o_date"].value = DateToNumbers(obj.start) + ";" + DateToNumbers(obj.end);
	this.calendar.edit.smartform[0].parseform();
	// this.calendar.edit.smartform[0].validate();
	
	if (this.child) {
		$("repeatedevent").show();
		$("event_cancel").show();
		$("esource").enable();
		$("enew").enable();
	} else {
		$("repeatedevent").hide();
		$("event_cancel").hide();
		$("esource").disable();
		$("enew").disable();
	};
	
	event_setrepinterval();
  },
  // sends the event for deletion
  del: function () {
  	if (confirm("Are you sure to delete this event")) {
  	url = "/event/delete";
	url = SITEPREFIX + url;
	qq = $H(this.data);
	
	myR = new Ajax.Request(url,{
			method: 'post',
			parameters: qq,
			onSuccess: function (transport) {
					return true;
				}
			}
		);  
		
		this.calendar.redraw();
		} else { return; }
  },
  // clones the event with new start and enddate 
  repeats: function (dstart,dend,I) {
  	Calendar_Item_clone(this, dstart,dend, I);
  },
  getBottom: function () {
  	return (this.end.getHours() == 0 && this.end.getMinutes() == 0) ? CALBOTTOM : Math.round((this.end.getHours()*60+this.end.getMinutes())/PXSCALE);
    },
  getTop: function () {
  	return Math.round((this.start.getHours()*60+this.start.getMinutes())/PXSCALE);
  },
  getX: function (day) {
  	return (getDayOfWeek(day))*GRDWIDTH+HINDEXWIDTH;
  },
  // magic function to remove the events from the hour grid
  purgeEvents: function () {
  	if (this.siblings.length > 0) {
    	for (var i = 0; i < this.siblings.length; i++) {
      		this.container.removeChild(this.siblings[i].element);
      		}
    	this.siblings = new Array();
    	} else {
			if (this.element) {
				this.container.removeChild(this.element)
				};
		};
	}
}

/*
 * Calendar_Data Class for holding the Ajax Data
 * Calendar_Data.Events[userid] maps to the events from a certain user
 * Calendar_Data.shareIndex maps to an array of this users shared calendars 
 */
var Calendar_Data = new Class.create();
Calendar_Data.prototype = {
	initialize: function (_userid, _data) {
  	this.dayIndex = new Array();
    this.userId = _userid.toString();
	this.Events = _data[1];
    this.shareIndex = _data[0];    
	}
};

var CalendarSettings = {
	Launch: function () {
		url = "/calendar/"+USERID+"/settings";
		$("settings_win").show();
		$("settings_win").update("<div id='loading'>&nbsp;</div>");
		
		new Ajax.Request(url,{
			method: "get",
			onSuccess: function (transport) {
				$("settings_win").update(transport.responseText);
				
			}
		})
	},
	AddHoliday: function () {
		
		var cntry_name = $("country_chooser")[$("country_chooser").selectedIndex].innerHTML;
		var cntry_code = $("country_chooser").value;
		var output_code = {ccode: cntry_code, cname: cntry_name};
		
		if (!$("holidays_"+cntry_code)) {
			
			cDiv = document.createElement("div");
			cDiv.id = "holidays_"+cntry_code;
			cRemButton = document.createElement("input");
			cRemButton.type = "button";
			cRemButton.className = "submit_btn_small";
			cRemButton.value = "-";
			cRemButton.onclick = CalendarSettings.RemoveHolidayE;
			cText = document.createElement("span");
			cText.innerHTML = cntry_name;
			cHiddenInput = document.createElement("input");
			cHiddenInput.type = "hidden";
			cHiddenInput.name = "holiday[]";
			cHiddenInput.value = cntry_code;
			
			cDiv.appendChild(cRemButton);
			cDiv.appendChild(cText);
			cDiv.appendChild(cHiddenInput);

			$("selected_holidays").appendChild(cDiv);
			
		} else {
			return;
		}
	},
	RemoveHolidayE: function (event) {
		e = event || window.event;
		t = e.target || e.srcElement;
		
		$("selected_holidays").removeChild($(t).up());
		
	},
	RemoveHoliday: function (ele) {
	
	$("selected_holidays").removeChild($(ele).up());
		
	},
	SaveHolidays: function () {
		$("eventsettings").request({
			onSuccess: function(){$("settings_win").hide()}
		});
		$("settings_win").update("<div id='loading'>&nbsp;</div>");
		RelopiaRoot.application.redraw();
	},
	CancelHolidays: function() {
		$("settings_win").update("");
		$("settings_win").hide();
	}
}

////////////////////////////////////////////////////
//Functions/////////////////////////////////////////
////////////////////////////////////////////////////


function Calendar_Item_clone(obj, dstart, dend, i) {
	tmp = Object.clone(obj.data);
	if ( !obj.data.start || !obj.data.end ) {
		return;
	}
	dstart = dstart.split(" ");
	dstart_t = obj.data.start.substr(12).split(":");
	tmp.start = new Date(dstart[0],(Number(dstart[1])-1),dstart[2],dstart_t[0],dstart_t[1],dstart_t[2]);
	dend = ( dend == null ? dstart : dend );
	if (dend != dstart) dend = dend.split(" ");
	dend_t = obj.data.end.substr(12).split(":");
	tmp.end = new Date(dend[0],(Number(dend[1])-1),dend[2],dend_t[0],dend_t[1],dend_t[2]);
	cloned_object = new Calendar_Item(tmp, obj.container, obj.calendar, obj.canedit)
	cloned_object.child = obj;
	cloned_object.rep = "off";
	cloned_object.id = obj.id +"_"+ i;
	cloned_object.data.pid = cloned_object.data.id;
	cloned_object.show_();

}

// utility function for day of week (0==monday != 0==sunday)
function getDayOfWeek( dateDay ) {
	if ( dateDay == 0) {
  	return 6;
    } else {
    return dateDay-1;
    }
}


// Calculates the CalendarTime based on Timezone 
function timeConv(t) {
	// t.setUTCHours(t.getUTCHours()+TIMEZONE);
	t.setUTCMinutes( t.getUTCMinutes() + TIMEZONE );
}

// Date to numbers conversion needed for various db stuff 
function DateToNumbers (date) {
	y = date.getFullYear();
	m = date.getMonth()+1
	d = date.getDate();
	if (m < 10) { m = "0"+m}
	if (d < 10) { d = "0"+d }
	return (y + "-" + m + "-" + d );
	// return (d + "." + m +  "." + y)
}

function datefield_populate(year_field_id, month_field_id,day_field_id) {

	var day_field = $(day_field_id);

	var txd = new Date($(year_field_id).value,$(month_field_id).value,0);
	var txd_days = txd.getDate();
	var diff = 31 - txd_days;

	var farr = day_field.descendants();
	try {
		for (var i = 30; i >= 28; i--) {
				farr[i].hide();
			}
			
		for (var i = 28; i < txd_days; i++) {
			farr[i].show();
		}
		
		} catch (e) {
			// console.log(e);
		}
	/*
	day_field.innerHTML = "";
	for (var i = 1; i <= txd_days; i++) {
		val = (i < 10 ? "0"+i : i);
		day_field.innerHTML += "<option value='"+val+"'>"+val+"</option>";
	}
	*/
	
}

function set_date (prefx, datestring) {
	yf_id = prefx + "_date_yy";
	mf_id = prefx + "_date_mm";
	df_id = prefx + "_date_dd"; 
	$(yf_id).value = datestring.split("-")[0];
	$(mf_id).value = datestring.split("-")[1];
	datefield_populate(yf_id,mf_id,df_id);
	$(df_id).value = datestring.split("-")[2];
}

/* Calendar - next week from this monday */

function nextWeek(thisMonday) {
	nM = new Date(thisMonday.getFullYear(),thisMonday.getMonth(),thisMonday.getDate()+7);
	nS = new Date(nM.getFullYear(),nM.getMonth(),nM.getDate()+7);
	return [nM,nS]
}

function prevWeek(thisMonday) {
	pM = new Date(thisMonday.getFullYear(),thisMonday.getMonth(),thisMonday.getDate()-7)
   	pS = new Date(pM.getFullYear(),pM.getMonth(),pM.getDate()+7)
	return [pM,pS];
}

function todaysWeek(thisDay) {
	tM = new Date(thisDay.getFullYear(),thisDay.getMonth(),thisDay.getDate()-thisDay.getDay()+1);
 	tS = new Date(tM.getFullYear(),tM.getMonth(),tM.getDate()+7);
	return [tM,tS];
}

function dateFormat(aDate) {
	return aDate.getFullYear().toString()+"-"+((aDate.getMonth()+1) < 10 ? ("0" + (aDate.getMonth()+1)) : aDate.getMonth()+1)+"-"+ (aDate.getDate() < 10 ? ("0" + aDate.getDate()) : aDate.getDate());
}

function event_setrepinterval() {
	var repInt = $("rep_interval").value;
	if (repInt == "MON2") {
		$("rep_interval_settings").show();
	} else {
		$("rep_interval_settings").hide();
	}
}