function btnEditCalendar_onClick(){
	document.addEventForm.pageMode.value = "adminCalendar";
	document.addEventForm.action = document.addEventForm.action+"#EventForm";
	document.addEventForm.submit();
}

function btnBackCalendar_onClick(){
	document.addEventForm.hidAddEventFormAction.value = "none";
	document.addEventForm.pageMode.value = "";
	document.addEventForm.submit();
}

function btnCancelCalendar_onClick(){
	with (document.addEventForm){
		//reset form
		document.getElementById("btnEventAddUpdate").src="/images/button_addevent.gif";
		hidAddEventFormAction.value = "insert";	
		hidEventID.value = "";
		txtContactName.value = "";
		txtContactPhone.value = "";
		txtContactEmail.value = "";
		if(selDepartment.options.namedItem("Expired")!=null){ 
			selDepartment.remove(selDepartment.options.namedItem("Expired").index)
		}
		selDepartment.options.namedItem(primaryDep).selected = true;
		txtEventName.value = "";
		var curdate = new Date();
		txtStartDate.value = (curdate.getMonth()+1)+"/"+curdate.getDate()+"/"+curdate.getYear();
		txtStartTime.value = "";
		var futureDate = new Date(curdate.getYear(),curdate.getMonth()+3,curdate.getDate());
		txtEndDate.value = (futureDate.getMonth()+1)+"/"+futureDate.getDate()+"/"+futureDate.getYear();
		txtEndTime.value = "";
		chkShowTime.checked = false;
		txtLocation.value = "";
		if(cboCategory.options.namedItem("Expired")!=null){ 
			cboCategory.remove(cboCategory.options.namedItem("Expired").index)
		}
		cboCategory.options.item("0").selected = true;
		chkHighlight.checked = false;
		chkGeneralCalendar.checked = true;
		txtEventURL.value = "";
		txtEventDescription.value = "";
		//alert('here');
		var x=document.getElementById("lstRegistration");
		for (i=0;i<x.length;i++){
			x.options[i].selected = false;
		}
	}
}

function btnDateRangeSearch_onClick(script_name, dep){
	//Construct the url query string from the calendarSearchForm
	//txtStartDate and txtEndDate
	var start_date = new Date(document.calendarSearchForm.txtStartDate.value);
	var end_date = new Date(document.calendarSearchForm.txtEndDate.value);
	var queryString="?m1="+(start_date.getMonth()+1)+
									"&d1="+start_date.getDate()+
									"&y1="+start_date.getYear()+
									"&m2="+(end_date.getMonth()+1)+
									"&d2="+end_date.getDate()+
									"&d2="+end_date.getDate()+
									"&GroupID="+dep;
									
	document.calendarSearchForm.action = script_name+queryString;
	document.calendarSearchForm.submit();
}

function btnMonthSearch_onClick(script_name, dep){
	//Construct the url query string from the calendarSearchForm
	//txtStartDate and txtEndDate
	var cboSelectedMonth = document.calendarSearchForm.cboMonth
	var strSelected = cboSelectedMonth.options[cboSelectedMonth.selectedIndex].value
	var strOps = strSelected.split(",");
	var month = strOps[0];
	var year = strOps[1];
	var queryString="?m="+month+"&y="+year+"&GroupID="+dep;
	
	document.calendarSearchForm.action = script_name+queryString;
	document.calendarSearchForm.submit();
}

function btnEventAddUpdate_onClick(){
	//form validation
		var addEventFormIsValid = true;
		var strValidationHelp = "";
		var tmpForm = document.addEventForm;
		//required fields
		if(tmpForm.txtContactName.value == ""){
			strValidationHelp = strValidationHelp + "Contact Name is a required field\n";
			addEventFormIsValid = false;
		}
		/*if(tmpForm.txtContactPhone.value == ""){
			strValidationHelp = strValidationHelp + "Contact Phone is a required field\n";
			addEventFormIsValid = false;
		}
		if(tmpForm.txtContactEmail.value == ""){
			strValidationHelp = strValidationHelp + "Contact Email is a required field\n";
			addEventFormIsValid = false;
		}*/
		//alert(tmpForm.selDepartment.selectedIndex+" "+tmpForm.chkGeneralCalendar.checked)
		if((tmpForm.selDepartment.selectedIndex == 0) && (tmpForm.chkGeneralCalendar.checked==false)){
			strValidationHelp = strValidationHelp + "Department or General Calendar is a required field\n";
			addEventFormIsValid = false;
		}
		if(tmpForm.txtEventName.value == ""){
			strValidationHelp = strValidationHelp + "Event Name is a required field\n";
			addEventFormIsValid = false;
		}
		if(tmpForm.txtStartDate.value == ""){
			strValidationHelp = strValidationHelp + "Start Date is a required field\n";
			addEventFormIsValid = false;
		}
		if(tmpForm.txtEndDate.value == ""){
			strValidationHelp = strValidationHelp + "End Date is a required field\n";
			addEventFormIsValid = false;
		}
		if(tmpForm.txtLocation.value == ""){
			strValidationHelp = strValidationHelp + "Location is a required field\n";
			addEventFormIsValid = false;
		}
		if(tmpForm.cboCategory.selectedIndex == 0){
			strValidationHelp = strValidationHelp + "Category is a required field\n";
			addEventFormIsValid = false;
		}
		
		//regExp validations
		if(tmpForm.txtContactPhone.value != ""){
			if( !(tmpForm.txtContactPhone.value.match(/(\d\d\d( |-)\d\d\d( |-)\d\d\d\d|\(\d\d\d\) \d\d\d-\d\d\d\d)/))){
				//alert(tmpForm.txtContactPhone.value.match(/(\d\d\d( |-)\d\d\d( |-)\d\d\d\d|\(\d\d\d\) \d\d\d-\d\d\d\d)/)
				strValidationHelp = strValidationHelp + "Please reformat Contact Phone\n";
				addEventFormIsValid = false;
			}
		}
		if(tmpForm.txtContactEmail.value != ""){
			if( !(tmpForm.txtContactEmail.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/))){
				strValidationHelp = strValidationHelp + "Please reformat Contact Email\n";
				addEventFormIsValid = false;
			}
		}
		var reDatePattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [APap][Mm]))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
		if( !(tmpForm.txtStartDate.value.match(reDatePattern))){
			strValidationHelp = strValidationHelp + "Please reformat the Start Date\n";
			addEventFormIsValid = false;
		}
		if( !(tmpForm.txtEndDate.value.match(reDatePattern))){
			strValidationHelp = strValidationHelp + "Please reformat End Date\n";
			addEventFormIsValid = false;
		}
	
		if (addEventFormIsValid){
			//maintain page mode and submit	
			showPleaseWait();
			document.addEventForm.pageMode.value = "adminCalendar";
			document.addEventForm.submit();
		}
		else{
		 alert(strValidationHelp);
		}
}

