/*
 * studentMin.js
 * 
 *
 * Form validation and utility code for the bulletin request form.
 *
 * copyright	2004-05 North Point Ministries
 * author	Josh Justice <josh.justice@northpoint.org>
 * version	1.0  6/21/2005
 */
    
/* global variables ***********************************************************/

// list of form names of required fields
var checkFieldList = ["fhscol_FirstName", "fhscol_LastName", "fhscol_DateOfBirth", "fhscol_Address", 
	"fhscol_City", "fhscol_State", "fhscol_ZIP", "fhscol_HomePhone", "fhscol_Email",
	"fhscol_WayToContact", "fhscol_Occupation", "fhscol_Employer", "fhscol_Education", 
	"fhscol_ChurchMemb", "fhscol_HowLearnedAbout",
	"fhscol_HowBecameChristian", "fhscol_UnderstandingOfGospel", "fhscol_Covenant", "fhscol_Media", "fhscol_InCommGroup",
	"fhscol_WhyMSHSWork", "fhscol_ExplainAlcohol",
	"fhscol_ExplainSmoking", "fhscol_ExplainDrugs", 
	"fhscol_ExplainSex", "fhscol_ExplainHomosex", 
	"fhscol_GreatestStrength", "fhscol_NeedToLearn",
	"fhscol_FelonyConvicted", "fhscol_BackgroundCheck",
	"fhscol_Ref1Name", "fhscol_Ref1Phone", "fhscol_Ref1Relation", 
	"fhscol_Ref2Name", "fhscol_Ref2Phone", "fhscol_Ref2Relation", 
	"fhscol_Ref3Name", "fhscol_Ref3Phone", "fhscol_Ref3Relation"];

// user-friendly names of required fields to display to user
var properNameList = ["First Name","Last Name", "Date of Birth", "Address",
	"City", "State", "ZIP", "Home Phone", "Email",
	"Best way to contact", "Occupation", "Place of Employment", "Education", 
	"North Point/Buckhead Church membership", "How learned about this ministry",
	"How you became a Christian", "Understanding Of Gospel", "Family Ministry Covenant", "Media Release", "In a community group",
	"Why Student Ministry",
	"Drinking Alcohol", "Smoking", "Illegal Drug Use",
	"Pre-marital Sex", "Homosexuality", "Greatest strength", 
	"Need to Learn", "Felony conviction", "Background check",
	"Reference 1 Name", "Reference 1 Phone", "Reference 1 Relationship", 
	"Reference 2 Name", "Reference 2 Phone", "Reference 2 Relationship", 
	"Reference 3 Name", "Reference 3 Phone", "Reference 3 Relationship"];

