/*
 * 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_Email", "fhscol_FirstName", "fhscol_LastName", "fhscol_Age", "fhscol_MaritalLength", "fhscol_Campus", "fhscol_Address", "fhscol_City", "fhscol_State", "fhscol_Zip", "fhscol_Phone", "fhscol_WorkPhone", "fhscol_PreferredLocation", "fhscol_Problem_conf", "fhscol_Notice_conf", "fhscol_Previous_conf" ];

// user-friendly names of required fields to display to user
var properNameList = ["Your Email Address", "Your First Name","Your Last Name", "Age", "Years Married", "Campus You Attend", "Address", "City", "State", "Zipcode", "Home Phone", "Work Phone", "Do you prefer a counselor closer to work or home?", "Reason seeking help", "First noticed concern", "Have you had counseling before" ];

/* 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_Email"].value;
	
	// set receipient
//	theForm["fhs_recipients"].value = theForm["fhscol_recipients"].value;
	

	// check fields
	for (var i = 0; i < checkFieldList.length; i++)
	{
		curField = checkFieldList[i];
		curFieldProp = properNameList[i];
		//alert(curField);
		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 hideElement (elementId) {
	  var element;
	  if (document.all)
			element = document.all[elementId];
	  else if (document.getElementById)
			element = document.getElementById(elementId);
	  if (element && element.style)
			element.style.display = 'none';
 }
 function showElement (elementId) {
	  var element;
	  if (document.all)
			element = document.all[elementId];
	  else if (document.getElementById)
			element = document.getElementById(elementId);
	  if (element && element.style)
			element.style.display = '';
 }
 
 function updateForm(){
	
	hideElement("attendOther");
	
	if (document.theForm["fhscol_Campus"].selectedIndex == 4){
		showElement("attendOther");
	}
	
}
