Array.prototype.contains = function (element)
{
	for (var i = 0;i < this.length;i++)
	{
		if (this[i] == element)
		{
			return true;
		}
	}
	
	return false;
}

if (!Array.prototype.push)
{
	function array_push(element)
	{
		this[this.length] = element;
		
		return this.length;
	}
	
	Array.prototype.push = array_push;
}

if (!Array.prototype.splice)
{
	function array_splice(ind, cnt)
	{
		if (arguments.length == 0)
		{
			return ind;
		}
		
		if (typeof ind != "number")
		{
			ind = 0;
		}
		
		if (ind < 0)
		{
			ind = Math.max(0,this.length + ind);
		}
		
		if (ind > this.length)
		{
			if (arguments.length > 2)
			{
				ind = this.length;
			}
			else
			{
				return [];
			}
		}
		
		if (arguments.length < 2)
		{
			cnt = this.length - ind;
		}
		
		cnt = (typeof cnt == "number") ? Math.max(0, cnt) : 0;
		
		removeArray = this.slice(ind, ind + cnt);
		endArray = this.slice(ind+cnt);
		
		this.length = ind;
		
		for(var i=2;i<arguments.length;i++)
		{
			this[this.length] = arguments[i];
		}
		
		for(var i = 0;i < endArray.length;i++)
		{
			this[this.length] = endArray[i];
		}
		return removeArray;
	}
	
	Array.prototype.splice = array_splice;
}

function toggle(obj)
{
	if (!sending)
	{
		updateView(obj);
		
		beginQuery(obj);
	}
	
	return false;
}

function toggle2(obj)
{
	var o = obj.parentNode.firstChild;
	
	if (!sending && !o.checked && !o.disabled)
	{
		o.checked = true;
		
		updateView(o);
		
		beginQuery(o);
	}
	
	return false;
}

function updateView(obj)
{
	// this is an ugly function needed because the CSS styles for input elements didn't work in all browsers
	// the function must be called whenever there is a state change in a radio button
	
	// WARNING: getElementsByName in Explorer may also return the elements that have an id attribute with the given name!!!
	
	var l = obj ? document.getElementsByName(obj.name) : document.getElementsByTagName('input');
	
	for (var i = 0;i < l.length;i++)
	{
		if (l[i].type == 'radio')
		{
			var o = l[i].nextSibling;
			
			if (l[i].checked)
			{
				o.style.fontWeight = 'bold';
			}
			else
			{
				o.style.fontWeight = 'normal';
			}
			
			if (l[i].disabled)
			{
				o.style.fontStyle = 'italic';
				o.style.color = '#666';
				o.style.cursor = 'default';
			}
			else
			{
				o.style.fontStyle = 'normal';
				o.style.color = '#000';
				o.style.cursor = 'pointer';
			}
		}
	}
}

function beginQuery(obj)
{
	var found = false;
	
	for (var i = 0;i < sq.length;i++)
	{
		if (sq[i].name == obj.name)
		{
			sq[i] = obj;
			
			var found = true;
			
			break;
		}
	}
	
	if (!found)
	{
		sq.push(obj);
	}
	else
	{
		for (++i;i < sq.length;)
		{
			sq.splice(i, 1);
		}
	}
	
	sendQuery();
}

function selectDefaultRadioByName(name)
{
	// WARNING: see note about getElementsByName
	
	var l = document.getElementsByName(name);
	
	l[0].checked = true;
	
	updateView(l[0]);
}

function disableRadiosByName(name)
{
	// WARNING: see note about getElementsByName
	
	var l = document.getElementsByName(name);
	
	for (var i = 0;i < l.length;i++)
	{
		l[i].disabled = true;
	}
	
	updateView(l[0]);
}

function sendQuery()
{
	setOrderNumber();
	
	setSending();
	
	// this may be a query sent to a server-side script
	// the query could be sent using AJAX or maybe an iframe
	// however, here we use instead a local file
	// self.query.location = 'query.php?t=' + document.forms['form'].t.value + '&m=' + document.forms['form'].m.value + '&s=' + document.forms['form'].s.value;
	
	var t = setTimeout('endQuery()', 500);
}

