
// Javascript File for Search Engine GUI
//
// Provides various function calls for use with the search engine GUI. This acts
// like an advanced type search. It requires certain arrays to be defined in the
// calling html source. This file is generic for any search folders.

function SE_RenderGUI(nodeContainer)
{
    this.m_nodeTopLevelParent = nodeContainer;
    
    if (this.m_nodeTopLevelParent)
    {
        
        // Create and initialise the containing table for the rows
        // of search engine selectors
        nodeContainingTable         = document.createElement('table');
        nodeContainingTable.width   = '100%';
        nodeTBODY = document.createElement('tbody');
        nodeContainingTable.appendChild(nodeTBODY);

        trNode                      = document.createElement('tr');
        tdNode                      = document.createElement('td');
        tdNode.className            = "admintablelabel";
        tdNode.style.paddingTop     = "8px";
        tdNode.appendChild(document.createTextNode('Find pictures which match '));
        combiSelect = document.createElement('select');
        combiSelect.className       = 'selectboxtext';
        combiSelect.name            = 'SE_' + this.m_strIdentifier + '_searchcombi';
        combiSelect.id              = 'SE_' + this.m_strIdentifier + '_searchcombi_id';
        combiSelect.options[combiSelect.options.length] = new Option("ALL","0",false,(this.m_nSearchCombination == 0));
        combiSelect.options[combiSelect.options.length] = new Option("ANY","1",false,(this.m_nSearchCombination == 1));

        // fix for IE
        combiSelect.selectedIndex = this.m_nSearchCombination;
        
        tdNode.appendChild(combiSelect);
        tdNode.appendChild(document.createTextNode(' of the following conditions:'));
        trNode.appendChild(tdNode);
        nodeTBODY.appendChild(trNode);
        combiSelect.selectedIndex = combiSelect.selectedIndex;

        trNode                      = document.createElement('tr');
        tdNode                      = document.createElement('td');
        tdNode.style.textAlign      = "center";
        nodeContainingTable2         = document.createElement('table');
        nodeContainingTable2.width   = '100%';
        nodeTBODY2 = document.createElement('tbody');
        nodeContainingTable2.appendChild(nodeTBODY2);
        tdNode.appendChild(nodeContainingTable2);
        trNode.appendChild(tdNode);
        nodeTBODY.appendChild(trNode);

        trNode                      = document.createElement('tr');
        tdNode                      = document.createElement('td');
        tdNode.className            = "admintablelabel";
        divNode                     = document.createElement('div');
        divNode.style.margin        = 0;
        divNode.style.padding       = 0;
        divNode.style.cssFloat      = 'left';
        divNode.style.styleFloat    = 'left'; // IE fix
        divNode.style.textAlign     = 'left';
        divNode.style.width         = '45%';
        addAnchor                   = document.createElement('a');
        addAnchor.href              = '#';
        addAnchor.onclick           = new Function (this.m_strObjectName + '.AddARow(); return false;' );
        addAnchor.appendChild(document.createTextNode('Add another condition...'));
        divNode.appendChild(addAnchor);
        tdNode.appendChild(divNode);
        if (this.m_strGeneralSearchLink != "" || this.m_strSearchTipsLink != "")
        {
            divNode                     = document.createElement('div');
            divNode.style.margin        = 0;
            divNode.style.padding       = 0;
            divNode.style.cssFloat      = 'right';
            divNode.style.styleFloat    = 'right'; // IE fix
            divNode.style.textAlign     = 'right';
            divNode.style.width         = '45%';
            smallNode                   = document.createElement('small');
            divNode.appendChild(smallNode);
            if (this.m_strGeneralSearchLink != "")
            {
                addAnchor                   = document.createElement('a');
                addAnchor.href              = this.m_strGeneralSearchLink;
                addAnchor.appendChild(document.createTextNode('simple search'));
                smallNode.appendChild(addAnchor);
            }
            if (this.m_strGeneralSearchLink != "" && this.m_strSearchTipsLink != "")
            {
                breakText                   = document.createTextNode(' | ');
                smallNode.appendChild(breakText);
            }
            if (this.m_strSearchTipsLink != "")
            {
                addAnchor                   = document.createElement('a');
                addAnchor.href              = this.m_strSearchTipsLink;
                addAnchor.appendChild(document.createTextNode('help'));
                smallNode.appendChild(addAnchor);
            }
            tdNode.appendChild(divNode);
        }
        
        
        trNode.appendChild(tdNode);
        nodeTBODY.appendChild(trNode);

        if (this.m_bSearchBoxEnabled)
        {
            trNode                      = document.createElement('tr');
            tdNode                      = document.createElement('td');
            tdNode.className            = "admintablelabel";
            tdNode.style.paddingTop     = "8px";
            tdNode.style.textAlign      = this.m_strSearchButtonAlign;
            inputNode                   = document.createElement('input');
            inputNode.type              = "button";
            inputNode.className         = "imsbutton";
            inputNode.value             = "   SEARCH   ";
            inputNode.onclick           = new Function (this.m_strObjectName + '.SubmitForm();' );
            tdNode.appendChild(inputNode);
            trNode.appendChild(tdNode);
            nodeTBODY.appendChild(trNode);
        }

        
        if (this.m_strFormSubmitLocation != "")
        {
            // Create and initialise the form which will be used to send the SF data
            this.m_nodeForm = document.createElement('form');
            this.m_nodeForm.method      = 'POST';
            this.m_nodeForm.action      = this.m_strFormSubmitLocation;
            this.m_nodeForm.name        = 'SE_' + this.m_strIdentifier + '_form';
            this.m_nodeForm.id          = 'SE_' + this.m_strIdentifier + '_form_id';
            this.m_nodeTopLevelParent.appendChild(this.m_nodeForm);
        }

        this.m_nodeTopLevelParent.appendChild(nodeContainingTable);
        this.m_nodeSelectorsParent = nodeTBODY2;
        
        this.AddARow();
    }
}

