// Javascript Document

/*  This script should perform various form validations for
	the HerpAtlas e-form (submitting sightings).

	It should check for an email address, and suggest
	that people add that so Jim can get in touch with them
	if he has questions.

	It should also prevent many spammy reports that have nothing to
	do with reptiles and amphibians.
	
	It may eventually take the printform function out of the inline script on the eform page.
	I wonder if there's a way to print the form without javascript?
*/

//  EMAIL ADDRESS REMINDER
	//	This is not a required field (per JA), but we like it. So: reminders.
	// 	When the focus is on the email field, show some reminder text
	//	I suppose these could be rolled into 1 'toggle' function. Next time!
function showHint(n) // function is called from within form with onfocus
	{
	var showme = document.getElementById(n);
	if (showme.style.display = 'none')
		{
		showme.style.display = 'block';
		}
	}	
function hideHint(n)  // f is called from within form
	{
	var showme = document.getElementById(n);
	if (showme.style.display = 'block')
		{
		showme.style.display = 'none';
		}
	}	

// OVERALL FORM VALIDATION 
	// Adapted from http://www.quirksmode.org/dom/error.html
	// Thanks, PPK...
	// For details re PPK's terrific work, copyright thoughts and permssions, see
	// http://www.quirksmode.org/about/copyright.html

var W3CDOM = (document.getElementsByTagName && document.createElement);

// this variable is used to scan the form for unrelated concepts, and prevent (as much as possible) them from being sent.
var junkword = new Array('unknown-','ka4man','skunk.middlebury','Skolosov','kestov', 'boris', 'margana','Margana', 'asmanow', 'feechka', 'olgunka', 'icantgame', 'listik','lesbian', 'women', 'horror', 'breast','fart','pussy','voyeur','porn','bootleg', 'goblin', 'cock ', 'buy', ' ass ','spam', 'bikini', 'suck my', 'dick', 'playboy', 'nude', 'bollywood', 'sexy', 'xxx', 'viagra');

function validate() {
	validForm = true;
	firstError = null;
	errorstring = '';
	var x = document.forms[0].elements;
	// The Atlas doesn't require all fields, but it's pretty important to have some sort of animal name
	// even if they write "unknown"
	if (x['Common_Name'].value == '' && x['Latin_Name'].value == '')
		writeError(x['Common_Name'],'A name (common or latin/scientific) for the species is required.');
	if (x['Town'].value == '')
		writeError(x['Town'],'This field is required.');
	if (x['Date'].value == '')
		writeError(x['Date'],'This field is required.');
	if (x['First_Name'].value == '' && (x['Last_Name'].value == ''))
		writeError(x['First_Name'],'We do ask for your name (first initial for first name is OK) for our records.');
	if (x['First_Name'].value != '' && x['Last_Name'].value == '')
		writeError(x['Last_Name'],'Please provide your last name for our records. Thank you.');
	if (x['First_Name'].value == '' && x['Last_Name'].value != '')
		writeError(x['First_Name'],'We do ask for your name (first initial for first name is OK) for our records.');


	// use PPK's bit when all fields are required
	/*
	for (var i=0;i<x.length;i++) {
		if (!x[i].value)
			writeError(x[i],'This field is required');
	}
	*/

	// The Atlas doesn't require an email address, but if they'll put it in, let's check the format
	// this is a slight mod of PPK's (with the Boolean AND condition)
	if (x['email'].value.indexOf('@') == -1 && x['email'].value != '') 
		writeError(x['email'],'This is not a valid email address. Did you mistype it?');
	if (x['email'].value ==  '') {
		showHint('eAlert');
		return false; 
		}

	// KJT's change:
	// Check over the form and hang up on the junkwords stuff
	// The form will NOT submit as long as junkwords are in it.
	// You may need to keep adding to the junkwords array (bleah!)

	for (var i=0;i<x.length;i++) {
		for (var j=0;j<junkword.length;j++) {
			var content=x[i].value.toLowerCase();
			var yuck=junkword[j];
			var foundjunk=content.search(yuck);
			if (foundjunk != -1)
					writeError(x[i],'There is a problem or typo in this field.');
			if ( ( (content.search('http') != -1) || (content.search('href') != -1) ) && (x[i] != x['return_link_url']) )
					writeError(x[i],'We cannot accept URLs in this field.');
			}
		}
	
	if (!W3CDOM)
		alert(errorstring);
	if (firstError)
		firstError.focus();
	if (validForm) {
		 alert('The form looks good! Click OK to send it.');
		 return true;  
		}
	return false;
}


function writeError(obj,message) {
	validForm = false;
	if (obj.hasError) return;
	if (W3CDOM) {
		obj.className += ' error';
		obj.onchange = removeError;
		var sp = document.createElement('span');
		sp.className = 'error';
		sp.appendChild(document.createTextNode(message));
		obj.parentNode.appendChild(sp);
		obj.hasError = sp;
	}
	else {
		errorstring += obj.name + ': ' + message + '\n';
		obj.hasError = true;
	}
	if (!firstError)
		firstError = obj;
}

function removeError()
{
	this.className = this.className.substring(0,this.className.lastIndexOf(' '));
	this.parentNode.removeChild(this.hasError);
	this.hasError = null;
	this.onchange = null;
}


function closeAlert() {
	hideHint('eAlert'); 
	if (firstError) 
		{firstError.focus();} 
	else document.herp_observe.email.focus();	
	}

function submitNoEmail() {
	document.herp_observe.email.value='noemail@'; 
	if (firstError) 
		{firstError.focus();} 
	else return true; 
	hideHint('eAlert');
	}