// JavaScript Document

function get_month_name (month_num) {
	// Set value of date of news release.
	// If 0 is passed in, it means that the user set a value for the day or the year without setting a value for the month.  The user will need to set a value
	// for the month (before he/she clicks Submit).  Function Validate_Release_Entry_Form will ensure that the user enters a month.
					
	switch (month_num) {
		case "0": name_of_month = "";
					break;
		case "1": name_of_month = "January";
					break;
		case "2": name_of_month =  "February";
					break;
		case "3": name_of_month =  "March";
					break;
		case "4": name_of_month =  "April";
					break;
		case "5": name_of_month =  "May";
					break;
		case "6": name_of_month =  "June";
					break;
		case "7": name_of_month =  "July";
					break;
		case "8": name_of_month =  "August";
					break;
		case "9": name_of_month =  "September";
					break;
		case "10": name_of_month =  "October";
					break;
		case "11": name_of_month =  "November";
					break;
		case "12": name_of_month =  "December";
					break;			
	}
	
	return (name_of_month);
}


function get_month_abbrev (month_num) {
	// Returns abbreviation associated with month number.
					
	switch (month_num) {
		case "1": name_of_month = "jan";
					break;
		case "2": name_of_month =  "feb";
					break;
		case "3": name_of_month =  "mar";
					break;
		case "4": name_of_month =  "apr";
					break;
		case "5": name_of_month =  "may";
					break;
		case "6": name_of_month =  "jun";
					break;
		case "7": name_of_month =  "july";
					break;
		case "8": name_of_month =  "aug";
					break;
		case "9": name_of_month =  "sept";
					break;
		case "10": name_of_month =  "oct";
					break;
		case "11": name_of_month =  "nov";
					break;
		case "12": name_of_month =  "dec";
					break;			
	}
	
	return (name_of_month);
}


function get_portion_of_item (fieldname, fielditem) {
	// Returns portion of fieldname after fielditem.  For example, assume the following:
	//  	fieldname.value is apr_05/test.jpg
	//		this function is called in this manner:
	// 			get_portion_of_item (photo_filename, "/")
	//		the function returns test.jpg
	
	var entire_fieldname_length;			// length of fieldname(15)
	var array_location_of_fielditem;		// location of fielditem within fieldname (6)
	var returned_portion_start_location;	// array location of 1st character that will be returned (7)
	var returned_portion_end_location;		// array location of last character that will be returned (14)
	var length_of_returned_portion;			// length of item that will be returned (8)
	
//	length of fieldname
	entire_fieldname_length = fieldname.value.length;
	
	
// location of fielditem within fieldname
	array_location_of_fielditem = fieldname.value.indexOf(fielditem);
	returned_portion_end_location = (entire_fieldname_length - 1);
	returned_portion_start_location = array_location_of_fielditem + 1;
	returned_portion_length = returned_portion_end_location - returned_portion_start_location + 1;
	return (fieldname.value.substring(returned_portion_start_location, entire_fieldname_length) );
}


function set_photo_filename() {
	var month_abbrev, short_year, filename_only;
	
	month_abbrev = get_month_abbrev(news_release.month_of_news_release.value);
	short_year = news_release.year_of_news_release.value.substring(2, 5);	
	if (news_release.photo_filename.value != "") {
		filename_only = get_portion_of_item(news_release.photo_filename, "/");
		news_release.photo_filename.value = month_abbrev + news_release.day_of_news_release.value + "_" + short_year + "/" + filename_only;
	}
	else		
		news_release.photo_filename.value = month_abbrev + news_release.day_of_news_release.value + "_" + short_year + "/";
}



function Validate_Release_Entry_Form(news_release) {
	
	
	with (news_release) {
		var form_valid_so_far;
		var filename_only, curr_year;
		
		filename_only = get_portion_of_item(photo_filename, "/");
		curr_year = year_of_news_release.value.substring(2, 5);
		

		if (reqd_value_missing (contactname1, "", "Please enter the name of the contact person for this news release.") )
			return (false);
		if (reqd_value_missing (contactphone1, "", "Please enter the telephone number of the contact person for this news release.") )
			return (false);		
		
		// Ensure that the user entered a valid date for this news release.
			
		if (reqd_value_missing (month_of_news_release, "0", "Please enter the month associated with this news release.") )
			return (false); 
		
		if (reqd_value_missing (day_of_news_release, "0", "Please enter the day associated with this news release.") )
			return (false);
		
		if (reqd_value_missing (year_of_news_release, "0", "Please enter the year associated with this news release.") )
			return (false);

		if (date_inaccurate_format (month_of_news_release, day_of_news_release, year_of_news_release) )
			return (false); 
			
		if (reqd_value_missing (title, "", "Please enter the title of this news release.") )
			return (false);
			
				
		if (reqd_value_missing (body, "", "Please enter the body of this news release.") )
			return (false); 
						
		if (reqd_radio_value_missing(is_cutline, "Please indicate whether this news release is a cutline."))
			return (false);
						
		if (reqd_radio_value_missing(photo_present, "Please indicate whether a photograph will be displayed with this news release."))
			return (false);

		
		// If a photo is present, obtain information about it.  Additionally, ensure that the user did not enter information 
		// about a photo, if no photo is present.
		
		var photo_button_selected = which_radio_button_value_was_selected (photo_present);

		if (photo_button_selected == "user_answered_yes") { // user indicated that there is a photograph associated with this news release.
			if (reqd_value_missing (photo_filename, "", "Please enter the name of the photograph associated with this news release.") )
				return (false); 
				
			if (reqd_value_missing (photo_alt, "", "Please enter the alt text that will be displayed with this photograph.") )
				return (false); 
				
			if (reqd_value_missing (photo_width, "", "Please enter the width of the photograph associated with this news release.") )
				return (false); 
			
			form_valid_so_far = only_valid_chars_entered ("0123456789", photo_width.value.length, photo_width.value);
			if (!form_valid_so_far) {
				alert ("The value you entered associated with the width of this photograph appears to be invalid.  Please re-examine the value in this field.");
				return (false);
			}				
							
			if (reqd_value_missing (photo_height, "", "Please enter the height of the photograph associated with this news release.") )
				return (false); 
				
			form_valid_so_far = only_valid_chars_entered ("0123456789", photo_height.value.length, photo_height.value);
			if (!form_valid_so_far) {
				alert ("The value you entered associated with the height of this photograph appears to be invalid.  Please re-examine the value in this field.");
				return (false);
			}				

		}
		
		
		
		else { // user indicated that there is no photograph associated with this news release.
			
			if (value_set_unnecessarily (photo_filename, "", "You indicated that a photograph will not be displayed with this news release.  However, you have also entered a value for the name of the photograph associated with this news release.  Please re-examine the value in this field.") )
				return (false); 
	
			if (value_set_unnecessarily (photo_alt, "", "You indicated that a photograph will not be displayed with this news release.  However, you have also entered alt text to be displayed along with a photograph.  Please re-examine the value in this field.") )
				return (false); 
	
			if (value_set_unnecessarily (photo_width, "", "You indicated that a photograph will not be displayed with this news release.  However, you have also entered a value for the width of a photograph.  Please re-examine the value in this field.") )
				return (false); 
	
			if (value_set_unnecessarily (photo_height, "", "You indicated that a photograph will not be displayed with this news release.  However, you have also entered a value for the height of a photograph.  Please re-examine the value in this field.") )
				return (false); 
					
		}
		
		
	} // with
		
	return (true);
} // function Validate_News_Release_Entry_Form