function lfnReset() {

	vobForm = document.forms['fobSearch'];

	//clear document tags checkboxes (this subroutine shared with document detail page)
	afnClearDocTags(vobForm,'fobSearchTermTags');

	//clear document file type checkboxes
	vinNumFileTypes = eval(vobForm['fobSearchResultFileTypeRowCount'].value);
	for (vinFileTypeIndex = 0; vinFileTypeIndex < vinNumFileTypes; vinFileTypeIndex++) {
		vobForm['fobSearchResultFileType[' + vinFileTypeIndex + ']'].checked = false;
	}

	//set search result type checkboxes to all true
	vinNumResultTypes = eval(vobForm['fobSearchResultTypeRowCount'].value);
	for (vinResultTypeIndex = 0; vinResultTypeIndex < vinNumResultTypes; vinResultTypeIndex++) {
		vobForm['fobSearchResultType[' + vinResultTypeIndex + ']'].checked = true;
	}

	//reset search term
	vobForm['fobSearchTermString'].value = '';

	//set the dirty flag (so that this counts as a change, in case the user now tries a pagination command)
	gfnSetDirty(vobForm['fobSearchTermString'],'Dirty');
	
}

function lfnSubmitWithNewParams(pynValidate, ptxURLParamsOther, ptxAnchor, ptxURLParamsPagination) {

	//NOTE: the one time this gets sidestepped is if 'enter' is hit to submit the form, in which case lfnOnSubmit is called instead

	vobForm = document.forms['fobSearch'];

	if (pynValidate) {
		if (!lfnValidate(vobForm)) {
			return;
		}
	}

	lfnAdjustURL(vobForm, ptxURLParamsOther, ptxAnchor, ptxURLParamsPagination);
	
	vobForm.submit();
}

function lfnOnSubmit(pobForm, ptxURLParamsOther) {
	//this is only needed if the form is submitted via hitting 'enter'

	if (!lfnValidate(pobForm)) {
		return false;
	}

	lfnAdjustURL(pobForm, ptxURLParamsOther, '', '');
	
	return true;
}

function lfnAdjustURL(pobForm, ptxURLParamsOther, ptxAnchor, ptxURLParamsPagination) {

	if (ptxURLParamsPagination != '') {
		if (eval(pobForm['fobDirty'].value)) {
			//if search parameters have changed, following any pagination command makes no sense, as the 
			//result set size may be smaller.  just go to page 1 instead... by ignoring any other pagination sent.
			ptxURLParamsPagination = '';
		}
	}

	vtxURL = pobForm.action;

	//for parameters pynHelp and pynAdvanced, use the value passed in ptxURLParamsOther, but if there isn't one, look to the
	//corresponding hidden control (representing the current state) instead.  Any other parameters are just appended.

	vynSearchModePassed = false;

	vtxParamsNew = '';
	if (ptxURLParamsOther != '') {
		varParamsOther = ptxURLParamsOther.split('&');
		for (vinIndex = 0; vinIndex < varParamsOther.length; vinIndex++) {
			vtxThisParam = varParamsOther[vinIndex];
			varThisParam = vtxThisParam.split('=');
			vtxThisParamName = varThisParam[0];
			vtxThisParamValue = varThisParam[1];
			if (vtxThisParamName == 'pinSearchMode') {
				vtxParamsNew = vtxParamsNew + '&pinSearchMode=' + vtxThisParamValue;
				vynSearchModePassed = true;
			} else {
				vtxParamsNew = vtxParamsNew + '&' + vtxThisParamName + '=' + vtxThisParamValue;
			}
			
		}
	}

	if (!vynSearchModePassed) {
		vtxParamsNew = vtxParamsNew + '&pinSearchMode=' + pobForm['fobSearchMode'].value;
	}
	
	//remove first '&' sign
	vtxParamsNew = vtxParamsNew.substr(1);

	if (vtxParamsNew != '') {
		if (vtxURL.indexOf('?') >= 0) {
			 vtxURL = vtxURL + "&" + vtxParamsNew;
		} else {
			vtxURL = vtxURL + "?" + vtxParamsNew;
		}
	}

	if (ptxURLParamsPagination != '') {
		if (vtxURL.indexOf('?') >= 0) {
			 vtxURL = vtxURL + "&" + ptxURLParamsPagination;
		} else {
			vtxURL = vtxURL + "?" + ptxURLParamsPagination;
		}
	}

	if (ptxAnchor != '') {
		if (vtxURL.indexOf('?') >= 0) {
			 vtxURL = vtxURL + "&" + 'pynAnchor=1';
		} else {
			vtxURL = vtxURL + "?" + 'pynAnchor=1';
		}
		vtxURL = vtxURL + '#' + ptxAnchor;
	}

	pobForm.action = vtxURL;

}

