function ValidateTrackForm(form)
{
  if(IsEmpty(form.idnum)) 
	{ 
    alert('Please enter a fish number.');
    form.idnum.focus();
		return false; 
	} 

	if (!IsNumeric(form.idnum.value)) 
	{ 
		alert('Please enter only numbers.');
		form.idnum.focus(); 
		return false; 
	}

	if (!InRange(form.idnum.value)) 
	{ 
		alert('Valid fish numbers are between 101 and 99999.');
		form.idnum.focus(); 
		return false; 
	}

	return true;
}

function IsWaypoint(sText) {
	var waypoint = sText.toUpperCase();
	var index = waypoint.indexOf('GC');
	if (index != 0) {
		return false;
	}
	return true;
}

function IsAN(sText)
{
	var validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	var isAN=true;
	var achar;

	for (i = 0; i < sText.length && isAN == true; i++) 
	{ 
		achar = sText.charAt(i); 
		if (validChars.indexOf(achar) == -1) 
		{
			isAN = false;
		}
	}
	return isAN;
}


function IsNumeric(sText)
{
	var validChars = "0123456789";
	var isNumber=true;
	var achar;

	for (i = 0; i < sText.length && isNumber == true; i++) 
	{ 
		achar = sText.charAt(i); 
		if (validChars.indexOf(achar) == -1) 
		{
			isNumber = false;
		}
	}
	return isNumber;
}

function IsEmpty(aTextField) {
	if ((aTextField.value.length==0) ||
			(aTextField.value==null)) {
		return true;
	}
	else { return false; }
}

function InRange(sText) {
	if (sText.length < 3 || sText.length > 5 || sText == 100) {
		return false;
	}
	else {
		return true;
	}
}

function ValidateLogForm(form) {
	errors = "";
	focusSet = false;
	if (!IsSelected(form.LogentryType)) {
		errors += "Please select a log type.\n";
		setFocus(form.LogentryType, focusSet);
		focusSet = true;
	}
	else {
		logtype = form.LogentryType.options[form.LogentryType.selectedIndex].value;
		if(IsEmpty(form.LogentryUsername)) 
		{
			errors += "Please enter your geocaching name.\n";
			setFocus(form.LogentryUsername, focusSet);
			focusSet = true;
		}
		if (logtype == "2" || logtype=="3") {
			fwpt = document.getElementById('LogentryCacheId');
			if (fwpt == null) {
				errors += "Please enter the cache waypoint.\n";
				focusSet = true;
			}
			else {
				if(IsEmpty(form.LogentryCacheId)) 
				{ 
					errors += "Please enter the cache waypoint.\n";
					focusSet = true;
				}
				if(!IsWaypoint(form.LogentryCacheId.value))
				{
					errors += "The cache waypoint must begin with \'GC\'.\n";
					focusSet = true;
				}
				if(form.LogentryCacheId.value.length < 4 || form.LogentryCacheId.value.length > 7) 
				{ 
					errors += "The cache waypoint should be greater than or equal to 4 and less than or equal to 7 characters in length.\n";
					focusSet = true;
				}				
			}
		}
		else {
			if (logtype == "5") {
				if(IsEmpty(form.LogentryComment)) 
				{ 
					errors += "Please enter a comment.\n";
					focusSet = true;
				}
			}
		}
	}
	if (errors) {
		alert(errors);
		return false;
	}
	else {
		return true;
	}
}

function LogTypeSelected(logtype) {
	comment_required_style = 'none';
	if (logtype == '2' || logtype == '3') {
		document.getElementById('cache_id').style.display = "";
	}
	else {
		document.getElementById('cache_id').style.display = "none";
		if (logtype == '5') {
			comment_required_style = 'inline';
		}
	}
	document.getElementById('comment_required').style.display = comment_required_style;
}

function setFocus(field, focusAlreadySet) {
	if (!focusAlreadySet) {
		field.focus();
	}
}

function IsSelected(field) {
	var i = field.selectedIndex;
	if (i <= 0) return false;
	return true;
}

function ValidateStatsForm(form)
{
	if(form.type.value=="geocacher" && !IsSelected(form.username)) 
	{
		alert('Please select a geocacher.');
		form.user.focus(); 
		return false; 
	} 
	return true;
}

function toggleGeocacherList(field) {
	if (field.value == "geocacher") {
		document.getElementById("username").style.display = "inline";
	}
	else {
		document.getElementById("username").style.display = "none";
	}
}
