window.onload = function()
{
	document.getElementById("pledgeForm").onsubmit = function()
	{
		return validateForm(this);
	};
};

function validateForm(theForm)
{
	var returnVal = true;
	var errMsg 	= "Your entry was not submitted because of the following.\n\n";
	
    if ( ( theForm.lightBulbs[0].checked == false )
    && ( theForm.lightBulbs[1].checked == false )
    && ( theForm.lightBulbs[2].checked == false ) )
    {
        errMsg += "You must answer Yes or No to using compact fluorescent light bulbs.\n";
		returnVal = false;
    }
    if ( ( theForm.thermostat[0].checked == false )
    && ( theForm.thermostat[1].checked == false )
    && ( theForm.thermostat[2].checked == false ) )
    {
        errMsg += "You must answer Yes or No to using a programmable thermostat.\n";
		returnVal = false;
    }
    if ( ( theForm.turnOffUnplug[0].checked == false )
    && ( theForm.turnOffUnplug[1].checked == false )
    && ( theForm.turnOffUnplug[2].checked == false ) )
    {
        errMsg += "You must answer Yes or No to unplugging chargers.\n";
		returnVal = false;
    }
    if ( ( theForm.householdProducts[0].checked == false )
    && ( theForm.householdProducts[1].checked == false )
    && ( theForm.householdProducts[2].checked == false ) )
    {
        errMsg += "You must answer Yes or No to using multiple green cleaning products.\n";
		returnVal = false;
    }
    if ( ( theForm.charcoalChimney[0].checked == false )
    && ( theForm.charcoalChimney[1].checked == false )
    && ( theForm.charcoalChimney[2].checked == false ) )
    {
        errMsg += "You must answer Yes or No to using a charcoal chimney or gas grill.\n";
		returnVal = false;
    }
    if ( ( theForm.gasolinePowered[0].checked == false )
    && ( theForm.gasolinePowered[1].checked == false )
    && ( theForm.gasolinePowered[2].checked == false ) )
    {
        errMsg += "You must answer Yes or No to using electric lawn equipment.\n";
		returnVal = false;
    }
    if ( ( theForm.energyStar[0].checked == false )
    && ( theForm.energyStar[1].checked == false )
    && ( theForm.energyStar[2].checked == false ) )
    {
        errMsg += "You must answer Yes or No to having an ENERGY STAR refrigerator.\n";
		returnVal = false;
    }
    if ( ( theForm.publicTransportation[0].checked == false )
    && ( theForm.publicTransportation[1].checked == false )
    && ( theForm.publicTransportation[2].checked == false ) )
    {
        errMsg += "You must answer Yes or No to taking public transit or Rideshare.\n";
		returnVal = false;
    }
    if ( ( theForm.bikeOrWalk[0].checked == false )
    && ( theForm.bikeOrWalk[1].checked == false )
    && ( theForm.bikeOrWalk[2].checked == false ) )
    {
        errMsg += "You must answer Yes or No to biking or walking to work or to school.\n";
		returnVal = false;
    }
    if ( ( theForm.runErrands[0].checked == false )
    && ( theForm.runErrands[1].checked == false )
    && ( theForm.runErrands[2].checked == false ) )
    {
        errMsg += "You must answer Yes or No to consolidating your errands.\n";
		returnVal = false;
    }
    if ( ( theForm.limitDriving[0].checked == false )
    && ( theForm.limitDriving[1].checked == false )
    && ( theForm.limitDriving[2].checked == false ) )
    {
        errMsg += "You must answer Yes or No to bringing lunch to work.\n";
		returnVal = false;
    }
    if ( ( theForm.airQuality[0].checked == false )
    && ( theForm.airQuality[1].checked == false )
    && ( theForm.airQuality[2].checked == false ) )
    {
        errMsg += "You must answer Yes or No to checking daily air quality forecast.\n";
		returnVal = false;
    }	
	
	
	//Validate years old
	if(!document.getElementById("yearsOld").checked)
	{
		errMsg += "You must be 18 years old or older in order to enter the contest.\n";
		returnVal = false;
	}
	
	// Validate the rules
	if (!document.getElementById("understoodRules").checked)
	{
		errMsg += "You must read the rules in order to enter the contest.\n"
		returnVal = false;
	}
	
	// Validate county
	if(theForm.county.value == "")
	{
		errMsg += "Please select the county you live in.\n";
		returnVal = false;
	}
	
	// Validate heard about
	if(theForm.heardAbout.value == "")
	{
		errMsg += "Please select how you heard about the event.\n";
		returnVal = false;
	}
	
	// Validate first name
	if(theForm.firstName.value == "")
	{
		errMsg += "Please enter first name.\n";
		returnVal = false;
	}
	
	// Validate last name
	if(theForm.lastName.value == "")
	{
		errMsg += "Please enter last name.\n";
		returnVal = false;
	}
	
	// Validate address
	
	if(theForm.addr1.value == "")
	{
		errMsg += "Please enter an address.\n";
		returnVal = false;
	}
	if(theForm.city.value == "")
	{
		errMsg += "Please enter a city.\n";
		returnVal = false;
	}
	if(theForm.state.value == "")
	{
		errMsg += "Please enter a state.\n";
		returnVal = false;
	}
	if(theForm.zip.value == "")
	{
		errMsg += "Please enter a postal code.\n";
		returnVal = false;
	}
	
	// Validate phone
	if(theForm.phone.value == "")
	{
		errMsg += "Please enter a telephone number.\n";
		returnVal = false;
	}
	else if(theForm.phone.value.length < 8)
	{
		errMsg += "Please enter a valid telephone number with area code.\n";
		returnVal = false;
	}
	
	// Validate email
	if(theForm.email.value == "")
	{
		errMsg += "Please enter your email address.\n";
		returnVal = false;
	}
	else if(!validateEmail(theForm.email.value))
	{
		errMsg += "Please enter a valid email address.\n";
		returnVal = false;
	}
	
	if(!returnVal)
		alert(errMsg);
	
	return returnVal;
}
