/**********************************************\
* Function:	swapsearchcriteria()
* Author: 	Greg Miley
* Created:	
* Description: 
*		Display selected search criteria and
*		hide unused criteria
*
* Modified:
*	Who		When		Why
*	ep		29 Mar 04	Changed names
*						Added queryaddl and
*						queryx10sv
*	ep		28 Mar 05	Made dynamic
*	ep		18 Aug 05	Change searchon combo
*						to match the Show Name
\**********************************************/
function swapsearchcriteria(lcShowName) 
{
	var laDivObj, laCritDivObj, laChildObj, laInput, laSelect;
	var lnPos;
	var i = 0;
	var j = 0;
	var lcObject, lcQuery;
	var lcDivName, lcObjName;
	var lcContainer, lcChild;
	var laObj = new Array(2);
	var lnObj;
	
	laObj[0] = "input";
	laObj[1] = "select";

	lcContainer = document.getElementById('divContainer');
	laDivObj = lcContainer.getElementsByTagName('DIV');
	for(i=0; i<laDivObj.length; i++) 
	{
		// Each one of the Criteria Blocks is a Div
		lcDivName = laDivObj[i].id;

		// If the div happens to be the one that is to be shown
		if(lcDivName == lcShowName) 
		{	
			// Show the Div
			Browser.ObjRef("searchon").value = lcShowName
			Browser.ObjRef(lcDivName).Show();
		}
		else
		{
			// Hide the Div
			Browser.ObjRef(lcDivName).Hide();
		}
		

		lcChild = document.getElementById(lcDivName);
	
		for (lnObj = 0; lnObj<2; lnObj++)
		{			
			laChildObj = lcChild.getElementsByTagName(laObj[lnObj]);
	
		
			for (j=0; j<laChildObj.length; j++)
			{
				lcObjName = laChildObj[j].id;
	
				re = /\Bquery\B/;
				lnPos = lcObjName.search(re);
				lcQuery = lcObjName.substr(lnPos);
						
				if (lnPos >= 0)
				{
					if(lcDivName == lcShowName) 
					{				
						laChildObj[j].name = lcQuery;
	
						if(laChildObj[j].required !== "0") 
						{ 
							laChildObj[j].required = "1";
						}
					}
					else
					{														
						if(laChildObj[j].required !== "0") 
						{ 
							laChildObj[j].required = "";
						}
						laChildObj[j].name = 'no' + lcQuery;
								
						// Only blank out if it is a text field
						if (laChildObj[j].type == 'text')
						{
							laChildObj[j].id.value = "";
						}
							
					}
				}
			}
		}
	}
}

/**********************************************\
* Function:	flatsearchcriteria()
* Author: 	Evie Platt
* Created:	9/15/06
* Description: 
*		Determine which of the criteria should 
*		be used for the search in the flat
*		search layout
*
* Modified:
*	Who		When		Why
*	EP		27 Oct 06	Problem with name vs 
*						id on searchon
\**********************************************/
function flatsearchcriteria() 
{
	var laDivObj, laCritDivObj, laChildObj, laInput, laSelect;
	var lnPos;
	var i = 0;
	var j = 0;
	var lcObject, lcQuery;
	var lcDivName, lcObjName;
	var lcContainer, lcChild;
	var laObj = new Array(2);
	var lnObj;
	var llFound = false;
	var lnFoundCnt = 0;
	var lnOldFound = 0;
	var lcFound = "";

	laObj[0] = "input";
	laObj[1] = "select";

	var lcSearchOn = document.getElementById('searchon');
	lcContainer = document.getElementById('divContainer');
	laDivObj = lcContainer.getElementsByTagName('DIV');
	for(i=0; i<laDivObj.length; i++) 
	{
		// Each one of the Criteria Blocks is a Div
		if (llFound == true) break;			
		lcDivName = laDivObj[i].id;
		lcChild = document.getElementById(lcDivName);
		for (lnObj = 0; lnObj<2; lnObj++)
		{			
			laChildObj = lcChild.getElementsByTagName(laObj[lnObj]);
			if (lcFound.length > 0 && laChildObj.length > 0)
			{
				lnOldFound = lnFoundCnt;
				lcFound += "or\n"
			}

			for (j=0; j<laChildObj.length; j++)
			{
				lcObjName = laChildObj[j].id;

				re = /\Bquery\B/;
				lnPos = lcObjName.search(re);
				lcQuery = lcObjName.substr(lnPos);
						
				if (lnPos >= 0)
				{
					// Ignore x10sv check boxes
					re = /x10sv\b/;
					lnPos = lcQuery.search(re);

					if (lnPos == -1) 
					{						
						// If it's filled in
						if(laChildObj[j].value.length > 0) 
						{											
							lcSearchOn.value = laDivObj[i].id;

							laChildObj[j].name = lcQuery;

							if(laChildObj[j].required !== "0") 
							{ 
								laChildObj[j].required = "1";
							}
							
							if(laChildObj[j].required == "1") 
							{
								lnFoundCnt++;
								
							}
							if (lnFoundCnt == laChildObj.length)
							{
								llFound = true;
								break;
							}
							
						}
						else
						{								
							if(laChildObj[j].required !== "0") 
							{ 
								laChildObj[j].required = "";
							}
							laChildObj[j].name = 'no' + lcQuery;
										
							// Only blank out if it is a text field
							if (laChildObj[j].type == 'text')
							{
								laChildObj[j].value = "";
							}
						
							if(laChildObj[j].required == "") 
							{
								lcFound += laChildObj[j].id + "\n";
							}


						}
					}
				}
			}
	
		}
	}

	if (llFound == true)
	{
		lcFound = "";
	}
	return lcFound;
}

