function GetElement( idElement )
{
	if( document.all )
		return document.all( idElement );

	return document.getElementById( idElement );
}

function appendFormParam(form, name, value)
{
	var e = document.createElement('input');
	e.setAttribute('type', 'hidden');
	e.setAttribute('name', name);
	e.setAttribute('value', value);
	form.appendChild(e);
}
function URLEncode( text )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = text;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
};
function quickSearch()
{
	if (GetElement('query').value.length>0)
	{
		document.location.href=GetElement('searchForm').action+URLEncode(GetElement('query').value)+'/1/'+GetElement('qs').value;
	}
	else
	{
		document.location.href=GetElement('searchForm').action+'1/'+GetElement('qs').value;
	}
}

function tagSearch(option, before, after)
{
	if (option == 'add')
	{
		if (GetElement('it').value != '')
		{
			var ids = GetElement('it').value.split('|');
			ids[ids.length] = GetElement('stid').value;
			GetElement('it').value = ids.join('|');
		}
		else
		{
			GetElement('it').value = GetElement('stid').value;
		}
	}
	else if (option == 'remove')
	{
		if (GetElement('nt').value != '')
		{
			var ids = GetElement('nt').value.split('|');
			ids[ids.length] = GetElement('stid').value;
			GetElement('nt').value = ids.join('|');
		}
		else
		{
			GetElement('nt').value = GetElement('stid').value;
		}
	}
	document.location.href = before+'/'+GetElement('it').value+'_'+GetElement('nt').value+after;
}

function tagRemove(id, option, before, after)
{
	var num = 0;
	var new_ids = new Array();
	if (option == 'added')
	{
		var ids = GetElement('it').value.split('|');
		for (var i=0; i<ids.length; i++)
		{
			if (ids[i] != id)
			{
				new_ids[num] = ids[i];
				num++;
			}
		}
		GetElement('it').value = new_ids.join('|');
	}
	else if (option == 'removed')
	{
		var ids = GetElement('nt').value.split('|');
		for (var i=0; i<ids.length; i++)
		{
			if (ids[i] != id)
			{
				new_ids[num] = ids[i];
				num++;
			}
		}
		GetElement('nt').value = new_ids.join('|');
	}
	if (GetElement('nt').value == '' && GetElement('it').value == '')
	{
		document.location.href = before+after;
	}
	else
	{
		document.location.href = before+'/'+GetElement('it').value+'_'+GetElement('nt').value+after;
	}
}
