// Copyright GlobalSpec, Inc, 2004. All rights reserved.

// the formSubmit function will not let the form
// be submitted until this value is set to true. This
// is set when the 
var formIsOkayToSubmit = false;

// keep track of the last section the user clicked on, defaut to 0
var prevSectionId = 0;

var HILITE_TEXT = "contentOrange";
var STYLE_TEXT_BOX_CONTENT =
	" onFocus=\"doClear(this)\" onkeypress=\"handleKeypress()\" onBlur=\"doReset(this);sfChanged(1);\" class=\"TextBoxContent\"";
var STYLE_PICKLIST_CONTENT = " class=\"TextBoxContent\" onchange=\"sfChanged(1);\" ";

var m_popupHelpLinks = false;

// "Marquee Mode" was added to support presell areas, and it
// displays all of the "Section" popup boxes. This mode is
// currently not viable for actual searching... it's just for 
// looking at. MRG 3/22/04
var m_marqueeMode = false;

// full page mode was added for display of full page search form
// provides look of marquee mode, with search filter functionality
// JEF 3/4/2006
var m_fullPageMode = false;

var m_brickletWidth = 200;

function dw(s) {
	document.write(s);
}


function processSection(section, helpFile) {

	for (var i = 0; i < section.prompts.length; i++) {
		processFormElement(section.prompts[i], helpFile + section.sectionId);
	}

}

function processFormElement(prompt, helpFile) {

	switch (prompt.promptType) {
		case "atleast": {
			renderAtLeast(prompt, helpFile);
			break;
		}
		case "nomorethan": {
			renderNoMoreThan(prompt, helpFile);
			break;
		}
		case "atleastnomorethan": {
			renderAtLeastNoMoreThan(prompt, helpFile);
			break;
		}
		case "fromto": {
			renderFromTo(prompt, helpFile);
			break;
		}
		case "checkbox": {
			renderCheckBox(prompt, helpFile);
			break;
		}
		case "yesno": {
			renderYesNo(prompt, helpFile);
			break;
		}
		case "text": {
			renderTextSearch(prompt, helpFile);
			break;
		}
		case "supplier": {
			renderSupplierSearch(prompt, helpFile);
			break;
		}
		default: {
			//dw("<BIG>Unknown prompt type '" & prompt.promptType & "'</BIG><BR>");
		}
	}

	// add a little space after each prompt
	dw("<TR><TD COLSPAN=\"5\"><img src=\"/pix/spacer.gif\" width=\"1\" height=\"5\" border=\"0\" /></TD></TR>\n");
}

function renderCheckBox(prompt, helpFile) {

//TODO
	if (!prompt.displayDropDown) {
		renderOptionsAsCheckBoxes(prompt, helpFile);
	} else {
		renderOptionsAsDropDown(prompt, helpFile);
	}
}

function renderOptionsAsDropDown(prompt, helpFile) {

	//start prompt row in table
	dw("<TR>");

	//display prompttext
	renderPromptText(prompt, 1, true);

	//set up first cell of checkboxes
	dw("<TD colspan=\"2\" valign=\"top\" NOWRAP>\n");

	//set up drop down with blank first choice
	dw("<SELECT NAME=\"" + prompt.promptId + "\"" + STYLE_PICKLIST_CONTENT + ">\n");
	htmlOption("", "No Preference", false);

	//loop through options adding them to the drop down
	//want to show either the choice selected by the user or
	//the choice selected by the engineer
	for (var i = 0; i < prompt.cboptions.length; i++) {
		var option = prompt.cboptions[i];

		htmlOption(option.promptId, option.promptText, option.isSelected);
	}

	dw("</SELECT></TD>\n");

	renderHelpLink(helpFile, prompt.helpLink);

	// display notes (automatically includes notes for options)
	renderPromptNotes(prompt);

	// end prompt row
	dw("</TR>\n");
}


function renderOptionsAsCheckBoxes(prompt, helpFile) {

	//start prompt row in table
	dw("<TR>");

	// display prompttext
	if ((prompt.promptText != null)
			&& (prompt.promptText.length > 0)) {
		renderPromptText(prompt, 1, false);

		if ((prompt.introNote != null) && (prompt.introNote.length > 0)) {
			// show the Intro text
			dw("<TD colspan=\"2\">");
			dw(prompt.introNote);
			dw("</TD>\n");

			renderHelpLink(helpFile, prompt.helpLink);
			dw("</TR>\n");

			// start a new row
			dw("<TR>");
			dw("<TD colspan=\"2\">&nbsp;</TD>\n");
		}
	} else {
		dw("<TD colspan=\"2\">&nbsp;</TD>\n");
	}

	// NEW SECTION
	dw("<TD COLSPAN=\"1\" valign=\"top\" width=\"35%\">\n");
	dw("<table cellspacing=\"0\" cellpadding=\"0\">\n");
	dw("<tr>\n");
	dw("<td valign=\"top\" height=\"20\">\n");
	dw("<INPUT TYPE=\"CHECKBOX\" NAME=\"dontcare_" + prompt.promptId + "\" ID=\"dontcare_" + prompt.promptId + "\" ONCLICK = \"doUncheckBoxes(this, document.SpecSearchForm);sfChanged(1);\" ");
	if (!prompt.anyOptionsSelected) {
		dw("CHECKED");
	}
	dw("></td>\n");
	dw("<td><strong><label for=\"dontcare_" + prompt.promptId + "\">No&nbsp;Preference</label></strong></td></tr>\n");

	// display first column of checkboxes
	var numInFirstCol = Math.floor(prompt.cboptions.length / 2 + prompt.cboptions.length % 2);
	for (var i = 0; i < numInFirstCol; i++) {
		renderOption(prompt.cboptions[i]);
	}

	dw("</table>\n");

	// min column width
	dw("<img src=\"/pix/spacer.gif\" width=\"120\" height=\"1\">\n");
	dw("</TD>\n");

	// setup second cell of checkboxes
	dw("<TD VALIGN=\"top\" width=\"35%\">");
	dw("<table cellspacing=\"0\" cellpadding=\"0\">\n");
	// add empty cell to balance don't care
	dw("<tr><td colspan=\"2\" height=\"20\">&nbsp;</td></tr>\n");

	// display second column of checkboxes
	for (var i = numInFirstCol; i < prompt.cboptions.length; i++) {
		renderOption(prompt.cboptions[i]);
	}

	dw("</table>\n");
	// min column width
	dw("<img src=\"/pix/spacer.gif\" width=\"120\" height=\"1\">\n");

	dw("</TD>\n");

	//put in help link if not already shown next to prompttext
	var promptText = prompt.promptText;
	var promptIntroNote = prompt.introNote;

	if ((promptText != null) && (promptText.length > 0)
			&& (promptIntroNote != null) && (promptIntroNote.length > 0)) {
		dw("<TD>&nbsp;</TD>");
	} else {
		renderHelpLink(helpFile, prompt.helpLink);
	}
	renderPromptNotes(prompt);
	dw("</TR>\n");
}

function renderYesNo(prompt, helpFile) {

	dw("<TR>");
	renderPromptText(prompt, 1, true);
	dw("<TD colspan=\"2\" NOWRAP height=\"25\" valign=\"top\"><SELECT NAME=\"" + prompt.promptId
			  + "\" " + STYLE_PICKLIST_CONTENT + ">\n");
	htmlOption("-", "No Preference", false);
	htmlOption("1", "Required", (prompt.hasDefault && prompt.defaultValue));
	htmlOption("0", "Must Not Have", (prompt.hasDefault && !prompt.defaultValue));
	dw("</SELECT></TD>\n");
	renderHelpLink(helpFile, prompt.helpLink);
	renderPromptNotes(prompt);
	dw("</TR>\n");
}

