function checkCountryField ( theField ) {
	var why = '';
	var country = trimString (theField.value.toLowerCase());

	if ( country == "united states" || 
		country == "u.s." || 
		country == "us" || 
		country == "u.s.a." || 
		country == "usa" )
		why += "Do not enter a country for the United States.\n";

	return why;
}

function checkPersonalFields ( theForm ) {
	var why = '';
	
	theForm.first_name.value = theForm.first_name.value.replace( "&", "and" );	// strip ampersands
	why += checkMinLength( "first name", theForm.first_name.value, 2 );
	theForm.last_name.value = theForm.last_name.value.replace( ",", "" );		// strip commas
	why += checkMinLength( "last name", theForm.last_name.value, 2 );
	why += checkMinLength( "address", theForm.address.value, 5 );
	why += checkMinLength( "city", theForm.city.value, 2 );
	why += checkMinLength( "state", theForm.state.value, 2 );
	why += checkMinLength( "zip code", theForm.zip.value, 5 );
	why += checkCountryField ( theForm.country );
	why += checkMinLength( "phone", theForm.phone.value, 10 );
	if ( theForm.e_mail.value.length < 1 )
		why += "The e-mail address cannot be empty.\n";
	else if ( !checkEMail(theForm.e_mail.value) )
		why += "That doesn\'t appear to be a valid e-mail address.\n";

	return why;
}

function checkWorkerForm( theForm ) {
	var why = checkPersonalFields ( theForm );
		
	why += checkMinLength( "user name", theForm.new_user_name.value, 8 );
	why += checkPassword ( theForm.password1.value, theForm.first_name.value, theForm.last_name.value );
	if ( theForm.password1.value != theForm.password2.value )
		why += "The two passwords must match.\n";
		
	if ( why !="" ) 
		alert (why);
		
	return why == "";
}

function checkCompRegForm( theForm ) {
	var why = "";
		
	why += checkMinLength( "driver's first name", theForm.driver_first_name.value, 2 );
	why += checkMinLength( "driver's last name", theForm.driver_last_name.value, 2 );
	why += checkCountryField ( theForm.driver_country );
	why += checkNumbersOnly( "driver's RallyAmerica ID", theForm.driver_ra_ID.value );

	why += checkMinLength( "co-driver's first name", theForm.codriver_first_name.value, 2 );
	why += checkMinLength( "co-driver's last name", theForm.codriver_last_name.value, 2 );
	why += checkCountryField ( theForm.codriver_country );
	why += checkNumbersOnly( "co-driver's RallyAmerica ID", theForm.codriver_ra_ID.value );
		
	if ( theForm.car_year.value.length > 0 && ( theForm.car_year.value.length < 4 || theForm.car_year.value < 1900 || !numbersOnly ( theForm.car_year.value, false ) ) )
		why += "The car year must be a 4-digit number.\n";
	
	if ( !theForm.record_ID.value ) {
		if ( !theForm.entry_fee.value )
			why += "You must select an entry fee.\n";
			
		if ( !getRadioValue (theForm.credit_card) )
			why += "You must specify a credit card type.\n";
			
		why += checkCreditCardNumber( "credit card", theForm.card_number.value, true );
		why += checkExpirationDate( theForm.expiration.value );
		why += checkMinLength( "credit card name", theForm.card_name.value, 4 );
	}
	
	if ( why !="" ) 
		alert (why);
		
	return why == "";
}

function checkPersonalForm( theForm ) {
	var why = checkPersonalFields ( theForm );
		
	if ( why !="" ) 
		alert (why);
		
	return why == "";
}

function checkWorkerRegistrationForm( theForm ) {
	var why = "";
	
	why += checkMinLength( "first name", theForm.first_name.value, 2 );
	why += checkMinLength( "last name", theForm.last_name.value, 2 );
		
	if ( why !="" ) 
		alert (why);
		
	return why == "";
}

function checkWorkerRegForm( theForm ) {
	var why = checkPersonalFields ( theForm );
			
	if ( why !="" ) 
		alert (why);
		
	return why == "";
}

function checkUserForm( theForm ) {
	var why = "";
		
	if ( why !="" ) 
		alert (why);
		
	return why == "";
}

