	function addSearchRow(vari, op, val)
	{
		var options = document.getElementById('adv-search-options');
		if(!options) return;
	
		var container = document.createElement('DIV');
		container.className = 'search-option';

		container.changeMode = function(newMode)
		{
			var oldMode = this.getAttribute('mode');
		
			switch(newMode)
			{
				case 'date':
					if(oldMode != 'date')
						this.dateMode();
					break;
			
				case 'section':
					if(oldMode != 'section')
						this.sectionMode();
					break;
			
				case 'category':
					if(oldMode != 'category')
						this.categoryMode();
					break;
			
				case 'mypreview':
					if(oldMode != 'mypreview')
						this.myPreviewMode();
					break;
			
				case 'title':
				case 'description':
				default:
					if(oldMode != 'title' && oldMode != 'description')
						this.textMode();
					break;
			}
			
			this.setAttribute('mode', newMode);
		}

		container.textMode = function(op, val)
		{
			var textCompareSelect = document.createElement('SELECT');
			textCompareSelect.name = 'op[]';
			
			textCompareSelect.options[0] = new Option('contains', 'like');
			textCompareSelect.options[1] = new Option('exactly matches', 'equal');
			textCompareSelect.selectedIndex = (op == 'equal') ? 1 : 0;
			
			var textVal = document.createElement('INPUT');
			textVal.setAttribute('type', 'text');
			textVal.className = 'text';
			textVal.name = 'val[]';
			if(val)
				textVal.value = val;
			
			var spans = this.getElementsByTagName('SPAN');
			for(var i = 0, span; span = spans[i]; i++)
			{
				if(span.className = 'mode')
				{
					while(span.firstChild)
						span.removeChild(span.firstChild);
						
					span.appendChild(textCompareSelect);
					span.appendChild(textVal);
					return;
				}
			}
		}
		
		container.myPreviewMode = function(op, val)
		{
			var opSelect = document.createElement('SELECT');
			opSelect.name = 'op[]';
			
			opSelect.options[0] = new Option('Search only in My Preview', 'true');
			opSelect.options[1] = new Option('Search only outside My Preview', 'false');
			opSelect.options[2] = new Option('Search everything', 'either');
			opSelect.value = op;
			
			var valInput = document.createElement('INPUT');
			valInput.setAttribute('type', 'hidden');
			valInput.name = 'val[]';
			valInput.value = val ? val : 'null';
			
			var spans = this.getElementsByTagName('SPAN');
			for(var i = 0, span; span = spans[i]; i++)
			{
				if(span.className = 'mode')
				{
					while(span.firstChild)
						span.removeChild(span.firstChild);
						
					span.appendChild(opSelect);
					span.appendChild(valInput);
					return;
				}
			}
		}
		
		container.dateMode = function(op, val)
		{
			var today = new Date();
			
			if(val)
			{
				var valParts = val.split('-');
				if(valParts.length == 3)
				{
					today.setFullYear(parseInt(valParts[0]));
					today.setMonth(parseInt(valParts[1]) - 1);
					today.setDate(parseInt(valParts[2]));
				}
			}
			
			var day = today.getDate();
			var month = today.getMonth();
			var year = today.getFullYear();
		
			var opSelect = document.createElement('SELECT');
			opSelect.name = 'op[]';
			
			opSelect.options[0] = new Option('is', 'equal');
			opSelect.options[1] = new Option('is before', 'less');
			opSelect.options[2] = new Option('is after', 'greater');
			switch(op)
			{
				case 'less':
					opSelect.selectedIndex = 1;
					break;
				case 'greater':
					opSelect.selectedIndex = 2;
					break;
				default:
					opSelect.selectedIndex = 0;
			}
			
			var daySelect = document.createElement('SELECT');
			daySelect.className = 'day';
			daySelect.onchange = function() 
			{
				var p = this.parentNode;
				while(!p.updateDateVal)
					p = p.parentNode;
				p.updateDateVal();
			}
			for(var i = 0; i < 31; i++)
				daySelect.options[i] = new Option((i+1), (i+1));
				
			var monthSelect = document.createElement('SELECT');
			monthSelect.onchange = function() 
			{
				var p = this.parentNode;
				while(!p.updateDateVal)
					p = p.parentNode;
				p.updateDateVal();
			}
			monthSelect.className = 'month';
			monthSelect.options[0] = new Option('January', 1);
			monthSelect.options[1] = new Option('February', 2);
			monthSelect.options[2] = new Option('March', 3);
			monthSelect.options[3] = new Option('April', 4);
			monthSelect.options[4] = new Option('May', 5);
			monthSelect.options[5] = new Option('June', 6);
			monthSelect.options[6] = new Option('July', 7);
			monthSelect.options[7] = new Option('August', 8);
			monthSelect.options[8] = new Option('September', 9);
			monthSelect.options[9] = new Option('October', 10);
			monthSelect.options[10] = new Option('November', 11);
			monthSelect.options[11] = new Option('December', 12);
			
			var yearSelect = document.createElement('SELECT');
			yearSelect.onchange = function() 
			{
				var p = this.parentNode;
				while(!p.updateDateVal)
					p = p.parentNode;
				p.updateDateVal();
			}
			yearSelect.className = 'year';
			for(var i = 0; i < 5; i++)
			{
				optYear = year - 4 + i;
				yearSelect.options[i] = new Option(optYear, optYear);
				if(optYear == year)
					yearSelect.options[i].selected = true;
			}
			
			daySelect.options[day - 1].selected = true;
			monthSelect.options[month].selected = true;
			
			var valInput = document.createElement('INPUT');
			valInput.setAttribute('type', 'hidden');
			valInput.setAttribute('name', 'val[]');
			
			var dateContainer = document.createElement('SPAN');
			dateContainer.className = 'date';
			dateContainer.appendChild(daySelect);
			dateContainer.appendChild(monthSelect);
			dateContainer.appendChild(yearSelect);
			
			var spans = this.getElementsByTagName('SPAN');
			for(var i = 0, span; span = spans[i]; i++)
			{
				if(span.className = 'mode')
				{
					while(span.firstChild)
						span.removeChild(span.firstChild);
						
					span.appendChild(opSelect);
					span.appendChild(dateContainer);
					span.appendChild(valInput);
					this.updateDateVal();
					return;
				}
			}
		}
		
		container.sectionMode = function(op, val)
		{
			var opInput = document.createElement('SELECT');
			opInput.name = 'op[]';
			opInput.options[0] = new Option('is', 'OR');
			opInput.options[1] = new Option('is not', 'NOT');
			opInput.selectedIndex = (op == 'NOT') ? 1 : 0;
			
			var defaultChecked = val ? false : true;
			var sections = [
				{'name' : 'Design Diary', 'value' : 'diary', 'checked' : defaultChecked}, 
				{'name' : 'Key Trends', 'value' : 'keytrend', 'checked' : defaultChecked},
				{'name' : 'Micro Trends', 'value' : 'microtrend', 'checked' : defaultChecked}, 
				{'name' : 'Trip Reports', 'value' : 'trip', 'checked' : defaultChecked}, 
				{'name' : 'Event Reports', 'value' : 'event', 'checked' : defaultChecked}, 
				{'name' : 'Global Reporting', 'value' : 'global', 'checked' : defaultChecked}, 
				{'name' : 'Colour Forecasts', 'value' : 'colour', 'checked' : defaultChecked}, 
				{'name' : 'Trend Seminars', 'value' : 'seminar', 'checked' : defaultChecked} 
				//{'name' : 'My Preview', 'value' : 'mypreview', 'checked' : defaultChecked}
			];
			
			if(val)
			{
				var valParts = val.split(',');
				var valIndex = 0;
				for(var j = 0; j < sections.length; j++)
				{
					for(var k = 0; k < valParts.length; k++)
						if(sections[j].value == valParts[k])
							sections[j].checked = true;
				}
			}
			
			var sectionContainer = document.createElement('SPAN');
			for(var j = 0; j < sections.length; j++)
			{
				var label = document.createElement('label');
				label.className = 'section';
				var checkbox = document.createElement('input');
				checkbox.onclick = function() 
				{
					var parent = this.parentNode;
					while(!parent.updateSectionVal)
						parent = parent.parentNode;
					parent.updateSectionVal();
				}
				checkbox.className = 'section';
				checkbox.setAttribute('type', 'checkbox');
				checkbox.setAttribute('value', sections[j].value);
				checkbox.checked = sections[j].checked;
				label.appendChild(checkbox);
				label.appendChild(document.createTextNode(sections[j].name));
				sectionContainer.appendChild(label);
			}
			
			var valInput = document.createElement('INPUT');
			valInput.setAttribute('type', 'hidden');
			valInput.setAttribute('name', 'val[]');
			
			var spans = this.getElementsByTagName('SPAN');
			for(var i = 0, span; span = spans[i]; i++)
			{
				if(span.className = 'mode')
				{
					while(span.firstChild)
						span.removeChild(span.firstChild);
						
					span.appendChild(opInput);
					span.appendChild(sectionContainer);
					span.appendChild(valInput);
					this.updateSectionVal();
					return;
				}
			}
		}
		
		container.categoryMode = function(op, val)
		{
			var opInput = document.createElement('SELECT');
			opInput.name = 'op[]';
			opInput.options[0] = new Option('is', 'OR');
			opInput.options[1] = new Option('is not', 'NOT');
			opInput.selectedIndex = (op == 'NOT') ? 1 : 0;
			
			var defaultChecked = val ? false : true;
			var categories = [
				{'name' : 'Consumer', 'value' : 'consumer', 'checked' : defaultChecked},
				{'name' : 'Culture', 'value' : 'culture', 'checked' : defaultChecked},
				{'name' : 'Design', 'value' : 'design', 'checked' : defaultChecked},
				{'name' : 'Economy', 'value' : 'economy', 'checked' : defaultChecked},
				{'name' : 'Environment', 'value' : 'environment', 'checked' : defaultChecked},
				{'name' : 'Media', 'value' : 'media', 'checked' : defaultChecked},
				{'name' : 'Retail', 'value' : 'retail', 'checked' : defaultChecked},
				{'name' : 'Social', 'value' : 'social', 'checked' : defaultChecked},
				{'name' : 'Technology', 'value' : 'technology', 'checked' : defaultChecked}
			];
			if(val)
			{
				var valParts = val.split(',');
				var valIndex = 0;
				for(var j = 0; j < categories.length; j++)
				{
					for(var k = 0; k < valParts.length; k++)
						if(categories[j].value == valParts[k])
							categories[j].checked = true;
				}
			}
			
			var catContainer = document.createElement('SPAN');
			catContainer.style.paddingLeft = '100px';
			for(var j = 0; j < categories.length; j++)
			{
				var checkbox = document.createElement('input');
				checkbox.onclick = function() 
				{
					var parent = this.parentNode;
					while(!parent.updateCategoryVal)
						parent = parent.parentNode;
					parent.updateCategoryVal();
				}
				checkbox.className = 'category';
				checkbox.setAttribute('type', 'checkbox');
				checkbox.setAttribute('value', categories[j].value);
				checkbox.checked = categories[j].checked;
				
				var label = pregoCreateElement('LABEL', [
					checkbox,
					categories[j].name
				]);
				label.className = 'category';

				catContainer.appendChild(label);
			}
			
			var valInput = document.createElement('INPUT');
			valInput.setAttribute('type', 'hidden');
			valInput.setAttribute('name', 'val[]');
			
			var spans = this.getElementsByTagName('SPAN');
			for(var i = 0, span; span = spans[i]; i++)
			{
				if(span.className = 'mode')
				{
					while(span.firstChild)
						span.removeChild(span.firstChild);
						
					span.appendChild(opInput);
					span.appendChild(catContainer);
					span.appendChild(valInput);
					this.updateCategoryVal();
					return;
				}
			}
		}
		
		container.updateDateVal = function()
		{
			var day, month, year;
		
			var selects = this.getElementsByTagName('SELECT');
			for(var i = 0, select; select = selects[i]; i++)
			{
				if(select.className == 'day')
					day = select.value;
				if(select.className == 'month')
					month = select.value;
				if(select.className == 'year')
					year = select.value;
			}
			
			var inputs = this.getElementsByTagName('INPUT');
			for(var i = 0, input; input = inputs[i]; i++)
				if(input.name == 'val[]')
					input.value = year+'-'+month+'-'+day;
		}
		
		container.updateSectionVal = function()
		{
			var valInput, value, valueIndex;
			
			values = new Array();
			valueIndex = 0;
			
			var inputs = this.getElementsByTagName('INPUT');
			for(var i = 0, input; input = inputs[i]; i++)
			{
				if(input.className == 'section' && input.checked)
					values[valueIndex++] = input.value;
				if(input.name == 'val[]')
					valInput = input;
			}
			
			valInput.value = values.join(',');
		}
		
		container.updateCategoryVal = function()
		{
			var valInput, value, valueIndex;
			
			values = new Array();
			valueIndex = 0;
			
			var inputs = this.getElementsByTagName('INPUT');
			for(var i = 0, input; input = inputs[i]; i++)
			{
				if(input.className == 'category' && input.checked)
					values[valueIndex++] = input.value;
				if(input.name == 'val[]')
					valInput = input;
			}
			
			valInput.value = values.join(',');
		}
		
		// Possible variables
		var varSelect = document.createElement('SELECT');
		varSelect.name = 'var[]';
		varSelect.onchange = function()
		{
			this.parentNode.changeMode(this.value);
		}
		
		varSelect.options[0] = new Option('Title', 'title');
		varSelect.options[1] = new Option('Description', 'description');
		varSelect.options[2] = new Option('Date', 'date');
		varSelect.options[3] = new Option('Section', 'section');
		varSelect.options[4] = new Option('Category', 'category');
		varSelect.options[5] = new Option('My Preview', 'mypreview');
		
		switch(vari)
		{
			case 'description':
				varSelect.selectedIndex = 1;
				break;
			case 'date':
				varSelect.selectedIndex = 2;
				break;
			case 'section':
				varSelect.selectedIndex = 3;
				break;
			case 'category':
				varSelect.selectedIndex = 4;
				break;
			case 'mypreview':
				varSelect.selectedIndex = 5;
				break;
			case 'title':
			default:
				varSelect.selectedIndex = 0;
		}
		
		var modeContainer = document.createElement('SPAN');
		modeContainer.className = 'mode';
		
		// Add a new item button
		var addButton = document.createElement('INPUT');
		addButton.className = 'add-button';
		addButton.setAttribute('type', 'button');
		addButton.setAttribute('value', '+');
		addButton.setAttribute('title', 'Add another search condition');
		addButton.onclick = addSearchRow;
		
		// Delete current item button
		var delButton = document.createElement('INPUT');
		delButton.className = 'delete-button';
		delButton.setAttribute('type', 'button');
		delButton.setAttribute('value', '-');
		delButton.setAttribute('title', 'Remove this search condition');
		delButton.onclick = function()
		{
			this.parentNode.parentNode.removeChild(this.parentNode);
		};
		if(!options.firstChild)
		{
			delButton.disabled = true;
			delButton.className = 'delete-button-disabled';
		}
		
		// Add everything to the container
		pregoUniversalAppend(container, [
			addButton,
			delButton,
			varSelect,
			modeContainer
		]);
		
		switch(vari)
		{
			case 'date':
				container.dateMode(op, val);
				break;
			case 'section':
				container.sectionMode(op, val);
				break;
			case 'category':
				container.categoryMode(op, val);
				break;
			case 'mypreview':
				container.myPreviewMode(op,val);
				break;
			case 'title':
			case 'description':
			default:
				container.textMode(op, val);
		}
		
		// Add the container to the form
		options.appendChild(container);
	}
	
	
	
	
	function advSearchValidate()
	{

		// Extract the search terms from the form
		var options = document.getElementById('adv-search-options');
		var divs = options.getElementsByTagName('DIV');
		
		var terms = [];
		var termIndex = 0;
		
		for(var i = 0, d; d = divs[i]; i++)
		{
			if(d.className == 'search-option')
			{
				var children = d.getElementsByTagName('*');
				var variable = false;
				var op = false;
				var val = false;
				for(var j = 0, child; child = children[j]; j++) 
				{
					if(child.name)
					{
						switch(child.name)
						{
							case 'var[]': variable = child.value; break;
							case 'op[]': op = child.value; break;
							case 'val[]': val = child.value; break;
						}
					}
				}
		
				if(variable && op)
				{
					terms[termIndex++] = {
						'vari': variable,
						'op'  : op,
						'val' : val,
						'node': d
					};
				}
			}
		}
		
		// Find the criteria operator (AND or OR)
		var frm = options.parentNode;
		while(frm.tagName != 'FORM')
			frm = frm.parentNode;
		var criteriaOp = frm.elements['criteria-op'].value;
		
		// Check the search terms are valid
		if(terms.length == 0)
		{
			alert('You must have at least one search criteria');
			return false;
		}
		
		var exactTitle = false;
		var exactDesc = false;
		var exactDate = false;
		var preDate = false;
		var postDate = false;
		
		var sections = [
			{name:'diary', is:0, not:0},
			{name:'keytrend', is:0, not:0},
			{name:'microtrend', is:0, not:0},
			{name:'trip', is:0, not:0},
			{name:'event', is:0, not:0},
			{name:'consumer', is:0, not:0},
			{name:'colour', is:0, not:0},
			{name:'seminar', is:0, not:0},
			{name:'mypreview', is:0, not:0}
		];
		
		var categories = [
			{name:'consumer', is:0, not:0},
			{name:'culture', is:0, not:0},
			{name:'design', is:0, not:0},
			{name:'economy', is:0, not:0},
			{name:'environment', is:0, not:0},
			{name:'media', is:0, not:0},
			{name:'retail', is:0, not:0},
			{name:'social', is:0, not:0},
			{name:'technology', is:0, not:0},
		];
		
		for(var i = 0, t; t = terms[i]; i++)
		{
			if(!t.val)
			{
				alert('You must enter a value for all search criteria');
				return false;
			}
			
			if(criteriaOp == 'AND')
			{
				if(t.vari == 'title' && t.op == 'equal')
				{
					if(exactTitle)
					{
						alert('You cannot have more than one "Title exactly matches..." criteria');
						return false;
					}
					exactTitle = true;
				}
				
				if(t.vari == 'description' && t.op == 'equal')
				{
					if(exactDesc)
					{
						alert('You cannot have more than one "Description exactly matches..." criteria');
						return false;
					}
					exactDesc = true;
				}
				
				if(t.vari == 'date' && t.op == 'equal')
				{
					if(exactDate)
					{
						alert('You cannot have more than one "Date is..." criteria');
						return false;
					}
					exactDate = true;
				}
				
				if(t.vari == 'date' && t.op == 'less')
				{
					if(preDate)
					{
						alert('You cannot have more than one "Date is before..." criteria');
						return false;
					}
					preDate = true;
				}
				
				if(t.vari == 'date' && t.op == 'greater')
				{
					if(postDate)
					{
						alert('You cannot have more than one "Date is after..." criteria');
						return false;
					}
					postDate = true;
				}

				if(t.vari == 'section')
				{
					var valParts = t.val.split(',');
					var k = 0;
					for(var j = 0; j < sections.length; j++)
					{
						if(sections[j].name == valParts[k])
						{
							if(t.op == 'OR')
								sections[j].is++;
							else
								sections[j].not++;
							
							if(sections[j].is > 0 && sections[j].not > 0)
							{
								alert('Your criteria specify that "Section is '+sections[j].name+'" and that "Section is not '+sections[j].name+'". This is not possible.'); 
								return false;
							}
							k++;
						}
					}
				}
				
				if(t.vari == 'category')
				{
					var valParts = t.val.split(',');
					var k = 0;
					for(var j = 0; j < sections.length; j++)
					{
						if(categories[j].name == valParts[k])
						{
							if(t.op == 'OR')
								categories[j].is++;
							else
								categories[j].not++;
							
							if(categories[j].is > 0 && categories[j].not > 0)
							{
								alert('Your criteria specify that "Category is '+categories[j].name+'" and that "Category is not '+categories[j].name+'". This is not possible.'); 
								return false;
							}
							k++;
						}
					}
				}
			}
		}
		
		if(exactDate && preDate || exactDate && postDate)
		{
			alert('You cannot specify "Date is ..." and "Date is before/after ..." in the same search');
			return false;
		}
		
		return true;
	}
	
	function ssRefineSearch()
	{	
		if(!document.getElementById)
			return true;
	
		var form = document.getElementById('ss-form');
		if(!form)
			return true;
		
		var criteriaOp;
		var vars = [];
		var ops = [];
		var vals = [];
		var varIndex = 0;
		var opIndex = 0;
		var valIndex = 0;
		for(var i = 0, el; el = form.elements[i]; i++)
		{
			if(el.name == 'var[]')
				vars[varIndex++] = el.value;
			else if(el.name == 'op[]')
				ops[opIndex++] = el.value;
			else if(el.name == 'val[]')
				vals[valIndex++] = el.value;
			else if(el.name == 'criteria-op')
				criteriaOp = el.value;
		}
		
		// Clear the search form
		pregoRemoveChildren(document.getElementById('adv-search-options'));
		
		// Add the options we're refining
		for(var i = 0; i < vars.length; i++)
			addSearchRow(vars[i], ops[i], vals[i]);
		
		// Set the criteriaOp
		var criteriaOpInput = document.getElementById('adv-search-criteria-op');
		criteriaOpInput.value = criteriaOp;
		
		// Display the form
		var advSearchForm = document.getElementById('advanced-search');
		if(advSearchForm.style.display != 'block')
		{
			var advSearchLink = document.getElementById('advanced-search-link');
			advSearchLink.onclick();
		}
		
		// Return FALSE to prevent the non-JS page from loading
		return false;
	}
	
	function ssSaveSubmit()
	{
		// Get a reference to the form
		var form = document.getElementById('ss-form');
		var fset = document.getElementById('ss-save-fieldset');
		if(!form || !fset)
			return true;
		
		// Get the params from the form
		var params = 'mode=ajax';
		for(var i = 0, el; el = form.elements[i]; i++)
		{
			if(el.value && el.name)
				params += '&' + el.name + '=' + encodeURI(el.value);
			else if(el.length)
			{
				for(var j = 0, subEl; subEl = el[j]; j++)
					if(subEl.value && subEl.name)
						params += '&' + subEl.name + '=' + encodeURI(subEl.value);
			}
		}
		
		// Function to deal with the completed request
		var saveCompleteFunction = function(xml,text,error)
		{
			var form = document.getElementById('ss-form');
			var fset = document.getElementById('ss-save-fieldset');
		
			var textParts = text.split('|');
		
			if(error || textParts[0] != 'OK')
			{
				var msg = error ? error : text;
				
				setAjaxStatus(fset, [
					pregoCreateElement('STRONG', 'Error: '),
					msg
				]);
				
				for(var i = 0, el; el = form.elements[i]; i++)
					el.disabled = false;
				
				return;
			}
			
			pregoRemoveChildren(fset);
			
			var id = textParts[1];
			var name = textParts[2];
			var mail = (textParts[3] == 'on');
			
			var nameInput = pregoCreateInput('text', 'name', name);
			nameInput.id = 'ss-name';
			var nameLabel = pregoCreateElement('LABEL', ['Name: ', nameInput]);
			nameLabel.setAttribute('for', 'ss-name');
			
			var mailInput = document.createElement('select');
			mailInput.name = 'mail';
			mailInput.id = 'ss-mail';
			mailInput.options[0] = new Option('By e-mail', 'on');
			mailInput.options[1] = new Option('None', 'off');
			mailInput.selectedIndex = mail ? 0 : 1;
			
			var mailLabel = pregoCreateElement('LABEL', ['Updates: ', mailInput]);
			mailLabel.setAttribute('for', 'ss-mail');
			
			var buttons = pregoCreateElement('DIV', pregoCreateInput('submit', '', 'Update'));
			buttons.className = 'buttons';
			
			setAjaxStatus(fset, 'Search saved.');
			setTimeout('clearAjaxStatus(document.getElementById("'+fset.id+'"));', 3000);
			
			pregoUniversalAppend(fset, [
				pregoCreateElement('LEGEND', name),
				pregoCreateInput('hidden', 'id', id),
				pregoCreateInput('hidden', 'function', 'update'),
				'This is one of your saved searches.',
				nameLabel,
				mailLabel,
				buttons
			]);
		}
		
		setAjaxLoadingStatus(fset, 'Processing...');
		
		// Make the request
		pregoAjaxPostRequest('action.search.php', params, saveCompleteFunction);
		return false;
	}
	
	
	var ssLoadSearchesComplete = false;
	function ssLoadSearches()
	{
		var list = document.getElementById('ss-list');

		// Clear the list
		pregoReplaceChildren(list, pregoCreateElement('LI', [pregoCreateImage('images/loading.gif', ''), ' Loading...']));
		
		// Create a function to process the list when we get it
		var listCompleteFunction = function(xml, text, error)
		{
			var list = document.getElementById('ss-list');
			pregoRemoveChildren(list);
		
			if(error)
			{
				list.appendChild(pregoCreateElement('LI', [
					pregoCreateElement('STRONG', 'Error: Could not load saved searches'),
					pregoCreateElement('BR'),
					'(', error, ')',
					pregoCreateElement('BR'),
					'Please try again later.'
				]));
				
				return;
			}
			
			var searches = xml.getElementsByTagName('search');
			for(var i = 0, s; s = searches[i]; i++)
			{
				var check = pregoCreateElement('INPUT');
				check.type = 'checkbox';
				check.name = 'saved[]';
				check.value = s.getAttribute('id');
				check.onclick = ssGUIUpdateSelection;
				
				var link = pregoCreateLink('search.php?id='+s.getAttribute('id'), s.getAttribute('name'));
				
				var item = pregoCreateElement('LI', [check, ' ', link]);
				if(s.getAttribute('mail'))
					item.className = 'mail';
					
				list.appendChild(item);
			}
			
			ssLoadSearchesComplete = true;
		}
		
		// Request the new list
		var params = 'mode=ajax&function=list';
		pregoAjaxPostRequest('action.search.php', params, listCompleteFunction);
	}
	
	
	function ssDeleteSelected()
	{
		var selected = ssGetSelected();
		
		// Check that we're not on search.php?id=x when deleting saved search x
		if(window.location.pathname.match(/search.php$/))
		{
			var matches = window.location.search.match(/id=([0-9]+)/);
			//console.log('Cannot delete: ' + matches[1]);
			
			if(matches)
			{
				for(var i = 0; i < selected.length; i++)
				{
					if(selected[i] == matches[1])
					{
						alert('Cannot delete the selected saved searches because you are currently viewing one of them. Please navigate to a different page and try again.');
						return;
					}
				}
			}
		}
		
		var deleteCompleteFunction = function(xml, text, error)
		{
			var status = document.getElementById('ss-status');
			clearAjaxStatus(status);
			ssGUIToggleDisabled(false);
		
			var list = document.getElementById('ss-list');
			if(list.firstChild && list.firstChild.className == 'error')
				list.removeChild(list.firstChild);
				
			if(error || text != 'OK')
			{
				var msg = error ? error : text;
			
				var err = pregoCreateElement('LI', [
					pregoCreateElement('STRONG', 'Error: Could not delete searches'),
					pregoCreateElement('BR'),
					'(', msg, ')',
					pregoCreateElement('BR'),
					'Please try again later.'
				]);
				err.className = 'error';
				
				list.insertBefore(err, list.firstChild);
				
				return;
			}
			
			ssGUIRemoveSelected();
		}
		
		
		var status = document.getElementById('ss-status');
		setAjaxLoadingStatus(status, 'Processing...');
		ssGUIToggleDisabled(true);
		
		var params = 'mode=ajax&function=delete&items=' + selected.join(',');
		pregoAjaxPostRequest('action.search.php', params, deleteCompleteFunction);
	}
	
	
	function ssToggleSelectedMail(mailSetting)
	{
		var selected = ssGetSelected();
		
		var toggleMailCompleteFunction = function(xml, text, error)
		{
			var status = document.getElementById('ss-status');
			clearAjaxStatus(status);
			ssGUIToggleDisabled(false);
		
			var list = document.getElementById('ss-list');
			if(list.firstChild && list.firstChild.className == 'error')
				list.removeChild(list.firstChild);
		
			var textParts = text.split('|');
		
			if(error || textParts[0] != 'OK')
			{
				var msg = error ? error : text;
			
				var err = pregoCreateElement('LI', [
					pregoCreateElement('STRONG', 'Error: Could not update searches'),
					pregoCreateElement('BR'),
					'(', msg, ')',
					pregoCreateElement('BR'),
					'Please try again later.'
				]);
				err.className = 'error';
				
				list.insertBefore(err, list.firstChild);
				
				return;
			}
			
			var className = (textParts[1] == 'on' ? 'mail' : '');
			ssGUISetSelectedClass(className);
		}
		
		var status = document.getElementById('ss-status');
		setAjaxLoadingStatus(status, 'Processing...');
		ssGUIToggleDisabled(true);
		
		var params = 'mode=ajax&function=mail&items=' + selected.join(',') + '&toggle=' + (mailSetting ? 'on' : 'off');
		pregoAjaxPostRequest('action.search.php', params, toggleMailCompleteFunction);
	}
	
	function ssGetSelected()
	{
		var list = document.getElementById('ss-list');
		var inputs = list.getElementsByTagName('input');
		
		var selected = [];
		var selectedIndex = 0;
		
		for(var i = 0, input; input = inputs[i]; i++)
		{
			if(input.type == 'checkbox' && input.name == 'saved[]' && input.checked)
				selected[selectedIndex++] = input.value;
		}
		
		return selected;
	}
	
	function ssAllSelected()
	{
		var list = document.getElementById('ss-list');
		var inputs = list.getElementsByTagName('input');
		
		if(inputs.length == 0)
			return false;
		
		for(var i = 0, input; input = inputs[i]; i++)
		{
			if(input.type == 'checkbox' && input.name == 'saved[]' && !input.checked)
				return false;
		}
		
		return true;
	}
	
	function ssGUIRemoveSelected()
	{
		var list = document.getElementById('ss-list');
		var inputs = list.getElementsByTagName('input');
		
		for(var i = 0, input; input = inputs[i]; i++)
		{
			if(input.type == 'checkbox' && input.name == 'saved[]' && input.checked)
			{
				input.parentNode.parentNode.removeChild(input.parentNode);
				i--;
			}
		}
		
		ssGUIUpdateSelection();
	}
	
	function ssGUISetSelectedClass(className)
	{
		var list = document.getElementById('ss-list');
		var inputs = list.getElementsByTagName('input');
		
		for(var i = 0, input; input = inputs[i]; i++)
		{
			if(input.type == 'checkbox' && input.name == 'saved[]' && input.checked)
			{
				input.parentNode.className = className;
			}
		}
	}
	
	function ssGUIToggleSelection(checked)
	{
		var list = document.getElementById('ss-list');
		var inputs = list.getElementsByTagName('input');
		
		for(var i = 0, input; input = inputs[i]; i++)
		{
			if(input.type == 'checkbox' && input.name == 'saved[]')
			{
				input.checked = checked;
			}
		}
	}
	
	function ssGUIToggleDisabled(disabled)
	{
		var list = document.getElementById('saved-searches');
		
		var inputs = list.getElementsByTagName('input');
		for(var i = 0, input; input = inputs[i]; i++)
			input.disabled = disabled;
			
		var selects = list.getElementsByTagName('select');
		for(var i = 0, sel; sel = selects[i]; i++)
			sel.disabled = disabled;
	}
	
	function ssGUIUpdateSelection()
	{
		var selectAll = document.getElementById('ss-select-all');
		if(!selectAll)
			return;
		selectAll.checked = ssAllSelected();
	}
	
	function ssGUISubmit()
	{
		var fn = document.getElementById('ss-function');
		if(!fn)
			return;
		
		switch(fn.value)
		{
			case 'delete':		return ssDeleteSelected();
			case 'mail-on':		return ssToggleSelectedMail(true);
			case 'mail-off':	return ssToggleSelectedMail(false);
		}
		
		alert('Error: Unknown saved search function ('+fn.value+')');
	}