/*
 ***********************************************************************
* Copyright by State of Michigan (Unpublished) - All rights reserved.
*********************************************************************
* Name:  insurance_agent_criteria.js
*
* Description:  Client script for the Insurance Agent Criteria web page.
*
* Special Logic:  None
*
***********************************************************************
* Modification Log:
*
* Programmer      Date      Request No.      Version   Changes
* Doug Cook       03/20/02  N/A              1.0       Original Code                      
*
***********************************************************************
*/

// Function:  leftTrim
// Purpose: Remove the leading spaces from the search criteria fields.
function leftTrim(strLeftTrimString)
{
	return strLeftTrimString.replace(/^\s*/gi, "");
}

// Function:  rightTrim
// Purpose: Remove the trailing spaces from the search criteria fields.
function rightTrim(strRightTrimString)
{
	return strRightTrimString.replace(/\s+$/gi, "");
}

// Function: validateForm
// Purpose: Submit event for the form.  Verify the user completed the form
// correctly.
function validateForm()
{
	if (verifySelection())
	{
	  return true;
	}   
	else
	{
	  return false;
	}
}
    
// Function: verifySelection
// Purpose: Trim the leading and trailing spaces from the Agent Last Name, Agent First Name,  
// City, and Zip Code fields.  
function verifySelection()
{
	// Trim the leading and trailing spaces from the Agent Last Name, Agent First Name, 
	// Agent Middle Name, SSN, City, and Zip Code fields.  

 	strToTrim = document.frmIndustryCriteria.txtAgentLastName.value;
	strToTrim = rightTrim(strToTrim);
	strToTrim = leftTrim(strToTrim);
	document.frmIndustryCriteria.txtAgentLastName.value = strToTrim;
	
 	strToTrim = document.frmIndustryCriteria.txtAgentFirstName.value;
	strToTrim = rightTrim(strToTrim);
	strToTrim = leftTrim(strToTrim);
	document.frmIndustryCriteria.txtAgentFirstName.value = strToTrim;
	
	strToTrim = document.frmIndustryCriteria.txtAgentMiddleName.value;
	strToTrim = rightTrim(strToTrim);
	strToTrim = leftTrim(strToTrim);
	document.frmIndustryCriteria.txtAgentMiddleName.value = strToTrim;
	
	strToTrim = document.frmIndustryCriteria.txtSSN.value;
	strToTrim = rightTrim(strToTrim);
	strToTrim = leftTrim(strToTrim);
	document.frmIndustryCriteria.txtSSN.value = strToTrim;
	
	strToTrim = document.frmIndustryCriteria.txtSystemIDNumber.value;
	strToTrim = rightTrim(strToTrim);
	strToTrim = leftTrim(strToTrim);
	document.frmIndustryCriteria.txtSystemIDNumber.value = strToTrim;
	
 	strToTrim = document.frmIndustryCriteria.txtCity.value;
	strToTrim = rightTrim(strToTrim);
	strToTrim = leftTrim(strToTrim);
	document.frmIndustryCriteria.txtCity.value = strToTrim;
	
 	strToTrim = document.frmIndustryCriteria.txtZipCode.value;
	strToTrim = rightTrim(strToTrim);
	strToTrim = leftTrim(strToTrim);
	document.frmIndustryCriteria.txtZipCode.value = strToTrim;
	
	// The user is required to enter search criteria for at least one of the items below.
	
	if (document.frmIndustryCriteria.txtAgentLastName.value == "" &&
	    document.frmIndustryCriteria.txtAgentFirstName.value == "" &&
		document.frmIndustryCriteria.txtAgentMiddleName.value == "" &&
		document.frmIndustryCriteria.txtSSN.value == "" &&
		document.frmIndustryCriteria.txtSystemIDNumber.value == "" &&
		document.frmIndustryCriteria.txtCity.value == "" &&
		document.frmIndustryCriteria.lstState.value == "" &&
		document.frmIndustryCriteria.txtZipCode.value == "")
	{
		alert("Criteria must be entered for at least one of the listed items.  Please try again.");
		return false;
	}
	else
	{
	    // Critieria for the last name, city, and zip code must be at least two characters
	    // if criteria is entered.

		if (document.frmIndustryCriteria.txtAgentLastName.value != "")
		{
			if (document.frmIndustryCriteria.txtAgentLastName.value.length < 2)
			{
				alert("If you wish to search by Last Name, you must enter at least 2 characters.  Please try again.");
				return false;
			}		
		}
		
		if (document.frmIndustryCriteria.txtCity.value != "")
		{
			if (document.frmIndustryCriteria.txtCity.value.length < 2)
			{
				alert("If you wish to search by City, you must enter at least 2 characters.  Please try again.");
				return false;
			}		
		}
		
		if (document.frmIndustryCriteria.txtZipCode.value != "")
		{
			if (document.frmIndustryCriteria.txtZipCode.value.length < 4)
			{
				alert("If you wish to search by Zip Code, you must enter at least 4 characters.  Please try again.");
				return false;
			}		
		}
		
		// If the criteria for the first name is entered, there must be criteria entered for the last name.
		
		if (document.frmIndustryCriteria.txtAgentFirstName.value != "" &&
		    document.frmIndustryCriteria.txtAgentLastName.value == "")
		{
			alert("If you wish to search by Agent First Name, you must also have some criteria entered for Agent Last Name.  Please try again.");
				return false;		
		}		

		// If the criteria for the middle name is entered, there must be criteria entered for the last name.
		
		if (document.frmIndustryCriteria.txtAgentMiddleName.value != "" &&
		    document.frmIndustryCriteria.txtAgentLastName.value == "")
		{
			alert("If you wish to search by Agent Middle Name, you must also have some criteria entered for Agent Last Name.  Please try again.");
				return false;		
		}
		
		// If the criteria for the SSN is entered, the number must be formatted properly (all numerics).
		
		if (document.frmIndustryCriteria.txtSSN.value != "" )
		{
			strCompare = document.frmIndustryCriteria.txtSSN.value;
			
			if (strCompare.match(/\d\d\d\d/) == null)
			{
				alert("The SSN must contain 4 digits.  Please try again.");
				return false;
			}		
		}
		
		// If the criteria for the system ID number is entered, the number must be formatted properly (all numerics).
		
		if (document.frmIndustryCriteria.txtSystemIDNumber.value != "" )
		{
			strCompare = document.frmIndustryCriteria.txtSystemIDNumber.value;
			
			if (strCompare.match(/\d\d\d\d\d\d\d/) == null)
			{
				alert("The System ID Number must contain 7 digits.  Please try again.");
				return false;
			}		
		}
				
		// If the criteria for the state is entered, there must be additional criteria entered.
		
		if (document.frmIndustryCriteria.lstState.value != "" &&
		    document.frmIndustryCriteria.txtAgentLastName.value == "" &&
	    	document.frmIndustryCriteria.txtAgentFirstName.value == "" &&
			document.frmIndustryCriteria.txtSSN.value == "" &&
			document.frmIndustryCriteria.txtCity.value == "" &&
			document.frmIndustryCriteria.txtZipCode.value == "")
		{
			alert("If you select a State, you must enter criteria in at least one other column.  Please try again.");
				return false;		
		}		
	}		
	
    // If the Agent Last Name, Agent First Name, Middle Name, or City have a single quote, modify the single quote
	// to two single quotes so it will be compatible with Oracle.
    
    strTemp = document.frmIndustryCriteria.txtAgentLastName.value.replace(/\'/g, "''");
	document.frmIndustryCriteria.txtAgentLastNameSQL.value = strTemp;
 
    strTemp = document.frmIndustryCriteria.txtAgentFirstName.value.replace(/\'/g, "''");
	document.frmIndustryCriteria.txtAgentFirstNameSQL.value = strTemp;
	
	strTemp = document.frmIndustryCriteria.txtAgentMiddleName.value.replace(/\'/g, "''");
	document.frmIndustryCriteria.txtAgentMiddleNameSQL.value = strTemp;
 
    strTemp = document.frmIndustryCriteria.txtCity.value.replace(/\'/g, "''");
	document.frmIndustryCriteria.txtCitySQL.value = strTemp;
 	
 	document.frmIndustryCriteria.action = "/fis/ind_srch/ins_agnt/insurance_agent_list.asp";
	return true; 
}
	



	



