var checkedYahooId   = false;
var checkedNoContact = false;

function validatereq()
{
	var x = 0;
	var message = "Please complete the following field(s):\n\n";

	if (validateName(namecoll[0].value) > 0)          message += "First Name\n";
	if (validateName(namecoll[1].value) > 0)          message += "Middle Name or Initial\n";
	if (validateName(namecoll[2].value) > 0)          message += "Last Name\n";
	if (validateStudentid(namecoll[3].value) > 0)     message += "Student ID Number\n";
	if (validateAddress(addrcoll[0].value) > 0)       message += "Street Address\n";
	if (validateAddress(addrcoll[2].value) > 0)       message += "City\n";
	if (validateZIP(addrcoll[3].value) > 0)           message += "ZIP Code: #####-####\n";
	if (validateBday(memberform.Birthdate.value) > 0) message += "Birthdate: mm/dd\n";
	/*  validatePhone  */                             message += validatePhone(phonecoll);
	/*  validateEmail  */                             message += validateEmail(emailcoll);
	if (memberform.Major.value.length == 0)           message += "Major\n";
	if (gradcoll[0].value.length == 0)                message += "Expected Graduation Semester\n";
	if (validateGrad(gradcoll[1].value) > 0)          message += "Expected Graduation Year: yyyy\n";
	if (validateOQ() > 0)                             message += "You can not have quotes (\") in your responses to the open questions.\n";
	if (validateSched() > 0)                          message += "Your Availability Schedule (without this, we can't schedule events)\n";
	if (vercoll[1].checked == false)                  message += "\nYou MUST certify the validity of your information by\nchecking the box at the bottom of the form!\nYou may be under penalty of purjury and prosecution\nif you submit false or misleading information.\n";

	if (message.length > 45)
	{
	  alert(message);
	  x++;
	}

	if(conpcoll[4].checked == true &&
	   conpcoll[5].checked == true &&
	   !checkedNoContact)
	{
		if(window.confirm("You have chosen to NOT allow us to contact you by phone or email, is this correct?\n\n\nClick OK to confirm. Click Cancel to return to the form."))
			checkedNoContact = true;
	}

	if(emailcoll[2].value.length < 6 &&
	   !checkedYahooId)
	{
		x++;
		if(window.confirm("It appears that you didn't enter a Yahoo ID.\nUpsilon Kappa uses a Yahoo Group to communicate and distribute some materials to our members.\n\nIf you don't have a Yahoo ID, you can easily create an account at: www.yahoo.com\n\nClick OK to visit Yahoo and create an account. Click Cancel to continue."))
		{
			yw = window.open("https://edit.yahoo.com/config/eval_register?.intl=us&.done=http%3A//groups.yahoo.com/group/PTKfellowship/join%3F&.src=ygrp");
			window.focus();
			alert("Phi Theta Kappa Webform Message:\n\nYou may submit the form without providing a Yahoo ID by clicking the Submit button again.");
			yw.focus();

		}
		else if(x == 1 && window.confirm("Would you like to submit the form without providing a Yahoo ID?\n\nClick OK to continue. Click Cancel to return to the form."))
			return true;
		checkedYahooId = true;
	}

	if(x) return false;
	return true;
}

