var m_flag=0;

function ltrim(s){return String(s).replace(/^\s+/g, "")}
function rtrim(s){return String(s).replace(/\s+$/g, "")} 
function trim(s) { return ltrim(rtrim(s))}

// prevent the user from submitting invalid searches
function validateSearch()	{
	// Non-CCLI Number search types
	if (document.forms[0].search_type.selectedIndex!=5)	{
		var tmpSearchStr = trim(document.forms[0].search_words.value);
		tmpSearchStr = tmpSearchStr.replace(/\"/g, '');
		if (tmpSearchStr!="")	{
			if(tmpSearchStr.length == 1) {
				alert("Please enter a search word of more than one character.");
				document.forms[0].search_words.select();
			}
			else {
				document.forms[0].search_words.value = tmpSearchStr;
				document.forms[0].submit();
				document.forms[0].search_words.select();
			}
		}
	}
	// CCLI Number search type
	else	{
		if (isNaN(Number(document.forms[0].search_words.value))==false)	{
			//window.parent.frmBody.location.href="../../wait.cfm";
			document.forms[0].submit();
			document.forms[0].search_words.select();
		}
		else {
			alert("You have entered an invalid CCLI song number. There can be no non-didgit characters.");
			document.forms[0].search_words.select();
		}
	}
}

// steer the user away from search methods that don't apply to the search type they've chosen
function setDefaultMethod(search_type)	{
	var thisform=document.forms[0];
	
	// added 10/3/2001 GR to replace radio-button method selection
	if (search_type.selectedIndex<1)	{
		thisform.search_method.value='phrase';
	}
	else	{
		thisform.search_method.value='exact';
	}
		
	/* commented out 10/3/2001 GR to remove radio-button method selection
	// title search: enable everything
	if (search_type.selectedIndex==0)	{
		if (document.all)	{
			for (i=0; i<thisform.search_method.length; i++)	{
				thisform.search_method[i].disabled=false;
			}
		}
	}
	
	// text search: disable 'exact'
	if (search_type.selectedIndex==1)	{
		// force the selection away from 'exact'... default to 'phrase' instead
		for (i=0; i<thisform.search_method.length; i++)	{
			if (thisform.search_method[i].value=='exact')	{
				if (thisform.search_method[i].checked)	{
					// find the phrase element
					for (j=0; j<thisform.search_method.length; j++)	{
						if (thisform.search_method[j].value=='phrase')	{
							// select the phrase option
							thisform.search_method[j].checked=true;
						}
					}
				}
				// turn off the 'exact' option, enable all others
				if (document.all)	{
					thisform.search_method[i].disabled=true;
					for (k=0; k<thisform.search_method.length; k++)	{
						if (thisform.search_method[k].value!='exact')	{
							thisform.search_method[k].disabled=false;
						}
					}
				}
			}
		}
	}
	
	
	// author, catalog, theme, CCLINumber: force 'exact'
	if (search_type.selectedIndex>1)	{
		// find the exact element
		for (j=0; j<thisform.search_method.length; j++)	{
			if (thisform.search_method[j].value=='exact')	{
				// force selection to 'exact'
				thisform.search_method[j].checked=true;
				if (document.all)	{
					// turn on 'exact'
					thisform.search_method[j].disabled=false;
				}
			}
			else	{
				thisform.search_method[j].disabled=true;
			}
		}

	}
	*/
}

// refresh whatever results page is visible when options are changed and updated
function optionChange(checkbox_item)	{
	if (m_flag==0)	{
		var thisform=document.forms[0];
		var pathparts=window.parent.frmBody.location.href.split('/');
		var filename=pathparts[pathparts.length-1];
		/*if (filename.substr(0,10)=='search.cfm' ||
		    filename.substr(0,10)=='top100.cfm' ||
		    filename.substr(0,15)=='topActivity.cfm' ||
		    filename.substr(0,17)=='SongAdditions.cfm' ||
		    filename.substr(0,12)=='NewSongs.cfm' ||
			filename.substr(0,19)=='search_advanced.cfm')	{*/
			
		// if page in main frame has display_options field, refresh it
		if (window.parent.frmBody.document.forms[0].display_options) {
			var disp_options='';
			var num_checked=0;
			for (i=0; i<thisform.length; i++)	{
				if (thisform.elements[i].name=='display_options' && thisform.elements[i].type=='checkbox')	{
					if (thisform.elements[i].checked==true)	{
						disp_options+=thisform.elements[i].value+',';
						num_checked+=1;
					}
				}
			}
			if (num_checked == 0){
				alert('You must select at least one Display Option.');
				checkbox_item.checked='on';
			}
			else if (window.parent.frmBody.document.forms[0])	{
				m_flag=1;
				window.parent.frmBody.document.forms[0].display_options.value=disp_options;
				window.parent.frmBody.document.forms[0].submit();
			}
		}
	}
	else	{
		checkbox_item.checked=!(checkbox_item.checked);
	}
}