function htmlOption(val, txt, selected) {
	dw("<OPTION VALUE=\"" + val + "\"");
	if (selected) {
		dw(" SELECTED");
	}
	dw(">" + txt + "\n");
}

function renderTextSearch(prompt, helpFile) {
	var defaultValue = prompt.defaultValue || "";
	dw("<TR>");
	renderPromptText(prompt, 1, false);
	dw("<TD colspan=\"2\" NOWRAP valign=\"top\">\n");
	renderLabel(prompt.getLabel(), "lbl_" + prompt.promptId);
	dw("<INPUT TYPE=\"TEXT\" SIZE=\"20\"" + STYLE_TEXT_BOX_CONTENT + "NAME=\""
			  + prompt.promptId + "\" VALUE=\"" + defaultValue + "\">");
	dw("</TD>\n");
	renderHelpLink(helpFile, prompt.helpLink);
	renderPromptNotes(prompt);
	dw("</TR>\n");
}

function renderSupplierSearch(prompt, helpFile) {
	dw("<TR>");
	renderPromptText(prompt, 1, true);
	dw("<TD colspan=\"2\" NOWRAP valign=\"top\">\n");
	dw("<SELECT NAME=\"" + prompt.promptId
			  + "\" " + STYLE_PICKLIST_CONTENT + ">\n");
	htmlOption(m_searchForm.supplierId, m_searchForm.supplierName, true);
	htmlOption("0", "All companies", false);
	dw("</SELECT>\n");
	dw("</TD>\n");
	renderHelpLink(helpFile, prompt.helpLink);
	renderPromptNotes(prompt);
	dw("</TR>\n");
}

function renderFromTo(prompt, helpFile) {

	var uservalue_from = prompt.defaultvalue_from || "";
	var uservalue_to = prompt.defaultvalue_to || "";
	dw("<TR>");
	renderPromptText(prompt, (prompt.hasOverrange) ? 2 : 1, false);
	dw("<TD colspan=\"2\" NOWRAP>\n");
	renderUserInputSection("_from", uservalue_from, prompt, prompt.fromLabel);
	renderUserInputSection("_to", uservalue_to, prompt, prompt.toLabel);
	formatUnits(prompt);
	dw("</TD>\n");
	renderHelpLink(helpFile, prompt.helpLink);
	renderOverrange(prompt, helpFile);
	renderPromptNotes(prompt);
	dw("</TR>\n");
}

function renderAtLeast(prompt, helpFile) {

	var uservalue = prompt.defaultvalue || "";
	dw("<TR>");
	renderPromptText(prompt, 1, false);
	dw("<TD colspan=\"2\" NOWRAP>\n");
	renderUserInputSection("_atleast", uservalue, prompt, prompt.label);
	formatUnits(prompt, "2");
	dw("</TD>\n");
	renderHelpLink(helpFile, prompt.helpLink);
	renderPromptNotes(prompt);
	dw("</TR>\n");
}

function renderAtLeastNoMoreThan(prompt, helpFile) {

	uservalue_atleast = prompt.defaultvalue_atleast || "";
	uservalue_nomorethan = prompt.defaultvalue_nomorethan || "";
	dw("<TR>");
	renderPromptText(prompt, 1, false);
	dw("<TD colspan=\"2\" NOWRAP>\n");
	renderUserInputSection("_atleast", uservalue_atleast, prompt, prompt.atLeastLabel);
	renderUserInputSection("_nomore", uservalue_nomorethan, prompt, prompt.noMoreThanLabel);
	formatUnits(prompt);
	dw("</TD>\n");
	renderHelpLink(helpFile, prompt.helpLink);
	renderPromptNotes(prompt);
	dw("</TR>\n");
}

function renderNoMoreThan(prompt, helpFile) {

	var uservalue = prompt.defaultvalue || "";
	dw("<TR>");
	renderPromptText(prompt, 1, false);
	dw("<TD colspan=\"2\" NOWRAP>\n");
	renderUserInputSection("_nomore", uservalue, prompt, prompt.label);
	formatUnits(prompt, "2");
	dw("</TD>\n");
	renderHelpLink(helpFile, prompt.helpLink);
	renderPromptNotes(prompt);
	dw("</TR>\n");
}

function renderUserInputSection(inputid, userValue, prompt, label) {

	renderLabel(label, "lbl_" + prompt.promptId + inputid);

	if ((userValue == null) || (userValue.length == 0)) {
		userValue = "No Pref.";
	}

	if (prompt.inputList == null) {
		renderTextBox(prompt.promptId + inputid, userValue);
	} else {
		renderInputList(prompt.promptId + inputid, userValue, prompt.inputList);
	}
}

function formatUnits(prompt, colspan) {

	if (colspan == null) {
		colspan = "1";
	}

	var unitSel = prompt.unitSel;

	if (prompt.unitSel == null) {
		if (prompt.fixedUnit != null) {
			// fixed unit
			dw(prompt.fixedUnit);
		}
	} else if (unitSel.firstPrompt != prompt) {
		// not the first prompt
		dw("(same units)");
	} else {

		var unitName = unitSel.unitId;

		dw(" <SELECT NAME=\"" + unitName + "_UNIT\" " + STYLE_PICKLIST_CONTENT
				  + ">\n");

		for (var i = 0; i < unitSel.unitOptions.length; i++) {
			var singleUnit = unitSel.unitOptions[i];
			htmlOption(singleUnit.unitId, singleUnit.unitName, singleUnit.isSelected);
		}

		dw("</SELECT>");
	}
}

function renderFormTag() {

	dw("<FORM NAME=\"SpecSearchForm\" METHOD=\"POST\" style=\"margin:0px;\"");
	dw("ACTION=\"/SpecSearch/SearchFormProcess?SrchComp=" + m_searchForm.areaId);

	if (m_searchForm.queryId.length > 0) {
		dw("&PrevQID=" + m_searchForm.queryId);
	}

	// Call JS hook function; it can do whatever it wants to do,
	// including canceling the submit.
	dw("\" onsubmit=\"return formSubmit(this)\"");

	dw(">\n");
}

function renderStandardHiddenFields() {
	dw("<input type=\"hidden\" NAME=\"Comp\" VALUE=\"" + m_searchForm.areaId + "\">\n");
	if (m_searchForm.queryId.length > 0) {
		dw("<input type=\"hidden\" NAME=\"QID\" VALUE=\"" + m_searchForm.queryId + "\">\n");
	}
	dw("<input type=\"hidden\" NAME=\"SubComp\" VALUE=\"\">\n");
}

function renderPromptText(prompt, rowSpan, markAsPromptLabel) {

	dw("<TD rowspan=\"" + rowSpan + "\">&nbsp;&nbsp;</TD>\n");
	dw("<TD rowspan=\"" + rowSpan + "\" valign=\"top\">\n");
	if (markAsPromptLabel) {
		dw("<span id=\"lbl_" + prompt.promptId + "\">");
	}
	dw(prompt.promptText + "&nbsp;\n");
	if (markAsPromptLabel) {
		dw("</span>");
	}
	dw("</TD>\n");
}