/**********************************************\
* Function:	ClickPageButtons()
* Author: 	Evie Platt
* Created:	17 Mar 2004
* Description: 
*		Determine if Next or Previous was selected
* Modified:
*	Who		When		Why
*	ep		11 Aug 05	Set per page

\**********************************************/
function ClickPageButtons(tcAction)
{

	var loNewPerPage = document.getElementById('PerPage');
	var loPerPage = document.getElementById('per_page');
	if (loPerPage.value != loNewPerPage.value)
	{
		tcAction = "1";
	}
	loPerPage.value = loNewPerPage.value
	var loPageAction = document.getElementById('PageAction');
	loPageAction.value = tcAction;
	var loPageButton = document.getElementById('PageButton');
	loPageButton.submit();
}



function makeNumeric(tValue) {
	return tValue.replace(/[^\d]/, "");
}

/**********************************************\
* Function:	QuerySetAll()
* Author: 	Evie Platt
* Created:	29 Mar 2004
* Description: 
*		Set all items on the form with the same
*		name or no- to the selected value
\**********************************************/
function QuerySetAll (tcName, tcValue)
{
	var lnObjCount;
	var laElements;
	var loSearch = document.getElementById('search');

	for (lnObjCount=0; lnObjCount <= loSearch.elements.length - 1; lnObjCount++)
	{

		if (loSearch.elements[lnObjCount].name == tcName || loSearch.elements[lnObjCount].name == 'no' + tcName)
		{
		
			switch (loSearch.elements[lnObjCount].type)
			{
				case 'checkbox':
				case 'radio':
					loSearch.elements[lnObjCount].checked = tcValue;
					break;		
				default:	
					loSearch.elements[lnObjCount].value = tcValue;
					break;
			}
		}
	}
}

/**********************************************\
* Function:	ClickNewSearch()
* Author: 	Evie Platt
* Created:	19 Jan 05
* Description: 
*		Redirect for New Search Button
\**********************************************/
function ClickNewSearch(tcLink)
{
	location.href = tcLink;
}

/**********************************************\
* Function:	ClickStartSearch()
* Author: 	Evie Platt
* Created:	25 Sep 05
* Description: 
*		Click for Start Search Button
\**********************************************/
function ClickStartSearch(tcFormName, tcLayoutPage)
{
	var llSubmit = true;
	var lcSubmit = '';

	if (tcLayoutPage == 'FLAT')
	{
		lcSubmit = flatsearchcriteria();
		if (lcSubmit.length > 0)//(llSubmit == false)
		{
			lcMsg = "_____________________________________________________________\n\n";
			lcMsg += "The form was not submitted because the following fields contained \n";
			lcMsg += "invalid data. Please check each required field and try again.\n";
			lcMsg += "_____________________________________________________________\n\n";
			lcMsg += lcSubmit;
			alert(lcMsg);
			llSubmit = false;
		}
	}
	else
	{
		llSubmit = FieldCheck(tcFormName);
	}

	if (llSubmit == true)
	{

		var loSearchPage = document.getElementById('search');
		loSearchPage.submit();
	}
	return llSubmit;
}