function validateName(pv)
{
	if(pv.length == 0) return 1;
	if(outsideCharSet(pv, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ()-.")) return 1;
	return 0;
}

function validateStudentid(pv)
{
	if(pv.length < 6 ||
	   pv.length > 10) return 1;
	if(outsideCharSet(pv, "1234567890")) return 1;
	return 0;
}

function validateAddress(pv)
{
	if(pv.length == 0) return 1;
	if(!(pv.search(/\"/) == -1)) return 1;
	return 0;
}

function validateZIP(pv)
{
	if(pv.length < 5 ||
	   pv.length > 10 ) return 1;
	if(outsideCharSet(pv.substr(0, 5), "1234567890")) return 1;
	if(pv.length > 5)
	{
		if(pv.length == 6 ||
		   outsideCharSet(pv.substr(5, 1), "-")) return 1;
	}
	if(pv.length > 6)
	{
		if(pv.length != 10 ||
		   outsideCharSet(pv.substr(6), "1234567890")) return 1;
	}
	return 0;
}

function validateBday(pv)
{
	if(pv.length < 3 ||
	   pv.length > 5) return 1;
	if(outsideCharSet(pv, "1234567890/") ||
	   pv.indexOf("/") < 1 ||
	   outsideCharSet(pv.substr(pv.indexOf("/") + 1), "1234567890"))
		return 1;
	if(pv.substr(0, pv.indexOf("/")).valueOf()  > 12) return 1;
	if(pv.substr(pv.indexOf("/") + 1).valueOf() > 31) return 1;
	return 0;
}

function validatePhone(pc)
{
	var x = 0;
	var msg = "";

	// check for all empty phone fields
	for(i = 0; i < pc.length; i++)
	{
		if(pc[i].value.length == 0 ||
		   pc[i].value == "###-###-####")
			x++;
	}
	if(x == 3)
		return "You must enter at least one type of contact phone number\n";

	// check for length of filled fields
	x = 0;
	for(i = 0; i < pc.length; i++)
	{
		if((pc[i].value.length > 0   && pc[i].value.length < 10) ||
		   pc[i].value.length != 10 &&
		   pc[i].value.length != 12)
		{
			if(pc[i].value.length > 0)
				msg += "Phone number: \"" + pc[i].value.substr(0, 12) + "\" is not a valid phone number!\n";
		}
		else
		{
			if(pc[i].value.length == 10)
			{
				if(outsideCharSet(pc[i].value, "1234567890"))
					msg += "Phone number: \"" + pc[i].value + "\" is not a valid phone number!\n";
			}
			else if(pc[i].value.length == 12)
			{
				if(outsideCharSet(pc[i].value.substr(0, 3), "1234567890") ||
				   outsideCharSet(pc[i].value.substr(3, 1), "-")          ||
				   outsideCharSet(pc[i].value.substr(4, 3), "1234567890") ||
				   outsideCharSet(pc[i].value.substr(7, 1), "-")          ||
				   outsideCharSet(pc[i].value.substr(8, 4), "1234567890"))
					msg += "Phone number: \"" + pc[i].value + "\" is not a valid phone number!\n";

			}
			else
				msg += "Phone number: \"" + pc[i].value + "\" is not a valid phone number!\n";
		}
	}

	if(msg.length > 0)
		return msg += "        Phone number format: ###-###-####\n";
	else
		return "";
}

function validateEmail(pc)
{
	var msg = "";

	if(pc[0].value.length == 0)
		return "Primary Email Address\n";
	for(i = 0; i < 2; i++)
	{
		if(pc[i].value.length > 7 &&
		   pc[i].value.length < 51)
		{
			if(!outsideCharSet(pc[i].value, "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@._-"))
			{
				if(pc[i].value.indexOf("@") < 2)
				{
					msg += "Email address: \"" + pc[i].value + "\" is not a valid email address!\n";
				}
				else if(pc[i].value.indexOf(".", pc[i].value.indexOf("@") + 1) == -1)
				{
					msg += "Email address: \"" + pc[i].value + "\" is not a valid email address!\n";
				}
			}
			else
			{
				msg += "Email address: \"" + pc[i].value + "\" is not a valid email address!\n";
			}
		}
		else
		{
			if(pc[i].value.length > 0)
				msg += "Email address: \"" + pc[i].value.substr(0, 50) + "\" is not a valid email address!\n";
		}
	}

	if(msg.length > 0)
		return msg += "        Email address format: username@domainname.tld\n";
	else
		return "";
}

function validateGrad(pv)
{
	if(pv.length < 4) return 1;
	if(outsideCharSet(pv, "1234567890")) return 1;
	if(pv.valueOf() < 2000) return 1;
	return 0;
}

function validateOQ()
{
	if(!(document.getElementById("Business_Affiliations").value.search(/\"/) == -1)
           || !(document.getElementById("Hobbies").value.search(/\"/) == -1)
           || !(document.getElementById("Skills").value.search(/\"/) == -1)
           || !(document.getElementById("Special_Project").value.search(/\"/) == -1)
           || !(document.getElementById("Organization_Volunteer").value.search(/\"/) == -1)
        ) return 1;
	return 0;
}

function validateSched()
{
	var c = 0;
	for(x = 0; x < moncoll.length; x++) { if(moncoll[x].checked == false) c++; }
	for(x = 0; x < tuecoll.length; x++) { if(tuecoll[x].checked == false) c++; }
	for(x = 0; x < wedcoll.length; x++) { if(wedcoll[x].checked == false) c++; }
	for(x = 0; x < thucoll.length; x++) { if(thucoll[x].checked == false) c++; }
	for(x = 0; x < fricoll.length; x++) { if(fricoll[x].checked == false) c++; }
	for(x = 0; x < satcoll.length; x++) { if(satcoll[x].checked == false) c++; }
	if(c > 88)
		return 1;
	return 0;
}

function outsideCharSet(text, set)
{
	var outsideset = false;
	for(t = 0; t < text.length; t++)
	{
		var x = 0;
		for(s = 0; s < set.length; s++)
		{
			if(text.charCodeAt(t) != set.charCodeAt(s))
				x++;
		}
		if(x == set.length)
		{
			outsideset = true;
			break;
		}
	}
	return outsideset;
}

