function confirm_delete(text)
{
	if(!text)
	{
		text = "Entry";
	}
	return confirm('Delete "' + text + '"?');
}

function check_req_fields(form_name, fields)
{
	if(document.forms[form_name])
	{
		var fields_arr = fields.split(',');
		for(var i = 0; i < fields_arr.length; i++)
		{
			if(document.forms[form_name][fields_arr[i]].value == '')
			{
				alert('Please fill "' + fields_arr[i] + '"');
				return false;
			}
		}
	}
	else
	{
		alert('Form doesn\'t exist');
		return false;
	}
	return true;
	
}

function switch_display(elem)
{
	var obj = document.getElementById(elem);
	if(obj)
	{
		if(obj.style.display == 'none' || !obj.style.display)
		{
			obj.style.display = 'block';
		}
		else if(obj.style.display == 'block')
		{
			obj.style.display = 'none';
		}
	}
}

function show_images(filetag_id)
{
	var image_cont = document.getElementById('images_cont');
	var images = image_cont.getElementsByTagName('p');
	for(var i=0; i<images.length; i++)
	{
		var tmp_arr = images[i].id.split('_');
		if(tmp_arr[1] == filetag_id || !filetag_id)
		{
			images[i].style.display = 'block';
		}
		else
		{
			images[i].style.display = 'none';
		}
	}
}

function uni_tag(front, end)
{
	var agt = navigator.userAgent.toLowerCase();
	var is_major = parseInt(navigator.appVersion);
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_ie4up  = (is_ie && (is_major >= 4));
	var is_gecko = (agt.indexOf('gecko') != -1);
	
	var txt_area = document.getElementById('input_content');
  txt_area.focus();
	if(is_ie4up)
	{
		txt_area.caretPos.text = front + txt_area.caretPos.text + end;
	}
  else if(is_gecko)
	{
  	txt_area.value = 	txt_area.value.substring(0, txt_area.selectionStart) + front + 
											txt_area.value.substring(txt_area.selectionStart, txt_area.selectionEnd) + end +
           						txt_area.value.substring(txt_area.selectionEnd, txt_area.value.length);
  }
	else 
	{
		txt_area.value += front + end;
	}        
}

function update_textarea(text)
{
    uni_tag('', text);
}

function storeCaret(textEl) 
{
	if(textEl.createTextRange)
	{
		textEl.caretPos = document.selection.createRange().duplicate();
	}
}

function set_companies(id)
{
	if(String(id).match(/^[0-9]+$/))
	{
		// SET LIST
		var url = '/_inc/php/ajax_engine.php?set_comp_list=1&cat_id=' + id;
		jah (url, 'companies_list');
	}
}

function scroll_map_to(position)
{
	var container = $('companies_map_child');
	
	container.scrollTop = position;
}

function $(obj_id)
{
	if(document.getElementById(obj_id))
	{
		return document.getElementById(obj_id);
	}
	return false;
}

/* ----------------------------------- AJAX ----------------------------------- */
function jah (url, target, add_function)
{
	// loading indicator
	document.getElementById(target).innerHTML = '<div><img src="../_res/loader.gif" alt="Loading&hellip;"/></div>';
	
		// Native XMLHttpRequest object, also IE7
		if (window.XMLHttpRequest)
		{
			window[target] = new XMLHttpRequest();
			window[target].onreadystatechange = function() { jahDone(target, add_function); };
			window[target].open("GET", url, true);
			window[target].send(null);
		}
		else if (window.ActiveXObject)
		{
			// IE/Windows ActiveX object
			window[target] = new ActiveXObject("Microsoft.XMLHTTP");
			if (window[target])
			{
				window[target].onreadystatechange = function() { jahDone(target, add_function); };
				window[target].open("GET", url, true);
				window[target].setRequestHeader("Pragma", "no-cache");
				window[target].setRequestHeader("Cache-Control", "must-revalidate");
				window[target].send();
			}
		}
	return true;
}

function jahDone (target, add_function)
{
	// only if window[target] is "loaded"
	if (window[target].readyState == 4 && document.getElementById(target))
	{
		// only if "OK"
		if (window[target].status == 200)
		{
			results = window[target].responseText;
			document.getElementById(target).innerHTML = results;
			eval(add_function);
		}
		else
		{
			document.getElementById(target).innerHTML="<strong>Jah error:</strong> " + window[target].statusText;
		}
	}
}

/* ----------------------------------- SEARCH --------------------------------- */
function start_search(e)
{
	var keycode;
	
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	
	if (keycode == 13)
	{
		filterTable();
	}
}

function filterTable()
{
	var table = document.getElementById('companies_list_table');
	var term  = document.getElementById('company_search').value;

	dehighlight(table);

	var terms = term.toLowerCase().split(" ");

	for (var r = 0; r < table.rows.length; r++)
	{
		var display = '';
		for (var i = 0; i < terms.length; i++)
		{
			if (table.rows[r].innerHTML.replace(/<[^>]+>/g, "").replace(/\./g, "").toLowerCase().indexOf(terms[i].replace(/\./g, "")) < 0)
			{
				display = 'none';
			}
			else
			{
				if (terms[i].length) highlight(terms[i], table.rows[r]);
			}
			table.rows[r].style.display = display;
		}
	}
}

function dehighlight(container) {
	for (var i = 0; i < container.childNodes.length; i++) {
		var node = container.childNodes[i];

		if (node.attributes && node.attributes['class']
			&& node.attributes['class'].value == 'highlighted') {
			node.parentNode.parentNode.replaceChild(
					document.createTextNode(
						node.parentNode.innerHTML.replace(/<[^>]+>/g, "")),
					node.parentNode);
			// Stop here and process next parent
			return;
		} else if (node.nodeType != 3) {
			// Keep going onto other elements
			dehighlight(node);
		}
	}
}

function highlight(term, container) {
	for (var i = 0; i < container.childNodes.length; i++) {
		var node = container.childNodes[i];

		if (node.nodeType == 3) {
			// Text node
			var data = node.data;
			var data_low = data.toLowerCase();
			if (data_low.indexOf(term) >= 0) {
				//term found!
				var new_node = document.createElement('span');

				node.parentNode.replaceChild(new_node, node);

				var result;
				while ((result = data_low.indexOf(term)) != -1) {
					new_node.appendChild(document.createTextNode(
								data.substr(0, result)));
					new_node.appendChild(create_node(
								document.createTextNode(data.substr(
										result, term.length))));
					data = data.substr(result + term.length);
					data_low = data_low.substr(result + term.length);
				}
				new_node.appendChild(document.createTextNode(data));
			}
		} else {
			// Keep going onto other elements
			highlight(term, node);
		}
	}
}

function create_node(child) {
	var node = document.createElement('span');
	node.setAttribute('class', 'highlighted');
	node.attributes['class'].value = 'highlighted';
	node.appendChild(child);
	return node;
}