function confirmWorkerAction( theForm ) {
	var whichButton = getRadioValue (theForm.with_checked);
	var result = false;
	
	switch ( whichButton ) {
		case '1':
		case 'delete':
			result = confirm ( "Do you really want to delete all checked records?  This will also delete the associated personnel and competitor records." );
			break;
			
		case 'combine':
			result = confirm ( "You are about to combine all checked records into one.  Are you sure you want to do that?" );
			break;

		default:
			alert ( "You must select an action to perform on the checked records." );
			break;
	}
				
	return result;
}

function confirmCompetitorAction( theForm ) {
	var whichButton = getRadioValue (theForm.with_checked);
	var result = false;
	
	switch ( whichButton ) {
		case '1':
		case 'delete':
			result = confirm ( "Do you really want to delete all checked records?  This will also delete the associated personnel and worker records." );
			break;
			
		case 'combine':
			result = confirm ( "You are about to combine all checked records into one.  Are you sure you want to do that?" );
			break;

		default:
			alert ( "You must select an action to perform on the checked records." );
			break;
	}
				
	return result;
}

function confirmPersonnelAction( theForm ) {
	var whichButton = getRadioValue (theForm.with_checked);
	var result = false;
	
	switch ( whichButton ) {
		case 'delete':
			result = confirm ( "Do you really want to delete all checked records?  This will also delete the associated worker and competitor records." );
			break;
			
		case 'combine':
			result = confirm ( "You are about to combine all checked records into one.  Are you sure you want to do that?" );
			break;

		default:
			alert ( "You must select an action to perform on the checked records." );
			break;
	}
				
	return result;
}

function checkPersonnelForm( theForm ) {
	var why = "";
		
	theForm.first_name.value = theForm.first_name.value.replace( "&", "and" );	// strip ampersands
	why += checkMinLength( "first name", theForm.first_name.value, 2 );
	theForm.first_name.value = theForm.first_name.value.replace( ",", "" );		// strip commas
	why += checkMinLength( "last name", theForm.last_name.value, 2 );
	why += checkCountryField ( theForm.country );
	if ( theForm.e_mail.value && !checkEMail(theForm.e_mail.value) )
		why += "That doesn\'t appear to be a valid e-mail address.\n";

if ( why !="" ) 
		alert (why);
		
	return why == "";
}

function checkRoutesForm( theForm ) {
	var why = "";
		
	if ( theForm.rally_ID.value < 1 )
		why += "You must specify a rally.\n";
	why += checkMinLength( "route name", theForm.route_name.value, 5 );

if ( why !="" ) 
		alert (why);
		
	return why == "";
}

function checkRouteSegmentsForm( theForm ) {
	var why = "";
		
	if ( theForm.route_ID.value < 1 )
		why += "You must specify a route.\n";
	why += checkMinLength( "route name", theForm.route_name.value, 5 );

if ( why !="" ) 
		alert (why);
		
	return why == "";
}

function checkEmailerForm( theForm ) {
	var why = "";
		
	if ( theForm.subject.value == "" )
		why += "You must enter a subject.\n";
	if ( theForm.message.value == "" )
		why += "You must enter a message.\n";

	if ( why !="" ) 	
		alert (why);
	else if ( !confirm ( "Please use this system responsibly.  All mails you send with this system are monitored." ) )
		why = "a";
		
	return why == "";
}

function checkTulipForm( theForm ) {
	var why = "";
		
	if ( theForm.mileage.value == "" )
		why += "You must enter a mileage.\n";

if ( why !="" ) 
		alert (why);
		
	return why == "";
}

function checkSplitWorkerRegForm( theForm ) {
	var why = "";
		
	if ( theForm.pers_ID.value == 0 ) {
		why += "You must select a worker from this list or select \"(New worker)\" and enter first and last names.\n";
	}
	else if ( theForm.pers_ID.value == "new" ) {
		why += checkMinLength( "new worker first name", theForm.first_name.value, 2 );
		why += checkMinLength( "new workerlast name", theForm.last_name.value, 2 );
	}
	
	if ( why !="" ) 
		alert (why);
		
	return why == "";
}

