/*
 * resume.js
 * Resume Upload
 *
 * Form validation and utility code for the registration form.
 *
 * copyright	2006 North Point Ministries
 * author	Josh Justice <josh.justice@northpoint.org>
 * version	1.0
 */

/* global variables ***********************************************************/

// list of form names of required fields
var checkFieldList = ["fhscol_FirstName", "fhscol_LastName", "fhscol_Email", "fhscol_confirmEmail", "fhscol_StreetAddress", "fhscol_City", "fhscol_State", "fhscol_Zip", "fhscol_why", "fhscol_what_job", "fhscol_referral", "fhscol_campus", "fhscol_serving", "fhscol_relationship_with_God", "fhscol_invest_invite", "fhscol_like_dont_like" ];

// user-friendly names of required fields to display to user
var properNameList = ["First Name", "Last Name", "Email", "Confirm Email", "Address", "City", "State", "Zip", "Why do you want to work at NPM Inc?", "What job are you applying for and why do you want that job?", "Is a staff member referring you to submit a resume?", "Are you a member or regular attender of NPCC, BC, or BBCC?", "How are you involved or serving currently at NPCC, BC, or BBCC?", "Describe your current relationship with God.", "Share with us an invest and invite story in which you have been involved.", "Tell us what you currently like and don't like about NPM, Inc." ];

/* functions ******************************************************************/

/*
 * Checks all required fields to ensure that a value has been entered for them.
 * If any are missing, an error dialog is displayed to the user and the form is
 * not submitted. If none are missing, the form is submitted.
 *
 * param theForm   the form object
 */
function checkFields (theForm)
{
	var errorText = "";
	var curField = "";
	var curFieldProp = "";
	var count = 0; // number of empty fields

	// check fields
	for (var i = 0; i < checkFieldList.length; i++)
	{
		curField = checkFieldList[i];
		curFieldProp = properNameList[i];
		unhilite( theForm[curField] );
		if ( isEmpty( theForm[curField] ) || theForm[curField].value == "--" )
		{
			// add field to error list
			hilite( theForm[curField] );
			errorText += curFieldProp + ", ";
			count++;
		} // end if
	} // end for

	// check to make sure all confirm e-mail fields are correct
	if( errorText == "" )
	{
		if( theForm["fhscol_Email"].value != theForm["fhscol_confirmEmail"].value )
		{
			alert( "The e-mail address does not match the confirmation e-mail address. Please make sure the correct e-mail address is entered in both fields.");
			return false;
		}
	} // end if

	// if any errors, display
        if (errorText != "")
        {
            errorText = errorText.substring(0, errorText.length - 2);
            alert ("The following " + count + " field(s) must be completed:\n\n" + errorText + "\n\nPlease complete all required fields before submitting again.");
            return false;
        }
        
    	return true;
    	
} // end checkFields

/*
 * Returns true if a field is empty, false otherwise. Handles text fields,
 * select fields, checkboxes, and radio buttons.
 *
 * param formElement   the form element object
 */
function isEmpty( formElement )
{
	// checkboxes
	if( formElement.type == 'checkbox' )
	{
	    return !formElement.checked;
	}

	// text fields
	else if( formElement.type == 'text' || formElement.type == 'textarea' )
	{
	    return formElement.value == "";
	}

	// select fields
	else if( formElement.type == 'select-one' )
	{
	    return formElement.selectedIndex <= 0;
	}

	// radio buttons
	else
	{
	    for( var i = 0; i < formElement.length; i++ )
	    {
		if( formElement[i].checked )
		    return false;
	    }
	    return true;
	} // end if
} // end isEmpty

/*
 * Highlights a text field or select box by changing its background color to
 * yellow.
 *
 * param formElement   the form element object
 */
function hilite( formElement )
{
	// can't hilite radio groups
	if( typeof formElement[0] != 'undefined' )
		return;

	if( typeof formElement.style != 'undefined' )
		formElement.style.background = "#FFFF99";
} // end hilite

/*
 * Removes highlighting a text field or select box by changing its background
 * color to white.
 *
 * param formElement   the form element object
 */
function unhilite( formElement )
{
	// can't unhilite radio groups
	if( typeof formElement[0] != 'undefined' )
		return;

	if( typeof formElement.style != 'undefined' )
		formElement.style.background = "#FFFFFF";
} // end unhilite
