// How to Call the Script. Put these in the form parameter
// <form onsubmit="
// this.firstname.required = true;
// this.phonenumber.required = true;
// this.Email.required = true;
// this.Email.email = true;
// this.Kuaa_Terms_and_Conditions.checkbox = true;
// this.Kuaa_Terms_and_Conditions.required = true;
// this.zip.min = 0;
// this.zip.max = 99999;
// return verify(this);
// ">


// Student_BIrthdate
// Student_Current_School_Year
// Student_High_School_Graduation_Year

function CheckEmail(email)
{
	AtPos = email.indexOf("@")
	StopPos = email.lastIndexOf(".")

	//	if (email == "") {
	//	Message = "Not a valid Email address" + "\n"
	//	}

	if (AtPos == -1 || StopPos == -1)
	{
		// Message = "Not a valid email address"
		return false;
	}

	if (StopPos < AtPos)
	{
		// Message = "Not a valid email address"
		return false;
	}

	if (StopPos - AtPos == 1)
	{
		//Message = "Not a valid email address"
		return false;
	}

	return true;
}


// b = b.replace(/_/g," "); // Replaces all underscores with spaces.
//



// A utility function that returns true if a string contains only
// whitespace characters.
function isblank(s)
{
	for(var i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

// This is the function that performs form verification. It is invoked
// from the onsubmit event handler. The handler should return whatever
// value this function returns.
function verify(f)
{
	var msg;
	var empty_fields = "";
	var errors = "";



	var birthdate="";
	var schoolyear="";
	var graduationname="";
	var graduationyear="";

	var threeflag="";


	var numberofguestspresent="";


	// Loop through the elements of the form, looking for all
	// text and textarea elements that don't have an "optional" property
	// defined. Then, check for fields that are empty and make a list of them.
	// Also, if any of these elements have a "min" or a "max" property defined,
	// verify that they are numbers and in the right range.
	// If the element has a "numeric" property defined, verify that
	// it is a number, but don't check its range.
	// Put together error messages for fields that are wrong.
	for(var i = 0; i < f.length; i++)
	{
		// E IS THE ARRAY OF FORM FIELDS
		var e = f.elements[i];

		// begin IF
		if (((e.type == "text") || (e.type == "textarea")) && e.required)
		{
			// alert(e.name );

			// begin EMPTY CHECK
			//	alert(e.name );
			// first check if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value))
			{
 				// CLEAN UP MESSAGE
 				replacedname=e.name;
 				replacedname = replacedname.replace(/Kuaa_/g,""); // Replaces all underscores with spaces.
 				replacedname = replacedname.replace(/_/g," "); // Replaces all underscores with spaces.


 				empty_fields += "\n          " + replacedname;
				continue;
			}
			// close EMPTY CHECK


						// begin MAX AND MIN CHECK
						// Now check for fields that are supposed to be numeric.
						//if (e.numeric || (e.min != null) || (e.max != null))
						//{
						//	var v = parseFloat(e.value);
						//	if (isNaN(v) ||	((e.min != null) && (v < e.min)) || ((e.max != null) && (v > e.max)))
						//	{
						//		replacedname=e.name;
						//		replacedname = replacedname.replace(/_/g," "); // Replaces all underscores with spaces.
						//
						//		errors += "- The field " + replacedname + " must be a number";
						//		if (e.min != null)
						//			errors += " that is greater than " + e.min;
						//
						//		if (e.max != null && e.min != null)
						//		errors += " and less than " + e.max;
						//			else if (e.max != null)
						//				errors += " that is less than " + e.max;
						//			errors += ".\n";
						//
						//	} // END IF

						//	} // END IF

						// close MAX AND MIN CHECK

			// begin EMAIL CHECK
			// if (e.name== "Email" )
			if (e.email )
			{
				if (!CheckEmail(e.value))
				{
					replacedname=e.name;
					replacedname = replacedname.replace(/_/g," "); // Replaces all underscores with spaces.
					errors += "- The field " + replacedname + " must be a valid e-mail address";
					continue;

				} // END IF

			} // END IF

			// close EMAIL CHECK

		} // END IF TEXT OR TEXTAREA





		// begin IF CHECKBOX AND REQUIRED
		if ((e.type == "checkbox") && e.required)
		{
			//alert(e.name );
			//( document.contact_form.terms.checked == false )

			// begin EMPTY CHECK
			// alert(e.name );
			// first check if the field is empty
			if (!e.checked)
			{
 				// CLEAN UP MESSAGE
 				replacedname=e.name;
 				replacedname = replacedname.replace(/Kuaa_/g,""); // Replaces all underscores with spaces.
 				replacedname = replacedname.replace(/_/g," "); // Replaces all underscores with spaces.


 				empty_fields += "\n          " + replacedname;
				continue;
			}
			// close EMPTY CHECK

		} // END IF TEXT OR TEXTAREA
















		// CUSTOM CHECKS FOR JAYHAWKGENERATIONS
		if (e.name == "Student_Birthdate" )
		{
			if ((e.value == null) || (e.value == "") || isblank(e.value))
			{
				// alert("Birthdate is empty");
				 birthdate="no";
			}

		} // END IF

		if (e.name == "Student_Current_School_Year" )
		{
			if ((e.value == null) || (e.value == "") || isblank(e.value))
			{
				// alert("Student_Current_School_Year is empty");
				 schoolyear="no";
			}

		} // END IF

		if (e.name == "Student_Anticipated_High_School_Name" )
		{
			if ((e.value == null) || (e.value == "") || isblank(e.value))
			{
				// alert("Student_Anticipated_High_School_Name is empty");
				 graduationname ="no";
			}

		} // END IF

		if (e.name == "Student_Anticipated_Graduation_Year" )
		{
			if ((e.value == null) || (e.value == "") || isblank(e.value))
			{
				// alert("Student_Anticipated_Graduation_Year is empty");
				 graduationyear ="no";
			}

		} // END IF

		// CHECK ALL OF THE CUSTOMS AND SET FLAG
		if ( (birthdate == "no") && (schoolyear=="no") && (graduationname=="no") && (graduationyear=="no"))
		{
			// alert(" one is empty empty" );
			threeflag="yes";

		} // END IF



		// DO GUESTFIELDS Guest_Number_Attending Guest_Name_1
		// CUSTOM CHECKS TO SEE IF NUMBER IS IN FIELD IF SO CHECK IT FOR VALIDITY
		if (e.name == "Guest_Number_Attending" )
		{
			// alert ("There is a field named GUESTNUMBERATTENDING " + e.name + "    "  +   e.value  );

			// IF NULL EMPTY OR BLANK
			if ((e.value != null) && (e.value != "")  &&  !isblank(e.value))
			{
				// alert ("GUESTNUMBERATTENDING IS NOT NULL OR EMPTY OR BLANK");

				// SWAP EM OUT
				var x=e.value;

				// CHECK IF NUMBER IS 0
				if (x ==0)
				{
					errors += "- Number of guests cannot be 0. If no guests please leave field empty.\n";
				}
				else if (x != parseInt(x))
				{
					errors += "- Number of guests requires numeric input.\n";
				}
				else
				{
					numberofguestspresent = "yes";
					// alert ("number of guests present and valid " +  x + "number behind me");

				} // END IF

			} // END IF

		} // END IF

	} // END FOR EACH

	// IF THREEFLAG IS YES ADD ERROR
	if (threeflag=="yes")
	{
		errors += "- Either Student's Birth date, School Year, Anticipated HS Name \n  or Anticipated HS Graduation year must be entered. \n  Please complete as many of these as possible.";
	}

	// LETS NOW CHECK THE FIELDS ARE PRESENT TO THE AMOUNT OF GUESTS GIVEN


	// TEST FOR THE NUMBER OF FIELD IS TO RUN
	if (numberofguestspresent =="yes")
	{
		//alert ("number of guests present and valid " +  x + "number behind me");

		// FOR EACH NUMBER OF GUESTS LOOK UP THE NAME AND SEE IF IT IS EMPTY
		for (var i=1;i<=x; i++)
		{
			// alert ("the number " + i);
			var fieldname = "Guest_Name_" + i;

			// begin FOREACH ITEM IN FORM
			for(var kk = 0; kk < f.length; kk++)
			{
				// E CONTAINS ALL FORM ELEMENTS
				var ee = f.elements[kk];

				if (ee.name == fieldname)
				{
					// alert ( ee.name + " EQUALS "  +  fieldname )

					// alert ("EENAME  = " + ee.name   );
					// alert ("EEVALUE = " + ee.value   );

					if ((ee.value == null) || (ee.value == "") || isblank(ee.value))
					{
						errors += "- Guest Name " + i + " needs to be completed.\n" ;

					} // END IF

				}  // END IF

			} // END FOR

		} // END FOR

	} // END IF


	// Now, if there were any errors, display the messages, and
	// return false to prevent the form from being submitted.
	// Otherwise return true.
	if (!empty_fields && !errors) return true;

	msg  = "______________________________________________________\n\n"
	msg += "The form was not submitted because of the following error(s).\n";
	msg += "Please correct these error(s) and re-submit.\n";
	msg += "______________________________________________________\n\n"

	// IF EMPTY FIELD
	if (empty_fields)
	{
      		 msg += "- The following required field(s) are empty:" + empty_fields + "\n";

       		if (errors) msg += "\n";

    	} // END IF

	msg += errors;
	alert(msg);
	return false;

} // END FUNCTION