function endQuery()
{
	// this function should work with a result array returned
	// by a server-side script called from sendQuery(), but
	// by now we'll use a hardcoded list and filter it here
	
	var r = files.slice(); // copy by value!
	
	var s = [];
	
	for (var i = 0;i < sq.length;i++)
	{
		s.push(document.getElementById(sq[i].id).nextSibling.firstChild.data.replace(/^-/, ''));
		
		if (sq[i].value != '') // this will exclude the first input element ('All the options')
		{
			for (var j = 0;j < r.length;j++)
			{
				var c = r[j][sq[i].name];
				
				if (!c || !c.contains(sq[i].id)) // shortcut
				{
					r.splice(j, 1);
					
					j--;
				}
			}
		}
	}
	
	// put the filtered records into the 'records' div
	
	document.getElementById('recnum').innerHTML = s.join(' : ') + ' = ' + (r.length ? 'hi ha ' + r.length + ' ' + (r.length > 1 ? 'fitxes' : 'fitxa') : 'no hi ha cap fitxa');
	
	var d = '';
	
	for (var i = 0;i < r.length;i++)
	{
		d += '<div class="' + (i % 2 ? 'even' : 'odd') + '"><a href="javascript:window.open(\'http://educacio.mataro.cat/guia/fitxa.php?id=' + r[i].f + '\');void 0;">' + r[i].n + '<' + '/a><' + '/div>';
	}
	
	document.getElementById('records').innerHTML = d;
	
	// extract the categories and enable its radios
	
	var s = [];
	
	for (var i = 0;i < r.length;i++)
	{
		s = s.concat(r[i].m, r[i].t, r[i].s);
	}
	
	s.sort(); // s may contain duplicates, so we sort the array and...
	
	var o;
	
	for (var i = 0, j = 0;i < s.length;i++)
	{
		if (o != s[i]) // ...here we discard the duplicates
		{
			var e = document.getElementById(s[i]);
			
			if (!e)
			{
				alert('The element ' + s[i] + ' does not exist!');
			}
			else
			{
				e.disabled = false;
			}
			
			j++;
		}
		
		o = s[i];
	}
	
	document.getElementById('XX').disabled = false;
	document.getElementById('YY').disabled = false;
	document.getElementById('ZZ').disabled = false;
	
	updateView();
	
	// hide the "searching..." divs
	
	resetSending();
}

function setOrderNumber()
{
	for (var i = 0;i < sd.length;i++)
	{
		document.getElementById(sd[i] + '_o').innerHTML = '';
	}
	
	for (var i = 0;i < sq.length;i++)
	{
		document.getElementById(sq[i].name + '_o').innerHTML = '<a href="#" onclick="">' + (i + 1) + '<' + '/a>-';
	}
}

function setSending()
{
	var f = false;
	
	sending = true;
	
	for (var i = 0;i < sd.length;i++)
	{
		for (var j = 0;j < sq.length;j++)
		{
			if (sq[j])
			{
				if (sd[i] == sq[j].name)
				{
					f = true;
					
					break;
				}
			}
		}
		
		if (!f)
		{
			document.getElementById(sd[i] + '_s').style.display = 'inline';
			
			selectDefaultRadioByName(sd[i]);
			
			disableRadiosByName(sd[i]);
		}
		else
		{
			f = false;
		}
	}
	
	document.getElementById('r_s').style.display = 'inline';
}

function resetSending()
{
	for (var i = 0;i < sd.length;i++)
	{
		document.getElementById(sd[i] + '_s').style.display = 'none';
	}
	
	document.getElementById('r_s').style.display = 'none';
	
	sending = false;
}

function doSetup()
{
	var d = document.forms['form'].reset();
	
	return;
}

var sending = false; // a query is or not in progress?
var sq = []; // array of search queries
var sd = ['t', 'm', 's']; // array of search divs
