// Search_Field_Validations.js
// Created by: Ewa Poniatowska
// Date of creation: March 14, 2007
// Modified on: April 5, 2007


// Purpose: function frmSubmit(frm) validates the user's input
// Callers: called on the onSubmit event of the form
// Parameters: frm - reference to the form being processed
function frmSubmit(frm)
{
  	// If the focus is never on one of the fields 
  	// but somewhere else on the form and enter is pressed
  	// This may not occur, but if it does the form will not be submitted
  
  	if(frm.zipFlag.value == "false" && frm.cityFlag.value == "false" && frm.nameFlag.value == "false")
  	{
		return false; // the form is not submitted
  	}
  						 
  	// Else the focus was on one of the fields and enter was pressed 
  	// or one of the go buttons was clicked on
  
  	else{
	
		// Check if the search is by zip code
		if(frm.zipFlag.value == "true")
		{
			// Validate zip code entered
			if(validateZip(frm))
			{
				frm.searchOn.value = "zipcode";
				return true; // zip code is valid, submit the form
			}
			else
				return false; // zip code is not valid, don't submit the form
		}
	
		// Check if the search is by city
		if(frm.cityFlag.value == "true")
		{
			// Validate city entered
			if(validateCity(frm))
			{
				frm.searchOn.value = "citystate";
				return true; // city is valid, submit the form
			}
			else
				return false; // city is not valid, don't submit the form
		}
	
		// Check if the search is by last name of an agent
		if(frm.nameFlag.value == "true")
		{
			// Validate last name entered
			if(validateLastName(frm))
			{
				frm.searchOn.value = "lnamestate";
				return true; // last name is valid, submit the form
			}
			else
				return false; // last name is not valid, don't submit the form
		}
  	}
}


// Purpose: function zipAction(frm) sets the zipFlag to true
// Callers: called on the onFocus event of the zipCode field and when zipcode go button is pressed
// Parameters: frm - reference to the form being processed
function zipAction(frm)
{
	frm.zipFlag.value = "true"; // activate 
	frm.nameFlag.value = "false"; // deactivate
    frm.cityFlag.value = "false"; // deactivate
}


// Purpose: function cityAction(frm) sets the city_flag to true
// Callers: called on the onFocus event of the city field and when city go button is pressed
// Parameters: frm - reference to the form being processed
function cityAction(frm)
{
    frm.zipFlag.value = "false"; //deactivate
    frm.nameFlag.value = "false"; // activate
    frm.cityFlag.value = "true"; // activate
}


// Purpose: function nameAction(frm) sets the name_flag to true
// Callers: called on the onFocus event of the lastName field and when lastname go button is pressed
// Parameters: frm - reference to the form being processed
function nameAction(frm)
{		
    frm.zipFlag.value = "false"; // deactivate
    frm.nameFlag.value = "true"; // activate
    frm.cityFlag.value = "false"; // deactivate
}


// Purpose: function validateZip(frm) validates if the zip code entered is a 5-digit number
// Callers: function frmSubmit(frm)
// Parameters: frm - reference to the form being processed
function validateZip(frm)
{	
	// Regular expression regexp defining a 5-digit number 
	var regexp1 = /^\d{5}$/;
	
	// Check if the zip code field is not empty
	if(frm.zipCode.value.length == 0)
	{	
		alert("Please enter a valid zip code and try again.");
		frm.zipCode.focus(); // bring the focus back to zip code field
		return false; // zip code is not valid
	}
	// Check if zip code entered does not match the regular expression regexp
	else if(regexp1.test(frm.zipCode.value) == false)
	{
		return validationFailed(frm.zipCode); // displays error message, highlights the field value and puts the focus back in the field
	}
	
	else
		return true; // Zip code entered is valid
}


// Purpose: function validateCity(frm) validates if the city entered contains  
// only alphabetical characters and few allowable symbols 
// Callers: function frmSubmit(frm)
// Parameters: frm - reference to the form being processed
function validateCity(frm)
{
	// Regular expression regexp defining city names
	// The only allowable characters are letters, period, dash and space
	var regexp2 = /^[a-zA-Z\s\.\-]+$/; 

	// Check if the city field is not empty
	if(frm.city.value.length == 0)
	{
	    alert("Please enter a city and try again.");
	    frm.city.focus(); // bring the focus back to the city field
	    return false; // city is not valid
	}
	// Check if city field does not match the regular expression regexp
	else if(regexp2.test(frm.city.value) == false) 
	{
		return validationFailed(frm.city); // displays error message, highlights the field value and puts the focus back in the field
	}
	
	else
		return true; // City entered is valid
}


// Purpose: function validateLastName(frm) validates if the name entered contains only 
// alphabetical characters and few allowable symbols 
// Callers: function frmSubmit(frm)
// Parameters: frm - reference to the form being processed
function validateLastName(frm)
{
	// Regular expression regexp defining last names
	// The only allowable characters are letters, period, dash and space
	// I don't know how to represent an apostrophe
	var regexp3 = /^[a-zA-Z\s\.'\-]+$/; 

	// Check if the name field is not empty
	if(frm.lastName.value.length == 0)
	{
	    alert("Please enter an agent name and try again.");
		frm.lastName.focus(); // bring the focus back to the name field
		return false; // name is not valid      
	}
	// Check if name field does not match the regular expression regexp
	else if(regexp3.test(frm.lastName.value) == false) 
	{
		return validationFailed(frm.lastName); // displays error message, highlights the field value and puts the focus back in the field
	}
	
	else
		return true; // Last name entered is valid
}


// Purpose: function validationFailed(field) displays error message, highlights the field value and puts the focus back in the field
// Callers: validateZip(frm) function, validateCity(frm) function and validateLastName(frm) function
// Parameters: field - reference to the field being validated (possible values: zipCode, city and lastName)
function validationFailed(field)
{
	var msg1 = "Please enter a 5-digit numeric value in the zip code field and try again.";
	var msg2 = "Please make sure your search contains alphabetical characters, . or - only and try again.";
	var msg3 = "Please make sure your search contains alphabetical characters, -, ' or . only and try again.";

	// Check if the field is a zip code field
	if(field == document.search_frm.zipCode)
	{
		alert(msg1);
	}
	// Check if the field is a city field
	else if(field == document.search_frm.city)
	{
		alert(msg2);
	}
	// The field is last name field
	else
	{
		alert(msg3);
	}	
	field.select(); // highlight the field
    field.focus(); // put the focus in the field
	return false; // always returns false
}
