  
/* global variables ***********************************************************/

// list of form names of required fields
var checkFieldList = ["fhscol_firstName","fhscol_lastName","fhscol_address1","fhscol_city","fhscol_zip","fhscol_DOB","fhscol_homePhone","fhscol_email","fhscol_member","fhscol_attendCampus","fhscol_attendWorship","fhscol_serving","fhscol_inSmallGroup","fhscol_arrested","fhscol_employed","fhscol_educationAmount","fhscol_educationContinuing","fhscol_maritalStatus","fhscol_children","fhscol_finCounselingBefore","fhscol_ledToMoneyWise","fhscol_affectingYouOthers","fhscol_changeAboutSelf","fhscol_fatherMoney","fhscol_motherMoney","fhscol_fatherDebtSavings","fhscol_motherDebtSavings","fhscol_fatherBest","fhscol_motherBest","fhscol_fatherWorst","fhscol_motherWorst","fhscol_parentsAffected","fhscol_dependentOnParents","fhscol_budget","fhscol_debtsConcerned","fhscol_giving","fhscol_creditCards","fhscol_saved","fhscol_religiousBackground","fhscol_seeGodDo","fhscol_seeHappenMoneyWise","fhscol_expectMentor","fhscol_godsSolution"];

// user-friendly names of required fields to display to user
var properNameList = ["firstName","lastName","address1","city","zip","DOB","homePhone","email","member","attendCampus","attendWorship","serving","inSmallGroup","arrested","employed","educationAmount","educationContinuing","maritalStatus","children","finCounselingBefore","ledToMoneyWise","affectingYouOthers","changeAboutSelf","fatherMoney","motherMoney","fatherDebtSavings","motherDebtSavings","fatherBest","motherBest","fatherWorst","motherWorst","parentsAffected","dependentOnParents","budget","debtsConcerned","giving","creditCards","saved","religiousBackground","seeGodDo","seeHappenMoneyWise","expectMentor","godsSolution"];

function checkFields (theForm)
{
	updateEmail( 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

	// 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
*/
	
		
    	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

function updateEmail( theForm )
{
	defaultRecipient = "moneywise@northpoint.org";
	
	recipients = defaultRecipient;

	if( theForm["fhscol_attendCampus"].value == "BBCC" )
		recipients = "moneywise@brownsbridge.org";

	if( theForm["fhscol_attendCampus"].value == "BC" )
		recipients = "moneywise@buckheadchurch.org";

	if( theForm["fhscol_attendCampus"].value == "NPCC" )
		recipients = "moneywise@northpoint.org";

	if( theForm["fhscol_attendCampus"].value == "NPCC" )
		recipients = "moneywise@northpoint.org";

	if( theForm["fhscol_attendCampus"].value == "None" )
		recipients = defaultRecipient;
		
	theForm["fhs_recipients"].value = recipients;
	
}

function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '(2000 Characters Max) - <span>0</span>/2000';
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
	}
}

function checkMaxLength() {
	var maxLength = 2000;
	var currentLength = this.value.length;
	if (currentLength > maxLength)
		this.relatedElement.className = 'toomuch';
	else
		this.relatedElement.className = '';

	this.relatedElement.firstChild.nodeValue = currentLength;
	
	// not innerHTML
}