function renderHelpLink(helpFile, helpLink) {

	dw("<TD align=\"right\" valign=\"top\" width=\"1%\">\n");
	if (helpLink != null) {
		dw("<a href=\"");
		if (helpLink.substring(0, 1) != "/") {
			// helpLink is anchor
			dw(helpFile + "#" + helpLink);
		} else {
			// helpLink is url
			dw(helpLink);
		}
		dw("\" onClick=\"return handleHelpLink(this, " + m_popupHelpLinks + ");\">");
		dw("<img src=\"/pix/buttons/help_button.gif\" vspace=2 hspace=2 width=\"23\" height=\"14\" alt=\"Help\" border=\"0\"></A>\n");
	}
	dw("</TD>\n");
}

function renderLabel(label, labelId) {
	dw("&nbsp;<span id=\"" +  labelId + "\">");
	dw(label);
	dw("</span> ");
}

function renderOption(option) {

	dw("<tr><td valign=\"top\"><INPUT TYPE=\"CHECKBOX\" NAME=\"" + option.promptId + "\" ID=\"" + option.promptId + "\"");
	if (option.isSelected) {
		dw(" CHECKED");
	}
	dw(" ONCLICK = \"doUncheckDontCare(this, document.SpecSearchForm);sfChanged(1)\"\n");
	dw("></td><td>\n");

	dw("<label for=\"" + option.promptId + "\" id=\"lbl_" + option.promptId + "\">");
	dw(option.promptText);
	dw("</label>");

	dw("</td></tr>\n");
}

function renderPromptNotes(prompt) {

	var notes = prompt.note;

	// Handle checkboxes differently... we want to include the
	// notes for any options too
	if (prompt.promptType == "checkbox") {
		notes = buildCheckBoxNotes(prompt);
	}

	if ((notes == null) || (notes.length == 0)) {
		return;
	}

	dw("</TR>\n");

	// start a new row for the notes
	dw("<TR>");
	dw("<TD COLSPAN=\"2\">&nbsp;</TD>\n");
	dw("<TD COLSPAN=\"3\">\n");
	dw(notes);
	dw("</TD>\n");
}

function buildCheckBoxNotes(prompt) {

	var notes = "";

	// Collect notes from the available options
	for (var i = 0; i < prompt.cboptions.length; i++) {
		var option = prompt.cboptions[i];

		if (option.note.length > 0) {
			notes = notes + option.promptText + " - " + option.note + "<BR>\n";
		}
	}

	// Append the note for the prompt itself
	if ((prompt.note != null) && (prompt.note.length > 0)) {
		if (notes.length > 0) {
			notes = notes + "<BR>\n";
		}
		notes = notes + prompt.note;
	}

	return notes;
}

/**
 *  This function renders the overrange portions of a fromto prompt.
 *  @param prompt the FromTo prompt being rendered
 */
function renderOverrange(prompt, helpFile) {

	if (prompt.hasOverrange) {
		dw("</TR>\n");
		dw("<TR><TD colspan=\"2\">\n");
		renderLabel("Allow up to: ", "lbl_" + prompt.promptId + "_over");
		dw("<SELECT NAME=\"" + prompt.promptId + "_over\" " + STYLE_PICKLIST_CONTENT + ">\n");
		htmlOption("", "No Pref.", false);
		var overrange_val = prompt.overrangeValue;
		if (overrange_val == 1) {
			// Hide "0%" option unless it is the one to be selected
			overrangeOption(1, overrange_val);
		}
		overrangeOption(1.1, overrange_val);
		overrangeOption(1.2, overrange_val);
		overrangeOption(1.3, overrange_val);
		overrangeOption(1.4, overrange_val);
		overrangeOption(1.5, overrange_val);
		overrangeOption(1.75, overrange_val);
		overrangeOption(2, overrange_val);
		overrangeOption(3, overrange_val);
		overrangeOption(4, overrange_val);
		dw("</SELECT>\n");
		dw("overrange/margin\n");
		dw("</TD>\n");
	
		// finish up the prompt row...
		renderHelpLink(helpFile, "overrange");
		dw("</TR>\n");

		dw("<TR><TD COLSPAN=\"2\">&nbsp;</TD>\n");
		dw("<TD COLSPAN=\"4\">\n");
		dw("Use the overrange/margin to restrict your search to items whose full-scale range is close to your requirements.\n");
		dw("<BR>\n");
		dw("<i>(Overrange/margin requires both 'From' and 'To' values to work.)</i>\n");
		dw("</TD>\n");
	}
}


function overrangeOption(optionVal, userValue) {
	htmlOption(optionVal, Math.round((optionVal-1)*100) + "%", (optionVal == userValue));
}

function renderTextBox(name, value) {
	dw("<INPUT TYPE=\"TEXT\" SIZE=\"9\" NAME=\"" + name + "\" VALUE=\""
			  + value + "\"" + STYLE_TEXT_BOX_CONTENT + ">");
}

function renderInputList(name, value, inputList) {	
	dw("<SELECT NAME=\"" + name + "\" " + STYLE_PICKLIST_CONTENT + ">\n");
	htmlOption("", "No Pref.", false);
	for (var i = 0; i < inputList.values.length; i++) {
		htmlOption(inputList.values[i].value, inputList.values[i].displayText, 
			(value == inputList.values[i].value));
	}
	dw("</SELECT>\n");
}

/////////////////////////////////////////////////////////////////////////
// client-side stuff

var m_sfChanged = false;
var m_sectionChanged = false;
var m_visibleSection = null;
var m_formState = new Object();

// define some images that will be toggled when the user clicks on sections
var sectionArrowNotSelected = new Image();
sectionArrowNotSelected.src = "/pix/sf/LargeDarkBlueArrows.gif";

var sectionArrowSelected = new Image();
sectionArrowSelected.src = "/pix/sf/LargeBrightBlueArrows.gif";

var updateEnabled = new Image();
updateEnabled.src = "/pix/sf/UpdateResultsBtn.gif";

var m_paletteBgColor = "#F0E68C";
var m_paletteTopBgColor = "#000080";
var m_paletteTopTextColor = "#FFD700";
var m_paletteTopTextProducts = "ADVANCED SEARCH";
var m_paletteTopTextProductsWithQuery = "ADVANCED SEARCH";
var m_paletteTopTextServices = "ADVANCED SEARCH";
var m_paletteTopTextServicesWithQuery = "ADVANCED SEARCH";
var m_paletteSpecLabelColor = "#A52A2A";
var m_paletteSpecLabelHighlightColor = "#E63333";
var m_paletteSpecLabelProducts = "SEARCH CRITERIA:";
var m_paletteSpecLabelServices = "SEARCH CRITERIA:";
var m_paletteSectionRowHighlightBgColor = "#DACF71";
var m_paletteSectionRowLinkStyle = "underlinedPaletteSectionLinks";
var m_paletteSectionRowLinkForcedColor = m_paletteSpecLabelColor;