function SE_CreateARow(nIndex, bRemovable)
{
    /**
     * To create a new row we need to create 2 select boxes and a widget for entering a search value.
     * The information for each select box comes from internal arrays provided in the constructor.
     * The search value widget will usually be a text box but may also be a date entry widgets or other.
     */
    
    // Create and initialise a select box for the fields we can choose
    var fieldSelect = document.createElement('select');
    fieldSelect.className       = 'selectboxtext';
    fieldSelect.style.width     = '98%';
    fieldSelect.name            = 'SE_' + this.m_strIdentifier + '_field' + nIndex;
    fieldSelect.id              = 'SE_' + this.m_strIdentifier + '_field_id' + nIndex;
    fieldSelect.onchange        = new Function (this.m_strObjectName + '.PopulateConditions(' + nIndex + ');' );

    // Now add fields to the select box according to our internal array
    var strPreviousGroupName = "";
    var optGroup = null;
    var selectOption = null;
    for (i=0; i < this.m_arrFields.length; i++)
    {
        if (this.m_arrFields[i][6] != strPreviousGroupName)
        {
            if (this.m_arrFields[i][6])
            {
                optGroup = document.createElement('optgroup');
                optGroup.label = this.m_arrFields[i][6];
                fieldSelect.appendChild(optGroup);
            }
            else
            {
                optGroup = null;
            }
        }
        selectOption = document.createElement('option');
        selectOption.value = this.m_arrFields[i][0];
        selectOption.appendChild(document.createTextNode(this.m_arrFields[i][1]));
        
        if (optGroup)
        {
            optGroup.appendChild(selectOption);
        }
        else
        {
            fieldSelect.appendChild(selectOption);
        }
        strPreviousGroupName = this.m_arrFields[i][6];
    }
    fieldSelect.selectedIndex = 0;

    
    
    // Create and initialise a select box for the conditions we can apply to the fields
    actionSelect = document.createElement('select');
    actionSelect.className      = 'selectboxtext';
    actionSelect.style.width    = '98%';
    actionSelect.name           = 'SE_' + this.m_strIdentifier + '_cond' + nIndex;
    actionSelect.id             = 'SE_' + this.m_strIdentifier + '_cond_id' + nIndex;
    actionSelect.onchange       = new Function (this.m_strObjectName + '.DisplayAppropriateInputType(' + nIndex + ');' );




    // Create and intialise a text box for the search values
    wordsInput = document.createElement('input');
    wordsInput.type             = 'text';
    wordsInput.style.width      = '98%';
    wordsInput.name             = 'SE_' + this.m_strIdentifier + '_text' + nIndex;
    wordsInput.id               = 'SE_' + this.m_strIdentifier + '_text_id' + nIndex;
    wordsInput.onkeypress       = new Function ('e', 'return ' + this.m_strObjectName + '.CheckEnter(e);');

    // Create and intialise the link used to remove a line
    removeAnchor = document.createElement('a');
    removeAnchor.href = '#';
    removeAnchor.onclick = new Function (this.m_strObjectName + '.RemoveARow(' + nIndex + '); return false;' );
    removeAnchor.appendChild(document.createTextNode('remove'));

    nowDate = new Date();
    daySelect = document.createElement('select');
    daySelect.className         = 'selectboxtext';
    daySelect.name              = 'SE_' + this.m_strIdentifier + '_day' + nIndex;
    daySelect.id                = 'SE_' + this.m_strIdentifier + '_day_id' + nIndex;
    for (i=1;i<=31;i++)
    {
    	daySelect.options[daySelect.options.length] = new Option(i,i,false,(i == nowDate.getDate()));
    }
    monthSelect = document.createElement('select');
    monthSelect.className       = 'selectboxtext';
    monthSelect.name            = 'SE_' + this.m_strIdentifier + '_month' + nIndex;
    monthSelect.id              = 'SE_' + this.m_strIdentifier + '_month_id' + nIndex;
    monthNames = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
    for (i=0;i<=11;i++)
    {
    	monthSelect.options[monthSelect.options.length] = new Option(monthNames[i],i,false,(i == nowDate.getMonth()));
    }
    yearSelect = document.createElement('select');
    yearSelect.className        = 'selectboxtext';
    yearSelect.name             = 'SE_' + this.m_strIdentifier + '_year' + nIndex;
    yearSelect.id               = 'SE_' + this.m_strIdentifier + '_year_id' + nIndex;
    endYear = nowDate.getFullYear() + 1;
    for (i= this.yearBase; i<=endYear; i++)
    {
    	yearSelect.options[yearSelect.options.length] = new Option(i,i,false,(i == nowDate.getFullYear()));
    }

    choiceSelect = document.createElement('select');
    choiceSelect.className        = 'selectboxtext';
    choiceSelect.name             = 'SE_' + this.m_strIdentifier + '_choiceselect' + nIndex;
    choiceSelect.id               = 'SE_' + this.m_strIdentifier + '_choiceselect_id' + nIndex;

    taxonomyDiv = document.createElement('div');
    taxonomyDiv.id               = 'SE_' + this.m_strIdentifier + '_taxonomydiv_id' + nIndex;
    taxonomySpan = document.createElement('span');
    taxonomySpan.id               = 'SE_' + this.m_strIdentifier + '_taxonomyspan_id' + nIndex;

    
    theTD1 = document.createElement('td');
    theTD1.className='admintablelabel';
    theTD1.style.verticalAlign='middle';
    theTD1.width = '10%';
    theTD1.height='25';
    if (bRemovable)
    {
        theTD1.appendChild(removeAnchor);
    }

    theTD2 = document.createElement('td');
    theTD2.className='admintableinput';
    theTD2.style.verticalAlign='middle';
    theTD2.width = '22%';
    theTD2.appendChild(fieldSelect);

    theTD3 = document.createElement('td');
    theTD3.className='admintableinput';
    theTD3.style.verticalAlign='middle';
    theTD3.width = '28%';
    theTD3.appendChild(actionSelect);

    theTD4 = document.createElement('td');
    theTD4.className='admintableinput';
    theTD4.style.verticalAlign='middle';
    theTD4.width = '40%';
    theTD4.id='SE_' + this.m_strIdentifier + '_simpletext_id' + nIndex;
    theTD4.appendChild(wordsInput);

    theTD5 = document.createElement('td');
    theTD5.className='admintableinput';
    theTD5.style.verticalAlign='middle';
    theTD5.width = '40%';
    theTD5.style.display='none';
    theTD5.id='SE_' + this.m_strIdentifier + '_date_id' + nIndex;
    theTD5.appendChild(daySelect);
    theTD5.appendChild(monthSelect);
    theTD5.appendChild(yearSelect);

    theTD6 = document.createElement('td');
    theTD6.className='admintableinput';
    theTD6.style.verticalAlign='middle';
    theTD6.width = '40%';
    theTD6.style.display='none';
    theTD6.id='SE_' + this.m_strIdentifier + '_choice_id' + nIndex;
    theTD6.appendChild(choiceSelect);

    theTD7 = document.createElement('td');
    theTD7.className='admintableinput';
    theTD7.style.verticalAlign='middle';
    theTD7.width = '40%';
    theTD7.style.display='none';
    theTD7.id='SE_' + this.m_strIdentifier + '_taxonomy_id' + nIndex;
    theTD7.appendChild(taxonomyDiv);
    theTD7.appendChild(taxonomySpan);

    theTD8 = document.createElement('td');
    theTD8.className='admintableinput';
    theTD8.style.verticalAlign='middle';
    theTD8.width = '40%';
    theTD8.id='SE_' + this.m_strIdentifier + '_blank_id' + nIndex;

    theTR = document.createElement('tr');
    theTR.id = 'SE_' + this.m_strIdentifier + '_row_id' + nIndex;
    theTR.appendChild(theTD1);
    theTR.appendChild(theTD2);
    theTR.appendChild(theTD3);
    theTR.appendChild(theTD4);
    theTR.appendChild(theTD5);
    theTR.appendChild(theTD6);
    theTR.appendChild(theTD7);
    theTR.appendChild(theTD8);

    return theTR;
}

