/*
 * www-ccr.js
 * Childcare Reimbursement
 * 
 * Form validation and utility code for the registration form.
 *
 * copyright	2005 North Point Ministries
 * author	Josh Justice <josh.justice@northpoint.org>
 * version	1.0
 * 4/19/05 modified by Wayne Helsby <wayne@helsby.us> for use on ChildCare Reiumbursement
 */

/* global variables ***********************************************************/

// list of form names of required fields
var checkFieldList = ["fhscol_firstName", "fhscol_lastName", "fhscol_address", "fhscol_city", "fhscol_state", "fhscol_zip", "fhscol_telephone", "fhs_sender", "fhscol_environment", "fhscol_attendBC"];

// user-friendly names of required fields to display to user
var properNameList = ["First Name", "Last Name", "Address", "City", "State", "ZIP", "Primary Phone", "Email", "Primary Environment", "Campus"];

/* functions ******************************************************************/

/* ******* removed by wayne 4/19/05
 * Copies the contact information entered in the top of the form, to the billing
 * fields at the bottom of the form.
 *
 * param theForm   the form object
 *
 */
/*
 * 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)
{
	//copy email so it will include use e-mail address in body of email ... added by wayne 4/19/05
	theForm["fhscol_email"].value = theForm["fhs_sender"].value;

	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];
		//alert(curField);
		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_ParentsEmail"].value != theForm["ParentsEmailConfirm"].value )
	//	{
	//		alert( "The parents' e-mail address does not match the confirmation parents' e-mail address. Please make sure the correct e-mail address is entered in both fields.");
	//		return false;
	//	}
	//	else if( theForm["fhscol_Email"].value != "" 
	//		&& theForm["fhscol_Email"].value != theForm["EmailConfirm"].value )
	//	{
	//		alert( "The student e-mail address does not match the confirmation student e-mail address. Please make sure the correct e-mail address is entered in both fields.");
	//		return false;
	//	}
	//	else if( theForm["pps_email"].value != theForm["ppsEmailConfirm"].value )
	//	{
	//		alert( "The billing e-mail address does not match the confirmation billing 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;
        }
     // ********* begin commented out 4/19/05 by wayne helsby   
        // adjust submitted values
	//theForm.pps_recipients.value = (theForm.fhscol_Campus[0].checked ? "jennifer.thomas@northpoint.org" : "paige.raynor@buckheadchurch.org");
	//theForm.pps_recipients.value += ";" + theForm.pps_email;
	
	//if( theForm.RoommateRadio[0].checked )
	//{
	//	theForm.fhscol_Roommate.value = "small group";
	//}
	//else
	//{
	//	theForm.fhscol_Roommate.value = theForm.Roommate.value;
	//}
	
	//theForm.pps_comment2.value = theForm.fhscol_FirstName.value
	//				+ " " + theForm.fhscol_LastName.value;

	//theForm.fhscol_DOB.value = theForm.DOB_year.value
	//				+ "-" + theForm.DOB_month.value
	//				+ "-" + theForm.DOB_day.value;
    // ********* end commented out 4/19/05 by wayne helsby
/*        else
        {
            theForm.submit.value = 'Submitting...';
            theForm.submit.disabled = true;
	} // end if
*/
    	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' )
	{
	    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