/* 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	

	// choose ministry
	updateEmail( theForm );
	if( theForm["fhs_recipients"].value == "" )
	{
		alert( "Please choose at least one ministry area to apply for." );
		return false;
	}

	// 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;
        }
		
		
		errorText = "";

		if ( theForm["fhscol_HowBecameChristian"].value.length > theForm["fhscol_HowBecameChristian"].getAttribute('maxlength') ) {
			errorText += "-- How and when you accepted Christ\n";
		}
		if ( theForm["fhscol_UnderstandingOfGospel"].value.length > theForm["fhscol_UnderstandingOfGospel"].getAttribute('maxlength') ) {
			errorText += "-- Understanding Of Gospel\n";
		}
		if ( theForm["fhscol_ExperMSHS"].value.length > theForm["fhscol_ExperMSHS"].getAttribute('maxlength') ) {
			errorText += "-- Previous Ministry or Volunteer Work\n";
		}
		if ( theForm["fhscol_ExperLeadSmGrp"].value.length > theForm["fhscol_ExperLeadSmGrp"].getAttribute('maxlength') ) {
			errorText += "-- Experience Leading a Small Group\n";
		}
		if ( theForm["fhscol_CurrVolLeadWork"].value.length > theForm["fhscol_CurrVolLeadWork"].getAttribute('maxlength') ) {
			errorText += "-- Current Volunteer or Leadership Committments\n";
		}
		if ( theForm["fhscol_WhyMSHSWork"].value.length > theForm["fhscol_WhyMSHSWork"].getAttribute('maxlength') ) {
			errorText += "-- Why Student Ministry\n";
		}
		if ( theForm["fhscol_ExplainAlcohol"].value.length > theForm["fhscol_ExplainAlcohol"].getAttribute('maxlength') ) {
			errorText += "-- Drinking Alcohol\n";
		}
		if ( theForm["fhscol_ExplainSmoking"].value.length > theForm["fhscol_ExplainSmoking"].getAttribute('maxlength') ) {
			errorText += "-- Smoking\n";
		}
		if ( theForm["fhscol_ExplainDrugs"].value.length > theForm["fhscol_ExplainDrugs"].getAttribute('maxlength') ) {
			errorText += "-- Illegal Drug Use\n";
		}
		if ( theForm["fhscol_ExplainSex"].value.length > theForm["fhscol_ExplainSex"].getAttribute('maxlength') ) {
			errorText += "-- Premarital Sex\n";
		}
		if ( theForm["fhscol_ExplainHomosex"].value.length > theForm["fhscol_ExplainHomosex"].getAttribute('maxlength') ) {
			errorText += "-- Homosexuality\n";
		}
		if ( theForm["fhscol_GreatestStrength"].value.length > theForm["fhscol_GreatestStrength"].getAttribute('maxlength') ) {
			errorText += "-- Greatest Strength\n";
		}
		if ( theForm["fhscol_NeedToLearn"].value.length > theForm["fhscol_NeedToLearn"].getAttribute('maxlength') ) {
			errorText += "-- Need to Learn\n";
		}
		
		if (errorText != "")
        {
            errorText = errorText.substring(0, errorText.length - 1);
            alert ("The following answers are too long:\n\n" + errorText + "\n\nPlease edit your answers to keep them to a max of 2000 characters.");
            return false;
        }
		
		
		
		//if (!theForm["covenant"].checked){
			//alert("You must agree to the Family Ministry Covenant.");
			//return false;
		//}
		
/*        else
        {
            theForm.submit.value = 'Submitting...';
            theForm.submit.disabled = true;
	} // end if
*/
		if (theForm["fhscol_Covenant"][0].checked)
			theForm["fhscol_CovenantMessage"].value = "Yes, I am in agreement with the covenant.";
			
		if (theForm["fhscol_Covenant"][1].checked)
			theForm["fhscol_CovenantMessage"].value = "No, I have questions about the covenant.";
		
			
    	return true;
	
    	
} // end checkFields

function updateEmail( theForm )
{
	recipients = "";
	
	//if( theForm["fhscol_MinAreaNP6thGrade"].checked )
		//recipients += ";liz.velasquez@northpoint.org";

	if( theForm["fhscol_MinAreaNPXtreme"].checked )
		recipients += "liz.velasquez@northpoint.org;";
		
	if( theForm["fhscol_MinAreaNPInsideOut"].checked )
		recipients += "highschool@northpoint.org;kevin.turner@northpoint.org;lisa.crider@northpoint.org;";
	
	if( theForm["fhscol_MinAreaBHXtreme"].checked )
		recipients += "middleschool@buckheadchurch.org;";
		
	if( theForm["fhscol_MinAreaBHInsideOut"].checked )
		recipients += "highschool@buckheadchurch.org;";
	
	if( theForm["fhscol_MinAreaBBXtreme"].checked )
		recipients += "middleschool@brownsbridge.org;";
		
	if( theForm["fhscol_MinAreaBBInsideOut"].checked )
		recipients += "highschool@brownsbridge.org;";
	
	theForm["fhs_recipients"].value = recipients;
	
	//theForm["fhs_recipients"].value = "jdaws47@gmail.com";
	//alert(theForm["fhs_recipients"].value);
}

/*
 * 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 setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '('+x[i].getAttribute('maxlength') + ' Characters Max) - <span>0</span>/'+x[i].getAttribute('maxlength');
			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 = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
		this.relatedElement.className = 'toomuch';
	else
		this.relatedElement.className = '';

	this.relatedElement.firstChild.nodeValue = currentLength;
	
	// not innerHTML
}