function lfnValidate(pobForm) {

	if (eval(pobForm['fobSearchMode'].value == 1)) { //equivalent to ainSearchModeAdvanced

		//make sure that at least one result type checkbox is set	
		vinNumResultTypes = eval(pobForm['fobSearchResultTypeRowCount'].value);
		vynFound = false;
		vynNonDoc = false;
		for (vinResultTypeIndex = 0; vinResultTypeIndex < vinNumResultTypes; vinResultTypeIndex++) {
			vobControl = pobForm['fobSearchResultType[' + vinResultTypeIndex + ']'];
			if (vobControl.checked) {
				if (vobControl.value != 2) { //corresponds to ainSearchResultTypeDocument
					vynNonDoc = true;
				}
				vynFound = true;
			}
			if (vynFound && vynNonDoc) {
				break;
			}
		}
		if (!vynFound) {
			alert("At least one of the result type checkboxes (Web Page or Document) MUST be checked off!");
			pobForm['fobSearchResultType[0]'].focus();
			return false;
		}
		
		vynDocTagsSet = lfnCheckIfDocTagsSet(pobForm);

		if (!vynDocTagsSet && !gfnCheckValueIsSet(pobForm['fobSearchTermString'].value)) {
			alert("To conduct a search, you MUST specify some Keywords and/or Document Tags.");
			pobForm['fobSearchTermString'].focus();
			return false;
		}

		if (vynNonDoc && vynDocTagsSet) {
			if (!confirm("WARNING: if you have selected any Document Tags (from File Type, Department, etc.) then this search will ONLY look at Documents, and not Web Pages, News Items or any other result type.")) {
				return false;
			} else {
				//unset any check boxes other than document
				for (vinResultTypeIndex = 0; vinResultTypeIndex < vinNumResultTypes; vinResultTypeIndex++) {
					vobControl = pobForm['fobSearchResultType[' + vinResultTypeIndex + ']'];
					vinValue = vobControl.value;
					vobControl.checked = (vinValue == 2); //corresponds to ainSearchResultTypeDocument
				}
			}
		}

	} else {

			if (!gfnCheckValueIsSet(pobForm['fobSearchTermString'].value)) {
				alert("To conduct a search, you MUST specify some Keyword text to search for.");
				pobForm['fobSearchTermString'].focus();
				return false;
			}

	}

	return true;
}

function lfnNotImplemented() {
	alert("This feature not implemented yet.");
}

function lfnCheckIfDocTagsSet(pobForm) {

	//check doc tags checkboxes
	vtxControlName = "fobSearchTermTags";
	vinNumTagFamilies = eval(pobForm[vtxControlName + 'FamilyCount'].value);
	for (vinFamilyIndex = 0; vinFamilyIndex < vinNumTagFamilies; vinFamilyIndex++) {
		vinNumTagsThisFamily = eval(pobForm[vtxControlName + 'RowCount[' + vinFamilyIndex + ']'].value);
		for (vinRowIndex = 0; vinRowIndex < vinNumTagsThisFamily; vinRowIndex++) {
			if (pobForm[vtxControlName + '[' + vinFamilyIndex + '][' + vinRowIndex + ']'].checked) {
				return true;
			}
		}
	}

	//check file type checkboxes
	vinNumFileTypes = eval(pobForm['fobSearchResultFileTypeRowCount'].value);
	for (vinFileTypeIndex = 0; vinFileTypeIndex < vinNumFileTypes; vinFileTypeIndex++) {
		if (pobForm['fobSearchResultFileType[' + vinFileTypeIndex + ']'].checked) {
			return true;
		}
	}

	return false;

}

function lfnFileTypeChanged(pobControl) {

	vobForm = pobControl.form;
	if (lfnCheckIfDocTagsSet(vobForm)) {
		//if any of the document-specific checkboxes are set, change the Result Type checkboxes to 
		//show 'documents' as being the only one checked off
		vinNumResultTypes = eval(vobForm['fobSearchResultTypeRowCount'].value);
		for (vinResultTypeIndex = 0; vinResultTypeIndex < vinNumResultTypes; vinResultTypeIndex++) {
			vobControl = vobForm['fobSearchResultType[' + vinResultTypeIndex + ']'];
			vinValue = vobControl.value;
			vobControl.checked = (vinValue == 2); //corresponds to ainSearchResultTypeDocument
		}
	}

	//if this is the 'other' box, being checked, uncheck all the other file types.
	if (pobControl.checked && (pobControl.value == 8)) { //corresponds to ainResultFileTypeOTH		
		vinNumFileTypes = eval(vobForm['fobSearchResultFileTypeRowCount'].value);
		for (vinFileTypeIndex = 0; vinFileTypeIndex < vinNumFileTypes; vinFileTypeIndex++) {
			vobControl = vobForm['fobSearchResultFileType[' + vinFileTypeIndex + ']'];
			vinValue = vobControl.value;
			vobControl.checked = (vinValue == 8); //corresponds to ainResultFileTypeOTH		
		}
	}

	//if this is any box other than the 'other' box, being checked, uncheck the 'other' box
	if (pobControl.checked && (pobControl.value != 8)) { //corresponds to ainResultFileTypeOTH		
		vinNumFileTypes = eval(vobForm['fobSearchResultFileTypeRowCount'].value);
		for (vinFileTypeIndex = 0; vinFileTypeIndex < vinNumFileTypes; vinFileTypeIndex++) {
			vobControl = vobForm['fobSearchResultFileType[' + vinFileTypeIndex + ']'];
			vinValue = vobControl.value;
			if (vinValue == 8) { //corresponds to ainResultFileTypeOTH		
				vobControl.checked = false;
				break;
			}
		}
	}

	//set the dirty flag 
	gfnSetDirty(pobControl,'Dirty');

}

function lfnDocTagChanged(pobControl) {

	vobForm = pobControl.form;
	if (lfnCheckIfDocTagsSet(vobForm)) {
		//if any of the document-specific checkboxes are set, change the Result Type checkboxes to 
		//show 'documents' as being the only one checked off
		vinNumResultTypes = eval(vobForm['fobSearchResultTypeRowCount'].value);
		for (vinResultTypeIndex = 0; vinResultTypeIndex < vinNumResultTypes; vinResultTypeIndex++) {
			vobControl = vobForm['fobSearchResultType[' + vinResultTypeIndex + ']'];
			vinValue = vobControl.value;
			vobControl.checked = (vinValue == 2); //corresponds to ainSearchResultTypeDocument
		}
	}

	//set the dirty flag 
	gfnSetDirty(pobControl,'Dirty');

}