function SE_CheckEnter(e)
{
	//e is event object passed from function invocation
	var characterCode;// literal character code will be stored in this variable

	if(e && e.which)
	{ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else
	{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13)
	{ //if generated character code is equal to ascii 13 (if enter key)
		this.SubmitForm(); //submit the form
		return false;
	}
	else
	{
		return true;
	}
}

function SE_RemoveARow(nIndex)
{
    if (this.m_nNumberOfRows > 1)
    {
        nodeRowToRemove = document.getElementById('SE_' + this.m_strIdentifier + '_row_id' + nIndex);
        nodeRowToRemove.parentNode.removeChild(nodeRowToRemove);
        this.m_nNumberOfRows--;
    }
}

function SE_AddARow()
{
    nodeNewRow = this.CreateARow(this.m_nNextRowIndex, (this.m_nNumberOfRows != 0));
    this.m_nodeSelectorsParent.appendChild(nodeNewRow);
    this.PopulateConditions(this.m_nNextRowIndex);
    this.m_nNextRowIndex++;
    this.m_nNumberOfRows++;
}

function SE_PrePopulateRow(strFieldID, strConditionID, strValue, bCreateRowFirst)
{
    var nThisFieldIndex = -1;
    var nThisConditionIndex = -1;
    for (index = (this.m_arrFields.length - 1); index >= 0; --index)
    {
        if (this.m_arrFields[index][0] == strFieldID)
        {
            nThisFieldIndex = index;
            for (index2 = (this.m_arrFields[nThisFieldIndex][2].length - 1); index2 >= 0; --index2)
            {
                if (this.m_arrFields[nThisFieldIndex][2][index2][1] == strConditionID)
                {
                    nThisConditionIndex = index2;
                    break;
                }
            }
            break;
        }
    }

    if ((nThisFieldIndex >= 0) && (nThisConditionIndex >= 0))
    {
        if (bCreateRowFirst)
        {
            this.AddARow();
        }
        i = this.m_nNextRowIndex - 1;
        selectFields        = document.getElementById('SE_' + this.m_strIdentifier + '_field_id' + i);
        selectConditions    = document.getElementById('SE_' + this.m_strIdentifier + '_cond_id' + i);
        if (selectFields && selectConditions)
        {
            selectFields.selectedIndex      = nThisFieldIndex;
            this.PopulateConditions(i);
            selectConditions.selectedIndex  = nThisConditionIndex;
            this.DisplayAppropriateInputType(i);
            nFieldType = this.m_arrFields[nThisFieldIndex][3];
            // if field type is 5 then the field type is determined from the selected condition
            if (nFieldType == 5)
            {
                nFieldType = arrConditions[selectConditions.selectedIndex][2];
            }
            if (nFieldType == 0) // simple text
            {
                inputText           = document.getElementById('SE_' + this.m_strIdentifier + '_text_id' + i);
                inputText.value     = strValue;
            }
            else if (nFieldType == 1) // date
            {
                thisDate = new Date(1000 * strValue);
                selectDay           = document.getElementById('SE_' + this.m_strIdentifier + '_day_id' + i);
                selectDay.selectedIndex = thisDate.getDate() - 1;
                selectMonth         = document.getElementById('SE_' + this.m_strIdentifier + '_month_id' + i);
                selectMonth.selectedIndex = thisDate.getMonth();
                selectYear          = document.getElementById('SE_' + this.m_strIdentifier + '_year_id' + i);
                selectYear.selectedIndex = thisDate.getFullYear() - this.yearBase;
            }
            else if (nFieldType == 2) // choice
            {
                var selectChoices       = document.getElementById('SE_' + this.m_strIdentifier + '_choiceselect_id' + i);
                // loop through these choices till we find the right one
                for (var iChoice = (selectChoices.options.length - 1); iChoice >= 0; --iChoice)
                {
                    if (selectChoices.options[iChoice].value == strValue)
                    {
                        selectChoices.selectedIndex = iChoice;
                        break;
                    }
                }
            }
            else if (nFieldType == 3) // taxonomy
            {
                var taxonomyPicker = ALBUM_SELECTORS['SE_' + this.m_strIdentifier + '_taxonomy_selector' + i];
                if (taxonomyPicker)
                {
                    if (taxonomyPicker.m_Rendered)
                    {
                        taxonomyPicker.OnAlbumSelect(strValue);
                    }
                    else
                    {
                        taxonomyPicker.SelectAlbumOnRender(strValue);
                    }
                }
            }
            else if (nFieldType == 4) // blank
            {
            }
        }
    }
}

function SE_PopulateConditions(nIndex)
{
    // Get select boxes for row in question
    selectFields        = document.getElementById('SE_' + this.m_strIdentifier + '_field_id' + nIndex);
    selectConditions    = document.getElementById('SE_' + this.m_strIdentifier + '_cond_id' + nIndex);

    if (selectFields && selectConditions)
    {
        // Find which field has been selected and grab conditions from internal array
        nFieldIndex = selectFields.selectedIndex;
        arrConditions = this.m_arrFields[nFieldIndex][2];
        
        // Reset conditions select box
        selectConditions.options.length = 0;
        // Loop through conditions and populate select box
        for (ii = 0; ii < arrConditions.length; ii++)
        {
            selectConditions.options[selectConditions.options.length] = new Option(arrConditions[ii][0],arrConditions[ii][1],false,false);
        }
        
        // show the relevant input type - text box, date widgets, etc
        this.DisplayAppropriateInputType(nIndex);

        // now do something special for certain field types
        var nFieldType = this.m_arrFields[nFieldIndex][3];
        // if field type is 5 then the field type is determined from the selected condition
        if (nFieldType == 5)
        {
            nFieldType = arrConditions[selectConditions.selectedIndex][2];
        }
		this.UpdateChoices(nIndex, nFieldType);
    }
}

function SE_DisplayAppropriateInputType(nIndex)
{
    // Get select boxes for row in question
    var selectFields        = document.getElementById('SE_' + this.m_strIdentifier + '_field_id' + nIndex);
    var selectConditions    = document.getElementById('SE_' + this.m_strIdentifier + '_cond_id' + nIndex);
    if (selectFields && selectConditions)
    {
        // Find which field has been selected and determine its type
        var nFieldIndex = selectFields.selectedIndex;
        var nFieldType = this.m_arrFields[nFieldIndex][3];
        // if field type is 5 then the field type is determined from the selected condition
        if (nFieldType == 5)
        {
            var arrConditions = this.m_arrFields[nFieldIndex][2];
            var selectedConditionIndex = selectConditions.selectedIndex;
            var nFieldType = arrConditions[selectedConditionIndex][2];
            
			this.UpdateChoices(nIndex, nFieldType);
        }
        
        document.getElementById('SE_' + this.m_strIdentifier + '_simpletext_id' + nIndex).style.display = ( nFieldType == 0 ? '' : 'none' ); // simple text
        document.getElementById('SE_' + this.m_strIdentifier + '_date_id' + nIndex).style.display = ( nFieldType == 1 ? '' : 'none' ); // date
        document.getElementById('SE_' + this.m_strIdentifier + '_choice_id' + nIndex).style.display = ( nFieldType == 2 ? '' : 'none' ); // choice
        document.getElementById('SE_' + this.m_strIdentifier + '_taxonomy_id' + nIndex).style.display = ( nFieldType == 3 ? '' : 'none' ); // taxonomy
        document.getElementById('SE_' + this.m_strIdentifier + '_blank_id' + nIndex).style.display = ( nFieldType == 4 ? '' : 'none' ); // blank
    }
}

function SE_RegisterField(strFieldDescription, strFieldID, arrConditions, nType, mixedData, nPanel, strPanelName)
{
    if ((strFieldDescription != "") && (strFieldID != "") && (arrConditions.length > 0))
    {
        if  (
                (nType == 0) || 
                (nType == 1) || 
                ((nType == 2) && (mixedData.length > 0)) || 
                ((nType == 3) && (mixedData != "")) || 
                (nType == 4) || 
                (nType == 5)
            )
        {
            this.m_arrFields.push([strFieldID, strFieldDescription, arrConditions, nType, mixedData, nPanel, strPanelName]);
        }
    }
}

function SE_UpdateChoices(nIndex, nFieldType)
{
	if (nFieldType == 2) // choice
	{
		// In the case of a choice field we need to populate the choice drop down with relevant choices
		selectChoice = document.getElementById('SE_' + this.m_strIdentifier + '_choiceselect_id' + nIndex);
		selectChoice.options.length = 0;
		var arrChoices = this.m_arrFields[nFieldIndex][4];
		for (var jj = 0; jj < arrChoices.length; ++jj)
		{
			selectChoice.options[selectChoice.options.length] = new Option(arrChoices[jj][1],arrChoices[jj][0],false,false);
		}
	}
	else if (nFieldType == 3) // taxonomy
	{
		// In the case of a taxonomy field we need to reset the taxonomy chooser menu thing
		if (!ALBUM_SELECTORS['SE_' + this.m_strIdentifier + '_taxonomy_selector' + nIndex])
		{
			var albumSelectFunction = function(taxonomyPicker, nSelectedID)
			{
				var strAlbumPath = taxonomyPicker.GetAlbumPathFromAlbumID(nSelectedID);
				while (taxonomyPicker.m_Container.childNodes.length > 1)
				{
					taxonomyPicker.m_Container.removeChild(taxonomyPicker.m_Container.lastChild);
				}
				taxonomyPicker.m_Container.appendChild(document.createTextNode(strAlbumPath));
				var hiddenNode = document.createElement("input");
				hiddenNode.type = "hidden";
				hiddenNode.id = taxonomyPicker.m_Identifier + "selectedcategory";
				hiddenNode.name = taxonomyPicker.m_Identifier + "selectedcategory";
				hiddenNode.value = nSelectedID;
				taxonomyPicker.m_Container.appendChild(hiddenNode);
			}
			obj = new AlbumSelector(document.getElementById('SE_' + this.m_strIdentifier + '_taxonomydiv_id' + nIndex), albumSelectFunction, 'SE_' + this.m_strIdentifier + '_taxonomy_selector' + nIndex);
		}
		ALBUM_SELECTORS['SE_' + this.m_strIdentifier + '_taxonomy_selector' + nIndex].SetPathToTaxonomy(this.m_arrFields[nFieldIndex][4].strTaxonomyPath);
		ALBUM_SELECTORS['SE_' + this.m_strIdentifier + '_taxonomy_selector' + nIndex].SetSelectorStrings(this.m_arrFields[nFieldIndex][4]);
		ALBUM_SELECTORS['SE_' + this.m_strIdentifier + '_taxonomy_selector' + nIndex].Render();
	}
}

function SE_GatherFormData()
{
    var arrFormData = new Array();
    
    iNumValidRows = 0;
    for (i = 0; i < this.m_nNextRowIndex; i++)
    {
        if (document.getElementById('SE_' + this.m_strIdentifier + '_field_id' + i))
        {
            selectFields        = document.getElementById('SE_' + this.m_strIdentifier + '_field_id' + i);
            selectConditions    = document.getElementById('SE_' + this.m_strIdentifier + '_cond_id' + i);
            var nFieldType = this.m_arrFields[selectFields.selectedIndex][3];
            // if field type is 5 then the field type is determined from the selected condition
            if (nFieldType == 5)
            {
                var arrConditions = this.m_arrFields[selectFields.selectedIndex][2];
                nFieldType = arrConditions[selectConditions.selectedIndex][2];
            }
            var strValue = '';
            if (nFieldType == 0) // simple text
            {
                inputText       = document.getElementById('SE_' + this.m_strIdentifier + '_text_id' + i);
                strValue        = inputText.value;
            }
            else if (nFieldType == 1) // date
            {
                selectDay       = document.getElementById('SE_' + this.m_strIdentifier + '_day_id' + i);
                thisDay         = selectDay.options[selectDay.selectedIndex].value;
                selectMonth     = document.getElementById('SE_' + this.m_strIdentifier + '_month_id' + i);
                thisMonth       = selectMonth.options[selectMonth.selectedIndex].value;
                selectYear      = document.getElementById('SE_' + this.m_strIdentifier + '_year_id' + i);
                thisYear        = selectYear.options[selectYear.selectedIndex].value;
                newDate         = new Date(thisYear, thisMonth, thisDay);
                thisDate        = newDate.getTime();
                strValue        = Math.round(thisDate / 1000);
            }
            else if (nFieldType == 2) // choice
            {
                selectChoices   = document.getElementById('SE_' + this.m_strIdentifier + '_choiceselect_id' + i);
                strValue        = selectChoices.options[selectChoices.selectedIndex].value;
            }
            else if (nFieldType == 3) // taxonomy
            {
                hiddenInput     = document.getElementById('SE_' + this.m_strIdentifier + '_taxonomy_selector' + i + 'selectedcategory');
                if (hiddenInput)
                {
                    strValue    = hiddenInput.value;
                }
            }
            else if (nFieldType == 4) // blank
            {
                strValue        = "";
            }
            if (selectFields && 
                selectConditions && 
                ((strValue != "") || (nFieldType == 4)) )
            {
                arrFormData.push(['SE_' + this.m_strIdentifier + '_field' + iNumValidRows, selectFields.options[selectFields.selectedIndex].value]);
                arrFormData.push(['SE_' + this.m_strIdentifier + '_cond' + iNumValidRows, selectConditions.options[selectConditions.selectedIndex].value]);
                arrFormData.push(['SE_' + this.m_strIdentifier + '_value' + iNumValidRows, strValue]);
                
                iNumValidRows++;
            }
        }
    }
    
    arrFormData.push(['SE_' + this.m_strIdentifier + '_numberofparameters', iNumValidRows]);
    selectCombi = document.getElementById('SE_' + this.m_strIdentifier + '_searchcombi_id');
    arrFormData.push(['SE_' + this.m_strIdentifier + '_searchcombi', selectCombi.options[selectCombi.selectedIndex].value]);
    
    return arrFormData;
    
}

function SE_SubmitForm()
{
    if (this.m_nodeForm)
    {
        var arrFormData = this.GatherFormData();
        var iData = 0;
        for (iData = (arrFormData.length - 1); iData >= 0; --iData)
        {
            elementHidden = document.createElement('input');
            elementHidden.type      = 'hidden';
            elementHidden.name      = arrFormData[iData][0];
            elementHidden.value     = arrFormData[iData][1];
            this.m_nodeForm.appendChild(elementHidden);
        }
    
        this.m_nodeForm.submit();
    }
}

function SE_DisableSearchBox()
{
    this.m_bSearchBoxEnabled = false;
}

function SearchEngine(strIdentifier, strObjectName, strFormSubmitLocation, nSearchCombination)
{
    this.m_strIdentifier = strIdentifier;
    this.m_strObjectName = strObjectName;
    this.m_strFormSubmitLocation = strFormSubmitLocation;
    this.m_nSearchCombination = nSearchCombination;
    this.m_nNextRowIndex = 0;
    this.m_nNumberOfRows = 0;
    this.yearBase = 2002;
    this.m_bSearchBoxEnabled = true;
    this.m_nodeForm = null;
    this.m_strSearchTipsLink = "";
    this.m_strGeneralSearchLink = "";
    this.m_strSearchButtonAlign = "center";
    
    this.m_arrFields = new Array();
    
    this.PrePopulateRow     = SE_PrePopulateRow;
    this.RenderGUI          = SE_RenderGUI;
    this.CreateARow         = SE_CreateARow;
    this.RemoveARow         = SE_RemoveARow;
    this.AddARow            = SE_AddARow;
    this.PopulateConditions = SE_PopulateConditions;
    this.DisplayAppropriateInputType = SE_DisplayAppropriateInputType;
    this.RegisterField      = SE_RegisterField;
    this.GatherFormData     = SE_GatherFormData;
    this.SubmitForm         = SE_SubmitForm;
    this.CheckEnter         = SE_CheckEnter;
    this.DisableSearchBox   = SE_DisableSearchBox;
    this.UpdateChoices      = SE_UpdateChoices;
}

