// submit_form
function submit_form(action,message){
	form_valid = true;
	
	// category_id
	if(document.form_name.category_id[document.form_name.category_id.selectedIndex].value == ""){
		alert("Please choose a category.");
		return(false);
	}
	
	// title
	if(document.form_name.title.value == ""){
		alert("Please enter an event name.");
		return(false);
	}
	
	// start_datetime_date
	if(document.form_name.start_datetime_date.value == ""){
		alert("Please enter the start date in the following format: mm/dd/yyyy");
		return(false);
	}
	
	// repeats_until: If the event has a repeat until value
	repeats_dropdown = document.getElementById("recurring_dropdown");
	repeat_type = repeats_dropdown[repeats_dropdown.selectedIndex].value;
	if(repeat_type == "daily" || repeat_type == "weekly" || repeat_type == "monthly" || repeat_type == "yearly"){
		// if it repeats until a specific date
		if(document.form_name.repeats_until[1].checked == true){
			// ensure a date exists
			if(document.form_name.repeats_until_date.value == ""){
				alert("Please enter the until date in the following format: mm/dd/yyyy");
				return(false);
			}
			
			// date_start: ensure the end date is after the start date
			var date_start = new String(document.form_name.start_datetime_date.value);
			date_start_parts = date_start.split("/");
			date_start_number = date_start_parts[2]+""+date_start_parts[0]+""+date_start_parts[1];
			
			// date_end
			var date_end = new String(document.form_name.repeats_until_date.value);
			date_end_parts = date_end.split("/");
			date_end_number = date_end_parts[2]+""+date_end_parts[0]+""+date_end_parts[1];
			
			// the start date must be before the end date
			if(date_start_number >= date_end_number){
				alert("Please choose an end date later than your start date.");
				return(false);
			}
		}
	}
	
	// specific_days
	if(repeat_type == "specific_days"){
		if(document.form_name.repeat_specific_days_field.value == ""){
			alert("Please select at least one other day on which this event recurs.");
			return(false);
		}
	}
	
	// weekly
	if(repeat_type == "weekly"){
		if(document.form_name.weekly_day_sun.checked == false 
			&& document.form_name.weekly_day_mon.checked == false 
			&& document.form_name.weekly_day_tue.checked == false 
			&& document.form_name.weekly_day_wed.checked == false 
			&& document.form_name.weekly_day_thu.checked == false 
			&& document.form_name.weekly_day_fri.checked == false 
			&& document.form_name.weekly_day_sat.checked == false){
			alert("Please choose the day(s) of the week on which this event recurs.");
			return(false);
		}
	}
	
	// image_upload
	image_upload = new String(document.form_name.image_upload.value);
	image_upload = image_upload.toLowerCase();
	
	// only validate file type if a file has been selected
	if(image_upload != ""){
		if(image_upload.search(".jpg") == -1 && image_upload.search(".jpeg") == -1 && image_upload.search(".gif") == -1){
			alert("Please upload an image in either .gif or .jpeg format.");
			return(false);
		}
	}	
	
	// location: venue
	if(document.form_name.location[0].checked == true){
		if(document.form_name.venue_id.value == ""){
			alert('Please use the suggest field to choose a venue. If your venue does not appear, you may "Add a New Venue."');
			return(false);
		}
	}
	
	// location: private_address
	if(document.form_name.location[1].checked == true){
		// private_name
		if(document.form_name.private_name.value == ""){
			alert("Please enter a location name (i.e. John's Apartment).");
			return(false);
		}
		
		// address
		if(document.form_name.address.value == ""){
			alert('Please enter a street address.');
			return(false);
		}
		
		// city_id
		if(document.form_name.city_id.value == ""){
			alert('Please use the suggest field to choose a city.');
			return(false);
		}
	}
	
	// location: online
	if(document.form_name.location[2].checked == true){
		// online_name
		if(document.form_name.online_name.value == ""){
			alert("Please enter a website name.");
			return(false);
		}
		
		// online_url
		if(document.form_name.online_url.value == "" || document.form_name.online_url.value == "http://"){
			alert("Please enter a website URL.");
			return(false);
		}
	}
	
	// validate the time fields
	if(document.form_name.start_datetime_allday.checked == false){
		date_message = "Please enter the date in the following format:\nmm/dd/yyyy";
		time_message = "Please enter the time in the following format:\n1am or 3:15pm";
		
		// check the start time
		var datestart_parts = new String(document.form_name.start_datetime_date.value);
		var time_parts = new String(document.form_name.start_datetime_time.value);
		
		// create a numeric representation of the date
		datestart_parts_array = datestart_parts.split("/");
		if(datestart_parts_array.length != 3){
			form_valid = false;
			alert(date_message);
			return(false);
		}
		
		// look for AM time
		if(time_parts.search("am") >= 0){
			ampm = "am";
			not_am = false;
		
			time_parts = time_parts.replace("am","");
			time_parts = time_parts.replace(" ","");
			
			// look for minutes
			time_parts_array = time_parts.split(":");
			if(time_parts_array.length == 2){
				time_hour = time_parts_array[0];
				time_minutes = time_parts_array[1];
			}else{
				time_hour = time_parts;
				time_minutes = "00";
			}
			
			// generate a 24 hour value
			time_start_hour_military = Number(time_hour);
		}else{
			not_am = true;
		}
		
		// look for PM time
		if(time_parts.search("pm") >= 0){
			ampm = "pm";
			not_pm = false;
			
			time_parts = time_parts.replace("pm","");
			time_parts = time_parts.replace(" ","");
			
			// look for minutes
			time_parts_array = time_parts.split(":");
			if(time_parts_array.length == 2){
				time_hour = time_parts_array[0];
				
				time_minutes = time_parts_array[1];
			}else{
				time_hour = time_parts;
				time_minutes = "00";
			}
			
			// generate a 24 hour value
			time_start_hour_military = (Number(time_hour)+12);
		}else{
			not_pm = true;
		}
		
		// the time could not be detected as either AM or PM
		if(not_am == true && not_pm == true){
			form_valid = false;
			alert(time_message);
			return(false);
		}else{
			time_start_minutes = time_minutes;
		}
		
		// look for a valid hour value
		if(time_hour > 12){
			form_valid = false;
			alert(time_message);
			return(false);
		}
		
		// look for a valid minutes value
		if(time_minutes > 60){
			form_valid = false;
			alert(time_message);
			return(false);
		}
		
		// add the full time back to the time field
		document.form_name.start_datetime_time.value = time_hour+":"+time_minutes+""+ampm;
		
		// check the end time
		if(document.form_name.end_datetime_time.value != "" && document.form_name.end_datetime_time.value != "(All Day)"){
			// the end date is also the start date
			var dateend_parts = new String(document.form_name.start_datetime_date.value);
			var time_parts = new String(document.form_name.end_datetime_time.value);
			
			// look for AM time
			if(time_parts.search("am") >= 0){
				ampm = "am";
				not_am = false;
			
				time_parts = time_parts.replace("am","");
				time_parts = time_parts.replace(" ","");
				
				// look for minutes
				time_parts_array = time_parts.split(":");
				if(time_parts_array.length == 2){
					time_hour = time_parts_array[0];
					
					time_minutes = time_parts_array[1];
				}else{
					time_hour = time_parts;
					time_minutes = "00";
				}
				
				// generate a 24 hour value
				time_end_hour_military = time_hour;
			}else{
				not_am = true;
			}
			
			// look for PM time
			if(time_parts.search("pm") >= 0){
				ampm = "pm";
				not_pm = false;
				
				time_parts = time_parts.replace("pm","");
				time_parts = time_parts.replace(" ","");
				
				// look for minutes
				time_parts_array = time_parts.split(":");
				if(time_parts_array.length == 2){
					time_hour = time_parts_array[0];
					
					time_minutes = time_parts_array[1];
				}else{
					time_hour = time_parts;
					time_minutes = "00";
				}
				
				// generate a 24 hour value
				time_end_hour_military = (Number(time_hour)+12);
			}else{
				not_pm = true;
			}
			
			// the time could not be detected as either AM or PM
			if(not_am == true && not_pm == true){
				form_valid = false;
				alert(time_message);
				return(false);
			}else{
				time_end_minutes = time_minutes;
			}
			
			// look for a valid hour value
			if(time_hour > 12){
				form_valid = false;
				alert(time_message);
				return(false);
			}
			
			// look for a valid minutes value
			if(time_minutes > 60){
				form_valid = false;
				alert(time_message);
				return(false);
			}
			
			// add the full time back to the time field
			document.form_name.end_datetime_time.value = time_hour+":"+time_minutes+""+ampm;
		
			// make sure start time is before the end time
			if((time_start_hour_military > time_end_hour_military)){
				form_valid = false;
				alert("The end time cannot be before the start time.");
				return(false);
			}
			
			if((time_start_hour_military == time_end_hour_military && time_start_minutes > time_end_minutes)){
				form_valid = false;
				alert("The end time cannot be before the start time.");
				return(false);
			}
		}
	}
	
	// submit the form if validated
	if(form_valid == true){
		// enable these fields so that their contents will be posted
		enable_contact_form();
		
		// reveal the message
		toggle_visibility('submit_message','submit_buttons');
		
		// submit_message_id
		submit_message_id = document.getElementById('submit_message');
		submit_message_id.innerHTML = message;
		
		// control the requested action
		switch(action){
			case 'post':
				document.form_name.post_request.value = "post";
			break;
			case 'save_draft':
				document.form_name.post_request.value = "save_draft";
			break;
			case 'preview':
				document.form_name.post_request.value = "preview";
			break;
		}
		
		// disabled since it may create duplicate form posts
		//document.form_name.submit();
		return true;
	}else{
		return false;
	}
}