function renderSearchFormPalette() {

	dw("<style type=\"text/css\">\n");
	dw(".sectionlink:link { color : #003399; TEXT-DECORATION: none }\n");
	dw(".sectionlink:visited { color : #003399; TEXT-DECORATION: none }\n");
	dw(".sectionlink:hover { color : black; TEXT-DECORATION: none }\n");
	dw(".sectionlink:active { color : black; TEXT-DECORATION: none }\n");
	dw("</style>\n");
	
	dw("<TABLE bgcolor=\"" + m_paletteBgColor + "\" cellSpacing=0 cellPadding=0 width=\"100%\" border=0 style=\"clear:both\">\n");
	
	if (m_searchForm.supplierId != null) {
		dw("<TR vAlign=top>\n");
		
		// left hand side border
		dw("<td class=\"brickletOutline\"><img src=\"/pix/dot.gif\"></td>");
		
		dw("<TD style=\"PADDING-RIGHT: 3px; PADDING-LEFT: 10px; PADDING-BOTTOM: 3px; PADDING-TOP: 20px\">");
		dw("<SPAN ID=\"SearchFormNormalTitle\" style=\"FONT-SIZE: 80%; COLOR: #990000\">COMPANY FILTER</SPAN>\n");
		dw("<DIV><IMG height=5 src=\"/pix/spacer.gif\" width=1></DIV>\n");
		dw("<TABLE cellSpacing=1 cellPadding=0 width=\"100%\" border=0>\n");
	
		renderSectionPaletteEntry(m_searchForm.supplierSection);

		dw("</TABLE></TD>");
		
		// right hand side border (there is an extra <td> set in here to make it line up
		// with the upper section. if you remove this, remove the extra <td> set above
		dw("<td></td><td class=\"brickletOutline\"><img src=\"/pix/dot.gif\"></td>");
		
		dw("</TR>\n");		
	}
	
	dw("<TR vAlign=top>\n");
	
	// left hand side border
	dw("<td class=\"brickletOutline\"><img src=\"/pix/dot.gif\"></td>");
	
	dw("<TD style=\"PADDING-RIGHT: 3px; PADDING-LEFT: 10px; PADDING-BOTTOM: 3px; PADDING-TOP: 20px\">");
	dw("<SPAN ID=\"SearchFormNormalTitle\" style=\"FONT-SIZE: 80%; COLOR: " + m_paletteSpecLabelColor + "\">");

	if (m_searchForm.isServiceArea) {
		dw(m_paletteSpecLabelServices);
	} else {
		dw(m_paletteSpecLabelProducts);
	}
	
	dw("</SPAN>\n");
	dw("<DIV><IMG height=5 src=\"/pix/spacer.gif\" width=1></DIV>\n");
	dw("<TABLE cellSpacing=1 cellPadding=0 width=\"100%\" border=0>\n");

	// loop through panels and sections to create the clickable "palette"
	for (var i = 0; i < m_searchForm.panels.length; i++) {
		var panel = m_searchForm.panels[i];

		// Render panel's sections
		for (var j = 0; j < panel.sections.length; j++) {
			renderSectionPaletteEntry(panel.sections[j]);
		}
	}
	
	dw("</TABLE></TD>");
	
	// right hand side border (there is an extra <td> set in here to make it line up
	// with the upper section. if you remove this, remove the extra <td> set above
	dw("<td></td><td class=\"brickletOutline\"><img src=\"/pix/dot.gif\"></td>");
	
	dw("</TR>\n");

	dw("<TR>\n");
	
	// left hand side border
	dw("<td class=\"brickletOutline\"><img src=\"/pix/dot.gif\"></td>");
	
	dw("<TD style=\"PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px\" colSpan=2>\n");
	
	dw("<TABLE wcellPadding=0 width=\"100%\">\n");
	dw("<TR align=left>\n");
	dw("<TD>\n");
		
	//dw("<a class=\"linksWithUnderline\" style=\"PADDING-LEFT: 17px;\" href=\"#\" onclick=\"alert('This would have shown all of the search criteria, but it is not implemented. Sorry.');return false\">See All Specs</a>");
	
	// a "start over" button, if we have parameters
	if (m_searchForm.queryId.length > 0) {
		dw("<br><br><div align=\"center\">")
		dw("<a href=\"/SpecSearch/Suppliers?comp=" + m_searchForm.areaId + "\"><img name=\"sfReset\" src=\"/pix/sf/StartOverBtn.gif\" border=\"0\"></a>");
		dw("<\div>")
	}
		
	dw("<DIV><IMG height=20 src=\"/pix/spacer.gif\" width=1></DIV>Powered by <B>SpecSearch</B><SUP style=\"TEXT-DECORATION: none\">®</SUP>\n");
	dw("<DIV><IMG height=15 src=\"/pix/spacer.gif\" width=1></DIV>");
	dw("<a class=\"linksWithUnderline\" href=\"/specsearch/searchform?QID=" + m_searchForm.queryId + "&comp=" + m_searchForm.areaId + "\">View Entire Specification Form</a>");
	dw("<DIV><IMG height=15 src=\"/pix/spacer.gif\" width=1></DIV>");

	dw("</TD>");
			
	dw("</TR></TABLE>");
			
	dw("</TD>");
	
	// right hand side border
	dw("<td class=\"brickletOutline\"><img src=\"/pix/dot.gif\"></td>");
	
	dw("</TR>\n");
	
	dw("</TABLE>\n");
	
	// bottom row with curved corners
	dw("<table width=\"100%\" cellspacing=0 cellpadding=0>");
	dw("<tr>");
	dw("<td width=\"1%\"><img src=\"/pix/sf/PalletteleftCorner.gif\"></td>");
	dw("<td width=\"98%\" background=\"/pix/sf/PalletteBottomLine.gif\"><img src=\"/pix/dot.gif\"></td>");
	dw("<td width=\"1%\"align=right><img src=\"/pix/sf/PalletteRightCorner.gif\"></td>");
	dw("</tr></table>");
	
	// Note: setInitialRecaps() must be called at some point to complete the palette
}

function renderSearchFormSectionPopups() {	
	
	// helpFile is a help link url where the sectionid can be appended later.
	var helpFile;

	//set help file depending on whether this is the new help system or not
	if ((m_searchForm.specHelpFile != null) && (m_searchForm.specHelpFile != "")) {
		helpFile = "/help/spechelp.html?name=" + m_searchForm.specHelpFile + "&comp=" + m_searchForm.areaId + "&sectionid=";
		m_popupHelpLinks = true;
	} else {
		helpFile = "todo";
		m_popupHelpLinks = false;
	}

	if (m_searchForm.supplierId != null) {
		renderSectionPopup(m_searchForm.supplierSection, helpFile);
	}

	// loop through panels and sections to create a popup DIV for each section
	for (var i = 0; i < m_searchForm.panels.length; i++) {
		var panel = m_searchForm.panels[i];

		// Render panel's sections
		for (var j = 0; j < panel.sections.length; j++) {
			var section = panel.sections[j];

			renderSectionPopup(section, helpFile);
		}
	}
}

function setInitialRecaps() {
	// Now update the palette to reflect the current form prompts
	updateFormRecaps();
}

function dump(o) {
	document.write("dump...<BR>");

	for (var p in o) {
		document.write("prop " + p + "= '" + o[p] + "'<BR>");
	}
	document.write("====<BR>");
}


function renderSectionPaletteEntry(section) {
	dw("<TR id=\"sectionRow" + section.sectionId + "\" onclick=\"selectedSection('" + section.sectionId +"');return false;\" onmouseover=\"highliteSectionRow('" + section.sectionId +"')\" style=\"BACKGROUND-COLOR: " + m_paletteBgColor + "; cursor: pointer;\" onmouseout=\"unhighliteSectionRow('" + section.sectionId +"')\">\n");
	dw("<TD style=\"PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; PADDING-TOP: 5px\" align=left>");
	dw("<A id=\"sectionRowText" + section.sectionId + "\" style=\"color: " + m_paletteSectionRowLinkForcedColor + ";\" class=\"" + m_paletteSectionRowLinkStyle + "\" href=\"#\" title=\"Click to add specifications\">");
	dw(section.sectionTitle);
	dw("</A>");
	dw("&nbsp;&nbsp;<IMG id=\"sectionArrow" + section.sectionId +"\" src=\"/pix/sf/");
	
	dw("LargeDarkBlueArrows.gif");
	
	dw("\">\n");
	dw("</TD></TD></TR>\n");
	dw("<TR>\n");
	dw("<TD id=\"sectionRecap" + section.sectionId + "\" style=\"PADDING-RIGHT: 0px; PADDING-LEFT: 10px; FONT-SIZE: 10px; PADDING-BOTTOM: 5px; COLOR: #990000; PADDING-TOP: 0px\"></TD>");
	dw("</TR>\n");
}

