var browser = ""; // Will hold string code for browser

// if Mozilla-compliant
if (document.getElementById)
{
	browser = "mozilla";
}
// if IE 4
else if (document.all)
{
	browser = "ie4";
}
// if N 4
else if (document.layers)
{
	browser = "n4";
}
else
{
	browser = "other";
}

function verify(which)
{
	// define pubForm
	var pubForm;
	if (browser == "mozilla") // Mozilla-compliant, determined in rotateImages.js
	{
		pubForm = document.getElementById("pubForm");
	}
	if (browser == "ie4") // Internet Explorer 4, determined in rotateImages.js
	{
		pubForm = document.all.pubForm;
	}
	else
	{
		pubForm = document.pubForm;
	}
	
	var verifyMsg;
	
	if (which == "pubs")
	{
		verifyMsg = verifyPubs(pubForm);
		if (verifyMsg == "")
		{
			submitForm();
		}
		else
		{
			alert("Unable to submit form:\n" + verifyMsg);
		}
	}
	else if (which == "addr")
	{
		verifyMsg = verifyAddr(pubForm);
		if (verifyMsg == "")
		{
			submitForm();
		}
		else
		{
			alert("Unable to submit form:\n" + verifyMsg);
		}
	}
	else
	{
		submitForm();
	}
	
}

function verifyPubs(pubForm)
{
	var returnString = "";

	var notDigitRegEx = /\D/;
	if ( notDigitRegEx.test(pubForm.elements['practices'].value) ) 
	{	returnString += "\n" + "You did not enter a valid quantity of \"Professional Practices\"";	 }
	if ( notDigitRegEx.test(pubForm.elements['contract'].value) ) 
	{	returnString += "\n" + "You did not enter a valid quantity of \"Model Museum Director's Contracts\"";	}
	if ( notDigitRegEx.test(pubForm.elements['salarySurveyMember'].value) ) 
	{	returnString += "\n" + "You did not enter a valid quantity of \"Salary Surveys (member's price)\"";	}
	if ( notDigitRegEx.test(pubForm.elements['salarySurveyNonMember'].value) ) 
	{	returnString += "\n" + "You did not enter a valid quantity of \"Salary Surveys (non-member's price)\"";	}
	// returnString += "\n" + "...not proceeding...";

	if ( 0 + 
		 pubForm.elements['practices'].value + 
		 pubForm.elements['contract'].value +
		 pubForm.elements['salarySurveyMember'].value +
		 pubForm.elements['salarySurveyNonMember'].value
		 == 0 ) 
	{ returnString += "\n" + "You have not chosen any publications."; }

	return returnString;
}

function verifyAddr(pubForm)
{
	var returnString = "";

/*
//	Developing debugging: list out input values
	var valueReport = "firstName: " + pubForm.elements['firstName'].value + "\n";
	valueReport += "lastName: " + pubForm.elements['lastName'].value + "\n";
	valueReport += "address1: " + pubForm.elements['address1'].value + "\n";
	valueReport += "address2: " + pubForm.elements['address2'].value + "\n";
	valueReport += "state: " + pubForm.elements['state'].value + "\n";
	valueReport += "zip / postal: " + pubForm.elements['postalCode'].value + "\n";
	valueReport += "phoneNumber: " + pubForm.elements['phoneNumber'].value + "\n";
	valueReport += "member Yes?: " + pubForm.elements['member'][0].checked + "\n";
	valueReport += "member No?: " + pubForm.elements['member'][1].checked + "\n";
	valueReport += "email: " + pubForm.elements['email'].value + "\n";
	
//	alert(valueReport);
*/

//  Validate name and address
	var stringStripped = pubForm.elements['firstName'].value.replace(/\s/, '');
	if (stringStripped.length == 0)
	{
		returnString += "\nYou did not enter valid text into the \"First Name\" field.";
		pubForm.elements['firstName'].value = "";
	}
	
	stringStripped = pubForm.elements['lastName'].value.replace(/\s/, '');
	if (stringStripped.length == 0)
	{
		returnString += "\nYou did not enter valid text into the \"Last Name\" field.";
		pubForm.elements['lastName'].value = "";
	}

	stringStripped = pubForm.elements['address1'].value.replace(/\s/, '');
	if (stringStripped.length == 0)
	{
		returnString += "\nYou did not enter valid text into the \"Address 1\" field.";
		pubForm.elements['address1'].value = "";
	}

//	Second line of Address is optional.

//	Validate State
//	Find selected option from pulldown and check its value

	var selectedIndex = pubForm.elements['state'].selectedIndex;
	if ( pubForm.elements['state'].options[selectedIndex].value.length == 0 )
	{
		returnString += "\nYou did not select a state or province.";
	}

//	Validate (minimally) the postalCode

	stringStripped = pubForm.elements['postalCode'].value.replace(/\W/g, '');
	if (stringStripped.length == 0)
	{
		returnString += "\nYou did not enter valid text into the \"Zip/Postal Code\" field.";
		pubForm.elements['postalCode'].value = "";
	}	
	else if (stringStripped.length < 5) // Too short
	{
		returnString += "\nThe zip code / postal code you entered appears to be too short to be valid.";
	}	
//	Validate Phone Number

	//strip out acceptable non-numeric characters
	stringStripped = pubForm.elements['phoneNumber'].value.replace(/\s/, '');
	var stripped = pubForm.elements['phoneNumber'].value.replace(/[\(\)\.\-\ \+]/g, '');
	if (stringStripped.length == 0)
	{
		returnString += "\nYou did not enter valid text into the \"Phone Number\" field.";
		pubForm.elements['phoneNumber'].value = "";
	}	
	else if (stripped.length < 10) // Too short
	{
		returnString += "\nThe phone number you entered appears to be too short to be valid. Did you include your area code?";
	}
	
//	Make sure a radio button is checked
	if ( !pubForm.elements['member'][0].checked && !pubForm.elements['member'][1].checked)
	{
		returnString += "\nYou did not indicate whether or not you are a member of AAMD.";
	}
		
//	Validate email address
	stringStripped = pubForm.elements['email'].value.replace(/\s/, '');
	var emailFilter=/^.+@.+\..{2,3}$/;
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	
	if (stringStripped.length == 0)
	{
		returnString += "\nYou did not enter valid text into the \"Email\" field.";
		pubForm.elements['email'].value = "";
	}	
	else if (!(emailFilter.test(pubForm.elements['email'].value))) { 
	      returnString += "\nThe email address you entered does not appear to be valid. Please check it.";
	}
	else if (pubForm.elements['email'].value.match(illegalChars)) {
	    returnString += "\nThe email address you entered contains illegal characters.";
	}

//	returnString += "\n" + "...not proceeding...";
	return returnString;
}