// expand_all
function expand_all(){
	var expand_header_id = document.getElementById('expand_header');
	
	g1 = document.getElementById("group_Details").style.display;
	g2 = document.getElementById("group_Description").style.display;
	g3 = document.getElementById("group_Location").style.display;
	g4 = document.getElementById("group_Tickets").style.display;
	g5 = document.getElementById("group_Contact Info").style.display;
	
	// this ID doesn't exist on an edit page
	if(document.getElementById('image_People to Invite')){
		g6 = document.getElementById("group_People to Invite").style.display;
	}else{
		g6 = "";
	}
	
	g7 = document.getElementById("group_Privacy").style.display;
	
	// if they're all open then close them, otherwise open them all
	if(g1 == "" && g2 == "" && g3 == "" && g4 == "" && g5 == "" && g6 == "" && g7 == ""){
		// the header icon
		expand_header_id.src = 'templates/images/icons/right.png';
		
		// group icons
		document.getElementById('image_Details').src = 'templates/images/icons/right.png';
		document.getElementById('image_Description').src = 'templates/images/icons/right.png';
		document.getElementById('image_Location').src = 'templates/images/icons/right.png';
		document.getElementById('image_Tickets').src = 'templates/images/icons/right.png';
		document.getElementById('image_Contact Info').src = 'templates/images/icons/right.png';
		
		// this ID doesn't exist on an edit page
		if(document.getElementById('image_People to Invite')){
			document.getElementById('image_People to Invite').src = 'templates/images/icons/right.png';
		}
		
		document.getElementById('image_Privacy').src = 'templates/images/icons/right.png';
		
		// group visibility
		visibility("group_Details","hide");
		visibility("group_Description","hide");
		visibility("group_Location","hide");
		visibility("group_Tickets","hide");
		visibility("group_Contact Info","hide");
		
		// this ID doesn't exist on an edit page
		if(document.getElementById('image_People to Invite')){
			visibility("group_People to Invite","hide");
		}
		
		visibility("group_Privacy","hide");	
	}else{
		// the header icon
		expand_header_id.src = 'templates/images/icons/down.png';
		
		// group icons
		document.getElementById('image_Details').src = 'templates/images/icons/down.png';
		document.getElementById('image_Description').src = 'templates/images/icons/down.png';
		document.getElementById('image_Location').src = 'templates/images/icons/down.png';
		document.getElementById('image_Tickets').src = 'templates/images/icons/down.png';
		document.getElementById('image_Contact Info').src = 'templates/images/icons/down.png';
		
		// this ID doesn't exist on an edit page
		if(document.getElementById('image_People to Invite')){
			document.getElementById('image_People to Invite').src = 'templates/images/icons/down.png';
		}
		
		document.getElementById('image_Privacy').src = 'templates/images/icons/down.png';
		
		// group visibility
		visibility("group_Details","reveal");
		visibility("group_Description","reveal");
		visibility("group_Location","reveal");
		visibility("group_Tickets","reveal");
		visibility("group_Contact Info","reveal");
		
		// this ID doesn't exist on an edit page
		if(document.getElementById('image_People to Invite')){
			visibility("group_People to Invite","reveal");
		}
		
		visibility("group_Privacy","reveal");
	}
}