/*
* The programming and software materials herein are copyright Cyberhomes LLC (CH).
* The programming and software materials are owned, held, or licensed by CH. Personal, educational,
* non-commercial, commercial or any other use of these materials, without the written permission of the
* CH, is strictly prohibited.
*/

// 04/11/2002 - Jeff Mason - Added apostrophe and dash per issue 2852
// 10/24/2006 - Peter Liew - Added priod per HSET #17148
function check(text)
{
 var valid="-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\'. &,()"
 len=text.length;
 for(i=0;i<len;i++)
	 {
	  if (valid.indexOf(text.charAt(i)) < 0) {return true;}
	 }
	return false;
 }

 
function submitAgentSearchByName()
{
	var firstname = document.AgentSearchByName.elements['AgentSearchCriteria/FirstName'].value;
	var lastname = document.AgentSearchByName.elements['AgentSearchCriteria/LastName'].value;
	firstname = TrimString(firstname);
	
	if (document.AgentSearchByName.elements['AgentSearchCriteria/SearchType'].length > 1)
        var contains_searchtype = document.AgentSearchByName.elements['AgentSearchCriteria/SearchType'][1].checked
    else
        var contains_searchtype = document.AgentSearchByName.elements['AgentSearchCriteria/SearchType'].value;
	
	if (firstname == '' && lastname == '')
	{
		alert('Please specify at least one parameter to perform your search.');
		document.AgentSearchByName.elements['AgentSearchCriteria/FirstName'].focus();
		return false;
	}	
	else if (check(firstname))
	{	
		document.AgentSearchByName.elements['AgentSearchCriteria/FirstName'].focus();
		return false;
	}
	else if (check(lastname))
	{
		alert('\t Invalid characters found in Last Name.\n Please enter only letters, numbers, hyphens, apostrophes and spaces.');
		document.AgentSearchByName.elements['AgentSearchCriteria/LastName'].focus();
		return false;
	}
	// Adding condition of the length of the strings if the "Contains" is selected 
	else if (contains_searchtype = 'true')
	{
	    return checkAgentSearchByNameByContains(firstname, lastname);
	}
	else
	{
		return true;
	}
}

function checkAgentSearchByNameByContains(firstname, lastname)
{
    if (firstname == '' && TrimString(lastname).length < 2)  
    {
        alert('Please enter at least 2 character in the last name field before performing your search.');
        return false;
    }
    /*
    else if (lastname == '' && firstname.length < 3)  
    {
        alert('Please enter at least 3 character in the first name field before performing your search.');
        return false;
    } 
    */   
	else
	{
        return true;
    }
}

function submitAgentSearchByLanguage()
{
	// ##### NOTE: There may not be any Spoken Languages #####
	var spokenlanguage = -1;
	if (document.AgentSearchByLanguage.elements['AgentSearchCriteria/SpokenLanguages/SpokenLanguage/RECoSpokenLanguageID_FormMultiSelectNode']) {
		var spokenlanguage = document.AgentSearchByLanguage.elements['AgentSearchCriteria/SpokenLanguages/SpokenLanguage/RECoSpokenLanguageID_FormMultiSelectNode'].selectedIndex;
	}
	if (spokenlanguage == -1)
	{
		alert('Please specify at least one language to perform your search.');		
		return false;
	}
}		

function submitAgentSearchBySpecialty()
{
	// ##### NOTE: There may not be any Employee Titles #####
	var title = -1;
	if (document.AgentSearchBySpecialty.elements['AgentSearchCriteria/EmployeeTitles/EmployeeTitle/TitleID_FormMultiSelectNode']) {
		title = document.AgentSearchBySpecialty.elements['AgentSearchCriteria/EmployeeTitles/EmployeeTitle/TitleID_FormMultiSelectNode'].selectedIndex;
	}
	// ##### NOTE: There may not be any RECo Designations #####
	var recodesignation = -1;
	if (document.AgentSearchBySpecialty.elements['AgentSearchCriteria/RECoDesignations/RECoDesignation/RECoDesignationID_FormMultiSelectNode']) {
		recodesignation = document.AgentSearchBySpecialty.elements['AgentSearchCriteria/RECoDesignations/RECoDesignation/RECoDesignationID_FormMultiSelectNode'].selectedIndex;
	}
	// ##### NOTE: There may not be any NAR Designations #####
	var nardesignation = -1;
	if (document.AgentSearchBySpecialty.elements['AgentSearchCriteria/NARDesignations/NARDesignation/NARDesignationID_FormMultiSelectNode']) {
		var nardesignation = document.AgentSearchBySpecialty.elements['AgentSearchCriteria/NARDesignations/NARDesignation/NARDesignationID_FormMultiSelectNode'].selectedIndex;	
	}
	if(title == -1 && recodesignation == -1 && nardesignation == -1)
	{
		alert('Please specify at least one parameter to perform your search.');		
		return false;
	}
}
function State_onchange()
{
	document.forms.AgentSearchByOffice.elements.CountyID.value = "";
	document.forms.AgentSearchByOffice.elements.OfficeID.value = "";

	document.forms.AgentSearchByOffice.action="AgentSearch.asp?searchType=office";
	document.forms.AgentSearchByOffice.submit();
}
	
function County_onchange()
{
	document.forms.AgentSearchByOffice.elements.OfficeID.value = "";

	document.forms.AgentSearchByOffice.action="AgentSearch.asp?searchType=office";
	document.forms.AgentSearchByOffice.submit();
}


///////////////////////////////////////////////////////////////////////////
//   Function Name:  submitAgentSearchByOffice
//   Create Date:    10/02/2001
//   Author:         Jason Deitchler
//   Description:    This function checks if the OfficeID is valid before
//                   we proceed to the next page.
//   Revision Log: 
///////////////////////////////////////////////////////////////////////////
function submitAgentSearchByOffice()
{
	if (document.AgentSearchByOffice.OfficeID.options[document.AgentSearchByOffice.OfficeID.selectedIndex].value == '')
	{
		alert('You must select a valid Office.');
		return false;
	}
	else
	{
		return true;
	}
}

function TrimString(sInString) 
{
	sInString = sInString.replace( /^\s+/g, "" );	// strip leading
	return sInString.replace( /\s+$/g, "" );		// strip trailing
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