function renderSectionPopup(section, helpFile) {

	var popupPrompt = (section.sectionPopupPrompt == null) 
		? "Enter one or more " + section.sectionTitle + " criteria:" 
		: section.sectionPopupPrompt;
		
	if (m_fullPageMode) {
		popupPrompt = section.sectionTitle + " criteria:";
	}
	
	startPopup(section.sectionId, popupPrompt, section.sectionNote);

	processSection(section, helpFile);

	endPopup(section.sectionId);
}

function startPopup(sectionId, popupTitle, popupNote) {
	dw("<DIV id=sfPopUp" + sectionId);
	if (!m_marqueeMode && !m_fullPageMode) {
		dw(" style=\"LEFT: 235px; VISIBILITY: hidden; WIDTH: 70%; POSITION: absolute; TOP: 390px\"");
	} else {
		dw(" style=\"WIDTH: 100%;\"");
	}
	dw(">\n");
	
	dw("<TABLE cellSpacing=0 cellPadding=8 width=\"100%\" bgColor=#ffffff border=0 style=\"border:solid #003399 2px;\">\n");
	dw("<TR><TD>\n");

	// this table starts the "real" table that the prompts will be in
	dw("<TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n");
	//spacer row
	dw("<tr>\n");
	dw("<td colspan=\"2\" width=\"170\"><img src=\"/pix/spacer.gif\" width=\"170\" height=\"1\"  border=\"0\"></td>\n");
	dw("<td colspan=\"3\" width=\"100%\"><img src=\"/pix/spacer.gif\" width=\"255\" height=\"1\"  border=\"0\"></td>\n");
	dw("</tr>\n");

	// Now for the title
	dw("<TR valign=top>\n");
	dw("<TD colspan=5 align=\"left\">&nbsp;&nbsp;");
	dw("<span class=\"boldRedContent\">" + popupTitle + "</span>\n");

	if ((popupNote != null) && (popupNote.length > 0)) {
		dw("<BR><span class=\"content\">" + popupNote + "</span>");
	}

	dw("</TD></TR>\n");
	dw("<TR><TD colspan=5 align=\"left\"><img src=\"/pix/spacer.gif\" width=\"1\" height=\"5\" border=\"0\" /></TD></TR>\n");
	dw("<TR><TD colspan=5 align=\"left\"><img src=\"/pix/spacer.gif\" width=\"1\" height=\"5\" border=\"0\" /></TD></TR>\n");
}

function endPopup(sectionId) {
	// end the section's table
	dw("</TABLE>\n");
	dw("</TD></TR>\n");

	if (!m_marqueeMode && !m_fullPageMode) {	
		// add the Ok and Clear buttons

		dw("<TR>\n");
		dw("<TD align=middle height=30>\n");
		dw("<TABLE cellSpacing=1 cellPadding=5 width=\"60%\" border=0>\n");
		dw("<TR>\n");
		dw("<TD align=left>\n");
		dw("<a href=\"#\"><img name=\"sfCancel" + sectionId + "\" src=\"/pix/sf/CancelBtn.gif\" border=\"0\" onclick=\"cancelSectionPopup('" + sectionId + "');return false;\"></a>")
		dw("</TD>\n");
		dw("<TD align=right>\n");
		
		dw("<a href=\"#\"><img name=\"sfUpdateResults" + sectionId + "\" src=\"/pix/sf/UpdateResultsBtnOff.gif\" border=\"0\" onclick=\"setSubComp('');checkAndSubmitForm(); return false;\"></a>")
		
		dw("</TD></TR></TABLE>\n");
		dw("</TD></TR>\n");
	}
	dw("</TABLE>\n");
	
	dw("</DIV>\n");

	if (m_marqueeMode || m_fullPageMode) {
		dw("<P>\n");
	}
}

function renderSupplierSectionPopup() {


	renderSectionPopup(m_searchForm.supplierSection, "");
	
	startPopup(section.sectionId, "Select Companies to Search:", null);
	
	var prompt = section.prompts[0];

	dw("<TR><TD>&nbsp;&nbsp;</TD><TD valign=\"top\">\n");
	dw("<span id=\"lbl_VID\">Companies to search:</span>\n");
	dw("</TD>\n");
	dw("<TD colspan=\"2\" NOWRAP height=\"25\" valign=\"top\"><SELECT NAME=\"VID"
			  + "\" " + STYLE_PICKLIST_CONTENT + ">\n");
	htmlOption(m_searchForm.supplierId, m_searchForm.supplierName, true);
	htmlOption("", "All companies", false);
	dw("</SELECT></TD>\n");
	dw("<TD></TD>\n");	// help link
	//renderPromptNotes(prompt);
	dw("</TR>\n");
	
	endPopup(sectionId);
}


// This is called when a user clicks on a section title in the "palette".
// This is function is the one that puts the spec in the "Overlay Displayed" state
function selectedSection(sectionId) {
	
	// if there user is clicking a new section (there is already a pop up open and
	// they are switching to a new one) we need to hide the old one and reset the
	// display in the pallette
	if ((m_visibleSection != null) && (m_visibleSection != sectionId)) {
				
		// Hide the currently displayed section
		hideSectionPopup(m_visibleSection);
	
	}

	// get the pop up for the clicked section
	var popup = getObjectStyle("sfPopUp" + sectionId);
		
	// if its not open, the open it
	if (popup.visibility == "hidden") {
	
		showSectionPopup(sectionId);
		
	// if its already open, then hide it, and reset UI cues
	} else {
	
		hideSectionPopup(sectionId);
			
	}
		
	// remember that this was clicked on, could be useful
	prevSectionId = sectionId;
	
}

// show the pop up for a section and
// set the UI cues to selected
function showSectionPopup(sectionId) {
	// Invoke "hook" for user-defined behavior.
	// Default is no-op.
	if (!hookBeforeShowSectionPopup(sectionId)) {
		// abort if hook tells us to
		return;
	}

	// turn the text for the section to bright blue
	getObjectStyle("sectionRowText" + sectionId).color = m_paletteSpecLabelHighlightColor;
	
	// turn the arrows for the section to bright blue
	getRawObject("sectionArrow" + sectionId).src = sectionArrowSelected.src;
	
	// get the pop up for the section
	var popup = getObjectStyle("sfPopUp" + sectionId);

	// get/set the position of the pop up
	var scrollPos = scrolledAmt().y;
	var anchorPos = getObjectPos("searchSidebarPopupAnchorPos");
	var topMost = anchorPos.top;
	var leftMost = anchorPos.left;
	var newTop = topMost;
	if (scrollPos > newTop) {
		newTop = scrollPos + 5;
	}
	popup.top = newTop + "px";
	popup.left = leftMost;
	popup.zIndex = "3";
	popup.visibility = "visible";

	// show and enable appropriate buttons on the pop up
	if (!m_sfChanged) {
		disableUpdateButton(sectionId);
	} else {
		enableUpdateButton(sectionId);
	}

	// save our start state so we can go back to it on a cancel
	m_formState = saveFormState(document.SpecSearchForm);

	// set the section as unchanged so far
	m_sectionChanged = false;

	// set this as the visible pop up
	m_visibleSection = sectionId;
	
	// Invoke "hook" for user-defined behavior.
	// Default is no-op.
	hookShowSectionPopup(sectionId);
}

