var _globalSearchSuggestInited = false
var _productSearchSuggestInited = true
var _pakketSearchSuggestInited = true
var _leverancierSearchSuggestInited = true

var globalKeywordLookup = null
var productKeywordLookup = null
var pakketKeywordLookup = null
var leverancierKeywordLookup = null

function getSuggestData(str) {	
	// saves the string inside the suggest object
	window[selectedSuggestObject.name].setSearchString(str)
	// init data to send so CF
	var theData = new Object()
	theData.impl_name = _impl_name
	theData.criteria = window[selectedSuggestObject.name].searchString
	theData.productSearch = (selectedSuggestObject.name == 'productKeywordLookup')
	theData.pakketSearch = (selectedSuggestObject.name == 'pakketKeywordLookup')
	theData.leverancierSearch = (selectedSuggestObject.name == 'leverancierKeywordLookup')
	theData.website_id = _website_id

	if(theData.productSearch){
		// which method to use
		var theMethod = "selectPakkettenLeveranciers"
	}else if(theData.pakketSearch){
		// which method to use
		var theMethod = "selectPakketten"
	}else if(theData.leverancierSearch){
		// which method to use
		var theMethod = "selectLeveranciers"
	}else{
		// which method to use
		var theMethod = "select"
	}

	// do ajax request
	DWREngine._execute(_webfolder + "impls/" + _impl_name + "/framework/ajax/suggest.cfc", null, theMethod, theData, getSuggestDataResult)	
}

// call back function
function getSuggestDataResult(r) {
	// show query div
	window[selectedSuggestObject.name].showQueryDiv(r, "id", "title", "type")
}

function globalKeywordLookupSelectionListener(result) {
	var oForm = document.forms['globalSearch']
	//this function to sets the hidden field with key value.
	oForm.type.value = result.TYPE
	oForm.criteria_id.value = result.KEY
	// delayed click
	setTimeout("document.forms['globalSearch'].searchBtn.click()", 50)
}

function productKeywordLookupSelectionListener(result) {
	var oForm = document.forms['productSearch']
	//this function to sets the hidden field with key value.
	oForm.type.value = result.TYPE
	oForm.criteria_id.value = result.KEY
	// delayed click
	setTimeout("document.forms['productSearch'].submit()", 50)
}

function leverancierKeywordLookupSelectionListener(result) {
	var oForm = document.forms['leverancierSearch']
	//this function to sets the hidden field with key value.
	oForm.type.value = result.TYPE
	oForm.criteria_id.value = result.KEY
	// delayed click
	setTimeout("document.forms['leverancierSearch'].submit()", 50)
}

function pakketKeywordLookupSelectionListener(result) {
	var oForm = document.forms['pakketSearch']
	//this function to sets the hidden field with key value.
	oForm.type.value = result.TYPE
	oForm.criteria_id.value = result.KEY
	// delayed click
	setTimeout("document.forms['pakketSearch'].submit()", 50)
}

function submitOnEnter(e, oForm) { //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)
		oForm.btnSubmit.click() //submit the form
	}
}

function initGlobalSearchSuggest() {
	// initialize a new suggest component
	globalKeywordLookup = new Suggest()
	globalKeywordLookup.minCharToStartSearch = 2
	globalKeywordLookup.InitQueryCode("globalKeywordLookup", "searchkeyword")
	globalKeywordLookup.setSelectionListener(globalKeywordLookupSelectionListener)
	// set inited flag
	_globalSearchSuggestInited = true	
}

function initproductSearchSuggest() {
	// initialize a new suggest component
	productKeywordLookup = new Suggest()
	productKeywordLookup.minCharToStartSearch = 2
	productKeywordLookup.InitQueryCode("productKeywordLookup", "extrasearchkeyword")
	productKeywordLookup.setSelectionListener(productKeywordLookupSelectionListener)
	// set inited flag
	_productSearchSuggestInited = true
}

function initleverancierSearchSuggest() {
	// initialize a new suggest component
	leverancierKeywordLookup = new Suggest()
	leverancierKeywordLookup.minCharToStartSearch = 2
	leverancierKeywordLookup.InitQueryCode("leverancierKeywordLookup", "extrasearchkeyword")
	leverancierKeywordLookup.setSelectionListener(leverancierKeywordLookupSelectionListener)
	// set inited flag
	_leverancierSearchSuggestInited = true
}

function initpakketSearchSuggest() {
	// initialize a new suggest component
	pakketKeywordLookup = new Suggest()
	pakketKeywordLookup.minCharToStartSearch = 2
	pakketKeywordLookup.InitQueryCode("pakketKeywordLookup", "extrasearchkeyword")
	pakketKeywordLookup.setSelectionListener(pakketKeywordLookupSelectionListener)
	// set inited flag
	_pakketSearchSuggestInited = true
}

// validate searchForm
function checkSearchForm(oForm) {
	
 	if ( (oForm.name == 'globalSearch' && oForm.searchkeyword.value == '') || (oForm.name == 'productSearch' && oForm.extrasearchkeyword.value == '') ) {
		oForm.criteria_id.value = ''
		alert('voer eerst een trefwoord in')		
		return false
	} else {
		return true
	}
}
