/**
* Instant Search module v1.4
*
* @copyright Nguyen Manh Cuong
*       Author`s email   : cuongnm@designformambo.com
*       Author`s hompage : http://designformambo.com
*
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
**/

var instant_search_ajax_engine = new d4m_ajax_engine();
instant_search_ajax_engine.setPersistent(persistent);
instant_search_ajax_engine.setResponseUpdate('function');
if (loading_status > 0) {
	instant_search_ajax_engine.setLoadingText(loading_text);
	instant_search_ajax_engine.setLoadingStatus(true);
}

function call_dosearch(keyword, limit, phrase, ordering, min_chars, result_length, enable_sef) {
	if (typeof document.instantSearchForm.ordering != 'undefined') {
		ordering = document.instantSearchForm.ordering.options[document.instantSearchForm.ordering.selectedIndex].value;
	}
	if (document.getElementById('instant_search_close'))
		document.getElementById('instant_search_close').src = 'modules/mod_d4m_instantsearch/images/loadingcircle.gif';
	search_no++;
	instant_search_ajax_engine.sendRequest(instant_search_backend_script, response_dosearch, 'func=doSearch', 'keyword='+keyword, 'limit='+limit, 'phrase='+phrase, 'ordering='+ordering, 'min_chars='+min_chars, 'result_length='+result_length, 'enable_sef='+enable_sef, 'search_no='+search_no);
}

function response_dosearch(result) {
	var show_result = true;
	if (final_result == true && result.getElementsByTagName('ajaxResponse').item(0).getAttribute('search_no') < search_no) {
		show_result = false;
	}
	if (final_result == false && result.getElementsByTagName('ajaxResponse').item(0).getAttribute('search_no') < displaying_search_no) {
		show_result = false;
	}
	if (show_result == true) {
		displaying_search_no = result.getElementsByTagName('ajaxResponse').item(0).getAttribute('search_no');
		var search_results = '';
		var k = 0;
		var this_result = '';
		search_results += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
		search_results += '<tr><td class="sectiontableheader" align="center">';
		search_results += _PROMPT_KEYWORD + ' &quot;' + result.getElementsByTagName('ajaxResponse').item(0).getAttribute('keyword') + '&quot; ';
		search_results += _SEARCH_MATCHES.replace('%d', result.getElementsByTagName('ajaxResponse').item(0).getAttribute('found'));
		search_results += '<br/></td></tr></table><br/>';
		if (result.getElementsByTagName('ajaxResponse').item(0).getAttribute('found') != '0') {
			if (display == 'table') {
				search_results += '<table class="contentpaneopen" width="100%">';
			}
			for (var i = 0; i < result.getElementsByTagName('result').length; i++) {
				this_result = result.getElementsByTagName('result').item(i);
				if (display == 'table') {
					search_results += '<tr class="sectiontableentry' + String(k + 1) + '">';
					search_results += '<td><span class="small">' + String(i + 1) + '. </span>';
					search_results += '<a href="' + this_result.getElementsByTagName('url').item(0).firstChild.data + '"' + (this_result.getElementsByTagName('url').item(0).getAttribute('target') == '1' ? ' target="_blank"' : '') + '>';
					search_results += this_result.getElementsByTagName('title').item(0).firstChild.data + '</a>';
					search_results += (this_result.getElementsByTagName('section').item(0).firstChild.data != '' ? ('&nbsp;<span class="small">(' + this_result.getElementsByTagName('section').item(0).firstChild.data + ')</span>') : '') + '</td></tr>';
					search_results += '<tr class="sectiontableentry' + (k + 1) + '"><td>';
					search_results += this_result.getElementsByTagName('text').item(0).firstChild.data + '</td></tr>';
					search_results += (this_result.getElementsByTagName('created').item(0).firstChild.data != '-' ? ('<tr><td class="small">' + this_result.getElementsByTagName('created').item(0).firstChild.data + '</td></tr>') : '');
					search_results += (result.getElementsByTagName('result').length == (i + 1) ? '' : '<tr><td>&nbsp;</td></tr>');
					k = 1 - k;
				} else { // display results in field style
					search_results += '<fieldset style="text-align: left"><div><span class="small">' + (i + 1) + '. </span>';
					search_results += '<a href="' + this_result.getElementsByTagName('url').item(0).firstChild.data + '"' + (this_result.getElementsByTagName('url').item(0).getAttribute('target') == '1' ? ' target="_blank"' : '') + '>';
					search_results += this_result.getElementsByTagName('title').item(0).firstChild.data + '</a>';
					search_results += (this_result.getElementsByTagName('section').item(0).firstChild.data != '' ? ('<br/><span class="small">(' + this_result.getElementsByTagName('section').item(0).firstChild.data + ')</span>') : '');
					search_results += '</div><div>' + this_result.getElementsByTagName('text').item(0).firstChild.data + '</div>';
					search_results += (this_result.getElementsByTagName('created').item(0).firstChild.data != '-' ? ('<div class="small">' + this_result.getElementsByTagName('created').item(0).firstChild.data + '</div>') : '') + '</fieldset>';
					search_results += (result.getElementsByTagName('result').length == (i + 1) ? '' : '<br/>');
				}
			}
			if (display == 'table') {
				search_results += '</table>';
			}
		}
		if (showhideresult)
			document.getElementById('showhideresult').style.display = 'block';
		PopupContent('instant_search', '', '', '', search_results, width, height, 'modules/mod_d4m_instantsearch/images/close.png', null, padding_to, padding_width);
		if (bgcolor != '') {
			document.getElementById('instant_search_list').style.backgroundColor = bgcolor;
		} else {
			document.getElementById('instant_search_list').style.backgroundColor = getBackgroundColor(document.getElementById('instant_search_node'));
		}
		if (document.getElementById('instant_search_close'))
			document.getElementById('instant_search_close').src = 'modules/mod_d4m_instantsearch/images/close.png';
	}
}

function prepareSearch(keyword) {
	if (keyword != '' && keyword != text && keyword.length >= min_chars && keyword != previous) {
		previous = keyword;
		call_dosearch(keyword, limit, phrase, ordering, min_chars, result_length, enable_sef);
	}
}