// Define the "hook" for user-defined behavior,
// to be called whenever a Section is displayed.
// Default is no-op.
function hookShowSectionPopup(sectionId) {
}

// Define the "hook" for user-defined behavior,
// to be called before a Section is displayed.
// Default is no-op.
function hookBeforeShowSectionPopup(sectionId) {
	return true;
}

// hide the pop up for a given section and
// reset the UI cues to not selected
function hideSectionPopup(sectionId) {

	// turn the text for the section to dark blue
	getObjectStyle("sectionRowText" + sectionId).color = m_paletteSpecLabelColor;
	
	// turn the arrows for the section to dark blue
	getRawObject("sectionArrow" + sectionId).src = sectionArrowNotSelected.src;
		
	// get the pop up for the section
	var popup = getObjectStyle("sfPopUp" + sectionId);
	
	// hide the section
	popup.visibility = "hidden";
	
	// clear the visible section property if needed
	if (m_visibleSection == sectionId) {
		m_visibleSection = null;
	}
	
}

// Called from the cancel button on the pop up for a 
// spec search section
function cancelSectionPopup(sectionId) {
	
	// restore the form values
	restoreFormState(document.SpecSearchForm, m_formState);
	
	updateFormRecaps();
	
	// now close the popup
	hideSectionPopup(sectionId);
	
}

// Called whenever a search form prompt is changed
function sfChanged(updateRecaps) {
	if (updateRecaps) {
		updateFormRecaps();
	}

	if (!m_sfChanged) {
		// first change made to the sf since page loaded
		m_sfChanged = true;
	}

	// Is this the current section, and did it change?
	if (m_visibleSection != null) {
		if ((getObjectStyle("sfPopUp" + m_visibleSection).visibility == "visible") && !m_sectionChanged) {
			// first change made in the currently displayed section since it was opened
			m_sectionChanged = true;
			enableUpdateButton(m_visibleSection);
		}
	}
}

function handleKeypress() {
	sfChanged(0);	// don't update recaps on keypress... it loses focus badly
}


// Update the "recap" text for each section in the palette.
// Call this whenever the recaps might be out of date.
function updateFormRecaps() {

	if (m_marqueeMode) {
		return;
	}

	for (var i = 0; i < m_searchForm.panels.length; i++) {
		var panel = m_searchForm.panels[i];

		// Render panel's sections
		for (var j = 0; j < panel.sections.length; j++) {
			var section = panel.sections[j];

			if ((m_visibleSection != null) && (m_visibleSection != section.sectionId)) {
				continue;
			}
			
			updateSectionRecaps(section);
		}
	}
	
	if ((m_searchForm.supplierSection != null) && 
		((m_visibleSection == null) || (m_visibleSection == "Supplier"))) {
		updateSectionRecaps(m_searchForm.supplierSection);
	}
}

function updateSectionRecaps(section) {
	var recapTxt = "";
	
	// Render recaps for this section's prompts
	for (var i = 0; i < section.prompts.length; i++) {
		var prompt = section.prompts[i];

		//determine which prompt to process
		switch (prompt.promptType) {
			case "atleast": {
				recapTxt = recapTxt + recapNumericalPrompt(prompt, "_atleast", prompt.label);
				break;
			}
			case "nomorethan": {
				recapTxt = recapTxt + recapNumericalPrompt(prompt, "_nomore", prompt.label);
				break;
			}
			case "atleastnomorethan": {
				recapTxt = recapTxt + recapNumericalPrompt(prompt, "_atleast", prompt.atLeastLabel, "_nomore", prompt.noMoreThanLabel);
				break;
			}
			case "fromto": {
				recapTxt = recapTxt + recapNumericalPrompt(prompt, "_from", prompt.fromLabel, "_to", prompt.toLabel);
				break;
			}
			case "checkbox": {
				recapTxt = recapTxt + recapCheckbox(prompt);
				break;
			}
			case "yesno": {
				recapTxt = recapTxt + recapYesNo(prompt);
				break;
			}
			case "text": {
				recapTxt = recapTxt + recapTextSearch(prompt);
				break;
			}
			case "supplier": {
				recapTxt = recapTxt + recapSupplierSearch(prompt);
				break;
			}
			default: {
				//dw("<BIG>Unknown prompt type '" & prompt.promptType & "'</BIG><BR>");
			}
		}
	}

	if (!m_fullPageMode) {
		// load the recap into the palette
		var recapDiv = getRawObject("sectionRecap" + section.sectionId);
		if (recapDiv.innerHTML != recapTxt) {
			recapDiv.innerHTML = recapTxt;
		}
	}
}

function strIt(s) {
	var txt = "";
	for (i = 0; i<s.length; i++) {
		txt = txt + s.charCodeAt(i) + ".";
	}
	return txt;
}

function recapNumericalPrompt(prompt, suffix1, label1, suffix2, label2) {
	var v1 = document.SpecSearchForm[prompt.promptId + suffix1].value;
	var v2 = (suffix2 != null) ? document.SpecSearchForm[prompt.promptId + suffix2].value : "";
	if (v1 == "No Pref.") {
		v1 = "";
	}
	if (v2 == "No Pref.") {
		v2 = "";
	}

	if (prompt.inputList != null) {
		for (var i = 0; i < prompt.inputList.values.length; i++) {
			if (v1 == prompt.inputList.values[i].value) {
				v1 = prompt.inputList.values[i].displayText;
			}
			if (v2 == prompt.inputList.values[i].value) {
				v2 = prompt.inputList.values[i].displayText;
			}
		}
	}
	
	var over = "";
	var overLabel = null;
	if ((prompt.hasOverrange != null) && prompt.hasOverrange) {
		overLabel = getRawObject("lbl_" + prompt.promptId + "_over");
		if ((v1 != "") && (v2 != "")) {
			var overOpts = document.SpecSearchForm[prompt.promptId + "_over"].options;
			// start loop at "1" to skip No Pref
			for (var i = 1; i < overOpts.length; i++) {
				if (overOpts[i].selected) {
					over = " (allow " + overOpts[i].text + " overrange)";
					break;
				}
			}
		}
	}

	// highlight the label(s) associated with this prompt
	var promptLabel1 = getRawObject("lbl_" + prompt.promptId + suffix1);
	var promptLabel2 = (suffix2 != null) ? getRawObject("lbl_" + prompt.promptId + suffix2) : null;
	
	if (promptLabel1 != null) {
		promptLabel1.className = (v1 != "") ? HILITE_TEXT : "";
	}
	if (promptLabel2 != null) {
		promptLabel2.className = (v2 != "") ? HILITE_TEXT : "";
	}
	if (overLabel != null) {
		overLabel.className = (over != "") ? HILITE_TEXT : "";
	}

	if ((v1 == "") && (v2 == "")) {
		return "";
	}
	

	var txt = prompt.promptText;
	if (txt.indexOf(":") < 0) {
		txt = txt + ":";
	}
	txt = txt + "<BR>\r\n<LI>";

	if ((v1 != "") && (v2 != "")) {
		txt = txt + label1 + " " + v1 + " " + label2.toLowerCase() + " " + v2;
	} else if (v1 != "") {
		txt = txt + label1 + " " + v1;
	} else if (v2 != "") {
		txt = txt + label2 + " " + v2;
	}

	var units = "";

	if (prompt.unitSel == null) {
		if (prompt.fixedUnit != null) {
			// fixed unit
			units = prompt.fixedUnit;
		}
	} else {
		var options = document.SpecSearchForm[prompt.unitSel.unitId + "_UNIT"].options;
	    for (var i = 0; i < options.length; i++) {
	        if (options[i].selected) {
				units = options[i].text;
				break;
	        }
	    }
	
	}	
	txt = txt + " " + units + over +"</LI>";
	
	return txt;
}

