// popup help system functions
function showHelp(context) {

	var helpFile = "http://www.psychologicalscience.org/cfs/author/help/help.cfm?topic=" + context;
	var winName = "helper";
	var winConfig = "width=400,height=350,left=350,top=75,scrollbars=yes,resizable=yes,toolbar=no";
	
	window.open(helpFile, winName, winConfig);
}

function validateSubmission(form, page) {
	// check for the abstract type, symposium have 4 co-authors, posters have 7
	if(form.Abstract_Type_ID.value == 1) {co_authors = 4;}
	else{co_authors = 7;}
	
	if(page == 1 || page == 3)
	{
		if(form.Abstract_Title.value.length == 0)
		{
			showError(form.Abstract_Title, "Please enter an Abstract Title");
			return false;
		}
		if(form.Primary_Subject.value == "")
		{
			showError(form.Primary_Subject, "Please select a Subject Area");
			return false;
		}
		if(form.keyword != null && form.keyword.value.length == 0)
		{
			showError(form.keyword, "Please select a Keyword");
			return false;
		}
		if(form.Abstract_Summary.value.length == 0)
		{
			showError(form.Abstract_Summary, "Please enter an Abstract Summary");
			return false;
		}
		<!--- check to ensure the abstract summary is 50 words or less --->
		var summaryArray = new Array();
		var summary = form.Abstract_Summary.value;
		summaryArray = summary.split(" ");
		if(summaryArray.length > 60)
		{
			showError(form.Abstract_Summary, "The Abstract is over 50 words.");
			return false;
		}
		if(form.Abstract_Support.value.length == 0)
		{
			showError(form.Abstract_Support, "Please enter a Supporting Summary");
			return false;
		}
		<!--- check to ensure the abstract support is 500 words or less --->
		var supportArray = new Array();
		var support = form.Abstract_Support.value;
		supportArray = support.split(" ");
		if(supportArray.length > 600)
		{
			showError(form.Abstract_Support, "The Supporting Summary is over 500 words.");
			return false;
		}
		if(form.student.value == "true") {
			if(form.student_competition[1].checked == true || form.student_competition[2].checked == true) {
				if(form.rse_up_category.value == "" && form.src_category.value == "") {
					if(form.student_competition[1].checked == true)	showError(form.src_category, "Please select a category");
					if(form.student_competition[2].checked == true)	showError(form.rse_up_category, "Please select a category");
					return false;
				}
				if(form.student_competition[1].checked == true && form.src_category.value == 1 && form.other_src.value == "") {
					showError(form.other_src, "Please enter a category.");
					return false;
				}
				if(form.student_competition[2].checked == true && form.rse_up_category.value == 1 && form.other_rse_up.value == "") {
					showError(form.other_rse_up, "Please enter a category.");
					return false;
				}
				if(form.student_type[1].checked != true && form.student_type[2].checked != true) {
					showError(form.student_type[0], "Please select whether you are a Graduate or Undergraduate student.");
					return false;
				}
			}
		}
	}
	if(page == 2 || page == 3)
	{
		if(form.Last_Name.value.length == 0)
		{
			showError(form.Last_Name, "Please enter a Last Name");
			return false;
		}
		// check to make sure if someone put in a last name for a co-presenter or co-author in a symposium they have entered a topic
		if (form.Abstract_Type_ID.value == 1) {
			var co_authors = 4;
		} else {
			var co_authors = 15;
		}
		if (form.Abstract_Type_ID.value == 1) {
			var FirstAuthor = "1st Chair";
			var SecondAuthor = "2nd Co-chair";
			var coAuthor = "Presenter";
		} else if (form.Abstract_Type_ID.value == 6 || form.Abstract_Type_ID.value == 7) {
			var FirstAuthor = "Chair/Moderator";
			var coAuthor = "Presenter";
		} else if (form.Abstract_Type_ID.value == 5 || form.Abstract_Type_ID.value == 3) {
			var FirstAuthor = "First Author";
			var coAuthor = "Co-Author";
		} else {
			var FirstAuthor = "Presenter"; 
			var coAuthor = "Co-Author";
		}
		if (form.First_Name.value.length != 0 && 
				form.Last_Name.value.length != 0 && 
				form.Affiliation.value.length == 0) {
			var FirstAuthor = "1st Chair";
			showError(form.Affiliation, "Please enter an Affiliation for the " + FirstAuthor + ".");
			return false;
		}
		if (form.Abstract_Type_ID.value == 1) {
			if (form.First_Name2.value.length != 0 && 
					form.Last_Name2.value.length != 0 && 
					form.Affiliation2.value.length == 0) {
				showError(form.Affiliation2, "Please enter an Affiliation for the " + SecondAuthor + ".");
				return false;
			}
		}
		for (i=1; i < co_authors; i++) {
			if (form["First_Name_" + i].value.length != 0 && 
					form["Last_Name_" + i].value.length != 0 &&
					form["Affiliation_" + i].value.length == 0) {
				showError(form["Affiliation_" + i], "Please enter an Affiliation for all " + coAuthor + "s.");
				return false;
			}
		}

	}
	if(page == 2 && form.Abstract_Type_ID.value == 1)
	{
		// loop through all the fields, get the last name, topic field, and topic length
		for(topicCount = 1; topicCount <= co_authors; topicCount++) {
			// use eval() to get the length of lastname, length of the topic, and topic
			var lastname = eval("form.Last_Name_" + topicCount + ".value.length");
			var topicLength = eval("form.Sub_Topic_" + topicCount + ".value.length");
			var topic = eval("form.Sub_Topic_" + topicCount);
			// if the last name length is not 0 (there is a name) but the topic is zero, prompt the user to enter a topic
			if(lastname != 0)
			{
				if(topicLength == 0)
				{
					
					showError(topic, "Please enter a Topic");
					return false;
				}
			}
		}
	}
	// check for the topic being entered if the user is on the edit page
	else if(page == 3 && form.Abstract_Type_ID.value == 1)
	{
		// loop through all the fields, get the last name, topic field, and topic length
		for(topicCount = 1; topicCount <= co_authors; topicCount++) {
			// use eval() to get the length of lastname, length of the topic, and topic
			var lastname = eval("form.Last_Name_" + topicCount + ".value.length");
			var topicLength = eval("form.Sub_Topic_Title_" + topicCount + ".value.length");
			var topic = eval("form.Sub_Topic_Title_" + topicCount);
			// if the last name length is not 0 (there is a name) but the topic is zero, prompt the user to enter a topic
			if(lastname != 0)
			{
				if(topicLength == 0)
				{
					
					showError(topic, "Please enter a Topic");
					return false;
				}
			}
		}
	}
	// call ValidateAffiliation from js_abstract_validation.cfm
	if (page == 3) { ValidateAffiliation() }
	// use second page == 2 || page == 3 if statement to keep error checking in nice order
	if(page == 2 || page == 3)
	{
		// check to see that there are no two presentations with the same order
		for(orderCount1 = 1; orderCount1 < co_authors; orderCount1++)
		{
			for(orderCount2 = (orderCount1 + 1); orderCount2 < (co_authors + 1); orderCount2++)
			{
				var firstOrder = eval("form.order_" + orderCount1 + ".value");
				var secondOrder = eval("form.order_" + orderCount2 + ".value");
				var firstName = eval("form.Last_Name_" + orderCount1 + ".value");
				var secondName = eval("form.Last_Name_" + orderCount2 + ".value");
				// check to make sure that two orders are not the same and they are not both null
				if(firstOrder == secondOrder && firstOrder != 99 && secondOrder != 99 && (firstName != "" && secondName != ""))
				{
					showError(eval("form.order_" + orderCount1), "Two Presenters have the same number. Please check the order.");
					return false;
				}
				// make sure there is a value for the order select
				if(firstOrder == "99" && firstName != "") {
					showError(eval("form.order_" + orderCount1), "Please select an order for a presenter.");
					return false;
				}
			}
		}
	}
	return true;
}

function showError(ctrl, msg) {
	ctrl.focus();
	alert(msg);
}
