

function validateForm( theForm ) {
	//main attendee -----------------------------------
	if( !isValidType( theForm.firstName, 'firstname' ) ) {
		warnInvalid(theForm.firstName, 'First Name' );
		return false;
	}
	
	if( !isValidType( theForm.lastName, 'lastname' ) ) {
		warnInvalid(theForm.lastName, 'Last Name' );
		return false;
	}
	
	if( !theForm.gender[0].checked &&
		!theForm.gender[1].checked ){
		alert("'Gender of Main Attendee' is missing.");
		return false;
	}	
	
	if( !isValidType( theForm.program, 'select' ) ) {
		warnInvalid(theForm.program, 'Program to Attend', 'select' );
		return false;
	}
	
	if( theForm.addr1.value.length <= 0){
			warnInvalid(theForm.addr1, 'Address' );
			return false;
	}
	
	if( !isValidType( theForm.city, 'city' ) ) {
		warnInvalid(theForm.city, 'City' );
		return false;
	}

	//Only for US & Canada
	if( theForm.country.selectedIndex < 2){
		if( !isValidType( theForm.state, 'select' ) ) {
			warnInvalid(theForm.state, 'State/Province', 'select');
			return false;
		}

		if( !isValidType( theForm.phone, 'phone' ) ) {
			warnInvalid(theForm.phone, 'Phone' );
			return false;
		}
		
		if( theForm.zip.value == "" ){
			warnInvalid(theForm.zip, 'Zip/Postal Code' );
			return false;
		}
		
		if( theForm.country.selectedIndex == 0){	//US Only
			if( !isValidType( theForm.zip, 'zip' ) ) {
				warnInvalid(theForm.zip, 'Zip Code', 'zip');
				return false;
			}
		}
		
		if( !isValidType( theForm.phone, 'phone' ) ) {
			warnInvalid(theForm.phone, 'Phone' );
			return false;
		}

	}

	//other countries
	if(theForm.country.selectedIndex > 1){
		if( !isValidType( theForm.otherCountry, 'firstname' ) ) {
			warnInvalid(theForm.otherCountry, 'Other Country' );
			return false;
		}
	}

	if( !isValidType( theForm.email, 'email' ) ) {
		warnInvalid(theForm.email, 'Email Address' );
		return false;
	}

	//------------------------------------------------------
	//dependents

	//Dependent 1 - if at least one field is not null, all fields become required
	if(	theForm.category1.selectedIndex > 0 ||
		theForm.firstName1.value.length > 0 ||
		theForm.lastName1.value.length > 0 ||
		theForm.age1.value.length > 0 ||
		theForm.gender1.selectedIndex > 0 ||
		theForm.program1.selectedIndex  > 0 ){

		if( !isValidType( theForm.category1, 'select' ) ) {
			warnInvalid(theForm.category1, 'Category of 1st Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.firstName1, 'firstname' ) ) {
			warnInvalid(theForm.firstName1, 'First Name of 1st Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.lastName1, 'lastname' ) ) {
			warnInvalid(theForm.lastName1, 'Last Name of 1st Dependent/Group Member' );
			return false;
		}
						
		if( !isValidType( theForm.age1, 'number' ) ) {
			warnInvalid(theForm.age1, 'Age of 1st Dependent/Group Member', 'select' );
			return false;
		}
		
		if( !isValidType( theForm.gender1, 'select' ) ) {
			warnInvalid(theForm.gender1, 'Gender of 1st Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.program1, 'select' ) ) {
			warnInvalid(theForm.program1, 'Program of 1st Dependent/Group Member', 'select' );
			return false;
		}
	}

	//Dependent 2 - if at least one field is not null, all fields become required
	if(	theForm.category2.selectedIndex > 0 ||
		theForm.firstName2.value.length > 0 ||
		theForm.lastName2.value.length > 0 ||
		theForm.age2.value.length > 0 ||
		theForm.gender2.selectedIndex > 0 ||
		theForm.program2.selectedIndex  > 0 ){

		if( !isValidType( theForm.category2, 'select' ) ) {
			warnInvalid(theForm.category2, 'Category of 2nd Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.firstName2, 'firstname' ) ) {
			warnInvalid(theForm.firstName2, 'First Name of 2nd Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.lastName2, 'lastname' ) ) {
			warnInvalid(theForm.lastName2, 'Last Name of 2nd Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.age2, 'age' ) ) {
			warnInvalid(theForm.age2, 'Age of 2nd Dependent/Group Member', 'select' );
			return false;
		}
		
		
		if( !isValidType( theForm.gender2, 'select' ) ) {
			warnInvalid(theForm.gender2, 'Gender of 2nd Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.program2, 'select' ) ) {
			warnInvalid(theForm.program2, 'Program of 2nd Dependent/Group Member', 'select' );
			return false;
		}
	}

	//Dependent 3 - if at least one field is not null, all fields become required
	if(	theForm.category3.selectedIndex > 0 ||
		theForm.firstName3.value.length > 0 ||
		theForm.lastName3.value.length > 0 ||
		theForm.age3.value.length > 0 ||
		theForm.gender3.selectedIndex > 0 ||
		theForm.program3.selectedIndex  > 0 ){

		if( !isValidType( theForm.category3, 'select' ) ) {
			warnInvalid(theForm.category3, 'Category of 3rd Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.firstName3, 'firstname' ) ) {
			warnInvalid(theForm.firstName3, 'First Name of 3rd Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.lastName3, 'lastname' ) ) {
			warnInvalid(theForm.lastName3, 'Last Name of 3rd Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.age3, 'age' ) ) {
			warnInvalid(theForm.age3, 'Age of 3rd Dependent/Group Member', 'select' );
			return false;
		}
		
		if( !isValidType( theForm.gender3, 'select' ) ) {
			warnInvalid(theForm.gender3, 'Gender of 3rd Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.program3, 'select' ) ) {
			warnInvalid(theForm.program3, 'Program of 3rd Dependent/Group Member', 'select' );
			return false;
		}
	}

	//Dependent 4 - if at least one field is not null, all fields become required
	if(	theForm.category4.selectedIndex > 0 ||
		theForm.firstName4.value.length > 0 ||
		theForm.lastName4.value.length > 0 ||
		theForm.age4.value.length > 0 ||
		theForm.gender4.selectedIndex > 0 ||
		theForm.program4.selectedIndex  > 0 ){

		if( !isValidType( theForm.category4, 'select' ) ) {
			warnInvalid(theForm.category4, 'Category of 4th Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.firstName4, 'firstname' ) ) {
			warnInvalid(theForm.firstName4, 'First Name of 4th Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.lastName4, 'lastname' ) ) {
			warnInvalid(theForm.lastName4, 'Last Name of 4th Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.age4, 'age' ) ) {
			warnInvalid(theForm.age4, 'Age of 4th Dependent/Group Member', 'select' );
			return false;
		}
		
		if( !isValidType( theForm.gender4, 'select' ) ) {
			warnInvalid(theForm.gender4, 'Gender of 4th Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.program4, 'select' ) ) {
			warnInvalid(theForm.program4, 'Program of 4th Dependent/Group Member', 'select' );
			return false;
		}
	}

	//Dependent 5 - if at least one field is not null, all fields become required
	if(	theForm.category5.selectedIndex > 0 ||
		theForm.firstName5.value.length > 0 ||
		theForm.lastName5.value.length > 0 ||
		theForm.age5.value.length > 0 ||
		theForm.gender5.selectedIndex > 0 ||
		theForm.program5.selectedIndex  > 0 ){

		if( !isValidType( theForm.category5, 'select' ) ) {
			warnInvalid(theForm.category5, 'Category of 5th Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.firstName5, 'firstname' ) ) {
			warnInvalid(theForm.firstName5, 'First Name of 5th Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.lastName5, 'lastname' ) ) {
			warnInvalid(theForm.lastName5, 'Last Name of 5th Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.age5, 'age' ) ) {
			warnInvalid(theForm.age5, 'Age of 5th Dependent/Group Member', 'select' );
			return false;
		}
		
		if( !isValidType( theForm.gender5, 'select' ) ) {
			warnInvalid(theForm.gender5, 'Gender of 5th Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.program5, 'select' ) ) {
			warnInvalid(theForm.program5, 'Program of 5th Dependent/Group Member', 'select' );
			return false;
		}
	}

	//Dependent 6 - if at least one field is not null, all fields become required
	if(	theForm.category6.selectedIndex > 0 ||
		theForm.firstName6.value.length > 0 ||
		theForm.lastName6.value.length > 0 ||
		theForm.age6.value.length > 0 ||
		theForm.gender6.selectedIndex > 0 ||
		theForm.program6.selectedIndex  > 0 ){

		if( !isValidType( theForm.category6, 'select' ) ) {
			warnInvalid(theForm.category6, 'Category of 6th Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.firstName6, 'firstname' ) ) {
			warnInvalid(theForm.firstName6, 'First Name of 6th Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.lastName6, 'lastname' ) ) {
			warnInvalid(theForm.lastName6, 'Last Name of 6th Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.age6, 'age' ) ) {
			warnInvalid(theForm.age6, 'Age of 6th Dependent/Group Member', 'select' );
			return false;
		}
		
		if( !isValidType( theForm.gender6, 'select' ) ) {
			warnInvalid(theForm.gender6, 'Gender of 6th Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.program6, 'select' ) ) {
			warnInvalid(theForm.program6, 'Program of 6th Dependent/Group Member', 'select' );
			return false;
		}
	}

	//Dependent 7 - if at least one field is not null, all fields become required
	if(	theForm.category7.selectedIndex > 0 ||
		theForm.firstName7.value.length > 0 ||
		theForm.lastName7.value.length > 0 ||
		theForm.age7.value.length > 0 ||
		theForm.gender7.selectedIndex > 0 ||
		theForm.program7.selectedIndex  > 0 ){

		if( !isValidType( theForm.category7, 'select' ) ) {
			warnInvalid(theForm.category7, 'Category of 7th Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.firstName7, 'firstname' ) ) {
			warnInvalid(theForm.firstName7, 'First Name of 7th Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.lastName7, 'lastname' ) ) {
			warnInvalid(theForm.lastName7, 'Last Name of 7th Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.age7, 'age' ) ) {
			warnInvalid(theForm.age7, 'Age of 7th Dependent/Group Member', 'select' );
			return false;
		}
		
		if( !isValidType( theForm.gender7, 'select' ) ) {
			warnInvalid(theForm.gender7, 'Gender of 7th Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.program7, 'select' ) ) {
			warnInvalid(theForm.program7, 'Program of 7th Dependent/Group Member', 'select' );
			return false;
		}
	}

	//Dependent 8 - if at least one field is not null, all fields become required
	if(	theForm.category8.selectedIndex > 0 ||
		theForm.firstName8.value.length > 0 ||
		theForm.lastName8.value.length > 0 ||
		theForm.age8.value.length > 0 ||
		theForm.gender8.selectedIndex > 0 ||
		theForm.program8.selectedIndex  > 0 ){

		if( !isValidType( theForm.category8, 'select' ) ) {
			warnInvalid(theForm.category8, 'Category of 8th Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.firstName8, 'firstname' ) ) {
			warnInvalid(theForm.firstName8, 'First Name of 8th Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.lastName8, 'lastname' ) ) {
			warnInvalid(theForm.lastName8, 'Last Name of 8th Dependent/Group Member' );
			return false;
		}
		
		if( !isValidType( theForm.age8, 'age' ) ) {
			warnInvalid(theForm.age8, 'Age of 8th Dependent/Group Member', 'select' );
			return false;
		}
		
		if( !isValidType( theForm.gender8, 'select' ) ) {
			warnInvalid(theForm.gender8, 'Gender of 8th Dependent/Group Member', 'select' );
			return false;
		}

		if( !isValidType( theForm.program8, 'select' ) ) {
			warnInvalid(theForm.program8, 'Program of 8th Dependent/Group Member', 'select' );
			return false;
		}
	}

	//-------------------------------------------------------------
	//Discount
	if (theForm.discountPercent.value > 0 &&
		theForm.discountAmount.value > 0 ){
		alert("Either 'Discount Percent' or 'Discount Amount' can be entered.");
		return false;
	}
	
	if (theForm.discountPercent.value > 0 ||
		theForm.discountAmount.value > 0 ){
		if (theForm.discountReason.value.length < 1){
			alert("'Discount Reason' is required when a discount is applied.");
			return false;
		}
	}
	
	//-------------------------------------------------------------

	//Registration Cost
	
	if(theForm.chargeAmount.value < 1){
		alert('Registration Cost cannot be zero.' );
		return false;
	}

	return true; //form is valid
}


//------------------------------------------------------
function isValidType( oInput, oType ) {
	switch( oType.toLowerCase() ) {
		case 'select':
			return oInput.selectedIndex;
			
		case 'number':
			if( !oInput.value ) { return false; }
			for( var mXi = 0; mXi < oInput.value.length; mXi++ ) {
				if( oInput.value.charAt( mXi ) != '' + parseInt( oInput.value.charAt( mXi ) ) + '' ) { return false; }
			} return true;
			
		case 'firstname':
			//tmpText = trim(oInput.value);
			return ( oInput.value && !oInput.value.replace( /[a-z]+([-. ]{0,2}[a-z]*){0,2}/i, "" )) ;

		case 'lastname':
			//tmpText = trim(oInput.value);
			return ( oInput.value && !oInput.value.replace( /[a-z]{2,}(-[a-z]{2,}){0,2}/i, "" ) );

		case 'phone':
			return ( oInput.value && !oInput.value.replace( /\d{3}-\d{3}-\d{4}/, "" ) );

		case 'email':
			return ( oInput.value && !oInput.value.replace( /[\w\-]+(\.[\w\-]+)*@[\w\-]+(\.[\w\-]+)+/i, "" ) );

		case 'zip':
			return ( oInput.value && !oInput.value.replace( /\d{5}(-\d{4}){0,1}/i, "" ) );
	}
	return true;
}


//------------------------------------------------------
function warnInvalid( theField, s, type ){
	theField.focus();
	if (type == 'select'){
		alert("Please select a '" + s + "'.");
	}
	else{
		theField.select();
		alert("'" + s + "' is missing or invalid.");
	}
	return false
}