function recapCheckbox(prompt) {
	var txt = "";

	if (!prompt.displayDropDown) {
		// scan checkboxes
		for (var i = 0; i < prompt.cboptions.length; i++) {
			if (document.SpecSearchForm[prompt.cboptions[i].promptId].checked) {
				txt = txt + "<LI>" + prompt.cboptions[i].promptText + "</LI>";
			}
			
			var promptLabel = getRawObject("lbl_" + prompt.cboptions[i].promptId);
			if (promptLabel != null) {
				promptLabel.className = (document.SpecSearchForm[prompt.cboptions[i].promptId].checked) ? HILITE_TEXT : "";
			}
		}
	} else {
		// handle drop-down case
		var options = document.SpecSearchForm[prompt.promptId].options;
		var selectedText = null;
	    for (var i = 0; i < options.length; i++) {
	        if ((options[i].selected) && (options[i].value != "")) {
				selectedText = options[i].text;
				break;
	        }
	    }

		if (selectedText != null) {
			txt = txt + "<LI>" + selectedText + "</LI>";
		}
		var promptLabel = getRawObject("lbl_" + prompt.promptId);
		if (promptLabel != null) {
			promptLabel.className = (selectedText != null) ? HILITE_TEXT : "";
		}
	}
	
	if (txt != "") {
		var lbl = prompt.promptText;
		if (lbl.indexOf("?") > 0) {
			lbl = lbl.substring(0, lbl.indexOf("?"));
		}
		if (lbl.indexOf(":") < 0) {
			lbl = lbl + ":";
		}

		txt = lbl + txt;
	}
	
	return txt;
}

function recapYesNo(prompt) {
	var txt = prompt.promptText;
	if (txt.indexOf("?") > 0) {
		txt = txt.substring(0, txt.indexOf("?"));
	}
	if (txt.indexOf(":") < 0) {
		txt = txt + ":";
	}
	txt = txt + "<BR><LI>";

	var isUsed = false;
	
	var options = document.SpecSearchForm[prompt.promptId].options;
    for (var i = 0; i < options.length; i++) {
        if (options[i].selected) {
			if (options[i].value == "0") {
				txt = txt + " Must not have</LI>";
				isUsed = true;
				break;
			} else if (options[i].value != "-") {
				txt = txt + " Required</LI>";
				isUsed = true;
				break;
			}
        }
    }

	var promptLabel = getRawObject("lbl_" + prompt.promptId);
	if (promptLabel != null) {
		promptLabel.className = (isUsed) ? HILITE_TEXT : "";
	}
	
	return (isUsed) ? txt : "";
}

function recapTextSearch(prompt) {

	var v = document.SpecSearchForm[prompt.promptId].value;

	var promptLabel = getRawObject("lbl_" + prompt.promptId);
	if (promptLabel != null) {
		promptLabel.className = (v != "") ? HILITE_TEXT : "";
	}

	var txt = "";

	if (v != "") {
		txt = prompt.promptText;
		if (txt.indexOf("?") > 0) {
			txt = txt.substring(0, txt.indexOf("?"));
		}
		if (txt.indexOf(":") < 0) {
			txt = txt + ":";
		}
		txt = txt + "<BR><LI>Match: " + v;
	}

	return txt;
}

function recapSupplierSearch(prompt) {
	var txt = prompt.promptText;
	if (txt.indexOf("?") > 0) {
		txt = txt.substring(0, txt.indexOf("?"));
	}
	if (txt.indexOf(":") < 0) {
		txt = txt + ":";
	}
	txt = txt + "<BR><LI>";

	var isUsed = false;
	
	var options = document.SpecSearchForm[prompt.promptId].options;
    for (var i = 0; i < options.length; i++) {
        if (options[i].selected) {
			isUsed = true;
			txt = txt + options[i].text + "</LI>";
			break;
        }
    }

	var promptLabel = getRawObject("lbl_" + prompt.promptId);
	if (promptLabel != null) {
		promptLabel.className = (isUsed) ? HILITE_TEXT : "";
	}

	return (isUsed) ? txt : "";
}

//=======================================


// This function is called on the mouse over of a section. It can
// be considered as the function to put the spec text into the "hover"
// state.
function highliteSectionRow(sectionId) {
	// highlight the section
	getObjectStyle("sectionRow" + sectionId).backgroundColor = m_paletteSectionRowHighlightBgColor;
}

// This function is called on the mouseout of a section row
function unhighliteSectionRow(sectionId) {
	getObjectStyle("sectionRow" + sectionId).backgroundColor = m_paletteBgColor;
}

//=======================================

/////////////////////////////////////////////////////////////////////////
function enableUpdateButton(sectionId) {
	document.images["sfUpdateResults" + sectionId].src = "/pix/sf/UpdateResultsBtn.gif";
	formIsOkayToSubmit = true;
}

function disableUpdateButton(sectionId) {
	document.images["sfUpdateResults" + sectionId].src = "/pix/sf/UpdateResultsBtnOff.gif";
	formIsOkayToSubmit = false;
}

function saveFormState(aform) {
	var formState = new Object();
	for (var i = 0; i < aform.elements.length; i++) {	
		if (aform.elements[i].type == 'text') { 
			formState[aform.elements[i].name] = aform.elements[i].value;
		} else if (aform.elements[i].type == 'select-one') {
		    for (var j=0; j < aform.elements[i].options.length; j++) {
		        if (aform.elements[i].options[j].selected) {
					formState[aform.elements[i].name] = j;
		        }
		    }
		} else if (aform.elements[i].type == 'checkbox') {
			formState[aform.elements[i].name] = aform.elements[i].checked;
		} 
	}
//	dump(formState);
	return formState;
}

function restoreFormState(aform, formState) {
	for (var i = 0; i < aform.elements.length; i++) {	
		if (aform.elements[i].type == 'text') { 
			aform.elements[i].value = formState[aform.elements[i].name] || "";
		} else if (aform.elements[i].type == 'select-one') {
	        aform.elements[i].options[formState[aform.elements[i].name] || 0].selected = true;
		} else if (aform.elements[i].type == 'checkbox') {
			aform.elements[i].checked = formState[aform.elements[i].name] || false;
		} 
	}
}

//function for sf controls

function setSubComp(id) { 
	document.SpecSearchForm.SubComp.value=id; 
}

function ResetSearchFormToBlank(aform) {
	ans = confirm("Are you sure you want to reset the form?");
	if (ans) {
		for (i=0; i < aform.elements.length; i++) {	
			if (aform.elements[i].type == 'text') { 
				aform.elements[i].value = "No Pref."; 
			} else if (aform.elements[i].type == 'select-one') {
				// skip if its a unit selector
				if (aform.elements[i].name.substring(
					aform.elements[i].name.length-5,
					aform.elements[i].name.length) != "_unit") {
				    aform.elements[i].selectedIndex = 0;
				}
			} else if (aform.elements[i].type == 'checkbox') {
				if (aform.elements[i].name.substring(0, 8) == "dontcare") {
					aform.elements[i].checked = true;
				} else  {
					aform.elements[i].checked = false;
				} 
			}
		}				
	}
}


