var lastInputs = 'all';
var selectedItem = null;
var lastItem = null;
var overOptions = false;
var overDrop = false;

//####################
//## Show hide columns
function showHideColumns(frm) {
	var boxes = frm.getElementsByTagName('INPUT');
	for (var i=0; i<boxes.length; i++) {
		createCookie(boxes[i].id, (boxes[i].checked?1:0), 1);
	}
	createCookie(frm.id, 1, 1);
	window.location.reload();
}

//####################
//## Page links
function page_link(e, inpt, current_page, last_page, url) {
	if (e.keyCode==13) {
		if (inpt.value > 0 && inpt.value <= last_page) {
			document.location.href = url+'&page='+inpt.value;
		}
		else {
			inpt.value = current_page;
		}
	}
}

//####################
//## QUERY
function queryBuild() {
	var field = getElement('searchFiled').name;
	var conds = new Array();
	var inpts = getElement('inputs_'+field).getElementsByTagName('INPUT');
	for (var i=0; i<inpts.length; i++) {
		if (inpts[i].type == 'text') {
			if (inpts[i].id.match('expression_') && !inpts[i].name) {
				alert('No expression selected.');
				return;
			}
			if (inpts[i].readonly && inpts[i].name) conds.push(inpts[i].name);
			else if (inpts[i].value) conds.push(inpts[i].value.trim());
		}
	}
	var conditions = conds.join(';').trim();
	if (!conditions) {
		alert('No condition set.');
		return;
	}
	
	var query = getElement('searchQuery').value.trim();
	if (query) query += "\n";
	if (queryAnd()) query += queryAnd() + ' ';
	if (field != 'all') query += field + ':';
	query += conditions;
	
	querySet(query);
	fieldsHide();
	setTimeout("getElement('searchQuery').focus();", 500);
}
function querySet(query) {
	// Set query
	queryAdjustBox(query);
	getElement('searchQuery').value = query;
}
function queryClean() {
	// Remove newlines before submit
	var queryBox = getElement('searchQuery');
	queryBox.value = queryBox.value.replace(/\r|\n/g, " ").replace(/\s+/, " ");
}
function queryAdjustBox(query) {
	// Resize box according to number of new lines
	var queryBox = getElement('searchQuery');
	var rows = query.match(/\n/g);
	rows = rows? rows.length + 1 : 1;
	if (rows == 1 && queryBox.tagName != 'INPUT') {
		var newBox = document.createElement('INPUT');
		newBox.type = 'text';
		newBox.id = queryBox.id;
		newBox.name = queryBox.name;
		newBox.tabindex = queryBox.tabindex;
		newBox.className = queryBox.className;
		queryBox.parentNode.replaceChild(newBox, queryBox);
		queryBox = newBox;
	}
	else if (rows > 1) {
		if (queryBox.tagName != 'TEXTAREA') {
			var newBox = document.createElement('TEXTAREA');
			newBox.id = queryBox.id;
			newBox.name = queryBox.name;
			newBox.tabindex = queryBox.tabindex;
			newBox.className = queryBox.className;
			queryBox.parentNode.replaceChild(newBox, queryBox);
			queryBox = newBox;
		}
		queryBox.style.height = (13*rows) +'px';
	}
	return queryBox;
}
function queryAnd() {
	// Toggle and get AND / NOT box
	var lbl = getElement('searchAnd_options').getElementsByTagName('LABEL')[0];
	if (getElement('searchQuery').value) {
		lbl.innerHTML = 'AND';
		if (!getElement('searchAnd').value) getElement('searchAnd').value = 'AND';
	}
	else {
		lbl.innerHTML = '';
		if (getElement('searchAnd').value == 'AND') getElement('searchAnd').value = '';
	}
	return getElement('searchAnd').value;
}

//####################
//## FIELDS
function fieldsShow() {
	queryAnd();
	getElement('searchFields').style.display = '';
	getElement('searchFieldsLink').style.display = 'none';
	disableInput(getElement('searchQuery'));
	disableInput(getElement('search'));
	disableInput(getElement('searchClear'));
	if (getElement('searchHelp')) getElement('searchHelp').style.display = 'none';
}
function fieldsHide() {
	fieldsReset();
	fieldShowInputs();
	getElement('searchFields').style.display = 'none';
	getElement('searchFieldsLink').style.display = '';
	enableInput(getElement('searchQuery'));
	enableInput(getElement('search'));
	enableInput(getElement('searchClear'));
}
function fieldsReset() {
	var frm = getElement('fieldsForm');
	frm.reset();
	var checkSpans = frm.getElementsByTagName('SPAN');
	for (var i=0; i<checkSpans.length; i++) {
		if (checkSpans[i].className == 'checkSpan') checkSpans[i].innerHTML = '&nbsp;';
	}
}
function fieldShowInputs(id) {
	if (!id) id = 'all';
	if (id != lastInputs) {
		getElement('inputs_'+lastInputs).style.display = 'none';
		lastInputs = id;
		getElement('inputs_'+id).style.display = '';
	}
}

