//function for checking integer values...
function check_int(checkStr)
{
	var checkOK = "0123456789";
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
		{
			ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
					break;
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
		}
	return (allValid);
}

//*****************************************************

//Login form validation...

function validateLogin(theForm)
{

	//test username field
	if (theForm.username.value == "")
	{
		alert("Please enter a username");
		theForm.username.focus();
		return(false);	
	}

	//test password field
	if (theForm.password.value == "")
	{
		alert("Please enter a password");
		theForm.password.focus();
		return(false);	
	}

	return(true);
}

//*****************************************************

//Registration Form Validation

function validateRegistration(theForm)
{

	//for adult registration only...
	if (theForm.reg_type.value == "adult")
	{
	
		//test firstname field
		if (theForm.firstname.value == "")
		{
			alert("Please enter your first name.");
			theForm.firstname.focus();
			return(false);	
		}
	
		//test lastname field
		if (theForm.lastname.value == "")
		{
			alert("Please enter your last name.");
			theForm.lastname.focus();
			return(false);	
		}

	}//end test for adult registration


	//test email field
	if (theForm.email.value == "")
	{
		alert("Please enter your email address.");
		theForm.email.focus();
		return(false);	
	}

	//test for valid email address
	var str = theForm.email.value; // email string
	if (window.RegExp) //for new browsers
	{ 
		var reg = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
		if (!reg.test(str)) { // if syntax is not valid
		  alert("Please enter a valid email address."); 
		  theForm.email.focus();
		  return false;
		}
	} else { //for old browsers
		if(str.indexOf("@") < 1){ // if syntax is not valid
		  alert("Please enter a valid email address."); 
		  theForm.email.focus();
		  return false;
		}
	}

	//test username field
	if (theForm.username.value == "")
	{
		alert("Please enter a username.");
		theForm.username.focus();
		return(false);	
	}

	//make sure username has at least 6 characters
	if (theForm.username.value.length < 6)
	{
		alert("Please enter a username with at least 6 characters.");
		theForm.username.focus();
		return(false);	
	}

	//test password field
	if (theForm.password.value == "")
	{
		alert("Please enter a password.");
		theForm.password.focus();
		return(false);	
	}

	//test password2 field
	if (theForm.password2.value == "")
	{
		alert("Please enter your password again.");
		theForm.password2.focus();
		return(false);	
	}

	//test both passwords to make sure they match
	if (theForm.password2.value != theForm.password.value)
	{
		alert("Your passwords do not match.  Please try again.");
		theForm.password2.focus();
		return(false);	
	}


	return(true);
}

//*********************************************************************

function validateGetPassword(theForm)
{

	//test username field
	if (theForm.username.value == "")
	{
		alert("Please enter a username.");
		theForm.username.focus();
		return(false);	
	}

return(true);

}

//*********************************************************************


function validateKeyword(theForm)
{

	//test keyword field
	if (theForm.keyword.value == "")
	{
		alert("Please enter a keyword or phrase.");
		theForm.keyword.focus();
		return(false);	
	}

return(true);

}

//*********************************************************************


function validateSearch(theForm)
{

	//make sure at least one search criteria is completed
	if (theForm.keyword.value == "" && theForm.county.value == "" && theForm.year1.value == "" && theForm.year2.value == "" && theForm.doc_type_id.selectedIndex == 0)
	{
		alert("Please complete at least one search criteria.");
		return(false);	
	}
	
	//The Year fields must be either both completed or both empty
	if ((theForm.year1.value == "") && (theForm.year2.value != "")) 
	{
		alert("Please enter both values for the year range.  If you want only one year, enter the same value for each field.");
		theForm.year1.focus();
		return(false);		
	}
	if ((theForm.year2.value == "") && (theForm.year1.value != ""))
	{
		alert("Please enter both values for the year range.  If you want only one year, enter the same value for each field.");
		theForm.year2.focus();
		return(false);		
	}	

	//valid dates if fields are filled
	if (theForm.year1.value != "")
	{
		if (!(check_int(theForm.year1.value)))
		{
			alert("Please enter only numeric values for the year");
			theForm.year1.focus();
			return(false);		
		}	
	}
	
	if (theForm.year2.value != "")
	{
		if (!(check_int(theForm.year2.value)))
		{
			alert("Please enter only numeric values for the year");
			theForm.year2.focus();
			return(false);		
		}	
	}

return(true);

}

//*********************************************************************