function checkCreateTeamsForm( theForm ) {
	var why = checkMinValue ( "number of stage teams", theForm.num_teams.value, 1 );
	
	if ( theForm.spectator_crews.value.length ) {
		why += checkMinValue ( "spectator crews", theForm.spectator_crews.value, 0 );
		why += checkMaxValue ( "spectator crews", theForm.spectator_crews.value, 9 );
	} else {
		why += "You must specify the number of spectator crews (0 is OK)\n";
	}

	if ( theForm.supermarshals.value.length ) {
		why += checkMinValue ( "supermarshals", theForm.supermarshals.value, 0 );
		why += checkMaxValue ( "supermarshals", theForm.supermarshals.value, 99 );
	} else {
		why += "You must specify the number of supermarshals (0 is OK)\n";
	}

	why += checkMinValue ( "safety sweeps", theForm.safety_sweeps.value, 1 );
	why += checkMaxValue ( "safety sweeps", theForm.safety_sweeps.value, 9 );
	why += checkMinValue ( "heavy sweeps", theForm.heavy_sweeps.value, 1 );
	why += checkMaxValue ( "heavy sweeps", theForm.heavy_sweeps.value, 9 );

	if ( why !="" ) 
		alert (why);
		
	return why == "";
}

function autoNameSegment ( theForm ) {
	if ( theForm.is_MTC ) {
		theForm.name_prefix.value = "MTC _";
	} else if ( theForm.transit_ID ) {
		theForm.name_prefix.value = "Transit to";
		if ( !theForm.seg_name.value ) {
			theForm.seg_name.value = "SS #_";
		}
	} else if ( theForm.stage_ID ) {
		if ( !theForm.name_prefix.value ) {
			theForm.name_prefix.value = "SS #_";
		}
		if ( !theForm.seg_name.value ) {
			theForm.seg_name.value = theForm.short_name.value;
		}
	}
}

function checkNewPasswords( theForm, firstName, lastName ) {
	var why = "";
		
	why += checkPassword ( theForm.password.value, firstName, lastName );
	
	if ( why == "" && theForm.password.value != theForm.password2.value ) {
		why += "The two passwords must match.";
	}
	
	if ( why !="" ) 
		alert (why);
		
	return why == "";
}

function collectAddressesFromForm ( theForm ) {
	var i;
	var	addresses = '';
	
	for ( i=0 ; i<theForm.elements.length ; i++) {
		if ( theForm.elements[i].name.substr(0,8) == "checked_" ) {
			//	It's a check box
			if ( theForm.elements[i].checked ) {
				theField = document.getElementById("address_"+theForm.elements[i].name.substr(8, 99));
				if ( theField ) {
					theAddress = theField.value;
					if ( theAddress ) {
						if ( addresses )
							addresses += ", ";
						addresses += theAddress;
					}
				}
			}
		}
	}
	return addresses;
}

function sendGroupEmail ( theForm ) {
	if ( confirm ( "This script will generate an e-mail in your e-mail client (Outlook, Mail, etc.). If you use your browser to send e-mail, it won't work.  Use another method. \n\nPlease use this method responsibly." ) ) {
		location="mailto:" + collectAddressesFromForm ( theForm )  + "?subject=" + theForm.subject.value + "&body=" + theForm.message.value;
	}
}

function groupEmailToClipbaord(theForm) {
	addresses = collectAddressesFromForm ( theForm );
	prompt ( "Copy the selected text to the clipboard, then switch to your e-mail client and paste them into the To: field.\n\nPlease use this method responsibly.", addresses );
}

function checkAll ( theForm, onOff )
{
	var	theElement;
	var	theString="Check boxes";
	
	for (var i=0; i < theForm.elements.length; i++) 
	{
		theElement = theForm.elements[i];
		if ( theElement.name.substr(0, 8) == "checked_" && theElement.disabled == false)
			theElement.checked = onOff;
	}
}

function MedicReg ( medicID, assignID, edit ) {
	var theWindow = window.open("/personnel/medic_information.php?ID="+medicID+"&assign="+assignID+"&edit="+edit, "MedicInfo", "width=750,height=600,location=0,toolbar=0,resizable=0,status=0,statusbar=");
	
	if ( theWindow ) {
		theWindow.focus();
	}
	
	return false;
}

function SweepReg ( persID, assignID, edit ) {
	var theWindow = window.open("/personnel/sweep_information.php?ID="+persID+"&assign="+assignID+"&edit="+edit, "SweepInfo", "width=750,height=600,location=0,toolbar=0,resizable=0,status=0,statusbar=0");
	
	if ( theWindow ) {
		theWindow.focus();
	}
	
	return false;
}

function WorkerHistory ( theWorker, theDiv ) {
	if ( theDiv.innerHTML.length == 0 )
		MakeRequestOnDiv( 'ajax_includes/worker_history.php?ID='+theWorker, theDiv );
	else
		theDiv.innerHTML="";
}