function disableInput(inpt) {
	inpt.disabled = true;
	inpt.className += ' disabled';
}
function enableInput(inpt) {
	inpt.disabled = false;
	inpt.className = inpt.className.replace(' disabled', '');
}

//####################
//## AUTOCOMPLETE
function autoComplete(e, field, id) {
	switch (e.keyCode) {
		case 13:	//enter
		case 9:		//tab
		case 27:	//esc
		case 40:	//down arrow
		case 38:	//up arrow
			return;
	}
	getElement(id+'_progress').src = '/images_static/progress.gif';
	connection = new AjaxConnection('/search_ajax.php?field='+field+'&input='+id+'&value='+getElement(id).value);
	connection.ajaxConnect('autoCompleteAjax', id);
}
function autoCompleteAjax(response, id) {
	getElement(id+'_options').innerHTML = response;
	dropShow(id);
	getElement(id+'_progress').src = '/images_static/progress_inactive.gif';
}

//####################
//## DYNAMIC DROP
function autoDrop(field, value, child) {
	connection = new AjaxConnection('/search_ajax.php?field='+field+'&input='+child+'&value='+value);
	connection.ajaxConnect('autoDropAjax', child);
}
function autoDropAjax(response, id) {
	getElement(id).value = '';
	getElement(id+'_options').innerHTML = response;
	if (response) getElement(id+'_all').style.display = '';
	else getElement(id+'_all').style.display = 'none';
}

//####################
//## GENERAL DROP DOWN FUNCS
// TOGGLE
function dropToggle(id) {
	var opts = getElement(id+'_options');
	if (opts.style.display) dropShow(id);
	else opts.style.display = 'none';
}
// SHOW
function dropShow(id) {
	var opts = getElement(id+'_options');
	if (!opts.style.display) return;
	if (selectedItem) dropOut(selectedItem);
	if (opts.innerHTML) opts.style.display = '';
	getElement(id).focus();
}
// HIDE
function dropHide(id) {
	if (overOptions) {
		getElement(id).focus();
	}
	else if (!overDrop) {
		getElement(id+'_options').style.display = 'none';
	}
}
// OVER
function dropOver(item) {
	item.className = 'hover';
	if (selectedItem && selectedItem != item) dropOut(selectedItem);
	selectedItem = lastItem = item;
}
// OUT
function dropOut(item) {
	item.className = '';
	if (selectedItem && selectedItem != item) selectedItem.className = '';
	selectedItem = null;
}
// CLICK
function dropClick(e, item, id) {
	cancelDefault(e);
	var text = '';
	var value = '';
	var inpt = item.getElementsByTagName('INPUT');
	// dropCheck
	if (inpt.length) {
		// Checkbox
		inpt = inpt[0];
		inpt.checked = inpt.checked? false : true;
		// Check span
		var spn = item.getElementsByTagName('SPAN');
		if (spn[0]) spn[0].innerHTML = inpt.checked? '&#10003;' : '&nbsp;';
		// Concat all checked
		var boxes = getElement(id+'_options').getElementsByTagName('INPUT');
		var val = new Array();
		var txt = new Array();
		for (var i=0; i<boxes.length; i++) {
			if (boxes[i].checked) {
				txt.push(boxes[i].name);
				val.push(boxes[i].id);
			}
		}
		text = txt.join(',');
		value = val.join(',');
		//dropOver(item);
		getElement(id).focus();
	}
	else {
		text = item.innerHTML;
		value = item.id;
		overOptions = false;
		//getElement(id).focus();
		dropHide(id);
	}
	text = text.replace('&lt;', '<');
	text = text.replace('&gt;', '>');
	getElement(id).value = text;
	getElement(id).name = value;
	if (getElement(id).onchange) getElement(id).onchange();
}
// NAVIGATE
function dropNavigate(e, id) {
	switch (e.keyCode) {
		case 9:		//tab
		case 13:	//enter
		case 32:	//space
			if (selectedItem) dropClick(e, selectedItem, id);
			if (e.keyCode != 9) cancelDefault(e);
		return;
		case 40:	//down arrow
			if (getElement(id+'_options').style.display) dropShow(id);
			if (!selectedItem) {
				selectedItem = lastItem = getElement(id+'_options').getElementsByTagName('LABEL')[0];
				if (selectedItem) dropOver(selectedItem);
			}
			else {
				var item = selectedItem;
				while (item.nextSibling && (item.nextSibling.tagName != 'LABEL' || !item.nextSibling.id || item.nextSibling.id*1 < 0)) item = item.nextSibling;
				if (item.nextSibling) {
					dropOver(item.nextSibling);
				}
			}
			cancelDefault(e);
		return;
		case 38:	//up arrow
			if (selectedItem) {
				var item = selectedItem;
				while (item.previousSibling && (item.previousSibling.tagName != 'LABEL' || !item.previousSibling.id || item.previousSibling.id*1 < 0)) item = item.previousSibling;
				if (item.previousSibling) {
					dropOver(item.previousSibling);
				}
			}
			cancelDefault(e);
		return;
		case 27:	//esc
			var drop = overDrop;
			overOptions = overDrop = false;
			dropHide(id);
			overDrop = drop;
			cancelDefault(e);
		return;
	}
}