function deleteEvent(eventID){
	document.addEventForm.pageMode.value = "adminCalendar";
	document.addEventForm.hidEventID.value = eventID;
	document.addEventForm.hidAddEventFormAction.value = "delete";
	document.addEventForm.submit();	
}

function editEvent(eventID){
	document.addEventForm.hidEventID.value = eventID;
	DWREngine._execute(	'/calendar/event_xml.cfc',	null,
											'getCalendarEventDetail', eventID, editEvent_callback);
	DWREngine._execute(	'/calendar/event_xml.cfc',	null,
											'populateEventRegistration', eventID, 
											function(q){
												//document.write(q.dump() );
												with (document.getElementById('lstRegistration')){
													for(i=0;i<length;i++){
														options[i].selected = false;
													}
													for (j=0;j<q.getRowCount();j++){
														if (q.isselected[j]){
															for(i=0;i<length;i++){
																if(options[i].value == q.form_id[j]) options[i].selected = true;
															}
														}
													}
												}											
											});				
}

function editEvent_callback(q){
	//document.write(q.dump() );
	//paramater validation
	var departmentMatch = true;
	if (q.department_ld[0] == null){
		q.department_ld[0] = 0;
	}else{
		departmentMatch = false;
		for (dep in oDep){
			//alert(dep+" and "+q.department_ld[0]);
			if (dep.toUpperCase() == q.department_ld[0]){
				departmentMatch = true;		
				//alert("TRUE");
			}
		}	
	}
	if (q.category_ld[0] == null) q.category_ld[0] = 0;
	//if this is not the users department do not allow them to edit

	if(!departmentMatch){
		 btnCancelCalendar_onClick();
		alert("The Event that you have selected does not belong to your Department(s)")
	}else{
		with(document.addEventForm){
				txtContactName.value = q.contact_name[0];
				txtContactPhone.value = q.contact_phone[0];
				txtContactEmail.value = q.contact_email[0];
				var dep = q.department_ld[0];
				if(selDepartment.options.namedItem("Expired")!=null){ 
					selDepartment.remove(selDepartment.options.namedItem("Expired").index)
				}
				if(selDepartment.options.namedItem(dep) == null){
					//it is an expired code because it is not in the dropdown 
					//add it to the dropdown in this instance
					var opt = document.createElement('OPTION');
					opt.value = dep;
					opt.id = "Expired";
					opt.text = q.department[0];				
					selDepartment.options.add(opt, selDepartment.options.length+1)
					selDepartment.options.namedItem("Expired").selected = true;
				}else{
					selDepartment.options.namedItem(dep).selected = true;
				}
				txtEventName.value = q.event_name[0];
				txtStartDate.value = q.start_dt[0];
				txtStartTime.value = q.start_tm[0];
				txtLocation.value = q.location[0];
				txtEndDate.value = q.end_dt[0];
				txtEndTime.value =  q.end_tm[0];
				chkShowTime.checked = eval(q.showtime[0]);
				var cat = q.category_ld[0];
				if(cboCategory.options.namedItem("Expired")!=null){ 
					cboCategory.remove(cboCategory.options.namedItem("Expired").index)
				}
				if(cboCategory.options.namedItem(cat) == null){
					//it is an expired code because it is not in the dropdown 
					//add it to the dropdown in this instance
					var opt = document.createElement('OPTION');
					opt.value = cat;
					opt.id = "Expired";
					opt.text = q.category[0];				
					cboCategory.options.add(opt, cboCategory.options.length+1)
					cboCategory.options.namedItem("Expired").selected = true;
				}else{
					cboCategory.options.namedItem(cat).selected = true;
				}
				chkHighlight.checked = eval(q.highlight[0]);
				chkGeneralCalendar.checked = eval(q.general_calendar[0]);
				txtEventURL.value = q.url[0];
				txtEventDescription.value = q.description[0];
				hidAddEventFormAction.value = "update";
				document.getElementById("btnEventAddUpdate").src="/images/button_update.gif";
					//alert("Finished")
		}
	}
}
