var _globalSearchSuggestInited = false
var _shopSearchSuggestInited = true

var globalKeywordLookup = null
var shopKeywordLookup = null

function getSuggestData(str) {	
	// which method to use
	var theMethod = "select"
	// 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.shopSearch = (selectedSuggestObject.name == 'shopKeywordLookup')
	// set loading image
	//DWRUtil.useLoadingImage(_webfolder + "gfx/ajax/loading5.gif")
	// 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'].btnSubmit.click()", 50)
}

function shopKeywordLookupSelectionListener(result) {
	var oForm = document.forms['shopSearch']
	//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['shopSearch'].btnSubmit.click()", 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 initShopSearchSuggest() {
	// initialize a new suggest component
	shopKeywordLookup = new Suggest()
	shopKeywordLookup.minCharToStartSearch = 2
	shopKeywordLookup.InitQueryCode("shopKeywordLookup", "searchkeyword")
	shopKeywordLookup.setSelectionListener(shopKeywordLookupSelectionListener)
	// set inited flag
	_shopSearchSuggestInited = true
}

// validate searchForm
function checkSearchForm(oForm) {
 	if ( (oForm.name == 'globalSearch' && oForm.searchkeyword.value == '') || (oForm.name == 'shopSearch' && oForm.extrasearchkeyword.value == '') ) {
		oForm.criteria_id.value = ''
		alert('voer eerst een trefwoord in')		
		return false
	} else {
		return true
	}
}
