// $Header: /HBI Websites/_client_common/js/hirediversity/validate_job_search.js 11    6/10/04 3:28p Matthew.van.eerde $

// requires include.js
include("is_valid_date");
include("is_valid_email");

function validate_job_search(form)
{	var aok = true;
	var alertText = "";

// new search doesn't have any Keyword<n> fields
if (form.Keyword1)
{	if (	(!form.Keyword1.value && form.Keyword2.value) || // keyword 2 but not keyword 1
		(!form.Keyword2.value && form.Keyword3.value) || // keyword 3 but not keyword 2
		(!form.Keyword3.value && form.Keyword4.value) || // keyword 4 but not keyword 3
		(!form.Keyword4.value && form.Keyword5.value) || // keyword 5 but not keyword 4
		(!form.Keyword5.value && form.Keyword6.value) || // keyword 6 but not keyword 5
		(!form.Keyword6.value && form.Keyword7.value)    // keyword 7 but not keyword 6
	)
	{	aok = false;
		alertText += "Please fill in the keywords in order.\n";
	} 
}

	if (	form.RunEveryNDays &&                   // if the form element exists
		!/^\d+$/.test(form.RunEveryNDays.value) // but the value is not numeric
	)
	{	aok = false;
		alertText += "Number of days must be a number.\n";
		form.RunEveryNDays.focus();
	}
	
	if (	form.PostedAfter &&                    // if the form element exists
		form.PostedAfter.value &&              // and has something in it
		!is_valid_date(form.PostedAfter.value) // which is not a valid date
	)
	{	aok = false;
		alertText +=
			"The date you entered\n" +
			form.PostedAfter.value +
			"\ndoes not appear to be a valid date.\n";
		form.PostedAfter.focus();
	}

	if (	form.MaxRows &&                    // if the form element exists
		form.MaxRows.value &&              // and has something in it
		!/^\d+$/.test(form.MaxRows.value) // which is not a number
	)
	{	aok = false;
		alertText +=
			"The MaxRows you entered\n" +
			form.MaxRows.value +
			"\nis not a number.\n";
		form.MaxRows.focus();
	}

	if (	form.ClientID &&                    // if the form element exists
		form.ClientID.value &&              // and has something in it
		!/^\d+$/.test(form.ClientID.value) // which is not a number
	)
	{	aok = false;
		alertText +=
			"The ClientID you entered\n" +
			form.ClientID.value +
			"\nis not a number.\n";
		form.ClientID.focus();
	}

	if (	form.UserID &&                    // if the form element exists
		form.UserID.value &&              // and has something in it
		!/^\d+$/.test(form.UserID.value) // which is not a number
	)
	{	aok = false;
		alertText +=
			"The UserID you entered\n" +
			form.UserID.value +
			"\nis not a number.\n";
		form.UserID.focus();
	}	

	if (	form.BeginDate &&                    // if the form element exists
		form.BeginDate.value &&              // and has something in it
		!is_valid_date(form.BeginDate.value) // which is not a valid date
	)
	{	aok = false;
		alertText +=
			"The date you entered\n" +
			form.BeginDate.value +
			"\ndoes not appear to be a valid date.\n";
		form.BeginDate.focus();
	}

	if (	form.EndDate &&                    // if the form element exists
		form.EndDate.value &&              // and has something in it
		!is_valid_date(form.EndDate.value) // which is not a valid date
	)
	{	aok = false;
		alertText +=
			"The date you entered\n" +
			form.EndDate.value +
			"\ndoes not appear to be a valid date.\n";
		form.EndDate.focus();
	}

	if (	form.ContactEmail &&                     // if the form element exists
		form.ContactEmail.value &&               // and has something in it
		!is_valid_email(form.ContactEmail.value) // which is not a valid email
	)
	{	var confirm_prompt =
			"Contact Email\n" + form.ContactEmail.value +
			"\ndoes not appear to be a valid email address.\n" +
			"Press OK to use anyway, or Cancel to go back and fix.";

		if (!confirm(confirm_prompt))
		{	aok = false;
			alertText +=
			"The contact email you entered\n" +
				form.ContactEmail.value +
				"\ndoes not appear to be a valid email.\n";
			form.ContactEmail.focus();
		}
	}

	if (	form.locationids &&
		form.locationids.options
	) // no more than 20 should be selected
	{	var locations_selected = 0;
		var i = 0;
		var num_locations = form.locationids.options.length;

		for (i = 0; i < num_locations; i++)
		{	if (form.locationids.options[i].selected)
			{	locations_selected++;
			}
		}

		if (locations_selected > 20)
		{	aok = false;
			alertText +=
				"You have selected " +
				locations_selected +
				" locations - please limit yourself to 20 or fewer\n";
		}
	}

	if (	form.zipcode &&						// if there's an input
		form.zipcode.value &&					// and there's something in it
		!/^\d{5}$/.test(form.zipcode.value) &&			// which isn't a US Zip code
		!/^[a-z]\d[a-z] \d[a-z]\d$/i.test(form.zipcode.value)	// or a Canadian Postal Code
	)
	{	aok = false;
		alertText +=
			"Zip Code should be a five-digit US ZIP code or " +
			"a Canadian postal code\n" +
			"- you entered \"" + form.zipcode.value + "\"\n";
		form.zipcode.focus();
	}

	if (form.industryids && form.industryids.options) // no more than seven should be selected
	{	var industries_selected = 0;
		var i = 0;
		var num_industries = form.industryids.options.length;

		for (i = 0; i < num_industries; i++)
		{	if (form.industryids.options[i].selected)
			{	industries_selected++;
			}
		}

		if (industries_selected > 20)
		{	aok = false;
			alertText +=
				"You have selected " +
				industries_selected +
				" industries - please limit yourself to seven or fewer\n";
		}
	}

	if (!aok)
	{	alert(alertText);
	}
	
	return aok;
}
