//-->

/*
simplevalidation.js
Kristine Loomis
4/08/03
generalized validations for form fields
THIS FILE MUST BE IN SAME FOLDER (DIRECTORY) AS WEB PAGE WITH FORM
*/

//-----------------------------------------------------
function validateListbox(list, message) // validates listboxes
//-----------------------------------------------------
{ 
    if (list.selectedIndex == 0)	
    { 
      alert(message);	
      list.focus();	
      return false;
    }	
		
    else 
      return true;
  }
//-----------------------------------------------------
function validateOptionButton(button, message) // validates option buttons
//-----------------------------------------------------
{
	var optionSelected = -1;
	for (var i=0; i<button.length; i++)
	{
		if (button[i].checked)
			optionSelected = i;
	}
	
	if (optionSelected == -1)
	{
		alert (message);
		button[0].focus();
		return false;
	}
	else
		return true;
}
//-----------------------------------------------------
function validateCheckbox(checkbox, message) // validates checkboxes
//-----------------------------------------------------
{
	var count = 0;
	for (var i=0; i<checkbox.length; i++)
	{
		if (checkbox[i].checked)
			count++;
	}

	if (count == 0)
	{
		alert (message);
		checkbox[0].focus();
		return false;
	}
	else
		return true;
}
//----------------------------------------------------------
function validateTextbox(textbox, message) // validates textboxes
//----------------------------------------------------------
  {
    if (textbox.value == "")
    {
      alert(message);
      textbox.select();
      textbox.focus();
      return false;
    }	
		
    else
      return true;
  }
//----------------------------------------------------------
//----------------------------------------------------------
function validateEmail(textbox, message)  // is this a valid email address?
//----------------------------------------------------------
{
	var email = textbox.value;  // get email address from form
	var validEmail = 0;  // assume email address is valid

	if (email == "")	  // error if email address is blank
		validEmail = -1;

	var firstAt = email.indexOf("@")  // there must be one
	var lastAt = email.lastIndexOf("@")  // and only one "@"
	var space = email.indexOf(" ")  // no spaces
	var dot = email.lastIndexOf(".")  // must be a dot
	 
	
	if ((firstAt == -1) || (firstAt != lastAt)) 
		validEmail = -1;
		
	if ((firstAt == 0) || // @ cannot be first
		(dot == -1) ||	//must be a dot
		(dot < firstAt) ||	// "." cannot be before @
		(email.length -1 == dot) ||	//"." cannot be at the end
		(space != -1))	//no spaces
		
		validEmail = -1;
	if (validEmail == -1)  // if there is an error
	{
		displayError(textbox, message);  // display the error message passed in
		return false;  // return an error condition
	}
	else	
		return true;  // return a success condition
}
//-->
//-------------------------------------------------------------->

function displayError(textbox, message)

{
		alert(message);				// display the error message passed in
		textbox.select();			// select this textbox
		textbox.focus();			// and set the focus there
		return;
}
//----------------------------------------------------------

