/*
 * contactUs.js
 * 
 *
 * Form validation and utility code for the bulletin request form.
 *
 * copyright	2004 North Point Ministries
 * author	Josh Justice <josh.justice@northpoint.org>
 * modified	David Wallace <david.wallace@4-integrity.com>
 * version	1.0
 */
    
/* global variables ***********************************************************/

// list of form names of required fields
var checkFieldList = ["fhscol_GroupType", "fhscol_LeaderName", "fhscol_LeaderEmail" ];

// user-friendly names of required fields to display to user
var properNameList = ["Group Type","Leader Name","Leader Email" ];

/* functions ******************************************************************/

/*
 * 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
 *

function copyContactInfoToBilling(theForm)
{
	theForm["pps_first_name"].value = theForm["fhscol_FirstName"].value;
	theForm["pps_last_name"].value = theForm["fhscol_LastName"].value;
	theForm["pps_address"].value = theForm["fhscol_Address"].value;
	theForm["pps_city"].value = theForm["fhscol_City"].value;
	theForm["pps_state"].value = theForm["fhscol_State"].value;
	theForm["pps_zip_code"].value = theForm["fhscol_Zip"].value;
	theForm["pps_email"].value = theForm["fhscol_Email"].value;
	theForm["ppsEmailConfirm"].value = theForm["EmailConfirm"].value;
}
 */
/*
 * 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
	
	// set sender
	theForm["fhs_sender"].value = theForm["fhscol_LeaderEmail"].value;
	
	// set receipient
	if (theForm["fhscol_GroupType"].value == "Womens") 
	{
		if (theForm["fhscol_Campus"].value == "BC") {
			theForm["fhs_recipients"].value = "ladiesgroups@buckheadchurch.org";
		} else if (theForm["fhscol_Campus"].value == "BBCC"){
			theForm["fhs_recipients"].value = "ladieslink@northpoint.org";
		} else {
			theForm["fhs_recipients"].value = "ladieslink@northpoint.org";
		}
	}
	if (theForm["fhscol_GroupType"].value == "Married Couples") 
	{
		if (theForm["fhscol_Campus"].value == "BC") {
			theForm["fhs_recipients"].value = "marriedgroupupdates@buckheadchurch.org";
		} else if (theForm["fhscol_Campus"].value == "BBCC"){
			theForm["fhs_recipients"].value = "marriedgroupupdates@brownsbridge.org";
		} else {
			theForm["fhs_recipients"].value = "marriedgroupupdates@northpoint.org";
		}
		
	}
	if (theForm["fhscol_GroupType"].value == "Single Female") 
	{
		if (theForm["fhscol_Campus"].value == "BC") {
			theForm["fhs_recipients"].value = "singlesgroups@buckheadchurch.org";
		} else if (theForm["fhscol_Campus"].value == "BBCC"){
			theForm["fhs_recipients"].value = "singlesgroups@northpoint.org";
		} else {
			theForm["fhs_recipients"].value = "singlesgroups@northpoint.org";
		}
		
	}
	if (theForm["fhscol_GroupType"].value == "Single Male") 
	{
		if (theForm["fhscol_Campus"].value == "BC") {
			theForm["fhs_recipients"].value = "singlesgroups@buckheadchurch.org";
		} else if (theForm["fhscol_Campus"].value == "BBCC"){
			theForm["fhs_recipients"].value = "singlesgroups@northpoint.org";
		} else {
			theForm["fhs_recipients"].value = "singlesgroups@northpoint.org";
		}
	}

	if (theForm["fhscol_GroupType"].value == "Mens") 
	{
		if (theForm["fhscol_Campus"].value == "BC") {
			theForm["fhs_recipients"].value = "anna.talbott@buckheadchurch.org";
		} else if (theForm["fhscol_Campus"].value == "BBCC"){
			theForm["fhs_recipients"].value = "angela.hurley@brownsbridge.org";
		} else {
			theForm["fhs_recipients"].value = "lydia.hatfield@northpoint.org";
		}
	}

	

	// 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

	// 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;
        }
/*        else
        {
            theForm.submit.value = 'Submitting...';
            theForm.submit.disabled = true;
	} // end if
*/
	
		theForm.fhs_subject.value = theForm.fhs_subject.value + " - " + theForm.fhscol_LeaderName.value;
	
    	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 == "";
	}

	// text areas
	else if( 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
