// JavaScript Document

function get_selected_radio_button_value (groupname) {
	// This function returns the value of the radio button that the user clicked.
	// groupname - Name of the set of radio buttons in question.
	// error_msg - text string provided to user if he/she did not select an 
		// option.
	var i;
	for (i = 0; i < groupname.length; i++)
		if (groupname[i].checked)
			return (groupname[i].value);
}

function only_valid_chars_entered (valid_chars, fieldlength, fieldvalue) {
	var allValid = true;
	for (i = 0;  i < fieldlength;  i++)
	{
		ch = fieldvalue.charAt(i);
   		for (j = 0;  j < valid_chars.length;  j++)
   			if (ch == valid_chars.charAt(j))
   				break;
    	if (j == valid_chars.length)
    	{
      		allValid = false;
      		break;
    	}
  	}  		
 	return (allValid);
}


function reqd_value_missing (fieldname, defaultvalue, error_msg) {
// fieldname - field being tested
// defaultvalue - value of this field before user enters a value
// if fieldname equals defaultvalue, the user did not provide a value 
// for a require field.  This function prompts the user to enter 
// a value in the field.
	if (fieldname.value == defaultvalue) {
		fieldname.focus();
		alert (error_msg);		
		return (true);
	}
}


function value_set_unnecessarily (fieldname, defaultvalue, error_msg) {
// fieldname - field being tested
// defaultvalue - value of this field before user enters a value
// if fieldname is not equal to defaultvalue, the user entered a value 
// for this field.  This function is called when a value should not be 
// set.  If the user has set a value that he/she should not have set,
// function prompts the user to re-examine the value in the field.

	if (fieldname.value != defaultvalue) {
		alert (error_msg);
		fieldname.focus();
		return (true);
	}
}



function reqd_radio_value_missing (groupname, error_msg) {
		// groupname - Name of the set of radio buttons being validated.
		// error_msg - text string provided to user if he/she did not select an 
		// option.
			var i, response_entered;
			response_entered = false;
			for (i = 0; i < groupname.length; i++)
				if (groupname[i].checked)
					response_entered = true;
  	
			if (!response_entered) {
				alert (error_msg);
				groupname[1].focus();
				return (true);
			}	
}


function field_value_incorrect_size (fieldname, correctfieldsize, error_msg) {
	if (fieldname.value.length != correctfieldsize) {
		alert (error_msg);
		fieldname.focus();
		return (true);
	}	
}

function year_is_leap_year (curr_year) {
	// Determine whether curr_year is a leap year.
	// 1.  Every year divisible by 4 is a leap year - except the references in items 2 and 3.
	// 2.  Years divisible by 100 that are also divisible by 400 are leap years.
	// 3.  Years divisible by 100 that are not divisible by 400 are not leap years.
	var is_leap_year;
	
	if ( (curr_year % 4) == 0)
		is_leap_year = "true"
	else
		is_leap_year = "false";
		
	if ( (curr_year % 100) == 0) 
		if ( (curr_year % 400) != 0)
			is_leap_year = "false";

	return (is_leap_year);

} 

function date_inaccurate_format (month_fieldname, day_fieldname, year_fieldname) {	
	var curr_year_is_leap_year;
	// Months with 30 days
	if ( (month_fieldname.value == 4) ||  (month_fieldname.value == 6) ||  (month_fieldname.value == 9) ||  (month_fieldname.value == 11) )
		if (day_fieldname.value > 30) {
			alert ("The date that was entered was invalid.  Please re-examine the date.");
			month_fieldname.focus ();
			return (true);
		}
	
	// Check for February	
	curr_year_is_leap_year = year_is_leap_year(year_fieldname.value);
	if ( (month_fieldname.value == 2) && (curr_year_is_leap_year == "false") )
		if (day_fieldname.value > 28) {
			alert ("The date that was entered was invalid.  Please re-examine the date.");
			month_fieldname.focus ();
			return (true);
		}
		
	if ( (month_fieldname.value == 2) && (curr_year_is_leap_year == "true") )
		if (day_fieldname.value > 29) {
			alert ("The date that was entered was invalid.  Please re-examine the date.");
			month_fieldname.focus ();
			return (true);
		}
		
	if ( (month_fieldname.value == 0) || (day_fieldname.value == 0) || (year_fieldname.value == 0) )	{
		alert ("The date that was entered was invalid.  Please re-examine the date.");
		month_fieldname.focus ();
		return (true);
	}
}



function which_radio_button_value_was_selected (groupname) {
// This function works when we need to determine which of two radio buttons the user selected. It also lets us know
// if the user did not select any radio buttons.  This function works only if there are exactly two radio buttons
// in the group.

	if ( (groupname[0].checked == false) && (groupname[1].checked == false) )
		return ("user_did_not_respond");

	if (groupname[0].checked == true)
		return ("user_answered_yes");

	if (groupname[1].checked == true)
		return ("user_answered_no");
}


function validate_phone_number (areacode, exchange, extension) {
	// This function validates a telephone number that was sent to it.

	var valid_so_var;

	// Validate the area code.
	valid_so_far = only_valid_chars_entered ("0123456789", areacode.value.length, areacode.value);	
	if (!valid_so_far) {
		alert("The telephone number that you entered appears to be invalid.  Please re-examine the value in this field.");
    	areacode.focus();
    	return (false);
	}
	
	if (field_value_incorrect_size (areacode, 3, "This part of the telephone number should be 3 digits long.  Please re-examine the value in this field.") )
		return (false);
			
	// Validate the exchange.		
	valid_so_far = only_valid_chars_entered ("0123456789", exchange.value.length, exchange.value);
	if (!valid_so_far) {
 		alert("The telephone number that you entered appears to be invalid.  Please re-examine the value in this field.");
   		exchange.focus();
   		return (false);
	} 
	
	if (field_value_incorrect_size (exchange, 3, "This part of the telephone number should be 3 digits long.  Please re-examine the value in this field.") )
		return (false);
	
	// Validate the extension
	valid_so_far = only_valid_chars_entered ("0123456789", extension.value.length, extension.value);
  	if (!valid_so_far) {
   		alert("The telephone number that you entered appears to be invalid.  Please re-examine the value in this field.");
   		extension.focus();
   		return (false);
 	}
		
	if (field_value_incorrect_size (extension, 4, "This part of the telephone number should be 4 digits long.  Please re-examine the value in this field.") )
		return (false);  				

	return (true);
}


function Validate_Login (form_under_test) {
// fieldname - field being tested
// defaultvalue - value of this field before user enters a value
// if fieldname equals defaultvalue, the user did not provide a value 
// for a require field.  This function prompts the user to enter 
// a value in the field.

	with (form_under_test) {
		if (reqd_value_missing (username, "", "Please enter your username."))
			return (false);
		
		if (reqd_value_missing (password, "", "Please enter your password."))
			return (false);
		
		return (true);
	} // with
		
} // function Validate_Login



