var	m_searchForm = new Object();

function includeSupplierSearch(supplierId, supplierName) {
	m_searchForm.supplierId = supplierId;
	m_searchForm.supplierName = supplierName;

	if (m_searchForm.supplierId != null) {
		var supplierSection = { sectionId: "Supplier", sectionTitle: "Companies to Search", 
			sectionNote: null, sectionPopupPrompt: "Select Companies to Search:",
			prompts: new Array() };

		var supplierPrompt =  { promptId: "vid", promptType: "supplier",
			promptText: "Companies to Search:", helpLink: "/help/SupplierHelp", note: null,
			supplierId: supplierId,
			supplierName: supplierName };

		supplierSection.prompts[0] = supplierPrompt;
		m_searchForm.supplierSection = supplierSection;
	}
}


function beginSearchForm(areaId, specHelpFile, isServiceArea, queryId) {
	m_searchForm.areaId = areaId; 
	m_searchForm.specHelpFile = specHelpFile;
	m_searchForm.isServiceArea = isServiceArea;
	m_searchForm.queryId = queryId;
}

function endSearchForm() {
}

var curUnitSelector = null;


function beginUnitSelector(unitId) {
	curUnitSelector = { unitId: unitId, unitOptions: new Array(), firstPrompt: null };
	if (m_searchForm.unitSelectors == null) {
		m_searchForm.unitSelectors = new Array();
	}
	m_searchForm.unitSelectors[m_searchForm.unitSelectors.length] = curUnitSelector;
}

function endUnitSelector() {
}

function addUnitToUnitSel(unitId, unitName, isSelected) {
	curUnitSelector.unitOptions[curUnitSelector.unitOptions.length] = { unitId: unitId, unitName: unitName, 
		isSelected: isSelected || false };
}

var curInputList = null;

function beginInputList(inputListId) {
	curInputList = { inputListId: inputListId, values: new Array() };
	if (m_searchForm.inputLists == null) {
		m_searchForm.inputLists = new Array();
	}
	m_searchForm.inputLists[m_searchForm.inputLists.length] = curInputList;
}

function endInputList() {
}

function addInputListOption(value, displayText) {
	curInputList.values[curInputList.values.length] = { value: value, displayText: displayText };
}

var curPanel = null;
var curSection = null;
var curPrompt = null;

function beginPanel(panelTitle) {
	curPanel = { panelTitle: panelTitle, sections: new Array() };

	if (m_searchForm.panels == null) {
		m_searchForm.panels = new Array();
	}

	m_searchForm.panels[m_searchForm.panels.length] = curPanel;
}

function endPanel() {
}

function beginSection(sectionTitle, sectionNote, sectionId) {
	curSection = { sectionId: sectionId, sectionTitle: sectionTitle, sectionNote: sectionNote, prompts: new Array() };
	curPanel.sections[curPanel.sections.length] = curSection;
}

function endSection() {
}

function beginCheckbox(promptId, helpLink, promptText, introNote, displayDropDown) {
	curPrompt = { promptId: promptId, promptType: "checkbox", 
		promptText: promptText, helpLink: helpLink, note: "", cboptions: new Array(),
		introNote: introNote, displayDropDown: displayDropDown, anyOptionsSelected: false };

	addPrompt(curPrompt);
}

function addCheckboxOption(promptId, helpLink, promptText, note, isSelected) {
	var option = { promptId: promptId, promptType: "cboption", 
		promptText: promptText, helpLink: helpLink, note: note || "",
		isSelected: isSelected || false };

	curPrompt.cboptions[curPrompt.cboptions.length] = option;

	if (option.isSelected) {
		curPrompt.anyOptionsSelected = true;
	}
}

function endCheckbox() {
}


function addYesNo(promptId, helpLink, promptText, note, hasDefault, defaultValue) {
	curPrompt = { promptId: promptId, promptType: "yesno",
		promptText: promptText, helpLink: helpLink, note: note,
		hasDefault: hasDefault || false, defaultValue: defaultValue || false };

	addPrompt(curPrompt);
}

function addTextSearch(promptId, helpLink, label, promptText, note, defaultValue) {
	curPrompt = { promptId: promptId, promptType: "text",
		label: label, promptText: promptText, helpLink: helpLink, note: note,
		defaultValue: defaultValue };

	addPrompt(curPrompt);
}


function addAtLeast(promptId, helpLink, label, promptText, note, unitId, inputListId, fixedUnit, defaultvalue) {
	curPrompt = { promptId: promptId, promptType: "atleast", label: label,
		promptText: promptText, helpLink: helpLink, note: note,
		unitId: unitId, inputListId: inputListId, fixedUnit: fixedUnit, 
		defaultvalue: defaultvalue };

	addPrompt(curPrompt);
}

function addNoMoreThan(promptId, helpLink, label, promptText, note, unitId, inputListId, fixedUnit, defaultvalue) {
	curPrompt = { promptId: promptId, promptType: "nomorethan", label: label,
		promptText: promptText, helpLink: helpLink, note: note,
		unitId: unitId, inputListId: inputListId, fixedUnit: fixedUnit, defaultvalue: defaultvalue };

	addPrompt(curPrompt);
}

function addAtLeastNoMoreThan(promptId, helpLink, atLeastLabel, noMoreThanLabel, promptText, note, unitId, inputListId, fixedUnit, atLeastValue, noMoreThanValue) {
	curPrompt = { promptId: promptId, promptType: "atleastnomorethan", atLeastLabel: atLeastLabel,
		noMoreThanLabel: noMoreThanLabel, promptText: promptText, helpLink: helpLink,
		note: note, unitId: unitId, inputListId: inputListId, fixedUnit: fixedUnit, defaultvalue_atleast: atLeastValue,
		defaultvalue_nomorethan: noMoreThanValue }

	addPrompt(curPrompt);
}

function addFromTo(promptId, helpLink, fromLabel, toLabel, promptText, note, hasOverrange, unitId, inputListId, fixedUnit, 
				defaultvalue_from, defaultvalue_to, overrangeValue) {
	curPrompt = { promptId: promptId, promptType: "fromto", fromLabel: fromLabel,
		toLabel: toLabel, promptText: promptText, helpLink: helpLink, note: note,
		unitId: unitId, inputListId: inputListId, fixedUnit: fixedUnit, defaultvalue_from: defaultvalue_from,
		defaultvalue_to: defaultvalue_to, 
		hasOverrange: hasOverrange || false, overrangeValue: overrangeValue }

	addPrompt(curPrompt);
}

// private fn
function addPrompt(prompt) {
	prompt.section = curSection;
	curSection.prompts[curSection.prompts.length] = prompt;

	// handle numerical prompt stuff
	if ((prompt.inputListId != null) && (m_searchForm.inputLists != null)) {
		for (var i = 0; i < m_searchForm.inputLists.length; i++) {
			var inputList = m_searchForm.inputLists[i];
			if (inputList.inputListId == prompt.inputListId) {
				prompt.inputList = inputList;
			}
		}
	}

	if ((prompt.unitId != null) && (m_searchForm.unitSelectors != null)) {
		for (var i = 0; i < m_searchForm.unitSelectors.length; i++) {
			var unitSel = m_searchForm.unitSelectors[i];
			if (unitSel.unitId == prompt.unitId) {
				prompt.unitSel = unitSel;
				if (unitSel.firstPrompt == null) {
					unitSel.firstPrompt = prompt;
				}
			}
		}
	}
}