<!--- called by help links (rendered in java) --->
function handleHelpLink(lnk, popup) {
	leavingPage();
	if (popup) {
		lnk.target = '_blank';
		openWindow(lnk.href,'GSSpecHelp','height=500,WIDTH=750,left=100,right=100,scrollbars=yes,resizable=yes',1);
		return false;
	} else {
		return true;
	}
}

<!--- called from the sfUpdateResults onclick button --->
<!--- note that the formSubmit event is not firing. do we still need this? --->
function checkAndSubmitForm() {

	if (formIsOkayToSubmit) {
	    document.SpecSearchForm.submit()
	}

}

<!--- called by specsearch form submit (rendered in java) --->
function formSubmit(aform) {
	leavingPage();
	return true;
}

<!--- called when using a link that should disable any exit survey --->
function leavingPage() {
	return true;
}

function doClear(theText) {
	if (theText.value == "No Pref.") {
		theText.value = "";
	}
}

function doReset(theText) {
	if (theText.value == "") {
		theText.value = "No Pref.";
	}
}

function doUncheckBoxes(aDontCare, aform) {
	if (aDontCare.checked) {
		for (i=0; i < aform.elements.length; i++) {
			if (aform.elements[i] == aDontCare) {
				i++;
				while ((aform.elements[i].name.substring(0, 8) != "dontcare") && (i < aform.elements.length - 1)) {
					aform.elements[i].checked = false;
					i++;
				}
				if (i == aform.elements.length - 1) {
					aform.elements[i].checked = false;
				}
			}
		}
	}
}

function doUncheckDontCare(aCheckBox, aform) {
	if (aCheckBox.checked) { // if checking
		for (i=0; i < aform.elements.length; i++) {
			if (aform.elements[i] == aCheckBox) {
				i--;
				if (aform.elements[i].name.substring(0, 8) == "dontcare"){
					aform.elements[i].checked = false;
					break;
				} else {
					while (aform.elements[i].name.substring(0, 8) != "dontcare") {
						i--;
					}
					aform.elements[i].checked = false;
					break;
				}
				break;
			}
		}
	} else { // if unchecking, test whether all others in that group are unchecked, recheck dontcase
		allUnchecked = true;
		for (i=0; i < aform.elements.length; i++) {
			if (aform.elements[i] == aCheckBox) {
				break;
			}
		}
		while (aform.elements[i].name.substring(0, 8) != "dontcare") {
			i--;
		}
		dc = i; // save the location of the dont care
		i++;
		while ((aform.elements[i].type == 'checkbox') && (aform.elements[i].name.substring(0, 8) != "dontcare") && (i < aform.elements.length -1)) {
			if (aform.elements[i].checked) {
				allUnchecked = false;
			}
			i++;
		}
		if ((i == aform.elements.length - 1) && (aform.elements[i].checked)) {
			allUnchecked = false;
		}
		if (allUnchecked) {
			aform.elements[dc].checked = true;
		}
	}
}

// The renderSearchFormPaletteAndSupportingStuff() function is the 
// main "public" function in this file.
// It is called by the html page that wants a working Search Form palette.
function renderSearchFormPaletteAndSupportingStuff() {
	RegisterOnLoadEvent("setInitialRecaps()");
	
	// create the main search palette
	dw("<TABLE cellSpacing=0 cellPadding=0 width=\"" + m_brickletWidth + "\" border=0>\n");
	// "Narrow your choices"...
	dw("<TR valign=middle align=center>");
	dw("<TD>");
	dw("<DIV style=\"BACKGROUND: " + m_paletteTopBgColor + "\">\n");
	dw("<DIV style=\"BACKGROUND: url(/pix/sf/tm.gif) repeat-x 50% top\">\n");
	dw("<DIV style=\"BACKGROUND: url(/pix/sf/bm.gif) repeat-x 50% bottom\">\n");
	dw("<DIV style=\"BACKGROUND: url(/pix/sf/lm.gif) repeat-y left 50%\">\n");
	dw("<DIV style=\"BACKGROUND: url(/pix/sf/rm.gif) repeat-y right 50%\">\n");
	dw("<DIV style=\"BACKGROUND: url(/pix/sf/tl.gif) no-repeat left top\">\n");
	dw("<DIV style=\"BACKGROUND: url(/pix/sf/tr.gif) no-repeat right top\">\n");
	dw("<DIV style=\"BACKGROUND: url(/pix/sf/bl.gif) no-repeat left bottom\">\n");
	dw("<DIV style=\"BACKGROUND: url(/pix/sf/br.gif) no-repeat right bottom\">");
//	dw("<IMG style=\"MARGIN: 5px\" src=\"/pix/sf/");
//	dw((m_searchForm.queryId.length > 0) ? "filteryourresults.gif" : "filterthislist.gif");
//	dw("\">");
	dw("<div style=\"color: " + m_paletteTopTextColor + "; font-weight: bold; font-size: 14px; padding-top: 7px; padding-bottom: 7px;\">");
	if (m_searchForm.isServiceArea) {
		dw((m_searchForm.queryId.length > 0) ? m_paletteTopTextServicesWithQuery : m_paletteTopTextServices);
	} else {
		dw((m_searchForm.queryId.length > 0) ? m_paletteTopTextProductsWithQuery : m_paletteTopTextProducts);
	}
	dw("</div>");
	dw("</DIV></DIV></DIV></DIV></DIV></DIV></DIV></DIV></DIV>");
	dw("</TD>\n");
	dw("</TR>\n");
	
	dw("<TR valign=top><TD>\n");
	dw("<DIV style=\"BACKGROUND: " + m_paletteBgColor + "\" >\n");		// bgdiv
	dw("<img src=\"/pix/dot.gif\" id=\"searchSidebarPopupAnchorPos\" width=1 height=1 style=\"position:absolute;\">\n");
	
	// Create the palette
	renderSearchFormPalette();
	
	dw("</DIV>\n");		// bgdiv
	dw("</TD></TR>\n");
	dw("<TR><TD><IMG src=\"/pix/spacer.gif\" height=1 width=" + m_brickletWidth + "></TD></TR>\n");
	dw("</TABLE>\n");
}

// this function needs to be called in a body level element so
// that IE can get the width of the absolute elements from the 
// containing element (the body tag).
function renderSectionPopups() {
	m_marqueeMode = false;
	
	renderFormTag();
	renderStandardHiddenFields();
	
	// Create the (hidden) section popups
	renderSearchFormSectionPopups();
	dw("</FORM>");
}

function renderSectionPopupsAsMarquee() {
	m_marqueeMode = true;
	
	// Create the now-visible section popups.
	// (m_marqueeMode causes them to be visible)
	dw("<FORM NAME=\"SpecSearchForm\" style=\"margin:0px;\">");
	renderSearchFormSectionPopups();
	dw("</FORM>");
}

// full-page search form
function renderSectionPopupsAsSearchForm() {
	m_fullPageMode = true;
	m_marqueeMode = false;
	formIsOkayToSubmit = true;
	// Create the now-visible section popups.
	// (m_marqueeMode causes them to be visible)
	renderSearchFormSectionPopups();
	setInitialRecaps();
}

