var xmlhttp = false;
var imageLoading = '<img src="ajax/loading.gif" alt="Loading ..." \/>';
var textLoading = 'Loading ...';

function $()
{
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++)
	{
		var element = arguments[i];
		if (element && typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function addslashes(str)
{
	return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
}

function stripslashes(str)
{
	return str.replace('/\0/g', '0').replace('/\(.)/g', '$1');
}

function strip_tags(str, allowed_tags)
{
	var key = '', tag = '';
	var matches = allowed_array = [];
	var allowed_keys = {};
	if (allowed_tags)
	{
		allowed_tags  = allowed_tags.replace(/[\<\> ]+/g, '');
		allowed_array = allowed_tags.split(',');

		for (key in allowed_array)
		{
			tag = allowed_array[key];
			allowed_keys['<' + tag + '>']   = true;
			allowed_keys['<' + tag + ' />'] = true;
			allowed_keys['</' + tag + '>']  = true;
		}
	}
	matches = str.match(/(<\/?[^>]+>)/gi);
	for (key in matches)
	{
		tag = matches[key];
		if (!allowed_keys[tag])
		{
			str = str.replace(tag, "");
		}
	}
	return str;
}

function chunk_split(body, argChunklen, argEnd)
{
	var result = '', chunklen = argChunklen || 76, end = argEnd || '\r\n';
	if (chunklen < 1)
	{
		return false;
	}
	while (body.length > chunklen)
	{
		result += body.substring(0, chunklen) + end;
		body = body.substring(chunklen);
	}
	return result + body + end;
}

function html_entity_decode(string)
{
	var ret;
	var textarea = document.createElement('textarea');
	textarea.style.position='absolute';
	textarea.style.top='-9999px';
	textarea.style.left='-9999px';
	document.body.appendChild(textarea);
	textarea.innerHTML = string;
	ret = textarea.value;
	document.body.removeChild(textarea);
	return ret;
}

function htmlentities(str)
{
	var div = document.createElement('div');
	div.style.position='absolute';
	div.style.top='-9999px';
	div.style.left='-9999px';
	document.body.appendChild(div);
	var text = document.createTextNode(str);
	div.appendChild(text);
	var html = div.innerHTML;
	document.body.removeChild(div);
	return html;
}

function doubleLines(str)
{
	return str.replace(/\n/g, '\n\n');
}

function in_array(needle, haystack, argStrict)
{
	var key = '', strict = !!argStrict;
	if (strict)
	{
		for (key in haystack)
		{
			if (haystack[key] === needle)
			{
				return true;
			}
		}
	}
	else
	{
		for (key in haystack)
		{
			if (haystack[key] == needle)
			{
				return true;
			}
		}
	}
	return false;
}

function getWriteableById(id)
{
	var el = $(id);
	return (el.type) ? 'value' : 'innerHTML';
}

function doWrite(id, content)
{
	var w = getWriteableById(id);
	switch(w)
	{
		case 'value': $(id).value = doubleLines(strip_tags(stripslashes(content))); break;
		case 'innerHTML': $(id).innerHTML = stripslashes(content); break;
	}
}

function getHTTPReq(url, query, destinationid)
{
	showLoading(destinationid);
	xmlhttp = false;
	url = url+'?'+query+'&jsid='+Math.random();
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
	else if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	if (xmlhttp)
	{
		xmlhttp.open("GET", url, true);
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState==4)
			{
				if (xmlhttp.status==200) { doWrite(destinationid, xmlhttp.responseText); }
				else { doWrite(destinationid, 'Fail'); }
			}
		}
		if (window.XMLHttpRequest) { xmlhttp.send(null); }
		else if (window.ActiveXObject) { xmlhttp.send(); }
	}
}

function postHTTPReq(url, query, destinationid)
{
	showLoading(destinationid);
	xmlhttp = false;
	query = query+'&jsid='+Math.random();
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
		if (xmlhttp.overrideMimeType) { xmlhttp.overrideMimeType('text/html'); }
	}
	else if (window.ActiveXObject)
	{
		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e)
		{
			try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } 
			catch (e) {}
		}
	}
	if (xmlhttp)
	{
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState==4)
			{
				if (xmlhttp.status==200) { doWrite(destinationid, xmlhttp.responseText); }
				else { doWrite(destinationid, 'Fail'); }
			}
		}
		xmlhttp.open("POST", url, true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", query.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.send(query);
	}
}
function showLoading(id)
{
	var w = getWriteableById(id);
	switch(w)
	{
		case 'value': $(id).value = textLoading; break;
		case 'innerHTML': $(id).innerHTML = imageLoading; break;
	}
}
function clearContent(id)
{
	var w = getWriteableById(id);
	switch(w)
	{
		case 'value': $(id).value = ''; break;
		case 'innerHTML': $(id).innerHTML = ''; break;
	}
}
function defaultQuery(id)
{
	getHTTPReq('ajax/response.php', 'search='+id, id);
}
function createQuery(id)
{
	var i;
	var q = 'search='+id;
	for(i=0;i<8;i++)
	{
		if($('cb'+i).checked==true)
		{
			q += '&reg[]='+encodeURI($('cb'+i).value);
		}
	}
	getHTTPReq('ajax/response.php', q, id);
}
function clickHandler(evt)
{
	var cbid = '';
	if(window.event && window.event.srcElement.tagName=='INPUT') { cbid = window.event.srcElement.id; }
	else if(evt && evt.target && evt.target.tagName=='INPUT') { cbid = evt.target.id; }
	if (cbid != '' && cbid.indexOf('cb')!==-1)
	{
		createQuery('searchResultContent');
	}
}
document.onclick=clickHandler;
window.onload=function() { defaultQuery('leftContent'); }